Adam Cozzette | 10ea251 | 2017-02-28 14:20:56 -0800 | [diff] [blame] | 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 | // Test suite is written using Jasmine -- see http://jasmine.github.io/ |
| 32 | |
| 33 | goog.setTestOnly(); |
| 34 | |
| 35 | goog.require('goog.json'); |
| 36 | goog.require('goog.testing.asserts'); |
| 37 | goog.require('goog.userAgent'); |
| 38 | |
| 39 | // CommonJS-LoadFromFile: google-protobuf jspb |
| 40 | goog.require('jspb.Message'); |
| 41 | |
| 42 | // CommonJS-LoadFromFile: test5_pb proto.jspb.exttest.beta |
| 43 | goog.require('proto.jspb.exttest.beta.floatingStrField'); |
| 44 | |
| 45 | // CommonJS-LoadFromFile: test3_pb proto.jspb.exttest |
| 46 | goog.require('proto.jspb.exttest.floatingMsgField'); |
| 47 | |
| 48 | // CommonJS-LoadFromFile: test4_pb proto.jspb.exttest |
| 49 | goog.require('proto.jspb.exttest.floatingMsgFieldTwo'); |
| 50 | |
| 51 | // CommonJS-LoadFromFile: test_pb proto.jspb.test |
| 52 | goog.require('proto.jspb.test.CloneExtension'); |
| 53 | goog.require('proto.jspb.test.Complex'); |
| 54 | goog.require('proto.jspb.test.DefaultValues'); |
| 55 | goog.require('proto.jspb.test.Empty'); |
| 56 | goog.require('proto.jspb.test.EnumContainer'); |
| 57 | goog.require('proto.jspb.test.floatingMsgField'); |
| 58 | goog.require('proto.jspb.test.FloatingPointFields'); |
| 59 | goog.require('proto.jspb.test.floatingStrField'); |
| 60 | goog.require('proto.jspb.test.HasExtensions'); |
| 61 | goog.require('proto.jspb.test.IndirectExtension'); |
| 62 | goog.require('proto.jspb.test.IsExtension'); |
| 63 | goog.require('proto.jspb.test.OptionalFields'); |
| 64 | goog.require('proto.jspb.test.OuterEnum'); |
| 65 | goog.require('proto.jspb.test.OuterMessage.Complex'); |
| 66 | goog.require('proto.jspb.test.Simple1'); |
| 67 | goog.require('proto.jspb.test.Simple2'); |
| 68 | goog.require('proto.jspb.test.SpecialCases'); |
| 69 | goog.require('proto.jspb.test.TestClone'); |
| 70 | goog.require('proto.jspb.test.TestEndsWithBytes'); |
| 71 | goog.require('proto.jspb.test.TestGroup'); |
| 72 | goog.require('proto.jspb.test.TestGroup1'); |
| 73 | goog.require('proto.jspb.test.TestMessageWithOneof'); |
| 74 | goog.require('proto.jspb.test.TestReservedNames'); |
| 75 | goog.require('proto.jspb.test.TestReservedNamesExtension'); |
| 76 | |
| 77 | // CommonJS-LoadFromFile: test2_pb proto.jspb.test |
| 78 | goog.require('proto.jspb.test.ExtensionMessage'); |
| 79 | goog.require('proto.jspb.test.TestExtensionsMessage'); |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | describe('Message test suite', function() { |
| 85 | it('testEmptyProto', function() { |
| 86 | var empty1 = new proto.jspb.test.Empty([]); |
| 87 | var empty2 = new proto.jspb.test.Empty([]); |
| 88 | assertObjectEquals({}, empty1.toObject()); |
| 89 | assertObjectEquals('Message should not be corrupted:', empty2, empty1); |
| 90 | }); |
| 91 | |
| 92 | it('testTopLevelEnum', function() { |
| 93 | var response = new proto.jspb.test.EnumContainer([]); |
| 94 | response.setOuterEnum(proto.jspb.test.OuterEnum.FOO); |
| 95 | assertEquals(proto.jspb.test.OuterEnum.FOO, response.getOuterEnum()); |
| 96 | }); |
| 97 | |
| 98 | it('testByteStrings', function() { |
| 99 | var data = new proto.jspb.test.DefaultValues([]); |
| 100 | data.setBytesField('some_bytes'); |
| 101 | assertEquals('some_bytes', data.getBytesField()); |
| 102 | }); |
| 103 | |
| 104 | it('testComplexConversion', function() { |
| 105 | var data1 = ['a',,, [, 11], [[, 22], [, 33]],, ['s1', 's2'],, 1]; |
| 106 | var data2 = ['a',,, [, 11], [[, 22], [, 33]],, ['s1', 's2'],, 1]; |
| 107 | var foo = new proto.jspb.test.Complex(data1); |
| 108 | var bar = new proto.jspb.test.Complex(data2); |
| 109 | var result = foo.toObject(); |
| 110 | assertObjectEquals({ |
| 111 | aString: 'a', |
| 112 | anOutOfOrderBool: 1, |
| 113 | aNestedMessage: { |
| 114 | anInt: 11 |
| 115 | }, |
| 116 | aRepeatedMessageList: [{anInt: 22}, {anInt: 33}], |
| 117 | aRepeatedStringList: ['s1', 's2'] |
| 118 | }, result); |
| 119 | |
| 120 | // Now test with the jspb instances included. |
| 121 | result = foo.toObject(true /* opt_includeInstance */); |
| 122 | assertObjectEquals({ |
| 123 | aString: 'a', |
| 124 | anOutOfOrderBool: 1, |
| 125 | aNestedMessage: { |
| 126 | anInt: 11, |
| 127 | $jspbMessageInstance: foo.getANestedMessage() |
| 128 | }, |
| 129 | aRepeatedMessageList: [ |
| 130 | {anInt: 22, $jspbMessageInstance: foo.getARepeatedMessageList()[0]}, |
| 131 | {anInt: 33, $jspbMessageInstance: foo.getARepeatedMessageList()[1]} |
| 132 | ], |
| 133 | aRepeatedStringList: ['s1', 's2'], |
| 134 | $jspbMessageInstance: foo |
| 135 | }, result); |
| 136 | |
| 137 | }); |
| 138 | |
| 139 | it('testMissingFields', function() { |
| 140 | var foo = new proto.jspb.test.Complex([ |
| 141 | undefined, undefined, undefined, [], |
| 142 | undefined, undefined, undefined, undefined]); |
| 143 | var bar = new proto.jspb.test.Complex([ |
| 144 | undefined, undefined, undefined, [], |
| 145 | undefined, undefined, undefined, undefined]); |
| 146 | var result = foo.toObject(); |
| 147 | assertObjectEquals({ |
| 148 | aString: undefined, |
| 149 | anOutOfOrderBool: undefined, |
| 150 | aNestedMessage: { |
| 151 | anInt: undefined |
| 152 | }, |
| 153 | // Note: JsPb converts undefined repeated fields to empty arrays. |
| 154 | aRepeatedMessageList: [], |
| 155 | aRepeatedStringList: [] |
| 156 | }, result); |
| 157 | |
| 158 | }); |
| 159 | |
| 160 | it('testNestedComplexMessage', function() { |
| 161 | // Instantiate the message and set a unique field, just to ensure that we |
| 162 | // are not getting jspb.test.Complex instead. |
| 163 | var msg = new proto.jspb.test.OuterMessage.Complex(); |
| 164 | msg.setInnerComplexField(5); |
| 165 | }); |
| 166 | |
| 167 | it('testSpecialCases', function() { |
| 168 | // Note: Some property names are reserved in JavaScript. |
| 169 | // These names are converted to the Js property named pb_<reserved_name>. |
| 170 | var special = |
| 171 | new proto.jspb.test.SpecialCases(['normal', 'default', 'function', |
| 172 | 'var']); |
| 173 | var result = special.toObject(); |
| 174 | assertObjectEquals({ |
| 175 | normal: 'normal', |
| 176 | pb_default: 'default', |
| 177 | pb_function: 'function', |
| 178 | pb_var: 'var' |
| 179 | }, result); |
| 180 | }); |
| 181 | |
| 182 | it('testDefaultValues', function() { |
| 183 | var defaultString = "default<>\'\"abc"; |
| 184 | var response = new proto.jspb.test.DefaultValues(); |
| 185 | |
| 186 | // Test toObject |
| 187 | var expectedObject = { |
| 188 | stringField: defaultString, |
| 189 | boolField: true, |
| 190 | intField: 11, |
| 191 | enumField: 13, |
| 192 | emptyField: '', |
| 193 | bytesField: 'bW9v' |
| 194 | }; |
| 195 | assertObjectEquals(expectedObject, response.toObject()); |
| 196 | |
| 197 | |
| 198 | // Test getters |
| 199 | response = new proto.jspb.test.DefaultValues(); |
| 200 | assertEquals(defaultString, response.getStringField()); |
| 201 | assertEquals(true, response.getBoolField()); |
| 202 | assertEquals(11, response.getIntField()); |
| 203 | assertEquals(13, response.getEnumField()); |
| 204 | assertEquals('', response.getEmptyField()); |
| 205 | assertEquals('bW9v', response.getBytesField()); |
| 206 | |
| 207 | function makeDefault(values) { |
| 208 | return new proto.jspb.test.DefaultValues(values); |
| 209 | } |
| 210 | |
| 211 | // Test with undefined values, |
| 212 | // Use push to workaround IE treating undefined array elements as holes. |
| 213 | response = makeDefault([undefined, undefined, undefined, undefined]); |
| 214 | assertEquals(defaultString, response.getStringField()); |
| 215 | assertEquals(true, response.getBoolField()); |
| 216 | assertEquals(11, response.getIntField()); |
| 217 | assertEquals(13, response.getEnumField()); |
| 218 | assertFalse(response.hasStringField()); |
| 219 | assertFalse(response.hasBoolField()); |
| 220 | assertFalse(response.hasIntField()); |
| 221 | assertFalse(response.hasEnumField()); |
| 222 | |
| 223 | // Test with null values, as would be returned by a JSON serializer. |
| 224 | response = makeDefault([null, null, null, null]); |
| 225 | assertEquals(defaultString, response.getStringField()); |
| 226 | assertEquals(true, response.getBoolField()); |
| 227 | assertEquals(11, response.getIntField()); |
| 228 | assertEquals(13, response.getEnumField()); |
| 229 | assertFalse(response.hasStringField()); |
| 230 | assertFalse(response.hasBoolField()); |
| 231 | assertFalse(response.hasIntField()); |
| 232 | assertFalse(response.hasEnumField()); |
| 233 | |
| 234 | // Test with false-like values. |
| 235 | response = makeDefault(['', false, 0, 0]); |
| 236 | assertEquals('', response.getStringField()); |
| 237 | assertEquals(false, response.getBoolField()); |
| 238 | assertEquals(true, response.getIntField() == 0); |
| 239 | assertEquals(true, response.getEnumField() == 0); |
| 240 | assertTrue(response.hasStringField()); |
| 241 | assertTrue(response.hasBoolField()); |
| 242 | assertTrue(response.hasIntField()); |
| 243 | assertTrue(response.hasEnumField()); |
| 244 | |
| 245 | // Test that clearing the values reverts them to the default state. |
| 246 | response = makeDefault(['blah', false, 111, 77]); |
| 247 | response.clearStringField(); response.clearBoolField(); |
| 248 | response.clearIntField(); response.clearEnumField(); |
| 249 | assertEquals(defaultString, response.getStringField()); |
| 250 | assertEquals(true, response.getBoolField()); |
| 251 | assertEquals(11, response.getIntField()); |
| 252 | assertEquals(13, response.getEnumField()); |
| 253 | assertFalse(response.hasStringField()); |
| 254 | assertFalse(response.hasBoolField()); |
| 255 | assertFalse(response.hasIntField()); |
| 256 | assertFalse(response.hasEnumField()); |
| 257 | |
| 258 | // Test that setFoo(null) clears the values. |
| 259 | response = makeDefault(['blah', false, 111, 77]); |
| 260 | response.setStringField(null); response.setBoolField(null); |
| 261 | response.setIntField(undefined); response.setEnumField(undefined); |
| 262 | assertEquals(defaultString, response.getStringField()); |
| 263 | assertEquals(true, response.getBoolField()); |
| 264 | assertEquals(11, response.getIntField()); |
| 265 | assertEquals(13, response.getEnumField()); |
| 266 | assertFalse(response.hasStringField()); |
| 267 | assertFalse(response.hasBoolField()); |
| 268 | assertFalse(response.hasIntField()); |
| 269 | assertFalse(response.hasEnumField()); |
| 270 | }); |
| 271 | |
| 272 | it('testMessageRegistration', function() { |
| 273 | // goog.require(SomeResponse) will include its library, which will in |
| 274 | // turn add SomeResponse to the message registry. |
| 275 | assertEquals(jspb.Message.registry_['res'], proto.jspb.test.SomeResponse); |
| 276 | }); |
| 277 | |
| 278 | it('testClearFields', function() { |
| 279 | var data = ['str', true, [11], [[22], [33]], ['s1', 's2']]; |
| 280 | var foo = new proto.jspb.test.OptionalFields(data); |
| 281 | foo.clearAString(); |
| 282 | foo.clearABool(); |
| 283 | foo.clearANestedMessage(); |
| 284 | foo.clearARepeatedMessageList(); |
| 285 | foo.clearARepeatedStringList(); |
| 286 | assertEquals('', foo.getAString()); |
| 287 | assertEquals(false, foo.getABool()); |
| 288 | assertUndefined(foo.getANestedMessage()); |
| 289 | assertFalse(foo.hasAString()); |
| 290 | assertFalse(foo.hasABool()); |
| 291 | assertObjectEquals([], foo.getARepeatedMessageList()); |
| 292 | assertObjectEquals([], foo.getARepeatedStringList()); |
| 293 | // NOTE: We want the missing fields in 'expected' to be undefined, |
| 294 | // but we actually get a sparse array instead. We could use something |
| 295 | // like [1,undefined,2] to avoid this, except that this is still |
| 296 | // sparse on IE. No comment... |
| 297 | var expected = [,,, [], []]; |
| 298 | expected[0] = expected[1] = expected[2] = undefined; |
| 299 | assertObjectEquals(expected, foo.toArray()); |
| 300 | |
| 301 | // Test set(null). We could deprecated this in favor of clear(), but |
| 302 | // it's also convenient to have. |
| 303 | data = ['str', true, [11], [[22], [33]], ['s1', 's2']]; |
| 304 | foo = new proto.jspb.test.OptionalFields(data); |
| 305 | foo.setAString(null); |
| 306 | foo.setABool(null); |
| 307 | foo.setANestedMessage(null); |
| 308 | foo.setARepeatedMessageList(null); |
| 309 | foo.setARepeatedStringList(null); |
| 310 | assertEquals('', foo.getAString()); |
| 311 | assertEquals(false, foo.getABool()); |
| 312 | assertNull(foo.getANestedMessage()); |
| 313 | assertFalse(foo.hasAString()); |
| 314 | assertFalse(foo.hasABool()); |
| 315 | assertObjectEquals([], foo.getARepeatedMessageList()); |
| 316 | assertObjectEquals([], foo.getARepeatedStringList()); |
| 317 | assertObjectEquals([null, null, null, [], []], foo.toArray()); |
| 318 | |
| 319 | // Test set(undefined). Again, not something we really need, and not |
| 320 | // supported directly by our typing, but it should 'do the right thing'. |
| 321 | data = ['str', true, [11], [[22], [33]], ['s1', 's2']]; |
| 322 | foo = new proto.jspb.test.OptionalFields(data); |
| 323 | foo.setAString(undefined); |
| 324 | foo.setABool(undefined); |
| 325 | foo.setANestedMessage(undefined); |
| 326 | foo.setARepeatedMessageList(undefined); |
| 327 | foo.setARepeatedStringList(undefined); |
| 328 | assertEquals('', foo.getAString()); |
| 329 | assertEquals(false, foo.getABool()); |
| 330 | assertUndefined(foo.getANestedMessage()); |
| 331 | assertFalse(foo.hasAString()); |
| 332 | assertFalse(foo.hasABool()); |
| 333 | assertObjectEquals([], foo.getARepeatedMessageList()); |
| 334 | assertObjectEquals([], foo.getARepeatedStringList()); |
| 335 | expected = [,,, [], []]; |
| 336 | expected[0] = expected[1] = expected[2] = undefined; |
| 337 | assertObjectEquals(expected, foo.toArray()); |
| 338 | }); |
| 339 | |
| 340 | it('testDifferenceRawObject', function() { |
| 341 | var p1 = new proto.jspb.test.HasExtensions(['hi', 'diff', {}]); |
| 342 | var p2 = new proto.jspb.test.HasExtensions(['hi', 'what', |
| 343 | {1000: 'unique'}]); |
| 344 | var diff = /** @type {proto.jspb.test.HasExtensions} */ |
| 345 | (jspb.Message.difference(p1, p2)); |
| 346 | assertEquals('', diff.getStr1()); |
| 347 | assertEquals('what', diff.getStr2()); |
| 348 | assertEquals('', diff.getStr3()); |
| 349 | assertEquals('unique', diff.extensionObject_[1000]); |
| 350 | }); |
| 351 | |
| 352 | it('testEqualsSimple', function() { |
| 353 | var s1 = new proto.jspb.test.Simple1(['hi']); |
| 354 | assertTrue(jspb.Message.equals(s1, new proto.jspb.test.Simple1(['hi']))); |
| 355 | assertFalse(jspb.Message.equals(s1, new proto.jspb.test.Simple1(['bye']))); |
| 356 | var s1b = new proto.jspb.test.Simple1(['hi', ['hello']]); |
| 357 | assertTrue(jspb.Message.equals(s1b, |
| 358 | new proto.jspb.test.Simple1(['hi', ['hello']]))); |
| 359 | assertTrue(jspb.Message.equals(s1b, |
| 360 | new proto.jspb.test.Simple1(['hi', ['hello', undefined, |
| 361 | undefined, undefined]]))); |
| 362 | assertFalse(jspb.Message.equals(s1b, |
| 363 | new proto.jspb.test.Simple1(['no', ['hello']]))); |
| 364 | // Test with messages of different types |
| 365 | var s2 = new proto.jspb.test.Simple2(['hi']); |
| 366 | assertFalse(jspb.Message.equals(s1, s2)); |
| 367 | }); |
| 368 | |
| 369 | it('testEquals_softComparison', function() { |
| 370 | var s1 = new proto.jspb.test.Simple1(['hi', [], null]); |
| 371 | assertTrue(jspb.Message.equals(s1, |
| 372 | new proto.jspb.test.Simple1(['hi', []]))); |
| 373 | |
| 374 | var s1b = new proto.jspb.test.Simple1(['hi', [], true]); |
| 375 | assertTrue(jspb.Message.equals(s1b, |
| 376 | new proto.jspb.test.Simple1(['hi', [], 1]))); |
| 377 | }); |
| 378 | |
| 379 | it('testEqualsComplex', function() { |
| 380 | var data1 = ['a',,, [, 11], [[, 22], [, 33]],, ['s1', 's2'],, 1]; |
| 381 | var data2 = ['a',,, [, 11], [[, 22], [, 34]],, ['s1', 's2'],, 1]; |
| 382 | var data3 = ['a',,, [, 11], [[, 22]],, ['s1', 's2'],, 1]; |
| 383 | var data4 = ['hi']; |
| 384 | var c1a = new proto.jspb.test.Complex(data1); |
| 385 | var c1b = new proto.jspb.test.Complex(data1); |
| 386 | var c2 = new proto.jspb.test.Complex(data2); |
| 387 | var c3 = new proto.jspb.test.Complex(data3); |
| 388 | var s1 = new proto.jspb.test.Simple1(data4); |
| 389 | |
| 390 | assertTrue(jspb.Message.equals(c1a, c1b)); |
| 391 | assertFalse(jspb.Message.equals(c1a, c2)); |
| 392 | assertFalse(jspb.Message.equals(c2, c3)); |
| 393 | assertFalse(jspb.Message.equals(c1a, s1)); |
| 394 | }); |
| 395 | |
| 396 | it('testEqualsExtensionsConstructed', function() { |
| 397 | assertTrue(jspb.Message.equals( |
| 398 | new proto.jspb.test.HasExtensions([]), |
| 399 | new proto.jspb.test.HasExtensions([{}]) |
| 400 | )); |
| 401 | assertTrue(jspb.Message.equals( |
| 402 | new proto.jspb.test.HasExtensions(['hi', {100: [{200: 'a'}]}]), |
| 403 | new proto.jspb.test.HasExtensions(['hi', {100: [{200: 'a'}]}]) |
| 404 | )); |
| 405 | assertFalse(jspb.Message.equals( |
| 406 | new proto.jspb.test.HasExtensions(['hi', {100: [{200: 'a'}]}]), |
| 407 | new proto.jspb.test.HasExtensions(['hi', {100: [{200: 'b'}]}]) |
| 408 | )); |
| 409 | assertTrue(jspb.Message.equals( |
| 410 | new proto.jspb.test.HasExtensions([{100: [{200: 'a'}]}]), |
| 411 | new proto.jspb.test.HasExtensions([{100: [{200: 'a'}]}]) |
| 412 | )); |
| 413 | assertTrue(jspb.Message.equals( |
| 414 | new proto.jspb.test.HasExtensions([{100: [{200: 'a'}]}]), |
| 415 | new proto.jspb.test.HasExtensions([,,, {100: [{200: 'a'}]}]) |
| 416 | )); |
| 417 | assertTrue(jspb.Message.equals( |
| 418 | new proto.jspb.test.HasExtensions([,,, {100: [{200: 'a'}]}]), |
| 419 | new proto.jspb.test.HasExtensions([{100: [{200: 'a'}]}]) |
| 420 | )); |
| 421 | assertTrue(jspb.Message.equals( |
| 422 | new proto.jspb.test.HasExtensions(['hi', {100: [{200: 'a'}]}]), |
| 423 | new proto.jspb.test.HasExtensions(['hi',,, {100: [{200: 'a'}]}]) |
| 424 | )); |
| 425 | assertTrue(jspb.Message.equals( |
| 426 | new proto.jspb.test.HasExtensions(['hi',,, {100: [{200: 'a'}]}]), |
| 427 | new proto.jspb.test.HasExtensions(['hi', {100: [{200: 'a'}]}]) |
| 428 | )); |
| 429 | }); |
| 430 | |
| 431 | it('testEqualsExtensionsUnconstructed', function() { |
| 432 | assertTrue(jspb.Message.compareFields([], [{}])); |
| 433 | assertTrue(jspb.Message.compareFields([,,, {}], [])); |
| 434 | assertTrue(jspb.Message.compareFields([,,, {}], [,, {}])); |
| 435 | assertTrue(jspb.Message.compareFields( |
| 436 | ['hi', {100: [{200: 'a'}]}], ['hi', {100: [{200: 'a'}]}])); |
| 437 | assertFalse(jspb.Message.compareFields( |
| 438 | ['hi', {100: [{200: 'a'}]}], ['hi', {100: [{200: 'b'}]}])); |
| 439 | assertTrue(jspb.Message.compareFields( |
| 440 | [{100: [{200: 'a'}]}], [{100: [{200: 'a'}]}])); |
| 441 | assertTrue(jspb.Message.compareFields( |
| 442 | [{100: [{200: 'a'}]}], [,,, {100: [{200: 'a'}]}])); |
| 443 | assertTrue(jspb.Message.compareFields( |
| 444 | [,,, {100: [{200: 'a'}]}], [{100: [{200: 'a'}]}])); |
| 445 | assertTrue(jspb.Message.compareFields( |
| 446 | ['hi', {100: [{200: 'a'}]}], ['hi',,, {100: [{200: 'a'}]}])); |
| 447 | assertTrue(jspb.Message.compareFields( |
| 448 | ['hi',,, {100: [{200: 'a'}]}], ['hi', {100: [{200: 'a'}]}])); |
| 449 | }); |
| 450 | |
| 451 | it('testToMap', function() { |
| 452 | var p1 = new proto.jspb.test.Simple1(['k', ['v']]); |
| 453 | var p2 = new proto.jspb.test.Simple1(['k1', ['v1', 'v2']]); |
| 454 | var soymap = jspb.Message.toMap([p1, p2], |
| 455 | proto.jspb.test.Simple1.prototype.getAString, |
| 456 | proto.jspb.test.Simple1.prototype.toObject); |
| 457 | assertEquals('k', soymap['k'].aString); |
| 458 | assertArrayEquals(['v'], soymap['k'].aRepeatedStringList); |
| 459 | var protomap = jspb.Message.toMap([p1, p2], |
| 460 | proto.jspb.test.Simple1.prototype.getAString); |
| 461 | assertEquals('k', protomap['k'].getAString()); |
| 462 | assertArrayEquals(['v'], protomap['k'].getARepeatedStringList()); |
| 463 | }); |
| 464 | |
| 465 | it('testClone', function() { |
| 466 | var supportsUint8Array = |
| 467 | !goog.userAgent.IE || goog.userAgent.isVersionOrHigher('10'); |
| 468 | var original = new proto.jspb.test.TestClone(); |
| 469 | original.setStr('v1'); |
| 470 | var simple1 = new proto.jspb.test.Simple1(['x1', ['y1', 'z1']]); |
| 471 | var simple2 = new proto.jspb.test.Simple1(['x2', ['y2', 'z2']]); |
| 472 | var simple3 = new proto.jspb.test.Simple1(['x3', ['y3', 'z3']]); |
| 473 | original.setSimple1(simple1); |
| 474 | original.setSimple2List([simple2, simple3]); |
| 475 | var bytes1 = supportsUint8Array ? new Uint8Array([1, 2, 3]) : '123'; |
| 476 | original.setBytesField(bytes1); |
| 477 | var extension = new proto.jspb.test.CloneExtension(); |
| 478 | extension.setExt('e1'); |
| 479 | original.setExtension(proto.jspb.test.IsExtension.extField, extension); |
| 480 | var clone = original.cloneMessage(); |
| 481 | assertArrayEquals(['v1',, ['x1', ['y1', 'z1']],, |
| 482 | [['x2', ['y2', 'z2']], ['x3', ['y3', 'z3']]], bytes1,, { 100: [, 'e1'] }], |
| 483 | clone.toArray()); |
| 484 | clone.setStr('v2'); |
| 485 | var simple4 = new proto.jspb.test.Simple1(['a1', ['b1', 'c1']]); |
| 486 | var simple5 = new proto.jspb.test.Simple1(['a2', ['b2', 'c2']]); |
| 487 | var simple6 = new proto.jspb.test.Simple1(['a3', ['b3', 'c3']]); |
| 488 | clone.setSimple1(simple4); |
| 489 | clone.setSimple2List([simple5, simple6]); |
| 490 | if (supportsUint8Array) { |
| 491 | clone.getBytesField()[0] = 4; |
| 492 | assertObjectEquals(bytes1, original.getBytesField()); |
| 493 | } |
| 494 | var bytes2 = supportsUint8Array ? new Uint8Array([4, 5, 6]) : '456'; |
| 495 | clone.setBytesField(bytes2); |
| 496 | var newExtension = new proto.jspb.test.CloneExtension(); |
| 497 | newExtension.setExt('e2'); |
| 498 | clone.setExtension(proto.jspb.test.CloneExtension.extField, newExtension); |
| 499 | assertArrayEquals(['v2',, ['a1', ['b1', 'c1']],, |
| 500 | [['a2', ['b2', 'c2']], ['a3', ['b3', 'c3']]], bytes2,, { 100: [, 'e2'] }], |
| 501 | clone.toArray()); |
| 502 | assertArrayEquals(['v1',, ['x1', ['y1', 'z1']],, |
| 503 | [['x2', ['y2', 'z2']], ['x3', ['y3', 'z3']]], bytes1,, { 100: [, 'e1'] }], |
| 504 | original.toArray()); |
| 505 | }); |
| 506 | |
| 507 | it('testCopyInto', function() { |
| 508 | var supportsUint8Array = |
| 509 | !goog.userAgent.IE || goog.userAgent.isVersionOrHigher('10'); |
| 510 | var original = new proto.jspb.test.TestClone(); |
| 511 | original.setStr('v1'); |
| 512 | var dest = new proto.jspb.test.TestClone(); |
| 513 | dest.setStr('override'); |
| 514 | var simple1 = new proto.jspb.test.Simple1(['x1', ['y1', 'z1']]); |
| 515 | var simple2 = new proto.jspb.test.Simple1(['x2', ['y2', 'z2']]); |
| 516 | var simple3 = new proto.jspb.test.Simple1(['x3', ['y3', 'z3']]); |
| 517 | var destSimple1 = new proto.jspb.test.Simple1(['ox1', ['oy1', 'oz1']]); |
| 518 | var destSimple2 = new proto.jspb.test.Simple1(['ox2', ['oy2', 'oz2']]); |
| 519 | var destSimple3 = new proto.jspb.test.Simple1(['ox3', ['oy3', 'oz3']]); |
| 520 | original.setSimple1(simple1); |
| 521 | original.setSimple2List([simple2, simple3]); |
| 522 | dest.setSimple1(destSimple1); |
| 523 | dest.setSimple2List([destSimple2, destSimple3]); |
| 524 | var bytes1 = supportsUint8Array ? new Uint8Array([1, 2, 3]) : '123'; |
| 525 | var bytes2 = supportsUint8Array ? new Uint8Array([4, 5, 6]) : '456'; |
| 526 | original.setBytesField(bytes1); |
| 527 | dest.setBytesField(bytes2); |
| 528 | var extension = new proto.jspb.test.CloneExtension(); |
| 529 | extension.setExt('e1'); |
| 530 | original.setExtension(proto.jspb.test.CloneExtension.extField, extension); |
| 531 | |
| 532 | jspb.Message.copyInto(original, dest); |
| 533 | assertArrayEquals(original.toArray(), dest.toArray()); |
| 534 | assertEquals('x1', dest.getSimple1().getAString()); |
| 535 | assertEquals('e1', |
| 536 | dest.getExtension(proto.jspb.test.CloneExtension.extField).getExt()); |
| 537 | dest.getSimple1().setAString('new value'); |
| 538 | assertNotEquals(dest.getSimple1().getAString(), |
| 539 | original.getSimple1().getAString()); |
| 540 | if (supportsUint8Array) { |
| 541 | dest.getBytesField()[0] = 7; |
| 542 | assertObjectEquals(bytes1, original.getBytesField()); |
| 543 | assertObjectEquals(new Uint8Array([7, 2, 3]), dest.getBytesField()); |
| 544 | } else { |
| 545 | dest.setBytesField('789'); |
| 546 | assertObjectEquals(bytes1, original.getBytesField()); |
| 547 | assertObjectEquals('789', dest.getBytesField()); |
| 548 | } |
| 549 | dest.getExtension(proto.jspb.test.CloneExtension.extField). |
| 550 | setExt('new value'); |
| 551 | assertNotEquals( |
| 552 | dest.getExtension(proto.jspb.test.CloneExtension.extField).getExt(), |
| 553 | original.getExtension( |
| 554 | proto.jspb.test.CloneExtension.extField).getExt()); |
| 555 | }); |
| 556 | |
| 557 | it('testCopyInto_notSameType', function() { |
| 558 | var a = new proto.jspb.test.TestClone(); |
| 559 | var b = new proto.jspb.test.Simple1(['str', ['s1', 's2']]); |
| 560 | |
| 561 | var e = assertThrows(function() { |
| 562 | jspb.Message.copyInto(a, b); |
| 563 | }); |
| 564 | assertContains('should have the same type', e.message); |
| 565 | }); |
| 566 | |
| 567 | it('testExtensions', function() { |
| 568 | var extension1 = new proto.jspb.test.IsExtension(['ext1field']); |
| 569 | var extension2 = new proto.jspb.test.Simple1(['str', ['s1', 's2']]); |
| 570 | var extendable = new proto.jspb.test.HasExtensions(['v1', 'v2', 'v3']); |
| 571 | extendable.setExtension(proto.jspb.test.IsExtension.extField, extension1); |
| 572 | extendable.setExtension(proto.jspb.test.IndirectExtension.simple, |
| 573 | extension2); |
| 574 | extendable.setExtension(proto.jspb.test.IndirectExtension.str, 'xyzzy'); |
| 575 | extendable.setExtension(proto.jspb.test.IndirectExtension.repeatedStrList, |
| 576 | ['a', 'b']); |
| 577 | var s1 = new proto.jspb.test.Simple1(['foo', ['s1', 's2']]); |
| 578 | var s2 = new proto.jspb.test.Simple1(['bar', ['t1', 't2']]); |
| 579 | extendable.setExtension( |
| 580 | proto.jspb.test.IndirectExtension.repeatedSimpleList, |
| 581 | [s1, s2]); |
| 582 | assertObjectEquals(extension1, |
| 583 | extendable.getExtension(proto.jspb.test.IsExtension.extField)); |
| 584 | assertObjectEquals(extension2, |
| 585 | extendable.getExtension(proto.jspb.test.IndirectExtension.simple)); |
| 586 | assertObjectEquals('xyzzy', |
| 587 | extendable.getExtension(proto.jspb.test.IndirectExtension.str)); |
| 588 | assertObjectEquals(['a', 'b'], extendable.getExtension( |
| 589 | proto.jspb.test.IndirectExtension.repeatedStrList)); |
| 590 | assertObjectEquals([s1, s2], extendable.getExtension( |
| 591 | proto.jspb.test.IndirectExtension.repeatedSimpleList)); |
| 592 | // Not supported yet, but it should work... |
| 593 | extendable.setExtension(proto.jspb.test.IndirectExtension.simple, null); |
| 594 | assertNull( |
| 595 | extendable.getExtension(proto.jspb.test.IndirectExtension.simple)); |
| 596 | extendable.setExtension(proto.jspb.test.IndirectExtension.str, null); |
| 597 | assertNull(extendable.getExtension(proto.jspb.test.IndirectExtension.str)); |
| 598 | |
| 599 | |
| 600 | // Extension fields with jspb.ignore = true are ignored. |
| 601 | assertUndefined(proto.jspb.test.IndirectExtension['ignored']); |
| 602 | assertUndefined(proto.jspb.test.HasExtensions['ignoredFloating']); |
| 603 | }); |
| 604 | |
| 605 | it('testFloatingExtensions', function() { |
| 606 | // From an autogenerated container. |
| 607 | var extendable = new proto.jspb.test.HasExtensions(['v1', 'v2', 'v3']); |
| 608 | var extension = new proto.jspb.test.Simple1(['foo', ['s1', 's2']]); |
| 609 | extendable.setExtension(proto.jspb.test.simple1, extension); |
| 610 | assertObjectEquals(extension, |
| 611 | extendable.getExtension(proto.jspb.test.simple1)); |
| 612 | |
| 613 | // From _lib mode. |
| 614 | extension = new proto.jspb.test.ExtensionMessage(['s1']); |
| 615 | extendable = new proto.jspb.test.TestExtensionsMessage([16]); |
| 616 | extendable.setExtension(proto.jspb.test.floatingMsgField, extension); |
| 617 | extendable.setExtension(proto.jspb.test.floatingStrField, 's2'); |
| 618 | assertObjectEquals(extension, |
| 619 | extendable.getExtension(proto.jspb.test.floatingMsgField)); |
| 620 | assertObjectEquals('s2', |
| 621 | extendable.getExtension(proto.jspb.test.floatingStrField)); |
| 622 | assertNotUndefined(proto.jspb.exttest.floatingMsgField); |
| 623 | assertNotUndefined(proto.jspb.exttest.floatingMsgFieldTwo); |
| 624 | assertNotUndefined(proto.jspb.exttest.beta.floatingStrField); |
| 625 | }); |
| 626 | |
| 627 | it('testToObject_extendedObject', function() { |
| 628 | var extension1 = new proto.jspb.test.IsExtension(['ext1field']); |
| 629 | var extension2 = new proto.jspb.test.Simple1(['str', ['s1', 's2'], true]); |
| 630 | var extendable = new proto.jspb.test.HasExtensions(['v1', 'v2', 'v3']); |
| 631 | extendable.setExtension(proto.jspb.test.IsExtension.extField, extension1); |
| 632 | extendable.setExtension(proto.jspb.test.IndirectExtension.simple, |
| 633 | extension2); |
| 634 | extendable.setExtension(proto.jspb.test.IndirectExtension.str, 'xyzzy'); |
| 635 | extendable.setExtension(proto.jspb.test.IndirectExtension.repeatedStrList, |
| 636 | ['a', 'b']); |
| 637 | var s1 = new proto.jspb.test.Simple1(['foo', ['s1', 's2'], true]); |
| 638 | var s2 = new proto.jspb.test.Simple1(['bar', ['t1', 't2'], false]); |
| 639 | extendable.setExtension( |
| 640 | proto.jspb.test.IndirectExtension.repeatedSimpleList, |
| 641 | [s1, s2]); |
| 642 | assertObjectEquals({ |
| 643 | str1: 'v1', str2: 'v2', str3: 'v3', |
| 644 | extField: { ext1: 'ext1field' }, |
| 645 | simple: { |
| 646 | aString: 'str', aRepeatedStringList: ['s1', 's2'], aBoolean: true |
| 647 | }, |
| 648 | str: 'xyzzy', |
| 649 | repeatedStrList: ['a', 'b'], |
| 650 | repeatedSimpleList: [ |
| 651 | { aString: 'foo', aRepeatedStringList: ['s1', 's2'], aBoolean: true}, |
| 652 | { aString: 'bar', aRepeatedStringList: ['t1', 't2'], aBoolean: false} |
| 653 | ] |
| 654 | }, extendable.toObject()); |
| 655 | |
| 656 | // Now, with instances included. |
| 657 | assertObjectEquals({ |
| 658 | str1: 'v1', str2: 'v2', str3: 'v3', |
| 659 | extField: { |
| 660 | ext1: 'ext1field', |
| 661 | $jspbMessageInstance: |
| 662 | extendable.getExtension(proto.jspb.test.IsExtension.extField) |
| 663 | }, |
| 664 | simple: { |
| 665 | aString: 'str', |
| 666 | aRepeatedStringList: ['s1', 's2'], |
| 667 | aBoolean: true, |
| 668 | $jspbMessageInstance: |
| 669 | extendable.getExtension(proto.jspb.test.IndirectExtension.simple) |
| 670 | }, |
| 671 | str: 'xyzzy', |
| 672 | repeatedStrList: ['a', 'b'], |
| 673 | repeatedSimpleList: [{ |
| 674 | aString: 'foo', |
| 675 | aRepeatedStringList: ['s1', 's2'], |
| 676 | aBoolean: true, |
| 677 | $jspbMessageInstance: s1 |
| 678 | }, { |
| 679 | aString: 'bar', |
| 680 | aRepeatedStringList: ['t1', 't2'], |
| 681 | aBoolean: false, |
| 682 | $jspbMessageInstance: s2 |
| 683 | }], |
| 684 | $jspbMessageInstance: extendable |
| 685 | }, extendable.toObject(true /* opt_includeInstance */)); |
| 686 | }); |
| 687 | |
| 688 | it('testInitialization_emptyArray', function() { |
| 689 | var msg = new proto.jspb.test.HasExtensions([]); |
| 690 | if (jspb.Message.MINIMIZE_MEMORY_ALLOCATIONS) { |
| 691 | assertArrayEquals([], msg.toArray()); |
| 692 | } else { |
| 693 | // Extension object is created past all regular fields. |
| 694 | assertArrayEquals([,,, {}], msg.toArray()); |
| 695 | } |
| 696 | }); |
| 697 | |
| 698 | it('testInitialization_justExtensionObject', function() { |
| 699 | var msg = new proto.jspb.test.Empty([{1: 'hi'}]); |
| 700 | // The extensionObject is not moved from its original location. |
| 701 | assertArrayEquals([{1: 'hi'}], msg.toArray()); |
| 702 | }); |
| 703 | |
| 704 | it('testInitialization_incompleteList', function() { |
| 705 | var msg = new proto.jspb.test.Empty([1, {4: 'hi'}]); |
| 706 | // The extensionObject is not moved from its original location. |
| 707 | assertArrayEquals([1, {4: 'hi'}], msg.toArray()); |
| 708 | }); |
| 709 | |
| 710 | it('testInitialization_forwardCompatible', function() { |
| 711 | var msg = new proto.jspb.test.Empty([1, 2, 3, {1: 'hi'}]); |
| 712 | assertArrayEquals([1, 2, 3, {1: 'hi'}], msg.toArray()); |
| 713 | }); |
| 714 | |
| 715 | it('testExtendedMessageEnsureObject', function() { |
| 716 | var data = new proto.jspb.test.HasExtensions(['str1', |
| 717 | {'a_key': 'an_object'}]); |
| 718 | assertEquals('an_object', data.extensionObject_['a_key']); |
| 719 | }); |
| 720 | |
| 721 | it('testToObject_hasExtensionField', function() { |
| 722 | var data = new proto.jspb.test.HasExtensions(['str1', {100: ['ext1']}]); |
| 723 | var obj = data.toObject(); |
| 724 | assertEquals('str1', obj.str1); |
| 725 | assertEquals('ext1', obj.extField.ext1); |
| 726 | }); |
| 727 | |
| 728 | it('testGetExtension', function() { |
| 729 | var data = new proto.jspb.test.HasExtensions(['str1', {100: ['ext1']}]); |
| 730 | assertEquals('str1', data.getStr1()); |
| 731 | var extension = data.getExtension(proto.jspb.test.IsExtension.extField); |
| 732 | assertNotNull(extension); |
| 733 | assertEquals('ext1', extension.getExt1()); |
| 734 | }); |
| 735 | |
| 736 | it('testSetExtension', function() { |
| 737 | var data = new proto.jspb.test.HasExtensions(); |
| 738 | var extensionMessage = new proto.jspb.test.IsExtension(['is_extension']); |
| 739 | data.setExtension(proto.jspb.test.IsExtension.extField, extensionMessage); |
| 740 | var obj = data.toObject(); |
| 741 | assertNotNull( |
| 742 | data.getExtension(proto.jspb.test.IsExtension.extField)); |
| 743 | assertEquals('is_extension', obj.extField.ext1); |
| 744 | }); |
| 745 | |
| 746 | /** |
| 747 | * Note that group is long deprecated, we only support it because JsPb has |
| 748 | * a goal of being able to generate JS classes for all proto descriptors. |
| 749 | */ |
| 750 | it('testGroups', function() { |
| 751 | var group = new proto.jspb.test.TestGroup(); |
| 752 | var someGroup = new proto.jspb.test.TestGroup.RepeatedGroup(); |
| 753 | someGroup.setId('g1'); |
| 754 | someGroup.setSomeBoolList([true, false]); |
| 755 | group.setRepeatedGroupList([someGroup]); |
| 756 | var groups = group.getRepeatedGroupList(); |
| 757 | assertEquals('g1', groups[0].getId()); |
| 758 | assertObjectEquals([true, false], groups[0].getSomeBoolList()); |
| 759 | assertObjectEquals({id: 'g1', someBoolList: [true, false]}, |
| 760 | groups[0].toObject()); |
| 761 | assertObjectEquals({ |
| 762 | repeatedGroupList: [{id: 'g1', someBoolList: [true, false]}], |
| 763 | requiredGroup: {id: undefined}, |
| 764 | optionalGroup: undefined, |
| 765 | requiredSimple: {aRepeatedStringList: [], aString: undefined}, |
| 766 | optionalSimple: undefined, |
| 767 | id: undefined |
| 768 | }, group.toObject()); |
| 769 | var group1 = new proto.jspb.test.TestGroup1(); |
| 770 | group1.setGroup(someGroup); |
| 771 | assertEquals(someGroup, group1.getGroup()); |
| 772 | }); |
| 773 | |
| 774 | it('testNonExtensionFieldsAfterExtensionRange', function() { |
| 775 | var data = [{'1': 'a_string'}]; |
| 776 | var message = new proto.jspb.test.Complex(data); |
| 777 | assertArrayEquals([], message.getARepeatedStringList()); |
| 778 | }); |
| 779 | |
| 780 | it('testReservedGetterNames', function() { |
| 781 | var message = new proto.jspb.test.TestReservedNames(); |
| 782 | message.setExtension$(11); |
| 783 | message.setExtension(proto.jspb.test.TestReservedNamesExtension.foo, 12); |
| 784 | assertEquals(11, message.getExtension$()); |
| 785 | assertEquals(12, message.getExtension( |
| 786 | proto.jspb.test.TestReservedNamesExtension.foo)); |
| 787 | assertObjectEquals({extension: 11, foo: 12}, message.toObject()); |
| 788 | }); |
| 789 | |
| 790 | it('testInitializeMessageWithUnsetOneof', function() { |
| 791 | var message = new proto.jspb.test.TestMessageWithOneof([]); |
| 792 | assertEquals( |
| 793 | proto.jspb.test.TestMessageWithOneof.PartialOneofCase. |
| 794 | PARTIAL_ONEOF_NOT_SET, |
| 795 | message.getPartialOneofCase()); |
| 796 | assertEquals( |
| 797 | proto.jspb.test.TestMessageWithOneof.RecursiveOneofCase. |
| 798 | RECURSIVE_ONEOF_NOT_SET, |
| 799 | message.getRecursiveOneofCase()); |
| 800 | }); |
| 801 | |
| 802 | it('testInitializeMessageWithSingleValueSetInOneof', function() { |
| 803 | var message = new proto.jspb.test.TestMessageWithOneof([,, 'x']); |
| 804 | |
| 805 | assertEquals('x', message.getPone()); |
| 806 | assertEquals('', message.getPthree()); |
| 807 | assertEquals( |
| 808 | proto.jspb.test.TestMessageWithOneof.PartialOneofCase.PONE, |
| 809 | message.getPartialOneofCase()); |
| 810 | }); |
| 811 | |
| 812 | it('testKeepsLastWireValueSetInUnion_multipleValues', function() { |
| 813 | var message = new proto.jspb.test.TestMessageWithOneof([,, 'x',, 'y']); |
| 814 | |
| 815 | assertEquals('', message.getPone()); |
| 816 | assertEquals('y', message.getPthree()); |
| 817 | assertEquals( |
| 818 | proto.jspb.test.TestMessageWithOneof.PartialOneofCase.PTHREE, |
| 819 | message.getPartialOneofCase()); |
| 820 | }); |
| 821 | |
| 822 | it('testSettingOneofFieldClearsOthers', function() { |
| 823 | var message = new proto.jspb.test.TestMessageWithOneof; |
| 824 | assertEquals('', message.getPone()); |
| 825 | assertEquals('', message.getPthree()); |
| 826 | assertFalse(message.hasPone()); |
| 827 | assertFalse(message.hasPthree()); |
| 828 | |
| 829 | message.setPone('hi'); |
| 830 | assertEquals('hi', message.getPone()); |
| 831 | assertEquals('', message.getPthree()); |
| 832 | assertTrue(message.hasPone()); |
| 833 | assertFalse(message.hasPthree()); |
| 834 | |
| 835 | message.setPthree('bye'); |
| 836 | assertEquals('', message.getPone()); |
| 837 | assertEquals('bye', message.getPthree()); |
| 838 | assertFalse(message.hasPone()); |
| 839 | assertTrue(message.hasPthree()); |
| 840 | }); |
| 841 | |
| 842 | it('testSettingOneofFieldDoesNotClearFieldsFromOtherUnions', function() { |
| 843 | var other = new proto.jspb.test.TestMessageWithOneof; |
| 844 | var message = new proto.jspb.test.TestMessageWithOneof; |
| 845 | assertEquals('', message.getPone()); |
| 846 | assertEquals('', message.getPthree()); |
| 847 | assertUndefined(message.getRone()); |
| 848 | assertFalse(message.hasPone()); |
| 849 | assertFalse(message.hasPthree()); |
| 850 | |
| 851 | message.setPone('hi'); |
| 852 | message.setRone(other); |
| 853 | assertEquals('hi', message.getPone()); |
| 854 | assertEquals('', message.getPthree()); |
| 855 | assertEquals(other, message.getRone()); |
| 856 | assertTrue(message.hasPone()); |
| 857 | assertFalse(message.hasPthree()); |
| 858 | |
| 859 | message.setPthree('bye'); |
| 860 | assertEquals('', message.getPone()); |
| 861 | assertEquals('bye', message.getPthree()); |
| 862 | assertEquals(other, message.getRone()); |
| 863 | assertFalse(message.hasPone()); |
| 864 | assertTrue(message.hasPthree()); |
| 865 | }); |
| 866 | |
| 867 | it('testUnsetsOneofCaseWhenFieldIsCleared', function() { |
| 868 | var message = new proto.jspb.test.TestMessageWithOneof; |
| 869 | assertEquals( |
| 870 | proto.jspb.test.TestMessageWithOneof.PartialOneofCase. |
| 871 | PARTIAL_ONEOF_NOT_SET, |
| 872 | message.getPartialOneofCase()); |
| 873 | |
| 874 | message.setPone('hi'); |
| 875 | assertEquals( |
| 876 | proto.jspb.test.TestMessageWithOneof.PartialOneofCase.PONE, |
| 877 | message.getPartialOneofCase()); |
| 878 | |
| 879 | message.clearPone(); |
| 880 | assertEquals( |
| 881 | proto.jspb.test.TestMessageWithOneof.PartialOneofCase. |
| 882 | PARTIAL_ONEOF_NOT_SET, |
| 883 | message.getPartialOneofCase()); |
| 884 | }); |
| 885 | |
| 886 | it('testMessageWithDefaultOneofValues', function() { |
| 887 | var message = new proto.jspb.test.TestMessageWithOneof; |
| 888 | assertEquals(1234, message.getAone()); |
| 889 | assertEquals(0, message.getAtwo()); |
| 890 | assertEquals( |
| 891 | proto.jspb.test.TestMessageWithOneof.DefaultOneofACase |
| 892 | .DEFAULT_ONEOF_A_NOT_SET, |
| 893 | message.getDefaultOneofACase()); |
| 894 | |
| 895 | message.setAone(567); |
| 896 | assertEquals(567, message.getAone()); |
| 897 | assertEquals(0, message.getAtwo()); |
| 898 | assertEquals( |
| 899 | proto.jspb.test.TestMessageWithOneof.DefaultOneofACase.AONE, |
| 900 | message.getDefaultOneofACase()); |
| 901 | |
| 902 | message.setAtwo(890); |
| 903 | assertEquals(1234, message.getAone()); |
| 904 | assertEquals(890, message.getAtwo()); |
| 905 | assertEquals( |
| 906 | proto.jspb.test.TestMessageWithOneof.DefaultOneofACase.ATWO, |
| 907 | message.getDefaultOneofACase()); |
| 908 | |
| 909 | message.clearAtwo(); |
| 910 | assertEquals(1234, message.getAone()); |
| 911 | assertEquals(0, message.getAtwo()); |
| 912 | assertEquals( |
| 913 | proto.jspb.test.TestMessageWithOneof.DefaultOneofACase |
| 914 | .DEFAULT_ONEOF_A_NOT_SET, |
| 915 | message.getDefaultOneofACase()); |
| 916 | }); |
| 917 | |
| 918 | it('testMessageWithDefaultOneofValues_defaultNotOnFirstField', function() { |
| 919 | var message = new proto.jspb.test.TestMessageWithOneof; |
| 920 | assertEquals(0, message.getBone()); |
| 921 | assertEquals(1234, message.getBtwo()); |
| 922 | assertFalse(message.hasBone()); |
| 923 | assertFalse(message.hasBtwo()); |
| 924 | assertEquals( |
| 925 | proto.jspb.test.TestMessageWithOneof.DefaultOneofBCase |
| 926 | .DEFAULT_ONEOF_B_NOT_SET, |
| 927 | message.getDefaultOneofBCase()); |
| 928 | |
| 929 | message.setBone(2); |
| 930 | assertEquals(2, message.getBone()); |
| 931 | assertEquals(1234, message.getBtwo()); |
| 932 | assertTrue(message.hasBone()); |
| 933 | assertFalse(message.hasBtwo()); |
| 934 | assertEquals( |
| 935 | proto.jspb.test.TestMessageWithOneof.DefaultOneofBCase.BONE, |
| 936 | message.getDefaultOneofBCase()); |
| 937 | |
| 938 | message.setBtwo(3); |
| 939 | assertEquals(0, message.getBone()); |
| 940 | assertFalse(message.hasBone()); |
| 941 | assertTrue(message.hasBtwo()); |
| 942 | assertEquals(3, message.getBtwo()); |
| 943 | assertEquals( |
| 944 | proto.jspb.test.TestMessageWithOneof.DefaultOneofBCase.BTWO, |
| 945 | message.getDefaultOneofBCase()); |
| 946 | |
| 947 | message.clearBtwo(); |
| 948 | assertEquals(0, message.getBone()); |
| 949 | assertFalse(message.hasBone()); |
| 950 | assertFalse(message.hasBtwo()); |
| 951 | assertEquals(1234, message.getBtwo()); |
| 952 | assertEquals( |
| 953 | proto.jspb.test.TestMessageWithOneof.DefaultOneofBCase |
| 954 | .DEFAULT_ONEOF_B_NOT_SET, |
| 955 | message.getDefaultOneofBCase()); |
| 956 | }); |
| 957 | |
| 958 | it('testInitializeMessageWithOneofDefaults', function() { |
| 959 | var message = |
| 960 | new proto.jspb.test.TestMessageWithOneof(new Array(9).concat(567)); |
| 961 | assertEquals(567, message.getAone()); |
| 962 | assertEquals(0, message.getAtwo()); |
| 963 | assertEquals( |
| 964 | proto.jspb.test.TestMessageWithOneof.DefaultOneofACase.AONE, |
| 965 | message.getDefaultOneofACase()); |
| 966 | |
| 967 | message = |
| 968 | new proto.jspb.test.TestMessageWithOneof(new Array(10).concat(890)); |
| 969 | assertEquals(1234, message.getAone()); |
| 970 | assertEquals(890, message.getAtwo()); |
| 971 | assertEquals( |
| 972 | proto.jspb.test.TestMessageWithOneof.DefaultOneofACase.ATWO, |
| 973 | message.getDefaultOneofACase()); |
| 974 | |
| 975 | message = |
| 976 | new proto.jspb.test.TestMessageWithOneof(new Array(9).concat(567, 890)); |
| 977 | assertEquals(1234, message.getAone()); |
| 978 | assertEquals(890, message.getAtwo()); |
| 979 | assertEquals( |
| 980 | proto.jspb.test.TestMessageWithOneof.DefaultOneofACase.ATWO, |
| 981 | message.getDefaultOneofACase()); |
| 982 | }); |
| 983 | |
| 984 | it('testInitializeMessageWithOneofDefaults_defaultNotSetOnFirstField', |
| 985 | function() { |
| 986 | var message; |
| 987 | |
| 988 | message = |
| 989 | new proto.jspb.test.TestMessageWithOneof(new Array(11).concat(567)); |
| 990 | assertEquals(567, message.getBone()); |
| 991 | assertEquals(1234, message.getBtwo()); |
| 992 | assertEquals( |
| 993 | proto.jspb.test.TestMessageWithOneof.DefaultOneofBCase.BONE, |
| 994 | message.getDefaultOneofBCase()); |
| 995 | |
| 996 | message = |
| 997 | new proto.jspb.test.TestMessageWithOneof(new Array(12).concat(890)); |
| 998 | assertEquals(0, message.getBone()); |
| 999 | assertEquals(890, message.getBtwo()); |
| 1000 | assertEquals( |
| 1001 | proto.jspb.test.TestMessageWithOneof.DefaultOneofBCase.BTWO, |
| 1002 | message.getDefaultOneofBCase()); |
| 1003 | |
| 1004 | message = new proto.jspb.test.TestMessageWithOneof( |
| 1005 | new Array(11).concat(567, 890)); |
| 1006 | assertEquals(0, message.getBone()); |
| 1007 | assertEquals(890, message.getBtwo()); |
| 1008 | assertEquals( |
| 1009 | proto.jspb.test.TestMessageWithOneof.DefaultOneofBCase.BTWO, |
| 1010 | message.getDefaultOneofBCase()); |
| 1011 | }); |
| 1012 | |
| 1013 | it('testOneofContainingAnotherMessage', function() { |
| 1014 | var message = new proto.jspb.test.TestMessageWithOneof; |
| 1015 | assertEquals( |
| 1016 | proto.jspb.test.TestMessageWithOneof.RecursiveOneofCase. |
| 1017 | RECURSIVE_ONEOF_NOT_SET, |
| 1018 | message.getRecursiveOneofCase()); |
| 1019 | |
| 1020 | var other = new proto.jspb.test.TestMessageWithOneof; |
| 1021 | message.setRone(other); |
| 1022 | assertEquals(other, message.getRone()); |
| 1023 | assertEquals('', message.getRtwo()); |
| 1024 | assertEquals( |
| 1025 | proto.jspb.test.TestMessageWithOneof.RecursiveOneofCase.RONE, |
| 1026 | message.getRecursiveOneofCase()); |
| 1027 | |
| 1028 | message.setRtwo('hi'); |
| 1029 | assertUndefined(message.getRone()); |
| 1030 | assertEquals('hi', message.getRtwo()); |
| 1031 | assertEquals( |
| 1032 | proto.jspb.test.TestMessageWithOneof.RecursiveOneofCase.RTWO, |
| 1033 | message.getRecursiveOneofCase()); |
| 1034 | }); |
| 1035 | |
| 1036 | it('testQueryingOneofCaseEnsuresOnlyOneFieldIsSetInUnderlyingArray', |
| 1037 | function() { |
| 1038 | var message = new proto.jspb.test.TestMessageWithOneof; |
| 1039 | message.setPone('x'); |
| 1040 | assertEquals('x', message.getPone()); |
| 1041 | assertEquals('', message.getPthree()); |
| 1042 | assertEquals( |
| 1043 | proto.jspb.test.TestMessageWithOneof.PartialOneofCase.PONE, |
| 1044 | message.getPartialOneofCase()); |
| 1045 | |
| 1046 | var array = message.toArray(); |
| 1047 | assertEquals('x', array[2]); |
| 1048 | assertUndefined(array[4]); |
| 1049 | array[4] = 'y'; |
| 1050 | |
| 1051 | assertEquals( |
| 1052 | proto.jspb.test.TestMessageWithOneof.PartialOneofCase.PTHREE, |
| 1053 | message.getPartialOneofCase()); |
| 1054 | assertUndefined(array[2]); |
| 1055 | assertEquals('y', array[4]); |
| 1056 | }); |
| 1057 | |
| 1058 | it('testFloatingPointFieldsSupportNan', function() { |
| 1059 | var assertNan = function(x) { |
Rafi Kamal | 4f02f05 | 2019-08-22 16:14:22 -0700 | [diff] [blame] | 1060 | assertTrue( |
| 1061 | 'Expected ' + x + ' (' + goog.typeOf(x) + ') to be NaN.', |
| 1062 | typeof x === 'number' && isNaN(x)); |
Adam Cozzette | 10ea251 | 2017-02-28 14:20:56 -0800 | [diff] [blame] | 1063 | }; |
| 1064 | |
| 1065 | var message = new proto.jspb.test.FloatingPointFields([ |
| 1066 | 'NaN', 'NaN', ['NaN', 'NaN'], 'NaN', |
| 1067 | 'NaN', 'NaN', ['NaN', 'NaN'], 'NaN' |
| 1068 | ]); |
| 1069 | assertNan(message.getOptionalFloatField()); |
| 1070 | assertNan(message.getRequiredFloatField()); |
| 1071 | assertNan(message.getRepeatedFloatFieldList()[0]); |
| 1072 | assertNan(message.getRepeatedFloatFieldList()[1]); |
| 1073 | assertNan(message.getDefaultFloatField()); |
| 1074 | assertNan(message.getOptionalDoubleField()); |
| 1075 | assertNan(message.getRequiredDoubleField()); |
| 1076 | assertNan(message.getRepeatedDoubleFieldList()[0]); |
| 1077 | assertNan(message.getRepeatedDoubleFieldList()[1]); |
| 1078 | assertNan(message.getDefaultDoubleField()); |
| 1079 | }); |
| 1080 | |
| 1081 | }); |