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