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 20 matching lines...) Expand all Loading... |
31 goog.require('goog.crypt.base64'); | 31 goog.require('goog.crypt.base64'); |
32 goog.require('goog.testing.asserts'); | 32 goog.require('goog.testing.asserts'); |
33 | 33 |
34 // CommonJS-LoadFromFile: testbinary_pb proto.jspb.test | 34 // CommonJS-LoadFromFile: testbinary_pb proto.jspb.test |
35 goog.require('proto.jspb.test.ForeignMessage'); | 35 goog.require('proto.jspb.test.ForeignMessage'); |
36 | 36 |
37 // CommonJS-LoadFromFile: proto3_test_pb proto.jspb.test | 37 // CommonJS-LoadFromFile: proto3_test_pb proto.jspb.test |
38 goog.require('proto.jspb.test.Proto3Enum'); | 38 goog.require('proto.jspb.test.Proto3Enum'); |
39 goog.require('proto.jspb.test.TestProto3'); | 39 goog.require('proto.jspb.test.TestProto3'); |
40 | 40 |
| 41 // CommonJS-LoadFromFile: google/protobuf/timestamp_pb proto.google.protobuf |
| 42 goog.require('proto.google.protobuf.Timestamp'); |
| 43 |
| 44 // CommonJS-LoadFromFile: google/protobuf/struct_pb proto.google.protobuf |
| 45 goog.require('proto.google.protobuf.Struct'); |
| 46 |
41 | 47 |
42 var BYTES = new Uint8Array([1, 2, 8, 9]); | 48 var BYTES = new Uint8Array([1, 2, 8, 9]); |
43 var BYTES_B64 = goog.crypt.base64.encodeByteArray(BYTES); | 49 var BYTES_B64 = goog.crypt.base64.encodeByteArray(BYTES); |
44 | 50 |
45 | 51 |
46 /** | 52 /** |
47 * Helper: compare a bytes field to an expected value | 53 * Helper: compare a bytes field to an expected value |
48 * @param {Uint8Array|string} arr | 54 * @param {Uint8Array|string} arr |
49 * @param {Uint8Array} expected | 55 * @param {Uint8Array} expected |
50 * @return {boolean} | 56 * @return {boolean} |
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
214 assertEquals(msg.getOneofString(), 'asdf'); | 220 assertEquals(msg.getOneofString(), 'asdf'); |
215 }); | 221 }); |
216 | 222 |
217 | 223 |
218 /** | 224 /** |
219 * Test that oneofs continue to have a notion of field presence. | 225 * Test that oneofs continue to have a notion of field presence. |
220 */ | 226 */ |
221 it('testOneofs', function() { | 227 it('testOneofs', function() { |
222 var msg = new proto.jspb.test.TestProto3(); | 228 var msg = new proto.jspb.test.TestProto3(); |
223 | 229 |
224 assertEquals(msg.getOneofUint32(), undefined); | 230 assertEquals(msg.getOneofUint32(), 0); |
225 assertEquals(msg.getOneofForeignMessage(), undefined); | 231 assertEquals(msg.getOneofForeignMessage(), undefined); |
226 assertEquals(msg.getOneofString(), undefined); | 232 assertEquals(msg.getOneofString(), ''); |
227 assertEquals(msg.getOneofBytes(), undefined); | 233 assertEquals(msg.getOneofBytes(), ''); |
| 234 assertFalse(msg.hasOneofUint32()); |
| 235 assertFalse(msg.hasOneofString()); |
| 236 assertFalse(msg.hasOneofBytes()); |
228 | 237 |
229 msg.setOneofUint32(42); | 238 msg.setOneofUint32(42); |
230 assertEquals(msg.getOneofUint32(), 42); | 239 assertEquals(msg.getOneofUint32(), 42); |
231 assertEquals(msg.getOneofForeignMessage(), undefined); | 240 assertEquals(msg.getOneofForeignMessage(), undefined); |
232 assertEquals(msg.getOneofString(), undefined); | 241 assertEquals(msg.getOneofString(), ''); |
233 assertEquals(msg.getOneofBytes(), undefined); | 242 assertEquals(msg.getOneofBytes(), ''); |
| 243 assertTrue(msg.hasOneofUint32()); |
| 244 assertFalse(msg.hasOneofString()); |
| 245 assertFalse(msg.hasOneofBytes()); |
234 | 246 |
235 | 247 |
236 var submsg = new proto.jspb.test.ForeignMessage(); | 248 var submsg = new proto.jspb.test.ForeignMessage(); |
237 msg.setOneofForeignMessage(submsg); | 249 msg.setOneofForeignMessage(submsg); |
238 assertEquals(msg.getOneofUint32(), undefined); | 250 assertEquals(msg.getOneofUint32(), 0); |
239 assertEquals(msg.getOneofForeignMessage(), submsg); | 251 assertEquals(msg.getOneofForeignMessage(), submsg); |
240 assertEquals(msg.getOneofString(), undefined); | 252 assertEquals(msg.getOneofString(), ''); |
241 assertEquals(msg.getOneofBytes(), undefined); | 253 assertEquals(msg.getOneofBytes(), ''); |
| 254 assertFalse(msg.hasOneofUint32()); |
| 255 assertFalse(msg.hasOneofString()); |
| 256 assertFalse(msg.hasOneofBytes()); |
242 | 257 |
243 msg.setOneofString('hello'); | 258 msg.setOneofString('hello'); |
244 assertEquals(msg.getOneofUint32(), undefined); | 259 assertEquals(msg.getOneofUint32(), 0); |
245 assertEquals(msg.getOneofForeignMessage(), undefined); | 260 assertEquals(msg.getOneofForeignMessage(), undefined); |
246 assertEquals(msg.getOneofString(), 'hello'); | 261 assertEquals(msg.getOneofString(), 'hello'); |
247 assertEquals(msg.getOneofBytes(), undefined); | 262 assertEquals(msg.getOneofBytes(), ''); |
| 263 assertFalse(msg.hasOneofUint32()); |
| 264 assertTrue(msg.hasOneofString()); |
| 265 assertFalse(msg.hasOneofBytes()); |
248 | 266 |
249 msg.setOneofBytes(goog.crypt.base64.encodeString('\u00FF\u00FF')); | 267 msg.setOneofBytes(goog.crypt.base64.encodeString('\u00FF\u00FF')); |
250 assertEquals(msg.getOneofUint32(), undefined); | 268 assertEquals(msg.getOneofUint32(), 0); |
251 assertEquals(msg.getOneofForeignMessage(), undefined); | 269 assertEquals(msg.getOneofForeignMessage(), undefined); |
252 assertEquals(msg.getOneofString(), undefined); | 270 assertEquals(msg.getOneofString(), ''); |
253 assertEquals(msg.getOneofBytes_asB64(), | 271 assertEquals(msg.getOneofBytes_asB64(), |
254 goog.crypt.base64.encodeString('\u00FF\u00FF')); | 272 goog.crypt.base64.encodeString('\u00FF\u00FF')); |
| 273 assertFalse(msg.hasOneofUint32()); |
| 274 assertFalse(msg.hasOneofString()); |
| 275 assertTrue(msg.hasOneofBytes()); |
255 }); | 276 }); |
256 | 277 |
257 | 278 |
258 /** | 279 /** |
259 * Test that "default"-valued primitive fields are not emitted on the wire. | 280 * Test that "default"-valued primitive fields are not emitted on the wire. |
260 */ | 281 */ |
261 it('testNoSerializeDefaults', function() { | 282 it('testNoSerializeDefaults', function() { |
262 var msg = new proto.jspb.test.TestProto3(); | 283 var msg = new proto.jspb.test.TestProto3(); |
263 | 284 |
264 // Set each primitive to a non-default value, then back to its default, to | 285 // 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 | 286 // ensure that the serialization is actually checking the value and not just |
266 // whether it has ever been set. | 287 // whether it has ever been set. |
267 msg.setOptionalInt32(42); | 288 msg.setOptionalInt32(42); |
268 msg.setOptionalInt32(0); | 289 msg.setOptionalInt32(0); |
269 msg.setOptionalDouble(3.14); | 290 msg.setOptionalDouble(3.14); |
270 msg.setOptionalDouble(0.0); | 291 msg.setOptionalDouble(0.0); |
271 msg.setOptionalBool(true); | 292 msg.setOptionalBool(true); |
272 msg.setOptionalBool(false); | 293 msg.setOptionalBool(false); |
273 msg.setOptionalString('hello world'); | 294 msg.setOptionalString('hello world'); |
274 msg.setOptionalString(''); | 295 msg.setOptionalString(''); |
275 msg.setOptionalBytes(goog.crypt.base64.encodeString('\u00FF\u00FF')); | 296 msg.setOptionalBytes(goog.crypt.base64.encodeString('\u00FF\u00FF')); |
276 msg.setOptionalBytes(''); | 297 msg.setOptionalBytes(''); |
277 msg.setOptionalForeignMessage(new proto.jspb.test.ForeignMessage()); | 298 msg.setOptionalForeignMessage(new proto.jspb.test.ForeignMessage()); |
278 msg.setOptionalForeignMessage(null); | 299 msg.setOptionalForeignMessage(null); |
279 msg.setOptionalForeignEnum(proto.jspb.test.Proto3Enum.PROTO3_BAR); | 300 msg.setOptionalForeignEnum(proto.jspb.test.Proto3Enum.PROTO3_BAR); |
280 msg.setOptionalForeignEnum(proto.jspb.test.Proto3Enum.PROTO3_FOO); | 301 msg.setOptionalForeignEnum(proto.jspb.test.Proto3Enum.PROTO3_FOO); |
281 msg.setOneofUint32(32); | 302 msg.setOneofUint32(32); |
282 msg.setOneofUint32(null); | 303 msg.clearOneofUint32(); |
283 | 304 |
284 | 305 |
285 var serialized = msg.serializeBinary(); | 306 var serialized = msg.serializeBinary(); |
286 assertEquals(0, serialized.length); | 307 assertEquals(0, serialized.length); |
287 }); | 308 }); |
288 | 309 |
289 /** | 310 /** |
290 * Test that base64 string and Uint8Array are interchangeable in bytes fields. | 311 * Test that base64 string and Uint8Array are interchangeable in bytes fields. |
291 */ | 312 */ |
292 it('testBytesFieldsInterop', function() { | 313 it('testBytesFieldsInterop', function() { |
(...skipping 11 matching lines...) Expand all Loading... |
304 assertTrue(bytesCompare(msg.getOptionalBytes(), BYTES)); | 325 assertTrue(bytesCompare(msg.getOptionalBytes(), BYTES)); |
305 | 326 |
306 msg = new proto.jspb.test.TestProto3(); | 327 msg = new proto.jspb.test.TestProto3(); |
307 // Set as a Uint8Array and check all the getters work. | 328 // Set as a Uint8Array and check all the getters work. |
308 msg.setOptionalBytes(BYTES); | 329 msg.setOptionalBytes(BYTES); |
309 assertTrue(bytesCompare(msg.getOptionalBytes_asU8(), BYTES)); | 330 assertTrue(bytesCompare(msg.getOptionalBytes_asU8(), BYTES)); |
310 assertTrue(bytesCompare(msg.getOptionalBytes_asB64(), BYTES)); | 331 assertTrue(bytesCompare(msg.getOptionalBytes_asB64(), BYTES)); |
311 assertTrue(bytesCompare(msg.getOptionalBytes(), BYTES)); | 332 assertTrue(bytesCompare(msg.getOptionalBytes(), BYTES)); |
312 | 333 |
313 }); | 334 }); |
| 335 |
| 336 it('testTimestampWellKnownType', function() { |
| 337 var msg = new proto.google.protobuf.Timestamp(); |
| 338 msg.fromDate(new Date(123456789)); |
| 339 assertEquals(123456, msg.getSeconds()); |
| 340 assertEquals(789000000, msg.getNanos()); |
| 341 var date = msg.toDate(); |
| 342 assertEquals(123456789, date.getTime()); |
| 343 }); |
| 344 |
| 345 it('testStructWellKnownType', function() { |
| 346 var jsObj = { |
| 347 abc: "def", |
| 348 number: 12345.678, |
| 349 nullKey: null, |
| 350 boolKey: true, |
| 351 listKey: [1, null, true, false, "abc"], |
| 352 structKey: {foo: "bar", somenum: 123}, |
| 353 complicatedKey: [{xyz: {abc: [3, 4, null, false]}}, "zzz"] |
| 354 }; |
| 355 |
| 356 var struct = proto.google.protobuf.Struct.fromJavaScript(jsObj); |
| 357 var jsObj2 = struct.toJavaScript(); |
| 358 |
| 359 assertEquals("def", jsObj2.abc); |
| 360 assertEquals(12345.678, jsObj2.number); |
| 361 assertEquals(null, jsObj2.nullKey); |
| 362 assertEquals(true, jsObj2.boolKey); |
| 363 assertEquals("abc", jsObj2.listKey[4]); |
| 364 assertEquals("bar", jsObj2.structKey.foo); |
| 365 assertEquals(4, jsObj2.complicatedKey[0].xyz.abc[1]); |
| 366 }); |
314 }); | 367 }); |
OLD | NEW |