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