blob: f8868716d16fddd80321db5c38b86567e65d392b [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
31goog.require('goog.testing.asserts');
Josh Habermane9f31ee2016-02-04 10:29:27 -080032
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 Habermane9f31ee2016-02-04 10:29:27 -080035
Josh Haberman77af5d02016-02-04 16:11:07 -080036// CommonJS-LoadFromFile: proto3_test_pb proto.jspb.test
Feng Xiaoe841bac2015-12-11 17:09:20 -080037goog.require('proto.jspb.test.Proto3Enum');
38goog.require('proto.jspb.test.TestProto3');
39
40/**
41 * Helper: compare a bytes field to a string with codepoints 0--255.
42 * @param {Uint8Array|string} arr
43 * @param {string} str
44 * @return {boolean}
45 */
46function bytesCompare(arr, str) {
47 if (arr.length != str.length) {
48 return false;
49 }
50 if (typeof arr == 'string') {
51 for (var i = 0; i < arr.length; i++) {
52 if (arr.charCodeAt(i) != str.charCodeAt(i)) {
53 return false;
54 }
55 }
56 return true;
57 } else {
58 for (var i = 0; i < arr.length; i++) {
59 if (arr[i] != str.charCodeAt(i)) {
60 return false;
61 }
62 }
63 return true;
64 }
65}
66
67
68describe('proto3Test', function() {
69 /**
70 * Test defaults for proto3 message fields.
71 */
72 it('testProto3FieldDefaults', function() {
73 var msg = new proto.jspb.test.TestProto3();
74
75 assertEquals(msg.getOptionalInt32(), 0);
76 assertEquals(msg.getOptionalInt64(), 0);
77 assertEquals(msg.getOptionalUint32(), 0);
78 assertEquals(msg.getOptionalUint64(), 0);
79 assertEquals(msg.getOptionalSint32(), 0);
80 assertEquals(msg.getOptionalSint64(), 0);
81 assertEquals(msg.getOptionalFixed32(), 0);
82 assertEquals(msg.getOptionalFixed64(), 0);
83 assertEquals(msg.getOptionalSfixed32(), 0);
84 assertEquals(msg.getOptionalSfixed64(), 0);
85 assertEquals(msg.getOptionalFloat(), 0);
86 assertEquals(msg.getOptionalDouble(), 0);
87 assertEquals(msg.getOptionalString(), '');
88
89 // If/when we change bytes fields to return Uint8Array, we'll want to switch
90 // to this assertion instead:
91 //assertEquals(msg.getOptionalBytes() instanceof Uint8Array, true);
92 assertEquals(typeof msg.getOptionalBytes(), 'string');
93
94 assertEquals(msg.getOptionalBytes().length, 0);
95 assertEquals(msg.getOptionalForeignEnum(), proto.jspb.test.Proto3Enum.PROTO3_FOO);
96 assertEquals(msg.getOptionalForeignMessage(), undefined);
97 assertEquals(msg.getOptionalForeignMessage(), undefined);
98
99 assertEquals(msg.getRepeatedInt32List().length, 0);
100 assertEquals(msg.getRepeatedInt64List().length, 0);
101 assertEquals(msg.getRepeatedUint32List().length, 0);
102 assertEquals(msg.getRepeatedUint64List().length, 0);
103 assertEquals(msg.getRepeatedSint32List().length, 0);
104 assertEquals(msg.getRepeatedSint64List().length, 0);
105 assertEquals(msg.getRepeatedFixed32List().length, 0);
106 assertEquals(msg.getRepeatedFixed64List().length, 0);
107 assertEquals(msg.getRepeatedSfixed32List().length, 0);
108 assertEquals(msg.getRepeatedSfixed64List().length, 0);
109 assertEquals(msg.getRepeatedFloatList().length, 0);
110 assertEquals(msg.getRepeatedDoubleList().length, 0);
111 assertEquals(msg.getRepeatedStringList().length, 0);
112 assertEquals(msg.getRepeatedBytesList().length, 0);
113 assertEquals(msg.getRepeatedForeignEnumList().length, 0);
114 assertEquals(msg.getRepeatedForeignMessageList().length, 0);
115
116 });
117
118
119 /**
120 * Test that all fields can be set and read via a serialization roundtrip.
121 */
122 it('testProto3FieldSetGet', function() {
123 var msg = new proto.jspb.test.TestProto3();
124
125 msg.setOptionalInt32(-42);
126 msg.setOptionalInt64(-0x7fffffff00000000);
127 msg.setOptionalUint32(0x80000000);
128 msg.setOptionalUint64(0xf000000000000000);
129 msg.setOptionalSint32(-100);
130 msg.setOptionalSint64(-0x8000000000000000);
131 msg.setOptionalFixed32(1234);
132 msg.setOptionalFixed64(0x1234567800000000);
133 msg.setOptionalSfixed32(-1234);
134 msg.setOptionalSfixed64(-0x1234567800000000);
135 msg.setOptionalFloat(1.5);
136 msg.setOptionalDouble(-1.5);
137 msg.setOptionalBool(true);
138 msg.setOptionalString('hello world');
139 msg.setOptionalBytes('bytes');
140 var submsg = new proto.jspb.test.ForeignMessage();
141 submsg.setC(16);
142 msg.setOptionalForeignMessage(submsg);
143 msg.setOptionalForeignEnum(proto.jspb.test.Proto3Enum.PROTO3_BAR);
144
145 msg.setRepeatedInt32List([-42]);
146 msg.setRepeatedInt64List([-0x7fffffff00000000]);
147 msg.setRepeatedUint32List([0x80000000]);
148 msg.setRepeatedUint64List([0xf000000000000000]);
149 msg.setRepeatedSint32List([-100]);
150 msg.setRepeatedSint64List([-0x8000000000000000]);
151 msg.setRepeatedFixed32List([1234]);
152 msg.setRepeatedFixed64List([0x1234567800000000]);
153 msg.setRepeatedSfixed32List([-1234]);
154 msg.setRepeatedSfixed64List([-0x1234567800000000]);
155 msg.setRepeatedFloatList([1.5]);
156 msg.setRepeatedDoubleList([-1.5]);
157 msg.setRepeatedBoolList([true]);
158 msg.setRepeatedStringList(['hello world']);
159 msg.setRepeatedBytesList(['bytes']);
160 submsg = new proto.jspb.test.ForeignMessage();
161 submsg.setC(1000);
162 msg.setRepeatedForeignMessageList([submsg]);
163 msg.setRepeatedForeignEnumList([proto.jspb.test.Proto3Enum.PROTO3_BAR]);
164
165 msg.setOneofString('asdf');
166
167 var serialized = msg.serializeBinary();
168 msg = proto.jspb.test.TestProto3.deserializeBinary(serialized);
169
170 assertEquals(msg.getOptionalInt32(), -42);
171 assertEquals(msg.getOptionalInt64(), -0x7fffffff00000000);
172 assertEquals(msg.getOptionalUint32(), 0x80000000);
173 assertEquals(msg.getOptionalUint64(), 0xf000000000000000);
174 assertEquals(msg.getOptionalSint32(), -100);
175 assertEquals(msg.getOptionalSint64(), -0x8000000000000000);
176 assertEquals(msg.getOptionalFixed32(), 1234);
177 assertEquals(msg.getOptionalFixed64(), 0x1234567800000000);
178 assertEquals(msg.getOptionalSfixed32(), -1234);
179 assertEquals(msg.getOptionalSfixed64(), -0x1234567800000000);
180 assertEquals(msg.getOptionalFloat(), 1.5);
181 assertEquals(msg.getOptionalDouble(), -1.5);
182 assertEquals(msg.getOptionalBool(), true);
183 assertEquals(msg.getOptionalString(), 'hello world');
184 assertEquals(true, bytesCompare(msg.getOptionalBytes(), 'bytes'));
185 assertEquals(msg.getOptionalForeignMessage().getC(), 16);
186 assertEquals(msg.getOptionalForeignEnum(),
187 proto.jspb.test.Proto3Enum.PROTO3_BAR);
188
189 assertElementsEquals(msg.getRepeatedInt32List(), [-42]);
190 assertElementsEquals(msg.getRepeatedInt64List(), [-0x7fffffff00000000]);
191 assertElementsEquals(msg.getRepeatedUint32List(), [0x80000000]);
192 assertElementsEquals(msg.getRepeatedUint64List(), [0xf000000000000000]);
193 assertElementsEquals(msg.getRepeatedSint32List(), [-100]);
194 assertElementsEquals(msg.getRepeatedSint64List(), [-0x8000000000000000]);
195 assertElementsEquals(msg.getRepeatedFixed32List(), [1234]);
196 assertElementsEquals(msg.getRepeatedFixed64List(), [0x1234567800000000]);
197 assertElementsEquals(msg.getRepeatedSfixed32List(), [-1234]);
198 assertElementsEquals(msg.getRepeatedSfixed64List(), [-0x1234567800000000]);
199 assertElementsEquals(msg.getRepeatedFloatList(), [1.5]);
200 assertElementsEquals(msg.getRepeatedDoubleList(), [-1.5]);
201 assertElementsEquals(msg.getRepeatedBoolList(), [true]);
202 assertElementsEquals(msg.getRepeatedStringList(), ['hello world']);
203 assertEquals(msg.getRepeatedBytesList().length, 1);
204 assertEquals(true, bytesCompare(msg.getRepeatedBytesList()[0], 'bytes'));
205 assertEquals(msg.getRepeatedForeignMessageList().length, 1);
206 assertEquals(msg.getRepeatedForeignMessageList()[0].getC(), 1000);
207 assertElementsEquals(msg.getRepeatedForeignEnumList(),
208 [proto.jspb.test.Proto3Enum.PROTO3_BAR]);
209
210 assertEquals(msg.getOneofString(), 'asdf');
211 });
212
213
214 /**
215 * Test that oneofs continue to have a notion of field presence.
216 */
217 it('testOneofs', function() {
218 var msg = new proto.jspb.test.TestProto3();
219
220 assertEquals(msg.getOneofUint32(), undefined);
221 assertEquals(msg.getOneofForeignMessage(), undefined);
222 assertEquals(msg.getOneofString(), undefined);
223 assertEquals(msg.getOneofBytes(), undefined);
224
225 msg.setOneofUint32(42);
226 assertEquals(msg.getOneofUint32(), 42);
227 assertEquals(msg.getOneofForeignMessage(), undefined);
228 assertEquals(msg.getOneofString(), undefined);
229 assertEquals(msg.getOneofBytes(), undefined);
230
231
232 var submsg = new proto.jspb.test.ForeignMessage();
233 msg.setOneofForeignMessage(submsg);
234 assertEquals(msg.getOneofUint32(), undefined);
235 assertEquals(msg.getOneofForeignMessage(), submsg);
236 assertEquals(msg.getOneofString(), undefined);
237 assertEquals(msg.getOneofBytes(), undefined);
238
239 msg.setOneofString('hello');
240 assertEquals(msg.getOneofUint32(), undefined);
241 assertEquals(msg.getOneofForeignMessage(), undefined);
242 assertEquals(msg.getOneofString(), 'hello');
243 assertEquals(msg.getOneofBytes(), undefined);
244
245 msg.setOneofBytes('\u00FF\u00FF');
246 assertEquals(msg.getOneofUint32(), undefined);
247 assertEquals(msg.getOneofForeignMessage(), undefined);
248 assertEquals(msg.getOneofString(), undefined);
249 assertEquals(msg.getOneofBytes(), '\u00FF\u00FF');
250 });
251
252
253 /**
254 * Test that "default"-valued primitive fields are not emitted on the wire.
255 */
256 it('testNoSerializeDefaults', function() {
257 var msg = new proto.jspb.test.TestProto3();
258
259 // Set each primitive to a non-default value, then back to its default, to
260 // ensure that the serialization is actually checking the value and not just
261 // whether it has ever been set.
262 msg.setOptionalInt32(42);
263 msg.setOptionalInt32(0);
264 msg.setOptionalDouble(3.14);
265 msg.setOptionalDouble(0.0);
266 msg.setOptionalBool(true);
267 msg.setOptionalBool(false);
268 msg.setOptionalString('hello world');
269 msg.setOptionalString('');
270 msg.setOptionalBytes('\u00FF\u00FF');
271 msg.setOptionalBytes('');
272 msg.setOptionalForeignMessage(new proto.jspb.test.ForeignMessage());
273 msg.setOptionalForeignMessage(null);
274 msg.setOptionalForeignEnum(proto.jspb.test.Proto3Enum.PROTO3_BAR);
275 msg.setOptionalForeignEnum(proto.jspb.test.Proto3Enum.PROTO3_FOO);
276 msg.setOneofUint32(32);
277 msg.setOneofUint32(null);
278
279
280 var serialized = msg.serializeBinary();
281 assertEquals(0, serialized.length);
282 });
283});