Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(141)

Side by Side Diff: third_party/protobuf/js/proto3_test.js

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

Powered by Google App Engine
This is Rietveld 408576698