OLD | NEW |
1 // Protocol Buffers - Google's data interchange format | 1 // Protocol Buffers - Google's data interchange format |
2 // Copyright 2008 Google Inc. All rights reserved. | 2 // Copyright 2008 Google Inc. All rights reserved. |
3 // https://developers.google.com/protocol-buffers/ | 3 // https://developers.google.com/protocol-buffers/ |
4 // | 4 // |
5 // Redistribution and use in source and binary forms, with or without | 5 // Redistribution and use in source and binary forms, with or without |
6 // modification, are permitted provided that the following conditions are | 6 // modification, are permitted provided that the following conditions are |
7 // met: | 7 // met: |
8 // | 8 // |
9 // * Redistributions of source code must retain the above copyright | 9 // * Redistributions of source code must retain the above copyright |
10 // notice, this list of conditions and the following disclaimer. | 10 // notice, this list of conditions and the following disclaimer. |
(...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
214 assertEquals(msg.getOneofString(), 'asdf'); | 214 assertEquals(msg.getOneofString(), 'asdf'); |
215 }); | 215 }); |
216 | 216 |
217 | 217 |
218 /** | 218 /** |
219 * Test that oneofs continue to have a notion of field presence. | 219 * Test that oneofs continue to have a notion of field presence. |
220 */ | 220 */ |
221 it('testOneofs', function() { | 221 it('testOneofs', function() { |
222 var msg = new proto.jspb.test.TestProto3(); | 222 var msg = new proto.jspb.test.TestProto3(); |
223 | 223 |
224 assertEquals(msg.getOneofUint32(), undefined); | 224 assertEquals(msg.getOneofUint32(), 0); |
225 assertEquals(msg.getOneofForeignMessage(), undefined); | 225 assertEquals(msg.getOneofForeignMessage(), undefined); |
226 assertEquals(msg.getOneofString(), undefined); | 226 assertEquals(msg.getOneofString(), ''); |
227 assertEquals(msg.getOneofBytes(), undefined); | 227 assertEquals(msg.getOneofBytes(), ''); |
| 228 assertFalse(msg.hasOneofUint32()); |
| 229 assertFalse(msg.hasOneofString()); |
| 230 assertFalse(msg.hasOneofBytes()); |
228 | 231 |
229 msg.setOneofUint32(42); | 232 msg.setOneofUint32(42); |
230 assertEquals(msg.getOneofUint32(), 42); | 233 assertEquals(msg.getOneofUint32(), 42); |
231 assertEquals(msg.getOneofForeignMessage(), undefined); | 234 assertEquals(msg.getOneofForeignMessage(), undefined); |
232 assertEquals(msg.getOneofString(), undefined); | 235 assertEquals(msg.getOneofString(), ''); |
233 assertEquals(msg.getOneofBytes(), undefined); | 236 assertEquals(msg.getOneofBytes(), ''); |
| 237 assertTrue(msg.hasOneofUint32()); |
| 238 assertFalse(msg.hasOneofString()); |
| 239 assertFalse(msg.hasOneofBytes()); |
234 | 240 |
235 | 241 |
236 var submsg = new proto.jspb.test.ForeignMessage(); | 242 var submsg = new proto.jspb.test.ForeignMessage(); |
237 msg.setOneofForeignMessage(submsg); | 243 msg.setOneofForeignMessage(submsg); |
238 assertEquals(msg.getOneofUint32(), undefined); | 244 assertEquals(msg.getOneofUint32(), 0); |
239 assertEquals(msg.getOneofForeignMessage(), submsg); | 245 assertEquals(msg.getOneofForeignMessage(), submsg); |
240 assertEquals(msg.getOneofString(), undefined); | 246 assertEquals(msg.getOneofString(), ''); |
241 assertEquals(msg.getOneofBytes(), undefined); | 247 assertEquals(msg.getOneofBytes(), ''); |
| 248 assertFalse(msg.hasOneofUint32()); |
| 249 assertFalse(msg.hasOneofString()); |
| 250 assertFalse(msg.hasOneofBytes()); |
242 | 251 |
243 msg.setOneofString('hello'); | 252 msg.setOneofString('hello'); |
244 assertEquals(msg.getOneofUint32(), undefined); | 253 assertEquals(msg.getOneofUint32(), 0); |
245 assertEquals(msg.getOneofForeignMessage(), undefined); | 254 assertEquals(msg.getOneofForeignMessage(), undefined); |
246 assertEquals(msg.getOneofString(), 'hello'); | 255 assertEquals(msg.getOneofString(), 'hello'); |
247 assertEquals(msg.getOneofBytes(), undefined); | 256 assertEquals(msg.getOneofBytes(), ''); |
| 257 assertFalse(msg.hasOneofUint32()); |
| 258 assertTrue(msg.hasOneofString()); |
| 259 assertFalse(msg.hasOneofBytes()); |
248 | 260 |
249 msg.setOneofBytes(goog.crypt.base64.encodeString('\u00FF\u00FF')); | 261 msg.setOneofBytes(goog.crypt.base64.encodeString('\u00FF\u00FF')); |
250 assertEquals(msg.getOneofUint32(), undefined); | 262 assertEquals(msg.getOneofUint32(), 0); |
251 assertEquals(msg.getOneofForeignMessage(), undefined); | 263 assertEquals(msg.getOneofForeignMessage(), undefined); |
252 assertEquals(msg.getOneofString(), undefined); | 264 assertEquals(msg.getOneofString(), ''); |
253 assertEquals(msg.getOneofBytes_asB64(), | 265 assertEquals(msg.getOneofBytes_asB64(), |
254 goog.crypt.base64.encodeString('\u00FF\u00FF')); | 266 goog.crypt.base64.encodeString('\u00FF\u00FF')); |
| 267 assertFalse(msg.hasOneofUint32()); |
| 268 assertFalse(msg.hasOneofString()); |
| 269 assertTrue(msg.hasOneofBytes()); |
255 }); | 270 }); |
256 | 271 |
257 | 272 |
258 /** | 273 /** |
259 * Test that "default"-valued primitive fields are not emitted on the wire. | 274 * Test that "default"-valued primitive fields are not emitted on the wire. |
260 */ | 275 */ |
261 it('testNoSerializeDefaults', function() { | 276 it('testNoSerializeDefaults', function() { |
262 var msg = new proto.jspb.test.TestProto3(); | 277 var msg = new proto.jspb.test.TestProto3(); |
263 | 278 |
264 // Set each primitive to a non-default value, then back to its default, to | 279 // Set each primitive to a non-default value, then back to its default, to |
265 // ensure that the serialization is actually checking the value and not just | 280 // ensure that the serialization is actually checking the value and not just |
266 // whether it has ever been set. | 281 // whether it has ever been set. |
267 msg.setOptionalInt32(42); | 282 msg.setOptionalInt32(42); |
268 msg.setOptionalInt32(0); | 283 msg.setOptionalInt32(0); |
269 msg.setOptionalDouble(3.14); | 284 msg.setOptionalDouble(3.14); |
270 msg.setOptionalDouble(0.0); | 285 msg.setOptionalDouble(0.0); |
271 msg.setOptionalBool(true); | 286 msg.setOptionalBool(true); |
272 msg.setOptionalBool(false); | 287 msg.setOptionalBool(false); |
273 msg.setOptionalString('hello world'); | 288 msg.setOptionalString('hello world'); |
274 msg.setOptionalString(''); | 289 msg.setOptionalString(''); |
275 msg.setOptionalBytes(goog.crypt.base64.encodeString('\u00FF\u00FF')); | 290 msg.setOptionalBytes(goog.crypt.base64.encodeString('\u00FF\u00FF')); |
276 msg.setOptionalBytes(''); | 291 msg.setOptionalBytes(''); |
277 msg.setOptionalForeignMessage(new proto.jspb.test.ForeignMessage()); | 292 msg.setOptionalForeignMessage(new proto.jspb.test.ForeignMessage()); |
278 msg.setOptionalForeignMessage(null); | 293 msg.setOptionalForeignMessage(null); |
279 msg.setOptionalForeignEnum(proto.jspb.test.Proto3Enum.PROTO3_BAR); | 294 msg.setOptionalForeignEnum(proto.jspb.test.Proto3Enum.PROTO3_BAR); |
280 msg.setOptionalForeignEnum(proto.jspb.test.Proto3Enum.PROTO3_FOO); | 295 msg.setOptionalForeignEnum(proto.jspb.test.Proto3Enum.PROTO3_FOO); |
281 msg.setOneofUint32(32); | 296 msg.setOneofUint32(32); |
282 msg.setOneofUint32(null); | 297 msg.clearOneofUint32(); |
283 | 298 |
284 | 299 |
285 var serialized = msg.serializeBinary(); | 300 var serialized = msg.serializeBinary(); |
286 assertEquals(0, serialized.length); | 301 assertEquals(0, serialized.length); |
287 }); | 302 }); |
288 | 303 |
289 /** | 304 /** |
290 * Test that base64 string and Uint8Array are interchangeable in bytes fields. | 305 * Test that base64 string and Uint8Array are interchangeable in bytes fields. |
291 */ | 306 */ |
292 it('testBytesFieldsInterop', function() { | 307 it('testBytesFieldsInterop', function() { |
(...skipping 12 matching lines...) Expand all Loading... |
305 | 320 |
306 msg = new proto.jspb.test.TestProto3(); | 321 msg = new proto.jspb.test.TestProto3(); |
307 // Set as a Uint8Array and check all the getters work. | 322 // Set as a Uint8Array and check all the getters work. |
308 msg.setOptionalBytes(BYTES); | 323 msg.setOptionalBytes(BYTES); |
309 assertTrue(bytesCompare(msg.getOptionalBytes_asU8(), BYTES)); | 324 assertTrue(bytesCompare(msg.getOptionalBytes_asU8(), BYTES)); |
310 assertTrue(bytesCompare(msg.getOptionalBytes_asB64(), BYTES)); | 325 assertTrue(bytesCompare(msg.getOptionalBytes_asB64(), BYTES)); |
311 assertTrue(bytesCompare(msg.getOptionalBytes(), BYTES)); | 326 assertTrue(bytesCompare(msg.getOptionalBytes(), BYTES)); |
312 | 327 |
313 }); | 328 }); |
314 }); | 329 }); |
OLD | NEW |