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 | |
47 | 41 |
48 var BYTES = new Uint8Array([1, 2, 8, 9]); | 42 var BYTES = new Uint8Array([1, 2, 8, 9]); |
49 var BYTES_B64 = goog.crypt.base64.encodeByteArray(BYTES); | 43 var BYTES_B64 = goog.crypt.base64.encodeByteArray(BYTES); |
50 | 44 |
51 | 45 |
52 /** | 46 /** |
53 * Helper: compare a bytes field to an expected value | 47 * Helper: compare a bytes field to an expected value |
54 * @param {Uint8Array|string} arr | 48 * @param {Uint8Array|string} arr |
55 * @param {Uint8Array} expected | 49 * @param {Uint8Array} expected |
56 * @return {boolean} | 50 * @return {boolean} |
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
220 assertEquals(msg.getOneofString(), 'asdf'); | 214 assertEquals(msg.getOneofString(), 'asdf'); |
221 }); | 215 }); |
222 | 216 |
223 | 217 |
224 /** | 218 /** |
225 * Test that oneofs continue to have a notion of field presence. | 219 * Test that oneofs continue to have a notion of field presence. |
226 */ | 220 */ |
227 it('testOneofs', function() { | 221 it('testOneofs', function() { |
228 var msg = new proto.jspb.test.TestProto3(); | 222 var msg = new proto.jspb.test.TestProto3(); |
229 | 223 |
230 assertEquals(msg.getOneofUint32(), 0); | 224 assertEquals(msg.getOneofUint32(), undefined); |
231 assertEquals(msg.getOneofForeignMessage(), undefined); | 225 assertEquals(msg.getOneofForeignMessage(), undefined); |
232 assertEquals(msg.getOneofString(), ''); | 226 assertEquals(msg.getOneofString(), undefined); |
233 assertEquals(msg.getOneofBytes(), ''); | 227 assertEquals(msg.getOneofBytes(), undefined); |
234 assertFalse(msg.hasOneofUint32()); | |
235 assertFalse(msg.hasOneofString()); | |
236 assertFalse(msg.hasOneofBytes()); | |
237 | 228 |
238 msg.setOneofUint32(42); | 229 msg.setOneofUint32(42); |
239 assertEquals(msg.getOneofUint32(), 42); | 230 assertEquals(msg.getOneofUint32(), 42); |
240 assertEquals(msg.getOneofForeignMessage(), undefined); | 231 assertEquals(msg.getOneofForeignMessage(), undefined); |
241 assertEquals(msg.getOneofString(), ''); | 232 assertEquals(msg.getOneofString(), undefined); |
242 assertEquals(msg.getOneofBytes(), ''); | 233 assertEquals(msg.getOneofBytes(), undefined); |
243 assertTrue(msg.hasOneofUint32()); | |
244 assertFalse(msg.hasOneofString()); | |
245 assertFalse(msg.hasOneofBytes()); | |
246 | 234 |
247 | 235 |
248 var submsg = new proto.jspb.test.ForeignMessage(); | 236 var submsg = new proto.jspb.test.ForeignMessage(); |
249 msg.setOneofForeignMessage(submsg); | 237 msg.setOneofForeignMessage(submsg); |
250 assertEquals(msg.getOneofUint32(), 0); | 238 assertEquals(msg.getOneofUint32(), undefined); |
251 assertEquals(msg.getOneofForeignMessage(), submsg); | 239 assertEquals(msg.getOneofForeignMessage(), submsg); |
252 assertEquals(msg.getOneofString(), ''); | 240 assertEquals(msg.getOneofString(), undefined); |
253 assertEquals(msg.getOneofBytes(), ''); | 241 assertEquals(msg.getOneofBytes(), undefined); |
254 assertFalse(msg.hasOneofUint32()); | |
255 assertFalse(msg.hasOneofString()); | |
256 assertFalse(msg.hasOneofBytes()); | |
257 | 242 |
258 msg.setOneofString('hello'); | 243 msg.setOneofString('hello'); |
259 assertEquals(msg.getOneofUint32(), 0); | 244 assertEquals(msg.getOneofUint32(), undefined); |
260 assertEquals(msg.getOneofForeignMessage(), undefined); | 245 assertEquals(msg.getOneofForeignMessage(), undefined); |
261 assertEquals(msg.getOneofString(), 'hello'); | 246 assertEquals(msg.getOneofString(), 'hello'); |
262 assertEquals(msg.getOneofBytes(), ''); | 247 assertEquals(msg.getOneofBytes(), undefined); |
263 assertFalse(msg.hasOneofUint32()); | |
264 assertTrue(msg.hasOneofString()); | |
265 assertFalse(msg.hasOneofBytes()); | |
266 | 248 |
267 msg.setOneofBytes(goog.crypt.base64.encodeString('\u00FF\u00FF')); | 249 msg.setOneofBytes(goog.crypt.base64.encodeString('\u00FF\u00FF')); |
268 assertEquals(msg.getOneofUint32(), 0); | 250 assertEquals(msg.getOneofUint32(), undefined); |
269 assertEquals(msg.getOneofForeignMessage(), undefined); | 251 assertEquals(msg.getOneofForeignMessage(), undefined); |
270 assertEquals(msg.getOneofString(), ''); | 252 assertEquals(msg.getOneofString(), undefined); |
271 assertEquals(msg.getOneofBytes_asB64(), | 253 assertEquals(msg.getOneofBytes_asB64(), |
272 goog.crypt.base64.encodeString('\u00FF\u00FF')); | 254 goog.crypt.base64.encodeString('\u00FF\u00FF')); |
273 assertFalse(msg.hasOneofUint32()); | |
274 assertFalse(msg.hasOneofString()); | |
275 assertTrue(msg.hasOneofBytes()); | |
276 }); | 255 }); |
277 | 256 |
278 | 257 |
279 /** | 258 /** |
280 * Test that "default"-valued primitive fields are not emitted on the wire. | 259 * Test that "default"-valued primitive fields are not emitted on the wire. |
281 */ | 260 */ |
282 it('testNoSerializeDefaults', function() { | 261 it('testNoSerializeDefaults', function() { |
283 var msg = new proto.jspb.test.TestProto3(); | 262 var msg = new proto.jspb.test.TestProto3(); |
284 | 263 |
285 // Set each primitive to a non-default value, then back to its default, to | 264 // Set each primitive to a non-default value, then back to its default, to |
286 // ensure that the serialization is actually checking the value and not just | 265 // ensure that the serialization is actually checking the value and not just |
287 // whether it has ever been set. | 266 // whether it has ever been set. |
288 msg.setOptionalInt32(42); | 267 msg.setOptionalInt32(42); |
289 msg.setOptionalInt32(0); | 268 msg.setOptionalInt32(0); |
290 msg.setOptionalDouble(3.14); | 269 msg.setOptionalDouble(3.14); |
291 msg.setOptionalDouble(0.0); | 270 msg.setOptionalDouble(0.0); |
292 msg.setOptionalBool(true); | 271 msg.setOptionalBool(true); |
293 msg.setOptionalBool(false); | 272 msg.setOptionalBool(false); |
294 msg.setOptionalString('hello world'); | 273 msg.setOptionalString('hello world'); |
295 msg.setOptionalString(''); | 274 msg.setOptionalString(''); |
296 msg.setOptionalBytes(goog.crypt.base64.encodeString('\u00FF\u00FF')); | 275 msg.setOptionalBytes(goog.crypt.base64.encodeString('\u00FF\u00FF')); |
297 msg.setOptionalBytes(''); | 276 msg.setOptionalBytes(''); |
298 msg.setOptionalForeignMessage(new proto.jspb.test.ForeignMessage()); | 277 msg.setOptionalForeignMessage(new proto.jspb.test.ForeignMessage()); |
299 msg.setOptionalForeignMessage(null); | 278 msg.setOptionalForeignMessage(null); |
300 msg.setOptionalForeignEnum(proto.jspb.test.Proto3Enum.PROTO3_BAR); | 279 msg.setOptionalForeignEnum(proto.jspb.test.Proto3Enum.PROTO3_BAR); |
301 msg.setOptionalForeignEnum(proto.jspb.test.Proto3Enum.PROTO3_FOO); | 280 msg.setOptionalForeignEnum(proto.jspb.test.Proto3Enum.PROTO3_FOO); |
302 msg.setOneofUint32(32); | 281 msg.setOneofUint32(32); |
303 msg.clearOneofUint32(); | 282 msg.setOneofUint32(null); |
304 | 283 |
305 | 284 |
306 var serialized = msg.serializeBinary(); | 285 var serialized = msg.serializeBinary(); |
307 assertEquals(0, serialized.length); | 286 assertEquals(0, serialized.length); |
308 }); | 287 }); |
309 | 288 |
310 /** | 289 /** |
311 * Test that base64 string and Uint8Array are interchangeable in bytes fields. | 290 * Test that base64 string and Uint8Array are interchangeable in bytes fields. |
312 */ | 291 */ |
313 it('testBytesFieldsInterop', function() { | 292 it('testBytesFieldsInterop', function() { |
(...skipping 11 matching lines...) Expand all Loading... |
325 assertTrue(bytesCompare(msg.getOptionalBytes(), BYTES)); | 304 assertTrue(bytesCompare(msg.getOptionalBytes(), BYTES)); |
326 | 305 |
327 msg = new proto.jspb.test.TestProto3(); | 306 msg = new proto.jspb.test.TestProto3(); |
328 // Set as a Uint8Array and check all the getters work. | 307 // Set as a Uint8Array and check all the getters work. |
329 msg.setOptionalBytes(BYTES); | 308 msg.setOptionalBytes(BYTES); |
330 assertTrue(bytesCompare(msg.getOptionalBytes_asU8(), BYTES)); | 309 assertTrue(bytesCompare(msg.getOptionalBytes_asU8(), BYTES)); |
331 assertTrue(bytesCompare(msg.getOptionalBytes_asB64(), BYTES)); | 310 assertTrue(bytesCompare(msg.getOptionalBytes_asB64(), BYTES)); |
332 assertTrue(bytesCompare(msg.getOptionalBytes(), BYTES)); | 311 assertTrue(bytesCompare(msg.getOptionalBytes(), BYTES)); |
333 | 312 |
334 }); | 313 }); |
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 }); | |
367 }); | 314 }); |
OLD | NEW |