blob: 8de157524bc6da80dbec5dff1650abff96892366 [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');
Joshua Haberman51daaba2021-02-04 14:09:49 -080044goog.require('jspb.Message');
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) {
Rafi Kamal58d44202019-11-11 17:06:56 -080057 if (typeof arr === 'string') {
Jisi Liu3b3c8ab2016-03-30 11:39:59 -070058 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 * Test default values don't affect equality test.
75 */
76 it('testEqualsProto3', function() {
77 var msg1 = new proto.jspb.test.TestProto3();
78 var msg2 = new proto.jspb.test.TestProto3();
Anton Kast4d6712e2020-06-05 16:39:02 -070079 msg2.setSingularString('');
Adam Cozzette13fd0452017-09-12 10:32:01 -070080
81 assertTrue(jspb.Message.equals(msg1, msg2));
82 });
83
84
85 /**
86 * Test setting when a field has default semantics.
87 */
88 it('testSetProto3ToValueAndBackToDefault', function() {
89 var msg = new proto.jspb.test.TestProto3();
90
91 // Setting should work normally.
Anton Kast4d6712e2020-06-05 16:39:02 -070092 msg.setSingularString('optionalString');
93 assertEquals(msg.getSingularString(), 'optionalString');
Adam Cozzette13fd0452017-09-12 10:32:01 -070094
95 // Clearing should work too ...
Anton Kast4d6712e2020-06-05 16:39:02 -070096 msg.setSingularString('');
97 assertEquals(msg.getSingularString(), '');
Adam Cozzette13fd0452017-09-12 10:32:01 -070098
99 // ... and shouldn't affect the equality with a brand new message.
100 assertTrue(jspb.Message.equals(msg, new proto.jspb.test.TestProto3()));
101 });
102
Feng Xiaoe841bac2015-12-11 17:09:20 -0800103 /**
104 * Test defaults for proto3 message fields.
105 */
106 it('testProto3FieldDefaults', function() {
107 var msg = new proto.jspb.test.TestProto3();
108
Anton Kast4d6712e2020-06-05 16:39:02 -0700109 assertEquals(msg.getSingularInt32(), 0);
110 assertEquals(msg.getSingularInt64(), 0);
111 assertEquals(msg.getSingularUint32(), 0);
112 assertEquals(msg.getSingularUint64(), 0);
113 assertEquals(msg.getSingularSint32(), 0);
114 assertEquals(msg.getSingularSint64(), 0);
115 assertEquals(msg.getSingularFixed32(), 0);
116 assertEquals(msg.getSingularFixed64(), 0);
117 assertEquals(msg.getSingularSfixed32(), 0);
118 assertEquals(msg.getSingularSfixed64(), 0);
119 assertEquals(msg.getSingularFloat(), 0);
120 assertEquals(msg.getSingularDouble(), 0);
121 assertEquals(msg.getSingularString(), '');
122
123 // TODO(b/26173701): when we change bytes fields default getter to return
124 // Uint8Array, we'll want to switch this assertion to match the u8 case.
125 assertEquals(typeof msg.getSingularBytes(), 'string');
126 assertEquals(msg.getSingularBytes_asU8() instanceof Uint8Array, true);
127 assertEquals(typeof msg.getSingularBytes_asB64(), 'string');
128 assertEquals(msg.getSingularBytes().length, 0);
129 assertEquals(msg.getSingularBytes_asU8().length, 0);
130 assertEquals(msg.getSingularBytes_asB64(), '');
131
Joshua Haberman6ba52412020-07-09 18:17:47 -0700132 assertEquals(
133 msg.getSingularForeignEnum(), proto.jspb.test.Proto3Enum.PROTO3_FOO);
Anton Kast4d6712e2020-06-05 16:39:02 -0700134 assertEquals(msg.getSingularForeignMessage(), undefined);
135 assertEquals(msg.getSingularForeignMessage(), undefined);
136
137 assertEquals(msg.getRepeatedInt32List().length, 0);
138 assertEquals(msg.getRepeatedInt64List().length, 0);
139 assertEquals(msg.getRepeatedUint32List().length, 0);
140 assertEquals(msg.getRepeatedUint64List().length, 0);
141 assertEquals(msg.getRepeatedSint32List().length, 0);
142 assertEquals(msg.getRepeatedSint64List().length, 0);
143 assertEquals(msg.getRepeatedFixed32List().length, 0);
144 assertEquals(msg.getRepeatedFixed64List().length, 0);
145 assertEquals(msg.getRepeatedSfixed32List().length, 0);
146 assertEquals(msg.getRepeatedSfixed64List().length, 0);
147 assertEquals(msg.getRepeatedFloatList().length, 0);
148 assertEquals(msg.getRepeatedDoubleList().length, 0);
149 assertEquals(msg.getRepeatedStringList().length, 0);
150 assertEquals(msg.getRepeatedBytesList().length, 0);
151 assertEquals(msg.getRepeatedForeignEnumList().length, 0);
152 assertEquals(msg.getRepeatedForeignMessageList().length, 0);
153 });
154
155 /**
156 * Test presence for proto3 optional fields.
157 */
158 it('testProto3Optional', function() {
159 var msg = new proto.jspb.test.TestProto3();
160
Feng Xiaoe841bac2015-12-11 17:09:20 -0800161 assertEquals(msg.getOptionalInt32(), 0);
162 assertEquals(msg.getOptionalInt64(), 0);
163 assertEquals(msg.getOptionalUint32(), 0);
164 assertEquals(msg.getOptionalUint64(), 0);
165 assertEquals(msg.getOptionalSint32(), 0);
166 assertEquals(msg.getOptionalSint64(), 0);
167 assertEquals(msg.getOptionalFixed32(), 0);
168 assertEquals(msg.getOptionalFixed64(), 0);
169 assertEquals(msg.getOptionalSfixed32(), 0);
170 assertEquals(msg.getOptionalSfixed64(), 0);
171 assertEquals(msg.getOptionalFloat(), 0);
172 assertEquals(msg.getOptionalDouble(), 0);
173 assertEquals(msg.getOptionalString(), '');
174
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700175 // TODO(b/26173701): when we change bytes fields default getter to return
176 // Uint8Array, we'll want to switch this assertion to match the u8 case.
Feng Xiaoe841bac2015-12-11 17:09:20 -0800177 assertEquals(typeof msg.getOptionalBytes(), 'string');
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700178 assertEquals(msg.getOptionalBytes_asU8() instanceof Uint8Array, true);
179 assertEquals(typeof msg.getOptionalBytes_asB64(), 'string');
Feng Xiaoe841bac2015-12-11 17:09:20 -0800180 assertEquals(msg.getOptionalBytes().length, 0);
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700181 assertEquals(msg.getOptionalBytes_asU8().length, 0);
182 assertEquals(msg.getOptionalBytes_asB64(), '');
183
Joshua Haberman51daaba2021-02-04 14:09:49 -0800184 assertEquals(
185 msg.getOptionalForeignEnum(), proto.jspb.test.Proto3Enum.PROTO3_FOO);
Feng Xiaoe841bac2015-12-11 17:09:20 -0800186 assertEquals(msg.getOptionalForeignMessage(), undefined);
187 assertEquals(msg.getOptionalForeignMessage(), undefined);
188
Anton Kast4d6712e2020-06-05 16:39:02 -0700189 // Serializing an empty proto yields the empty string.
190 assertEquals(msg.serializeBinary().length, 0);
Feng Xiaoe841bac2015-12-11 17:09:20 -0800191
Anton Kast4d6712e2020-06-05 16:39:02 -0700192 // Values start as unset, but can be explicitly set even to default values
193 // like 0.
194 assertFalse(msg.hasOptionalInt32());
195 msg.setOptionalInt32(0);
196 assertTrue(msg.hasOptionalInt32());
197
198 assertFalse(msg.hasOptionalInt64());
199 msg.setOptionalInt64(0);
200 assertTrue(msg.hasOptionalInt64());
201
202 assertFalse(msg.hasOptionalString());
Joshua Haberman6ba52412020-07-09 18:17:47 -0700203 msg.setOptionalString('');
Anton Kast4d6712e2020-06-05 16:39:02 -0700204 assertTrue(msg.hasOptionalString());
205
206 // Now the proto will have a non-zero size, even though its values are 0.
207 var serialized = msg.serializeBinary();
208 assertNotEquals(serialized.length, 0);
209
210 var msg2 = proto.jspb.test.TestProto3.deserializeBinary(serialized);
211 assertTrue(msg2.hasOptionalInt32());
212 assertTrue(msg2.hasOptionalInt64());
213 assertTrue(msg2.hasOptionalString());
214
215 // We can clear fields to go back to empty.
216 msg2.clearOptionalInt32();
217 assertFalse(msg2.hasOptionalInt32());
218
219 msg2.clearOptionalString();
220 assertFalse(msg2.hasOptionalString());
Feng Xiaoe841bac2015-12-11 17:09:20 -0800221 });
222
Feng Xiaoe841bac2015-12-11 17:09:20 -0800223 /**
Anton Kast4d6712e2020-06-05 16:39:02 -0700224 * Test that all fields can be set ,and read via a serialization roundtrip.
Feng Xiaoe841bac2015-12-11 17:09:20 -0800225 */
226 it('testProto3FieldSetGet', function() {
227 var msg = new proto.jspb.test.TestProto3();
228
Anton Kast4d6712e2020-06-05 16:39:02 -0700229 msg.setSingularInt32(-42);
230 msg.setSingularInt64(-0x7fffffff00000000);
231 msg.setSingularUint32(0x80000000);
232 msg.setSingularUint64(0xf000000000000000);
233 msg.setSingularSint32(-100);
234 msg.setSingularSint64(-0x8000000000000000);
235 msg.setSingularFixed32(1234);
236 msg.setSingularFixed64(0x1234567800000000);
237 msg.setSingularSfixed32(-1234);
238 msg.setSingularSfixed64(-0x1234567800000000);
239 msg.setSingularFloat(1.5);
240 msg.setSingularDouble(-1.5);
241 msg.setSingularBool(true);
242 msg.setSingularString('hello world');
243 msg.setSingularBytes(BYTES);
Feng Xiaoe841bac2015-12-11 17:09:20 -0800244 var submsg = new proto.jspb.test.ForeignMessage();
245 submsg.setC(16);
Anton Kast4d6712e2020-06-05 16:39:02 -0700246 msg.setSingularForeignMessage(submsg);
247 msg.setSingularForeignEnum(proto.jspb.test.Proto3Enum.PROTO3_BAR);
Feng Xiaoe841bac2015-12-11 17:09:20 -0800248
249 msg.setRepeatedInt32List([-42]);
250 msg.setRepeatedInt64List([-0x7fffffff00000000]);
251 msg.setRepeatedUint32List([0x80000000]);
252 msg.setRepeatedUint64List([0xf000000000000000]);
253 msg.setRepeatedSint32List([-100]);
254 msg.setRepeatedSint64List([-0x8000000000000000]);
255 msg.setRepeatedFixed32List([1234]);
256 msg.setRepeatedFixed64List([0x1234567800000000]);
257 msg.setRepeatedSfixed32List([-1234]);
258 msg.setRepeatedSfixed64List([-0x1234567800000000]);
259 msg.setRepeatedFloatList([1.5]);
260 msg.setRepeatedDoubleList([-1.5]);
261 msg.setRepeatedBoolList([true]);
262 msg.setRepeatedStringList(['hello world']);
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700263 msg.setRepeatedBytesList([BYTES]);
Feng Xiaoe841bac2015-12-11 17:09:20 -0800264 submsg = new proto.jspb.test.ForeignMessage();
265 submsg.setC(1000);
266 msg.setRepeatedForeignMessageList([submsg]);
267 msg.setRepeatedForeignEnumList([proto.jspb.test.Proto3Enum.PROTO3_BAR]);
268
269 msg.setOneofString('asdf');
270
271 var serialized = msg.serializeBinary();
272 msg = proto.jspb.test.TestProto3.deserializeBinary(serialized);
273
Anton Kast4d6712e2020-06-05 16:39:02 -0700274 assertEquals(msg.getSingularInt32(), -42);
275 assertEquals(msg.getSingularInt64(), -0x7fffffff00000000);
276 assertEquals(msg.getSingularUint32(), 0x80000000);
277 assertEquals(msg.getSingularUint64(), 0xf000000000000000);
278 assertEquals(msg.getSingularSint32(), -100);
279 assertEquals(msg.getSingularSint64(), -0x8000000000000000);
280 assertEquals(msg.getSingularFixed32(), 1234);
281 assertEquals(msg.getSingularFixed64(), 0x1234567800000000);
282 assertEquals(msg.getSingularSfixed32(), -1234);
283 assertEquals(msg.getSingularSfixed64(), -0x1234567800000000);
284 assertEquals(msg.getSingularFloat(), 1.5);
285 assertEquals(msg.getSingularDouble(), -1.5);
286 assertEquals(msg.getSingularBool(), true);
287 assertEquals(msg.getSingularString(), 'hello world');
288 assertEquals(true, bytesCompare(msg.getSingularBytes(), BYTES));
289 assertEquals(msg.getSingularForeignMessage().getC(), 16);
Joshua Haberman6ba52412020-07-09 18:17:47 -0700290 assertEquals(
291 msg.getSingularForeignEnum(), proto.jspb.test.Proto3Enum.PROTO3_BAR);
Feng Xiaoe841bac2015-12-11 17:09:20 -0800292
293 assertElementsEquals(msg.getRepeatedInt32List(), [-42]);
294 assertElementsEquals(msg.getRepeatedInt64List(), [-0x7fffffff00000000]);
295 assertElementsEquals(msg.getRepeatedUint32List(), [0x80000000]);
296 assertElementsEquals(msg.getRepeatedUint64List(), [0xf000000000000000]);
297 assertElementsEquals(msg.getRepeatedSint32List(), [-100]);
298 assertElementsEquals(msg.getRepeatedSint64List(), [-0x8000000000000000]);
299 assertElementsEquals(msg.getRepeatedFixed32List(), [1234]);
300 assertElementsEquals(msg.getRepeatedFixed64List(), [0x1234567800000000]);
301 assertElementsEquals(msg.getRepeatedSfixed32List(), [-1234]);
302 assertElementsEquals(msg.getRepeatedSfixed64List(), [-0x1234567800000000]);
303 assertElementsEquals(msg.getRepeatedFloatList(), [1.5]);
304 assertElementsEquals(msg.getRepeatedDoubleList(), [-1.5]);
305 assertElementsEquals(msg.getRepeatedBoolList(), [true]);
306 assertElementsEquals(msg.getRepeatedStringList(), ['hello world']);
307 assertEquals(msg.getRepeatedBytesList().length, 1);
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700308 assertEquals(true, bytesCompare(msg.getRepeatedBytesList()[0], BYTES));
Feng Xiaoe841bac2015-12-11 17:09:20 -0800309 assertEquals(msg.getRepeatedForeignMessageList().length, 1);
310 assertEquals(msg.getRepeatedForeignMessageList()[0].getC(), 1000);
Joshua Haberman51daaba2021-02-04 14:09:49 -0800311 assertElementsEquals(
312 msg.getRepeatedForeignEnumList(),
Feng Xiaoe841bac2015-12-11 17:09:20 -0800313 [proto.jspb.test.Proto3Enum.PROTO3_BAR]);
314
315 assertEquals(msg.getOneofString(), 'asdf');
316 });
317
318
319 /**
320 * Test that oneofs continue to have a notion of field presence.
321 */
322 it('testOneofs', function() {
Adam Cozzette13fd0452017-09-12 10:32:01 -0700323 // Default instance.
Feng Xiaoe841bac2015-12-11 17:09:20 -0800324 var msg = new proto.jspb.test.TestProto3();
Nikolai Vavilov970a4fd2016-04-24 14:38:16 +0300325 assertEquals(msg.getOneofUint32(), 0);
Feng Xiaoe841bac2015-12-11 17:09:20 -0800326 assertEquals(msg.getOneofForeignMessage(), undefined);
Nikolai Vavilov970a4fd2016-04-24 14:38:16 +0300327 assertEquals(msg.getOneofString(), '');
328 assertEquals(msg.getOneofBytes(), '');
Adam Cozzette13fd0452017-09-12 10:32:01 -0700329
Feng Xiao9086d962016-07-13 13:47:51 -0700330 assertFalse(msg.hasOneofUint32());
Adam Cozzette13fd0452017-09-12 10:32:01 -0700331 assertFalse(msg.hasOneofForeignMessage());
Feng Xiao9086d962016-07-13 13:47:51 -0700332 assertFalse(msg.hasOneofString());
333 assertFalse(msg.hasOneofBytes());
Feng Xiaoe841bac2015-12-11 17:09:20 -0800334
Adam Cozzette13fd0452017-09-12 10:32:01 -0700335 // Integer field.
Feng Xiaoe841bac2015-12-11 17:09:20 -0800336 msg.setOneofUint32(42);
337 assertEquals(msg.getOneofUint32(), 42);
338 assertEquals(msg.getOneofForeignMessage(), undefined);
Nikolai Vavilov970a4fd2016-04-24 14:38:16 +0300339 assertEquals(msg.getOneofString(), '');
340 assertEquals(msg.getOneofBytes(), '');
Adam Cozzette13fd0452017-09-12 10:32:01 -0700341
Feng Xiao9086d962016-07-13 13:47:51 -0700342 assertTrue(msg.hasOneofUint32());
Adam Cozzette13fd0452017-09-12 10:32:01 -0700343 assertFalse(msg.hasOneofForeignMessage());
Feng Xiao9086d962016-07-13 13:47:51 -0700344 assertFalse(msg.hasOneofString());
345 assertFalse(msg.hasOneofBytes());
Feng Xiaoe841bac2015-12-11 17:09:20 -0800346
Adam Cozzette13fd0452017-09-12 10:32:01 -0700347 // Sub-message field.
Feng Xiaoe841bac2015-12-11 17:09:20 -0800348 var submsg = new proto.jspb.test.ForeignMessage();
349 msg.setOneofForeignMessage(submsg);
Nikolai Vavilov970a4fd2016-04-24 14:38:16 +0300350 assertEquals(msg.getOneofUint32(), 0);
Feng Xiaoe841bac2015-12-11 17:09:20 -0800351 assertEquals(msg.getOneofForeignMessage(), submsg);
Nikolai Vavilov970a4fd2016-04-24 14:38:16 +0300352 assertEquals(msg.getOneofString(), '');
353 assertEquals(msg.getOneofBytes(), '');
Adam Cozzette13fd0452017-09-12 10:32:01 -0700354
Feng Xiao9086d962016-07-13 13:47:51 -0700355 assertFalse(msg.hasOneofUint32());
Adam Cozzette13fd0452017-09-12 10:32:01 -0700356 assertTrue(msg.hasOneofForeignMessage());
Feng Xiao9086d962016-07-13 13:47:51 -0700357 assertFalse(msg.hasOneofString());
358 assertFalse(msg.hasOneofBytes());
Feng Xiaoe841bac2015-12-11 17:09:20 -0800359
Adam Cozzette13fd0452017-09-12 10:32:01 -0700360 // String field.
Feng Xiaoe841bac2015-12-11 17:09:20 -0800361 msg.setOneofString('hello');
Nikolai Vavilov970a4fd2016-04-24 14:38:16 +0300362 assertEquals(msg.getOneofUint32(), 0);
Feng Xiaoe841bac2015-12-11 17:09:20 -0800363 assertEquals(msg.getOneofForeignMessage(), undefined);
364 assertEquals(msg.getOneofString(), 'hello');
Nikolai Vavilov970a4fd2016-04-24 14:38:16 +0300365 assertEquals(msg.getOneofBytes(), '');
Adam Cozzette13fd0452017-09-12 10:32:01 -0700366
Feng Xiao9086d962016-07-13 13:47:51 -0700367 assertFalse(msg.hasOneofUint32());
Adam Cozzette13fd0452017-09-12 10:32:01 -0700368 assertFalse(msg.hasOneofForeignMessage());
Feng Xiao9086d962016-07-13 13:47:51 -0700369 assertTrue(msg.hasOneofString());
370 assertFalse(msg.hasOneofBytes());
Feng Xiaoe841bac2015-12-11 17:09:20 -0800371
Adam Cozzette13fd0452017-09-12 10:32:01 -0700372 // Bytes field.
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700373 msg.setOneofBytes(goog.crypt.base64.encodeString('\u00FF\u00FF'));
Nikolai Vavilov970a4fd2016-04-24 14:38:16 +0300374 assertEquals(msg.getOneofUint32(), 0);
Feng Xiaoe841bac2015-12-11 17:09:20 -0800375 assertEquals(msg.getOneofForeignMessage(), undefined);
Nikolai Vavilov970a4fd2016-04-24 14:38:16 +0300376 assertEquals(msg.getOneofString(), '');
Joshua Haberman51daaba2021-02-04 14:09:49 -0800377 assertEquals(
378 msg.getOneofBytes_asB64(),
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700379 goog.crypt.base64.encodeString('\u00FF\u00FF'));
Adam Cozzette13fd0452017-09-12 10:32:01 -0700380
Feng Xiao9086d962016-07-13 13:47:51 -0700381 assertFalse(msg.hasOneofUint32());
Adam Cozzette13fd0452017-09-12 10:32:01 -0700382 assertFalse(msg.hasOneofForeignMessage());
Feng Xiao9086d962016-07-13 13:47:51 -0700383 assertFalse(msg.hasOneofString());
384 assertTrue(msg.hasOneofBytes());
Feng Xiaoe841bac2015-12-11 17:09:20 -0800385 });
386
387
388 /**
389 * Test that "default"-valued primitive fields are not emitted on the wire.
390 */
391 it('testNoSerializeDefaults', function() {
392 var msg = new proto.jspb.test.TestProto3();
393
394 // Set each primitive to a non-default value, then back to its default, to
395 // ensure that the serialization is actually checking the value and not just
396 // whether it has ever been set.
Anton Kast4d6712e2020-06-05 16:39:02 -0700397 msg.setSingularInt32(42);
398 msg.setSingularInt32(0);
399 msg.setSingularDouble(3.14);
400 msg.setSingularDouble(0.0);
401 msg.setSingularBool(true);
402 msg.setSingularBool(false);
403 msg.setSingularString('hello world');
404 msg.setSingularString('');
405 msg.setSingularBytes(goog.crypt.base64.encodeString('\u00FF\u00FF'));
406 msg.setSingularBytes('');
407 msg.setSingularForeignMessage(new proto.jspb.test.ForeignMessage());
408 msg.setSingularForeignMessage(null);
409 msg.setSingularForeignEnum(proto.jspb.test.Proto3Enum.PROTO3_BAR);
410 msg.setSingularForeignEnum(proto.jspb.test.Proto3Enum.PROTO3_FOO);
Feng Xiaoe841bac2015-12-11 17:09:20 -0800411 msg.setOneofUint32(32);
Bo Yangcc8ca5b2016-09-19 13:45:07 -0700412 msg.clearOneofUint32();
Feng Xiaoe841bac2015-12-11 17:09:20 -0800413
414
415 var serialized = msg.serializeBinary();
416 assertEquals(0, serialized.length);
417 });
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700418
419 /**
420 * Test that base64 string and Uint8Array are interchangeable in bytes fields.
421 */
422 it('testBytesFieldsInterop', function() {
423 var msg = new proto.jspb.test.TestProto3();
424 // Set as a base64 string and check all the getters work.
Anton Kast4d6712e2020-06-05 16:39:02 -0700425 msg.setSingularBytes(BYTES_B64);
426 assertTrue(bytesCompare(msg.getSingularBytes_asU8(), BYTES));
427 assertTrue(bytesCompare(msg.getSingularBytes_asB64(), BYTES));
428 assertTrue(bytesCompare(msg.getSingularBytes(), BYTES));
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700429
430 // Test binary serialize round trip doesn't break it.
431 msg = proto.jspb.test.TestProto3.deserializeBinary(msg.serializeBinary());
Anton Kast4d6712e2020-06-05 16:39:02 -0700432 assertTrue(bytesCompare(msg.getSingularBytes_asU8(), BYTES));
433 assertTrue(bytesCompare(msg.getSingularBytes_asB64(), BYTES));
434 assertTrue(bytesCompare(msg.getSingularBytes(), BYTES));
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700435
436 msg = new proto.jspb.test.TestProto3();
437 // Set as a Uint8Array and check all the getters work.
Anton Kast4d6712e2020-06-05 16:39:02 -0700438 msg.setSingularBytes(BYTES);
439 assertTrue(bytesCompare(msg.getSingularBytes_asU8(), BYTES));
440 assertTrue(bytesCompare(msg.getSingularBytes_asB64(), BYTES));
441 assertTrue(bytesCompare(msg.getSingularBytes(), BYTES));
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700442 });
Adam Cozzette5a76e632016-11-17 16:48:38 -0800443
444 it('testTimestampWellKnownType', function() {
445 var msg = new proto.google.protobuf.Timestamp();
446 msg.fromDate(new Date(123456789));
447 assertEquals(123456, msg.getSeconds());
448 assertEquals(789000000, msg.getNanos());
449 var date = msg.toDate();
450 assertEquals(123456789, date.getTime());
Adam Cozzette0894e072018-11-09 11:28:22 -0800451 var anotherMsg = proto.google.protobuf.Timestamp.fromDate(date);
452 assertEquals(msg.getSeconds(), anotherMsg.getSeconds());
453 assertEquals(msg.getNanos(), anotherMsg.getNanos());
Adam Cozzette5a76e632016-11-17 16:48:38 -0800454 });
455
456 it('testStructWellKnownType', function() {
457 var jsObj = {
Joshua Haberman51daaba2021-02-04 14:09:49 -0800458 abc: 'def',
Adam Cozzette5a76e632016-11-17 16:48:38 -0800459 number: 12345.678,
460 nullKey: null,
461 boolKey: true,
Joshua Haberman51daaba2021-02-04 14:09:49 -0800462 listKey: [1, null, true, false, 'abc'],
463 structKey: {foo: 'bar', somenum: 123},
464 complicatedKey: [{xyz: {abc: [3, 4, null, false]}}, 'zzz']
Adam Cozzette5a76e632016-11-17 16:48:38 -0800465 };
466
467 var struct = proto.google.protobuf.Struct.fromJavaScript(jsObj);
468 var jsObj2 = struct.toJavaScript();
469
Joshua Haberman51daaba2021-02-04 14:09:49 -0800470 assertEquals('def', jsObj2.abc);
Adam Cozzette5a76e632016-11-17 16:48:38 -0800471 assertEquals(12345.678, jsObj2.number);
472 assertEquals(null, jsObj2.nullKey);
473 assertEquals(true, jsObj2.boolKey);
Joshua Haberman51daaba2021-02-04 14:09:49 -0800474 assertEquals('abc', jsObj2.listKey[4]);
475 assertEquals('bar', jsObj2.structKey.foo);
Adam Cozzette5a76e632016-11-17 16:48:38 -0800476 assertEquals(4, jsObj2.complicatedKey[0].xyz.abc[1]);
477 });
Feng Xiaoe841bac2015-12-11 17:09:20 -0800478});