Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [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 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 31 | goog.require('goog.crypt.base64'); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 32 | goog.require('goog.testing.asserts'); |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 33 | |
| 34 | // CommonJS-LoadFromFile: testbinary_pb proto.jspb.test |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 35 | goog.require('proto.jspb.test.ForeignMessage'); |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 36 | |
| 37 | // CommonJS-LoadFromFile: proto3_test_pb proto.jspb.test |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 38 | goog.require('proto.jspb.test.Proto3Enum'); |
| 39 | goog.require('proto.jspb.test.TestProto3'); |
| 40 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 41 | // CommonJS-LoadFromFile: google/protobuf/timestamp_pb proto.google.protobuf |
| 42 | goog.require('proto.google.protobuf.Timestamp'); |
| 43 | |
| 44 | // CommonJS-LoadFromFile: google/protobuf/struct_pb proto.google.protobuf |
| 45 | goog.require('proto.google.protobuf.Struct'); |
| 46 | |
| 47 | |
| 48 | var BYTES = new Uint8Array([1, 2, 8, 9]); |
| 49 | var BYTES_B64 = goog.crypt.base64.encodeByteArray(BYTES); |
| 50 | |
| 51 | |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 52 | /** |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 53 | * Helper: compare a bytes field to an expected value |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 54 | * @param {Uint8Array|string} arr |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 55 | * @param {Uint8Array} expected |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 56 | * @return {boolean} |
| 57 | */ |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 58 | function bytesCompare(arr, expected) { |
| 59 | if (goog.isString(arr)) { |
| 60 | arr = goog.crypt.base64.decodeStringToUint8Array(arr); |
| 61 | } |
| 62 | if (arr.length != expected.length) { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 63 | return false; |
| 64 | } |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 65 | for (var i = 0; i < arr.length; i++) { |
| 66 | if (arr[i] != expected[i]) { |
| 67 | return false; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 68 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 69 | } |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 70 | return true; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | |
| 74 | describe('proto3Test', function() { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 75 | |
| 76 | /** |
| 77 | * Test default values don't affect equality test. |
| 78 | */ |
| 79 | it('testEqualsProto3', function() { |
| 80 | var msg1 = new proto.jspb.test.TestProto3(); |
| 81 | var msg2 = new proto.jspb.test.TestProto3(); |
| 82 | msg2.setOptionalString(''); |
| 83 | |
| 84 | assertTrue(jspb.Message.equals(msg1, msg2)); |
| 85 | }); |
| 86 | |
| 87 | |
| 88 | /** |
| 89 | * Test setting when a field has default semantics. |
| 90 | */ |
| 91 | it('testSetProto3ToValueAndBackToDefault', function() { |
| 92 | var msg = new proto.jspb.test.TestProto3(); |
| 93 | |
| 94 | // Setting should work normally. |
| 95 | msg.setOptionalString('optionalString'); |
| 96 | assertEquals(msg.getOptionalString(), 'optionalString'); |
| 97 | |
| 98 | // Clearing should work too ... |
| 99 | msg.setOptionalString(''); |
| 100 | assertEquals(msg.getOptionalString(), ''); |
| 101 | |
| 102 | // ... and shouldn't affect the equality with a brand new message. |
| 103 | assertTrue(jspb.Message.equals(msg, new proto.jspb.test.TestProto3())); |
| 104 | }); |
| 105 | |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 106 | /** |
| 107 | * Test defaults for proto3 message fields. |
| 108 | */ |
| 109 | it('testProto3FieldDefaults', function() { |
| 110 | var msg = new proto.jspb.test.TestProto3(); |
| 111 | |
| 112 | assertEquals(msg.getOptionalInt32(), 0); |
| 113 | assertEquals(msg.getOptionalInt64(), 0); |
| 114 | assertEquals(msg.getOptionalUint32(), 0); |
| 115 | assertEquals(msg.getOptionalUint64(), 0); |
| 116 | assertEquals(msg.getOptionalSint32(), 0); |
| 117 | assertEquals(msg.getOptionalSint64(), 0); |
| 118 | assertEquals(msg.getOptionalFixed32(), 0); |
| 119 | assertEquals(msg.getOptionalFixed64(), 0); |
| 120 | assertEquals(msg.getOptionalSfixed32(), 0); |
| 121 | assertEquals(msg.getOptionalSfixed64(), 0); |
| 122 | assertEquals(msg.getOptionalFloat(), 0); |
| 123 | assertEquals(msg.getOptionalDouble(), 0); |
| 124 | assertEquals(msg.getOptionalString(), ''); |
| 125 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 126 | // TODO(b/26173701): when we change bytes fields default getter to return |
| 127 | // Uint8Array, we'll want to switch this assertion to match the u8 case. |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 128 | assertEquals(typeof msg.getOptionalBytes(), 'string'); |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 129 | assertEquals(msg.getOptionalBytes_asU8() instanceof Uint8Array, true); |
| 130 | assertEquals(typeof msg.getOptionalBytes_asB64(), 'string'); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 131 | assertEquals(msg.getOptionalBytes().length, 0); |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 132 | assertEquals(msg.getOptionalBytes_asU8().length, 0); |
| 133 | assertEquals(msg.getOptionalBytes_asB64(), ''); |
| 134 | |
| 135 | assertEquals(msg.getOptionalForeignEnum(), |
| 136 | proto.jspb.test.Proto3Enum.PROTO3_FOO); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 137 | assertEquals(msg.getOptionalForeignMessage(), undefined); |
| 138 | assertEquals(msg.getOptionalForeignMessage(), undefined); |
| 139 | |
| 140 | assertEquals(msg.getRepeatedInt32List().length, 0); |
| 141 | assertEquals(msg.getRepeatedInt64List().length, 0); |
| 142 | assertEquals(msg.getRepeatedUint32List().length, 0); |
| 143 | assertEquals(msg.getRepeatedUint64List().length, 0); |
| 144 | assertEquals(msg.getRepeatedSint32List().length, 0); |
| 145 | assertEquals(msg.getRepeatedSint64List().length, 0); |
| 146 | assertEquals(msg.getRepeatedFixed32List().length, 0); |
| 147 | assertEquals(msg.getRepeatedFixed64List().length, 0); |
| 148 | assertEquals(msg.getRepeatedSfixed32List().length, 0); |
| 149 | assertEquals(msg.getRepeatedSfixed64List().length, 0); |
| 150 | assertEquals(msg.getRepeatedFloatList().length, 0); |
| 151 | assertEquals(msg.getRepeatedDoubleList().length, 0); |
| 152 | assertEquals(msg.getRepeatedStringList().length, 0); |
| 153 | assertEquals(msg.getRepeatedBytesList().length, 0); |
| 154 | assertEquals(msg.getRepeatedForeignEnumList().length, 0); |
| 155 | assertEquals(msg.getRepeatedForeignMessageList().length, 0); |
| 156 | |
| 157 | }); |
| 158 | |
| 159 | |
| 160 | /** |
| 161 | * Test that all fields can be set and read via a serialization roundtrip. |
| 162 | */ |
| 163 | it('testProto3FieldSetGet', function() { |
| 164 | var msg = new proto.jspb.test.TestProto3(); |
| 165 | |
| 166 | msg.setOptionalInt32(-42); |
| 167 | msg.setOptionalInt64(-0x7fffffff00000000); |
| 168 | msg.setOptionalUint32(0x80000000); |
| 169 | msg.setOptionalUint64(0xf000000000000000); |
| 170 | msg.setOptionalSint32(-100); |
| 171 | msg.setOptionalSint64(-0x8000000000000000); |
| 172 | msg.setOptionalFixed32(1234); |
| 173 | msg.setOptionalFixed64(0x1234567800000000); |
| 174 | msg.setOptionalSfixed32(-1234); |
| 175 | msg.setOptionalSfixed64(-0x1234567800000000); |
| 176 | msg.setOptionalFloat(1.5); |
| 177 | msg.setOptionalDouble(-1.5); |
| 178 | msg.setOptionalBool(true); |
| 179 | msg.setOptionalString('hello world'); |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 180 | msg.setOptionalBytes(BYTES); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 181 | var submsg = new proto.jspb.test.ForeignMessage(); |
| 182 | submsg.setC(16); |
| 183 | msg.setOptionalForeignMessage(submsg); |
| 184 | msg.setOptionalForeignEnum(proto.jspb.test.Proto3Enum.PROTO3_BAR); |
| 185 | |
| 186 | msg.setRepeatedInt32List([-42]); |
| 187 | msg.setRepeatedInt64List([-0x7fffffff00000000]); |
| 188 | msg.setRepeatedUint32List([0x80000000]); |
| 189 | msg.setRepeatedUint64List([0xf000000000000000]); |
| 190 | msg.setRepeatedSint32List([-100]); |
| 191 | msg.setRepeatedSint64List([-0x8000000000000000]); |
| 192 | msg.setRepeatedFixed32List([1234]); |
| 193 | msg.setRepeatedFixed64List([0x1234567800000000]); |
| 194 | msg.setRepeatedSfixed32List([-1234]); |
| 195 | msg.setRepeatedSfixed64List([-0x1234567800000000]); |
| 196 | msg.setRepeatedFloatList([1.5]); |
| 197 | msg.setRepeatedDoubleList([-1.5]); |
| 198 | msg.setRepeatedBoolList([true]); |
| 199 | msg.setRepeatedStringList(['hello world']); |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 200 | msg.setRepeatedBytesList([BYTES]); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 201 | submsg = new proto.jspb.test.ForeignMessage(); |
| 202 | submsg.setC(1000); |
| 203 | msg.setRepeatedForeignMessageList([submsg]); |
| 204 | msg.setRepeatedForeignEnumList([proto.jspb.test.Proto3Enum.PROTO3_BAR]); |
| 205 | |
| 206 | msg.setOneofString('asdf'); |
| 207 | |
| 208 | var serialized = msg.serializeBinary(); |
| 209 | msg = proto.jspb.test.TestProto3.deserializeBinary(serialized); |
| 210 | |
| 211 | assertEquals(msg.getOptionalInt32(), -42); |
| 212 | assertEquals(msg.getOptionalInt64(), -0x7fffffff00000000); |
| 213 | assertEquals(msg.getOptionalUint32(), 0x80000000); |
| 214 | assertEquals(msg.getOptionalUint64(), 0xf000000000000000); |
| 215 | assertEquals(msg.getOptionalSint32(), -100); |
| 216 | assertEquals(msg.getOptionalSint64(), -0x8000000000000000); |
| 217 | assertEquals(msg.getOptionalFixed32(), 1234); |
| 218 | assertEquals(msg.getOptionalFixed64(), 0x1234567800000000); |
| 219 | assertEquals(msg.getOptionalSfixed32(), -1234); |
| 220 | assertEquals(msg.getOptionalSfixed64(), -0x1234567800000000); |
| 221 | assertEquals(msg.getOptionalFloat(), 1.5); |
| 222 | assertEquals(msg.getOptionalDouble(), -1.5); |
| 223 | assertEquals(msg.getOptionalBool(), true); |
| 224 | assertEquals(msg.getOptionalString(), 'hello world'); |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 225 | assertEquals(true, bytesCompare(msg.getOptionalBytes(), BYTES)); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 226 | assertEquals(msg.getOptionalForeignMessage().getC(), 16); |
| 227 | assertEquals(msg.getOptionalForeignEnum(), |
| 228 | proto.jspb.test.Proto3Enum.PROTO3_BAR); |
| 229 | |
| 230 | assertElementsEquals(msg.getRepeatedInt32List(), [-42]); |
| 231 | assertElementsEquals(msg.getRepeatedInt64List(), [-0x7fffffff00000000]); |
| 232 | assertElementsEquals(msg.getRepeatedUint32List(), [0x80000000]); |
| 233 | assertElementsEquals(msg.getRepeatedUint64List(), [0xf000000000000000]); |
| 234 | assertElementsEquals(msg.getRepeatedSint32List(), [-100]); |
| 235 | assertElementsEquals(msg.getRepeatedSint64List(), [-0x8000000000000000]); |
| 236 | assertElementsEquals(msg.getRepeatedFixed32List(), [1234]); |
| 237 | assertElementsEquals(msg.getRepeatedFixed64List(), [0x1234567800000000]); |
| 238 | assertElementsEquals(msg.getRepeatedSfixed32List(), [-1234]); |
| 239 | assertElementsEquals(msg.getRepeatedSfixed64List(), [-0x1234567800000000]); |
| 240 | assertElementsEquals(msg.getRepeatedFloatList(), [1.5]); |
| 241 | assertElementsEquals(msg.getRepeatedDoubleList(), [-1.5]); |
| 242 | assertElementsEquals(msg.getRepeatedBoolList(), [true]); |
| 243 | assertElementsEquals(msg.getRepeatedStringList(), ['hello world']); |
| 244 | assertEquals(msg.getRepeatedBytesList().length, 1); |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 245 | assertEquals(true, bytesCompare(msg.getRepeatedBytesList()[0], BYTES)); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 246 | assertEquals(msg.getRepeatedForeignMessageList().length, 1); |
| 247 | assertEquals(msg.getRepeatedForeignMessageList()[0].getC(), 1000); |
| 248 | assertElementsEquals(msg.getRepeatedForeignEnumList(), |
| 249 | [proto.jspb.test.Proto3Enum.PROTO3_BAR]); |
| 250 | |
| 251 | assertEquals(msg.getOneofString(), 'asdf'); |
| 252 | }); |
| 253 | |
| 254 | |
| 255 | /** |
| 256 | * Test that oneofs continue to have a notion of field presence. |
| 257 | */ |
| 258 | it('testOneofs', function() { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 259 | // Default instance. |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 260 | var msg = new proto.jspb.test.TestProto3(); |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 261 | assertEquals(msg.getOneofUint32(), 0); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 262 | assertEquals(msg.getOneofForeignMessage(), undefined); |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 263 | assertEquals(msg.getOneofString(), ''); |
| 264 | assertEquals(msg.getOneofBytes(), ''); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 265 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 266 | assertFalse(msg.hasOneofUint32()); |
| 267 | assertFalse(msg.hasOneofForeignMessage()); |
| 268 | assertFalse(msg.hasOneofString()); |
| 269 | assertFalse(msg.hasOneofBytes()); |
| 270 | |
| 271 | // Integer field. |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 272 | msg.setOneofUint32(42); |
| 273 | assertEquals(msg.getOneofUint32(), 42); |
| 274 | assertEquals(msg.getOneofForeignMessage(), undefined); |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 275 | assertEquals(msg.getOneofString(), ''); |
| 276 | assertEquals(msg.getOneofBytes(), ''); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 277 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 278 | assertTrue(msg.hasOneofUint32()); |
| 279 | assertFalse(msg.hasOneofForeignMessage()); |
| 280 | assertFalse(msg.hasOneofString()); |
| 281 | assertFalse(msg.hasOneofBytes()); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 282 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 283 | // Sub-message field. |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 284 | var submsg = new proto.jspb.test.ForeignMessage(); |
| 285 | msg.setOneofForeignMessage(submsg); |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 286 | assertEquals(msg.getOneofUint32(), 0); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 287 | assertEquals(msg.getOneofForeignMessage(), submsg); |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 288 | assertEquals(msg.getOneofString(), ''); |
| 289 | assertEquals(msg.getOneofBytes(), ''); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 290 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 291 | assertFalse(msg.hasOneofUint32()); |
| 292 | assertTrue(msg.hasOneofForeignMessage()); |
| 293 | assertFalse(msg.hasOneofString()); |
| 294 | assertFalse(msg.hasOneofBytes()); |
| 295 | |
| 296 | // String field. |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 297 | msg.setOneofString('hello'); |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 298 | assertEquals(msg.getOneofUint32(), 0); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 299 | assertEquals(msg.getOneofForeignMessage(), undefined); |
| 300 | assertEquals(msg.getOneofString(), 'hello'); |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 301 | assertEquals(msg.getOneofBytes(), ''); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 302 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 303 | assertFalse(msg.hasOneofUint32()); |
| 304 | assertFalse(msg.hasOneofForeignMessage()); |
| 305 | assertTrue(msg.hasOneofString()); |
| 306 | assertFalse(msg.hasOneofBytes()); |
| 307 | |
| 308 | // Bytes field. |
| 309 | msg.setOneofBytes(goog.crypt.base64.encodeString('\u00FF\u00FF')); |
| 310 | assertEquals(msg.getOneofUint32(), 0); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 311 | assertEquals(msg.getOneofForeignMessage(), undefined); |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 312 | assertEquals(msg.getOneofString(), ''); |
| 313 | assertEquals(msg.getOneofBytes_asB64(), |
| 314 | goog.crypt.base64.encodeString('\u00FF\u00FF')); |
| 315 | |
| 316 | assertFalse(msg.hasOneofUint32()); |
| 317 | assertFalse(msg.hasOneofForeignMessage()); |
| 318 | assertFalse(msg.hasOneofString()); |
| 319 | assertTrue(msg.hasOneofBytes()); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 320 | }); |
| 321 | |
| 322 | |
| 323 | /** |
| 324 | * Test that "default"-valued primitive fields are not emitted on the wire. |
| 325 | */ |
| 326 | it('testNoSerializeDefaults', function() { |
| 327 | var msg = new proto.jspb.test.TestProto3(); |
| 328 | |
| 329 | // Set each primitive to a non-default value, then back to its default, to |
| 330 | // ensure that the serialization is actually checking the value and not just |
| 331 | // whether it has ever been set. |
| 332 | msg.setOptionalInt32(42); |
| 333 | msg.setOptionalInt32(0); |
| 334 | msg.setOptionalDouble(3.14); |
| 335 | msg.setOptionalDouble(0.0); |
| 336 | msg.setOptionalBool(true); |
| 337 | msg.setOptionalBool(false); |
| 338 | msg.setOptionalString('hello world'); |
| 339 | msg.setOptionalString(''); |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 340 | msg.setOptionalBytes(goog.crypt.base64.encodeString('\u00FF\u00FF')); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 341 | msg.setOptionalBytes(''); |
| 342 | msg.setOptionalForeignMessage(new proto.jspb.test.ForeignMessage()); |
| 343 | msg.setOptionalForeignMessage(null); |
| 344 | msg.setOptionalForeignEnum(proto.jspb.test.Proto3Enum.PROTO3_BAR); |
| 345 | msg.setOptionalForeignEnum(proto.jspb.test.Proto3Enum.PROTO3_FOO); |
| 346 | msg.setOneofUint32(32); |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 347 | msg.clearOneofUint32(); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 348 | |
| 349 | |
| 350 | var serialized = msg.serializeBinary(); |
| 351 | assertEquals(0, serialized.length); |
| 352 | }); |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 353 | |
| 354 | /** |
| 355 | * Test that base64 string and Uint8Array are interchangeable in bytes fields. |
| 356 | */ |
| 357 | it('testBytesFieldsInterop', function() { |
| 358 | var msg = new proto.jspb.test.TestProto3(); |
| 359 | // Set as a base64 string and check all the getters work. |
| 360 | msg.setOptionalBytes(BYTES_B64); |
| 361 | assertTrue(bytesCompare(msg.getOptionalBytes_asU8(), BYTES)); |
| 362 | assertTrue(bytesCompare(msg.getOptionalBytes_asB64(), BYTES)); |
| 363 | assertTrue(bytesCompare(msg.getOptionalBytes(), BYTES)); |
| 364 | |
| 365 | // Test binary serialize round trip doesn't break it. |
| 366 | msg = proto.jspb.test.TestProto3.deserializeBinary(msg.serializeBinary()); |
| 367 | assertTrue(bytesCompare(msg.getOptionalBytes_asU8(), BYTES)); |
| 368 | assertTrue(bytesCompare(msg.getOptionalBytes_asB64(), BYTES)); |
| 369 | assertTrue(bytesCompare(msg.getOptionalBytes(), BYTES)); |
| 370 | |
| 371 | msg = new proto.jspb.test.TestProto3(); |
| 372 | // Set as a Uint8Array and check all the getters work. |
| 373 | msg.setOptionalBytes(BYTES); |
| 374 | assertTrue(bytesCompare(msg.getOptionalBytes_asU8(), BYTES)); |
| 375 | assertTrue(bytesCompare(msg.getOptionalBytes_asB64(), BYTES)); |
| 376 | assertTrue(bytesCompare(msg.getOptionalBytes(), BYTES)); |
| 377 | |
| 378 | }); |
| 379 | |
| 380 | it('testTimestampWellKnownType', function() { |
| 381 | var msg = new proto.google.protobuf.Timestamp(); |
| 382 | msg.fromDate(new Date(123456789)); |
| 383 | assertEquals(123456, msg.getSeconds()); |
| 384 | assertEquals(789000000, msg.getNanos()); |
| 385 | var date = msg.toDate(); |
| 386 | assertEquals(123456789, date.getTime()); |
| 387 | }); |
| 388 | |
| 389 | it('testStructWellKnownType', function() { |
| 390 | var jsObj = { |
| 391 | abc: "def", |
| 392 | number: 12345.678, |
| 393 | nullKey: null, |
| 394 | boolKey: true, |
| 395 | listKey: [1, null, true, false, "abc"], |
| 396 | structKey: {foo: "bar", somenum: 123}, |
| 397 | complicatedKey: [{xyz: {abc: [3, 4, null, false]}}, "zzz"] |
| 398 | }; |
| 399 | |
| 400 | var struct = proto.google.protobuf.Struct.fromJavaScript(jsObj); |
| 401 | var jsObj2 = struct.toJavaScript(); |
| 402 | |
| 403 | assertEquals("def", jsObj2.abc); |
| 404 | assertEquals(12345.678, jsObj2.number); |
| 405 | assertEquals(null, jsObj2.nullKey); |
| 406 | assertEquals(true, jsObj2.boolKey); |
| 407 | assertEquals("abc", jsObj2.listKey[4]); |
| 408 | assertEquals("bar", jsObj2.structKey.foo); |
| 409 | assertEquals(4, jsObj2.complicatedKey[0].xyz.abc[1]); |
| 410 | }); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 411 | }); |