blob: 7f9d71d3f61ca8d49ec8a9807ad0a0a95ae4ab28 [file] [log] [blame]
Feng Xiaoe841bac2015-12-11 17:09:20 -08001// Protocol Buffers - Google's data interchange format
2// Copyright 2008 Google Inc. All rights reserved.
3// https://developers.google.com/protocol-buffers/
4//
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are
7// met:
8//
9// * Redistributions of source code must retain the above copyright
10// notice, this list of conditions and the following disclaimer.
11// * Redistributions in binary form must reproduce the above
12// copyright notice, this list of conditions and the following disclaimer
13// in the documentation and/or other materials provided with the
14// distribution.
15// * Neither the name of Google Inc. nor the names of its
16// contributors may be used to endorse or promote products derived from
17// this software without specific prior written permission.
18//
19// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
Jisi Liu3b3c8ab2016-03-30 11:39:59 -070031goog.require('goog.crypt.base64');
Feng Xiaoe841bac2015-12-11 17:09:20 -080032goog.require('goog.testing.asserts');
Josh Haberman77af5d02016-02-04 16:11:07 -080033// CommonJS-LoadFromFile: testbinary_pb proto.jspb.test
Feng Xiaoe841bac2015-12-11 17:09:20 -080034goog.require('proto.jspb.test.ForeignMessage');
Josh Haberman77af5d02016-02-04 16:11:07 -080035// CommonJS-LoadFromFile: proto3_test_pb proto.jspb.test
Feng Xiaoe841bac2015-12-11 17:09:20 -080036goog.require('proto.jspb.test.Proto3Enum');
37goog.require('proto.jspb.test.TestProto3');
Adam Cozzette0894e072018-11-09 11:28:22 -080038// CommonJS-LoadFromFile: google/protobuf/any_pb proto.google.protobuf
39goog.require('proto.google.protobuf.Any');
Adam Cozzette5a76e632016-11-17 16:48:38 -080040// CommonJS-LoadFromFile: google/protobuf/timestamp_pb proto.google.protobuf
41goog.require('proto.google.protobuf.Timestamp');
Adam Cozzette5a76e632016-11-17 16:48:38 -080042// CommonJS-LoadFromFile: google/protobuf/struct_pb proto.google.protobuf
43goog.require('proto.google.protobuf.Struct');
44
Jisi Liu3b3c8ab2016-03-30 11:39:59 -070045
46var BYTES = new Uint8Array([1, 2, 8, 9]);
47var BYTES_B64 = goog.crypt.base64.encodeByteArray(BYTES);
48
49
Feng Xiaoe841bac2015-12-11 17:09:20 -080050/**
Jisi Liu3b3c8ab2016-03-30 11:39:59 -070051 * Helper: compare a bytes field to an expected value
Feng Xiaoe841bac2015-12-11 17:09:20 -080052 * @param {Uint8Array|string} arr
Jisi Liu3b3c8ab2016-03-30 11:39:59 -070053 * @param {Uint8Array} expected
Feng Xiaoe841bac2015-12-11 17:09:20 -080054 * @return {boolean}
55 */
Jisi Liu3b3c8ab2016-03-30 11:39:59 -070056function bytesCompare(arr, expected) {
57 if (goog.isString(arr)) {
58 arr = goog.crypt.base64.decodeStringToUint8Array(arr);
59 }
60 if (arr.length != expected.length) {
Feng Xiaoe841bac2015-12-11 17:09:20 -080061 return false;
62 }
Jisi Liu3b3c8ab2016-03-30 11:39:59 -070063 for (var i = 0; i < arr.length; i++) {
64 if (arr[i] != expected[i]) {
65 return false;
Feng Xiaoe841bac2015-12-11 17:09:20 -080066 }
Feng Xiaoe841bac2015-12-11 17:09:20 -080067 }
Jisi Liu3b3c8ab2016-03-30 11:39:59 -070068 return true;
Feng Xiaoe841bac2015-12-11 17:09:20 -080069}
70
71
72describe('proto3Test', function() {
Adam Cozzette13fd0452017-09-12 10:32:01 -070073
74 /**
75 * Test default values don't affect equality test.
76 */
77 it('testEqualsProto3', function() {
78 var msg1 = new proto.jspb.test.TestProto3();
79 var msg2 = new proto.jspb.test.TestProto3();
80 msg2.setOptionalString('');
81
82 assertTrue(jspb.Message.equals(msg1, msg2));
83 });
84
85
86 /**
87 * Test setting when a field has default semantics.
88 */
89 it('testSetProto3ToValueAndBackToDefault', function() {
90 var msg = new proto.jspb.test.TestProto3();
91
92 // Setting should work normally.
93 msg.setOptionalString('optionalString');
94 assertEquals(msg.getOptionalString(), 'optionalString');
95
96 // Clearing should work too ...
97 msg.setOptionalString('');
98 assertEquals(msg.getOptionalString(), '');
99
100 // ... and shouldn't affect the equality with a brand new message.
101 assertTrue(jspb.Message.equals(msg, new proto.jspb.test.TestProto3()));
102 });
103
Feng Xiaoe841bac2015-12-11 17:09:20 -0800104 /**
105 * Test defaults for proto3 message fields.
106 */
107 it('testProto3FieldDefaults', function() {
108 var msg = new proto.jspb.test.TestProto3();
109
110 assertEquals(msg.getOptionalInt32(), 0);
111 assertEquals(msg.getOptionalInt64(), 0);
112 assertEquals(msg.getOptionalUint32(), 0);
113 assertEquals(msg.getOptionalUint64(), 0);
114 assertEquals(msg.getOptionalSint32(), 0);
115 assertEquals(msg.getOptionalSint64(), 0);
116 assertEquals(msg.getOptionalFixed32(), 0);
117 assertEquals(msg.getOptionalFixed64(), 0);
118 assertEquals(msg.getOptionalSfixed32(), 0);
119 assertEquals(msg.getOptionalSfixed64(), 0);
120 assertEquals(msg.getOptionalFloat(), 0);
121 assertEquals(msg.getOptionalDouble(), 0);
122 assertEquals(msg.getOptionalString(), '');
123
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700124 // TODO(b/26173701): when we change bytes fields default getter to return
125 // Uint8Array, we'll want to switch this assertion to match the u8 case.
Feng Xiaoe841bac2015-12-11 17:09:20 -0800126 assertEquals(typeof msg.getOptionalBytes(), 'string');
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700127 assertEquals(msg.getOptionalBytes_asU8() instanceof Uint8Array, true);
128 assertEquals(typeof msg.getOptionalBytes_asB64(), 'string');
Feng Xiaoe841bac2015-12-11 17:09:20 -0800129 assertEquals(msg.getOptionalBytes().length, 0);
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700130 assertEquals(msg.getOptionalBytes_asU8().length, 0);
131 assertEquals(msg.getOptionalBytes_asB64(), '');
132
133 assertEquals(msg.getOptionalForeignEnum(),
134 proto.jspb.test.Proto3Enum.PROTO3_FOO);
Feng Xiaoe841bac2015-12-11 17:09:20 -0800135 assertEquals(msg.getOptionalForeignMessage(), undefined);
136 assertEquals(msg.getOptionalForeignMessage(), undefined);
137
138 assertEquals(msg.getRepeatedInt32List().length, 0);
139 assertEquals(msg.getRepeatedInt64List().length, 0);
140 assertEquals(msg.getRepeatedUint32List().length, 0);
141 assertEquals(msg.getRepeatedUint64List().length, 0);
142 assertEquals(msg.getRepeatedSint32List().length, 0);
143 assertEquals(msg.getRepeatedSint64List().length, 0);
144 assertEquals(msg.getRepeatedFixed32List().length, 0);
145 assertEquals(msg.getRepeatedFixed64List().length, 0);
146 assertEquals(msg.getRepeatedSfixed32List().length, 0);
147 assertEquals(msg.getRepeatedSfixed64List().length, 0);
148 assertEquals(msg.getRepeatedFloatList().length, 0);
149 assertEquals(msg.getRepeatedDoubleList().length, 0);
150 assertEquals(msg.getRepeatedStringList().length, 0);
151 assertEquals(msg.getRepeatedBytesList().length, 0);
152 assertEquals(msg.getRepeatedForeignEnumList().length, 0);
153 assertEquals(msg.getRepeatedForeignMessageList().length, 0);
154
155 });
156
157
158 /**
159 * Test that all fields can be set and read via a serialization roundtrip.
160 */
161 it('testProto3FieldSetGet', function() {
162 var msg = new proto.jspb.test.TestProto3();
163
164 msg.setOptionalInt32(-42);
165 msg.setOptionalInt64(-0x7fffffff00000000);
166 msg.setOptionalUint32(0x80000000);
167 msg.setOptionalUint64(0xf000000000000000);
168 msg.setOptionalSint32(-100);
169 msg.setOptionalSint64(-0x8000000000000000);
170 msg.setOptionalFixed32(1234);
171 msg.setOptionalFixed64(0x1234567800000000);
172 msg.setOptionalSfixed32(-1234);
173 msg.setOptionalSfixed64(-0x1234567800000000);
174 msg.setOptionalFloat(1.5);
175 msg.setOptionalDouble(-1.5);
176 msg.setOptionalBool(true);
177 msg.setOptionalString('hello world');
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700178 msg.setOptionalBytes(BYTES);
Feng Xiaoe841bac2015-12-11 17:09:20 -0800179 var submsg = new proto.jspb.test.ForeignMessage();
180 submsg.setC(16);
181 msg.setOptionalForeignMessage(submsg);
182 msg.setOptionalForeignEnum(proto.jspb.test.Proto3Enum.PROTO3_BAR);
183
184 msg.setRepeatedInt32List([-42]);
185 msg.setRepeatedInt64List([-0x7fffffff00000000]);
186 msg.setRepeatedUint32List([0x80000000]);
187 msg.setRepeatedUint64List([0xf000000000000000]);
188 msg.setRepeatedSint32List([-100]);
189 msg.setRepeatedSint64List([-0x8000000000000000]);
190 msg.setRepeatedFixed32List([1234]);
191 msg.setRepeatedFixed64List([0x1234567800000000]);
192 msg.setRepeatedSfixed32List([-1234]);
193 msg.setRepeatedSfixed64List([-0x1234567800000000]);
194 msg.setRepeatedFloatList([1.5]);
195 msg.setRepeatedDoubleList([-1.5]);
196 msg.setRepeatedBoolList([true]);
197 msg.setRepeatedStringList(['hello world']);
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700198 msg.setRepeatedBytesList([BYTES]);
Feng Xiaoe841bac2015-12-11 17:09:20 -0800199 submsg = new proto.jspb.test.ForeignMessage();
200 submsg.setC(1000);
201 msg.setRepeatedForeignMessageList([submsg]);
202 msg.setRepeatedForeignEnumList([proto.jspb.test.Proto3Enum.PROTO3_BAR]);
203
204 msg.setOneofString('asdf');
205
206 var serialized = msg.serializeBinary();
207 msg = proto.jspb.test.TestProto3.deserializeBinary(serialized);
208
209 assertEquals(msg.getOptionalInt32(), -42);
210 assertEquals(msg.getOptionalInt64(), -0x7fffffff00000000);
211 assertEquals(msg.getOptionalUint32(), 0x80000000);
212 assertEquals(msg.getOptionalUint64(), 0xf000000000000000);
213 assertEquals(msg.getOptionalSint32(), -100);
214 assertEquals(msg.getOptionalSint64(), -0x8000000000000000);
215 assertEquals(msg.getOptionalFixed32(), 1234);
216 assertEquals(msg.getOptionalFixed64(), 0x1234567800000000);
217 assertEquals(msg.getOptionalSfixed32(), -1234);
218 assertEquals(msg.getOptionalSfixed64(), -0x1234567800000000);
219 assertEquals(msg.getOptionalFloat(), 1.5);
220 assertEquals(msg.getOptionalDouble(), -1.5);
221 assertEquals(msg.getOptionalBool(), true);
222 assertEquals(msg.getOptionalString(), 'hello world');
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700223 assertEquals(true, bytesCompare(msg.getOptionalBytes(), BYTES));
Feng Xiaoe841bac2015-12-11 17:09:20 -0800224 assertEquals(msg.getOptionalForeignMessage().getC(), 16);
225 assertEquals(msg.getOptionalForeignEnum(),
226 proto.jspb.test.Proto3Enum.PROTO3_BAR);
227
228 assertElementsEquals(msg.getRepeatedInt32List(), [-42]);
229 assertElementsEquals(msg.getRepeatedInt64List(), [-0x7fffffff00000000]);
230 assertElementsEquals(msg.getRepeatedUint32List(), [0x80000000]);
231 assertElementsEquals(msg.getRepeatedUint64List(), [0xf000000000000000]);
232 assertElementsEquals(msg.getRepeatedSint32List(), [-100]);
233 assertElementsEquals(msg.getRepeatedSint64List(), [-0x8000000000000000]);
234 assertElementsEquals(msg.getRepeatedFixed32List(), [1234]);
235 assertElementsEquals(msg.getRepeatedFixed64List(), [0x1234567800000000]);
236 assertElementsEquals(msg.getRepeatedSfixed32List(), [-1234]);
237 assertElementsEquals(msg.getRepeatedSfixed64List(), [-0x1234567800000000]);
238 assertElementsEquals(msg.getRepeatedFloatList(), [1.5]);
239 assertElementsEquals(msg.getRepeatedDoubleList(), [-1.5]);
240 assertElementsEquals(msg.getRepeatedBoolList(), [true]);
241 assertElementsEquals(msg.getRepeatedStringList(), ['hello world']);
242 assertEquals(msg.getRepeatedBytesList().length, 1);
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700243 assertEquals(true, bytesCompare(msg.getRepeatedBytesList()[0], BYTES));
Feng Xiaoe841bac2015-12-11 17:09:20 -0800244 assertEquals(msg.getRepeatedForeignMessageList().length, 1);
245 assertEquals(msg.getRepeatedForeignMessageList()[0].getC(), 1000);
246 assertElementsEquals(msg.getRepeatedForeignEnumList(),
247 [proto.jspb.test.Proto3Enum.PROTO3_BAR]);
248
249 assertEquals(msg.getOneofString(), 'asdf');
250 });
251
252
253 /**
254 * Test that oneofs continue to have a notion of field presence.
255 */
256 it('testOneofs', function() {
Adam Cozzette13fd0452017-09-12 10:32:01 -0700257 // Default instance.
Feng Xiaoe841bac2015-12-11 17:09:20 -0800258 var msg = new proto.jspb.test.TestProto3();
Nikolai Vavilov970a4fd2016-04-24 14:38:16 +0300259 assertEquals(msg.getOneofUint32(), 0);
Feng Xiaoe841bac2015-12-11 17:09:20 -0800260 assertEquals(msg.getOneofForeignMessage(), undefined);
Nikolai Vavilov970a4fd2016-04-24 14:38:16 +0300261 assertEquals(msg.getOneofString(), '');
262 assertEquals(msg.getOneofBytes(), '');
Adam Cozzette13fd0452017-09-12 10:32:01 -0700263
Feng Xiao9086d962016-07-13 13:47:51 -0700264 assertFalse(msg.hasOneofUint32());
Adam Cozzette13fd0452017-09-12 10:32:01 -0700265 assertFalse(msg.hasOneofForeignMessage());
Feng Xiao9086d962016-07-13 13:47:51 -0700266 assertFalse(msg.hasOneofString());
267 assertFalse(msg.hasOneofBytes());
Feng Xiaoe841bac2015-12-11 17:09:20 -0800268
Adam Cozzette13fd0452017-09-12 10:32:01 -0700269 // Integer field.
Feng Xiaoe841bac2015-12-11 17:09:20 -0800270 msg.setOneofUint32(42);
271 assertEquals(msg.getOneofUint32(), 42);
272 assertEquals(msg.getOneofForeignMessage(), undefined);
Nikolai Vavilov970a4fd2016-04-24 14:38:16 +0300273 assertEquals(msg.getOneofString(), '');
274 assertEquals(msg.getOneofBytes(), '');
Adam Cozzette13fd0452017-09-12 10:32:01 -0700275
Feng Xiao9086d962016-07-13 13:47:51 -0700276 assertTrue(msg.hasOneofUint32());
Adam Cozzette13fd0452017-09-12 10:32:01 -0700277 assertFalse(msg.hasOneofForeignMessage());
Feng Xiao9086d962016-07-13 13:47:51 -0700278 assertFalse(msg.hasOneofString());
279 assertFalse(msg.hasOneofBytes());
Feng Xiaoe841bac2015-12-11 17:09:20 -0800280
Adam Cozzette13fd0452017-09-12 10:32:01 -0700281 // Sub-message field.
Feng Xiaoe841bac2015-12-11 17:09:20 -0800282 var submsg = new proto.jspb.test.ForeignMessage();
283 msg.setOneofForeignMessage(submsg);
Nikolai Vavilov970a4fd2016-04-24 14:38:16 +0300284 assertEquals(msg.getOneofUint32(), 0);
Feng Xiaoe841bac2015-12-11 17:09:20 -0800285 assertEquals(msg.getOneofForeignMessage(), submsg);
Nikolai Vavilov970a4fd2016-04-24 14:38:16 +0300286 assertEquals(msg.getOneofString(), '');
287 assertEquals(msg.getOneofBytes(), '');
Adam Cozzette13fd0452017-09-12 10:32:01 -0700288
Feng Xiao9086d962016-07-13 13:47:51 -0700289 assertFalse(msg.hasOneofUint32());
Adam Cozzette13fd0452017-09-12 10:32:01 -0700290 assertTrue(msg.hasOneofForeignMessage());
Feng Xiao9086d962016-07-13 13:47:51 -0700291 assertFalse(msg.hasOneofString());
292 assertFalse(msg.hasOneofBytes());
Feng Xiaoe841bac2015-12-11 17:09:20 -0800293
Adam Cozzette13fd0452017-09-12 10:32:01 -0700294 // String field.
Feng Xiaoe841bac2015-12-11 17:09:20 -0800295 msg.setOneofString('hello');
Nikolai Vavilov970a4fd2016-04-24 14:38:16 +0300296 assertEquals(msg.getOneofUint32(), 0);
Feng Xiaoe841bac2015-12-11 17:09:20 -0800297 assertEquals(msg.getOneofForeignMessage(), undefined);
298 assertEquals(msg.getOneofString(), 'hello');
Nikolai Vavilov970a4fd2016-04-24 14:38:16 +0300299 assertEquals(msg.getOneofBytes(), '');
Adam Cozzette13fd0452017-09-12 10:32:01 -0700300
Feng Xiao9086d962016-07-13 13:47:51 -0700301 assertFalse(msg.hasOneofUint32());
Adam Cozzette13fd0452017-09-12 10:32:01 -0700302 assertFalse(msg.hasOneofForeignMessage());
Feng Xiao9086d962016-07-13 13:47:51 -0700303 assertTrue(msg.hasOneofString());
304 assertFalse(msg.hasOneofBytes());
Feng Xiaoe841bac2015-12-11 17:09:20 -0800305
Adam Cozzette13fd0452017-09-12 10:32:01 -0700306 // Bytes field.
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700307 msg.setOneofBytes(goog.crypt.base64.encodeString('\u00FF\u00FF'));
Nikolai Vavilov970a4fd2016-04-24 14:38:16 +0300308 assertEquals(msg.getOneofUint32(), 0);
Feng Xiaoe841bac2015-12-11 17:09:20 -0800309 assertEquals(msg.getOneofForeignMessage(), undefined);
Nikolai Vavilov970a4fd2016-04-24 14:38:16 +0300310 assertEquals(msg.getOneofString(), '');
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700311 assertEquals(msg.getOneofBytes_asB64(),
312 goog.crypt.base64.encodeString('\u00FF\u00FF'));
Adam Cozzette13fd0452017-09-12 10:32:01 -0700313
Feng Xiao9086d962016-07-13 13:47:51 -0700314 assertFalse(msg.hasOneofUint32());
Adam Cozzette13fd0452017-09-12 10:32:01 -0700315 assertFalse(msg.hasOneofForeignMessage());
Feng Xiao9086d962016-07-13 13:47:51 -0700316 assertFalse(msg.hasOneofString());
317 assertTrue(msg.hasOneofBytes());
Feng Xiaoe841bac2015-12-11 17:09:20 -0800318 });
319
320
321 /**
322 * Test that "default"-valued primitive fields are not emitted on the wire.
323 */
324 it('testNoSerializeDefaults', function() {
325 var msg = new proto.jspb.test.TestProto3();
326
327 // Set each primitive to a non-default value, then back to its default, to
328 // ensure that the serialization is actually checking the value and not just
329 // whether it has ever been set.
330 msg.setOptionalInt32(42);
331 msg.setOptionalInt32(0);
332 msg.setOptionalDouble(3.14);
333 msg.setOptionalDouble(0.0);
334 msg.setOptionalBool(true);
335 msg.setOptionalBool(false);
336 msg.setOptionalString('hello world');
337 msg.setOptionalString('');
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700338 msg.setOptionalBytes(goog.crypt.base64.encodeString('\u00FF\u00FF'));
Feng Xiaoe841bac2015-12-11 17:09:20 -0800339 msg.setOptionalBytes('');
340 msg.setOptionalForeignMessage(new proto.jspb.test.ForeignMessage());
341 msg.setOptionalForeignMessage(null);
342 msg.setOptionalForeignEnum(proto.jspb.test.Proto3Enum.PROTO3_BAR);
343 msg.setOptionalForeignEnum(proto.jspb.test.Proto3Enum.PROTO3_FOO);
344 msg.setOneofUint32(32);
Bo Yangcc8ca5b2016-09-19 13:45:07 -0700345 msg.clearOneofUint32();
Feng Xiaoe841bac2015-12-11 17:09:20 -0800346
347
348 var serialized = msg.serializeBinary();
349 assertEquals(0, serialized.length);
350 });
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700351
352 /**
353 * Test that base64 string and Uint8Array are interchangeable in bytes fields.
354 */
355 it('testBytesFieldsInterop', function() {
356 var msg = new proto.jspb.test.TestProto3();
357 // Set as a base64 string and check all the getters work.
358 msg.setOptionalBytes(BYTES_B64);
359 assertTrue(bytesCompare(msg.getOptionalBytes_asU8(), BYTES));
360 assertTrue(bytesCompare(msg.getOptionalBytes_asB64(), BYTES));
361 assertTrue(bytesCompare(msg.getOptionalBytes(), BYTES));
362
363 // Test binary serialize round trip doesn't break it.
364 msg = proto.jspb.test.TestProto3.deserializeBinary(msg.serializeBinary());
365 assertTrue(bytesCompare(msg.getOptionalBytes_asU8(), BYTES));
366 assertTrue(bytesCompare(msg.getOptionalBytes_asB64(), BYTES));
367 assertTrue(bytesCompare(msg.getOptionalBytes(), BYTES));
368
369 msg = new proto.jspb.test.TestProto3();
370 // Set as a Uint8Array and check all the getters work.
371 msg.setOptionalBytes(BYTES);
372 assertTrue(bytesCompare(msg.getOptionalBytes_asU8(), BYTES));
373 assertTrue(bytesCompare(msg.getOptionalBytes_asB64(), BYTES));
374 assertTrue(bytesCompare(msg.getOptionalBytes(), BYTES));
375
376 });
Adam Cozzette5a76e632016-11-17 16:48:38 -0800377
Adam Cozzette0894e072018-11-09 11:28:22 -0800378
Adam Cozzette5a76e632016-11-17 16:48:38 -0800379 it('testTimestampWellKnownType', function() {
380 var msg = new proto.google.protobuf.Timestamp();
381 msg.fromDate(new Date(123456789));
382 assertEquals(123456, msg.getSeconds());
383 assertEquals(789000000, msg.getNanos());
384 var date = msg.toDate();
385 assertEquals(123456789, date.getTime());
Adam Cozzette0894e072018-11-09 11:28:22 -0800386 var anotherMsg = proto.google.protobuf.Timestamp.fromDate(date);
387 assertEquals(msg.getSeconds(), anotherMsg.getSeconds());
388 assertEquals(msg.getNanos(), anotherMsg.getNanos());
Adam Cozzette5a76e632016-11-17 16:48:38 -0800389 });
390
391 it('testStructWellKnownType', function() {
392 var jsObj = {
393 abc: "def",
394 number: 12345.678,
395 nullKey: null,
396 boolKey: true,
397 listKey: [1, null, true, false, "abc"],
398 structKey: {foo: "bar", somenum: 123},
399 complicatedKey: [{xyz: {abc: [3, 4, null, false]}}, "zzz"]
400 };
401
402 var struct = proto.google.protobuf.Struct.fromJavaScript(jsObj);
403 var jsObj2 = struct.toJavaScript();
404
405 assertEquals("def", jsObj2.abc);
406 assertEquals(12345.678, jsObj2.number);
407 assertEquals(null, jsObj2.nullKey);
408 assertEquals(true, jsObj2.boolKey);
409 assertEquals("abc", jsObj2.listKey[4]);
410 assertEquals("bar", jsObj2.structKey.foo);
411 assertEquals(4, jsObj2.complicatedKey[0].xyz.abc[1]);
412 });
Feng Xiaoe841bac2015-12-11 17:09:20 -0800413});