| OLD | NEW |
| 1 // Protocol Buffers - Google's data interchange format | 1 // Protocol Buffers - Google's data interchange format |
| 2 // Copyright 2008 Google Inc. All rights reserved. | 2 // Copyright 2008 Google Inc. All rights reserved. |
| 3 // https://developers.google.com/protocol-buffers/ | 3 // https://developers.google.com/protocol-buffers/ |
| 4 // | 4 // |
| 5 // Redistribution and use in source and binary forms, with or without | 5 // Redistribution and use in source and binary forms, with or without |
| 6 // modification, are permitted provided that the following conditions are | 6 // modification, are permitted provided that the following conditions are |
| 7 // met: | 7 // met: |
| 8 // | 8 // |
| 9 // * Redistributions of source code must retain the above copyright | 9 // * Redistributions of source code must retain the above copyright |
| 10 // notice, this list of conditions and the following disclaimer. | 10 // notice, this list of conditions and the following disclaimer. |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | 30 |
| 31 // Test suite is written using Jasmine -- see http://jasmine.github.io/ | 31 // Test suite is written using Jasmine -- see http://jasmine.github.io/ |
| 32 | 32 |
| 33 goog.require('goog.crypt.base64'); | 33 goog.require('goog.crypt.base64'); |
| 34 goog.require('goog.testing.asserts'); | 34 goog.require('goog.testing.asserts'); |
| 35 goog.require('jspb.BinaryWriter'); | |
| 36 goog.require('jspb.Message'); | 35 goog.require('jspb.Message'); |
| 37 | 36 |
| 38 // CommonJS-LoadFromFile: ../testbinary_pb proto.jspb.test | 37 // CommonJS-LoadFromFile: ../testbinary_pb proto.jspb.test |
| 39 goog.require('proto.jspb.test.ExtendsWithMessage'); | 38 goog.require('proto.jspb.test.ExtendsWithMessage'); |
| 40 goog.require('proto.jspb.test.ForeignEnum'); | 39 goog.require('proto.jspb.test.ForeignEnum'); |
| 41 goog.require('proto.jspb.test.ForeignMessage'); | 40 goog.require('proto.jspb.test.ForeignMessage'); |
| 42 goog.require('proto.jspb.test.TestAllTypes'); | 41 goog.require('proto.jspb.test.TestAllTypes'); |
| 43 goog.require('proto.jspb.test.TestExtendable'); | 42 goog.require('proto.jspb.test.TestExtendable'); |
| 44 goog.require('proto.jspb.test.extendOptionalBool'); | 43 goog.require('proto.jspb.test.extendOptionalBool'); |
| 45 goog.require('proto.jspb.test.extendOptionalBytes'); | 44 goog.require('proto.jspb.test.extendOptionalBytes'); |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 81 goog.require('proto.jspb.test.extendRepeatedInt32List'); | 80 goog.require('proto.jspb.test.extendRepeatedInt32List'); |
| 82 goog.require('proto.jspb.test.extendRepeatedInt64List'); | 81 goog.require('proto.jspb.test.extendRepeatedInt64List'); |
| 83 goog.require('proto.jspb.test.extendRepeatedSfixed32List'); | 82 goog.require('proto.jspb.test.extendRepeatedSfixed32List'); |
| 84 goog.require('proto.jspb.test.extendRepeatedSfixed64List'); | 83 goog.require('proto.jspb.test.extendRepeatedSfixed64List'); |
| 85 goog.require('proto.jspb.test.extendRepeatedSint32List'); | 84 goog.require('proto.jspb.test.extendRepeatedSint32List'); |
| 86 goog.require('proto.jspb.test.extendRepeatedSint64List'); | 85 goog.require('proto.jspb.test.extendRepeatedSint64List'); |
| 87 goog.require('proto.jspb.test.extendRepeatedStringList'); | 86 goog.require('proto.jspb.test.extendRepeatedStringList'); |
| 88 goog.require('proto.jspb.test.extendRepeatedUint32List'); | 87 goog.require('proto.jspb.test.extendRepeatedUint32List'); |
| 89 goog.require('proto.jspb.test.extendRepeatedUint64List'); | 88 goog.require('proto.jspb.test.extendRepeatedUint64List'); |
| 90 | 89 |
| 91 // CommonJS-LoadFromFile: ../node_modules/google-protobuf/google/protobuf/any_pb
proto.google.protobuf | |
| 92 goog.require('proto.google.protobuf.Any'); | |
| 93 | |
| 94 | 90 |
| 95 var suite = {}; | 91 var suite = {}; |
| 96 | 92 |
| 97 var BYTES = new Uint8Array([1, 2, 8, 9]); | 93 var BYTES = new Uint8Array([1, 2, 8, 9]); |
| 98 | 94 |
| 99 var BYTES_B64 = goog.crypt.base64.encodeByteArray(BYTES); | 95 var BYTES_B64 = goog.crypt.base64.encodeByteArray(BYTES); |
| 100 | 96 |
| 101 | 97 |
| 102 /** | 98 /** |
| 103 * Helper: fill all fields on a TestAllTypes message. | 99 * Helper: fill all fields on a TestAllTypes message. |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 191 } | 187 } |
| 192 | 188 |
| 193 | 189 |
| 194 /** | 190 /** |
| 195 * Helper: verify contents of given TestAllTypes message as set by | 191 * Helper: verify contents of given TestAllTypes message as set by |
| 196 * fillAllFields(). | 192 * fillAllFields(). |
| 197 * @param {proto.jspb.test.TestAllTypes} original | 193 * @param {proto.jspb.test.TestAllTypes} original |
| 198 * @param {proto.jspb.test.TestAllTypes} copy | 194 * @param {proto.jspb.test.TestAllTypes} copy |
| 199 */ | 195 */ |
| 200 function checkAllFields(original, copy) { | 196 function checkAllFields(original, copy) { |
| 197 assertTrue(jspb.Message.equals(original, copy)); |
| 198 |
| 201 assertEquals(copy.getOptionalInt32(), -42); | 199 assertEquals(copy.getOptionalInt32(), -42); |
| 202 assertEquals(copy.getOptionalInt64(), -0x7fffffff00000000); | 200 assertEquals(copy.getOptionalInt64(), -0x7fffffff00000000); |
| 203 assertEquals(copy.getOptionalUint32(), 0x80000000); | 201 assertEquals(copy.getOptionalUint32(), 0x80000000); |
| 204 assertEquals(copy.getOptionalUint64(), 0xf000000000000000); | 202 assertEquals(copy.getOptionalUint64(), 0xf000000000000000); |
| 205 assertEquals(copy.getOptionalSint32(), -100); | 203 assertEquals(copy.getOptionalSint32(), -100); |
| 206 assertEquals(copy.getOptionalSint64(), -0x8000000000000000); | 204 assertEquals(copy.getOptionalSint64(), -0x8000000000000000); |
| 207 assertEquals(copy.getOptionalFixed32(), 1234); | 205 assertEquals(copy.getOptionalFixed32(), 1234); |
| 208 assertEquals(copy.getOptionalFixed64(), 0x1234567800000000); | 206 assertEquals(copy.getOptionalFixed64(), 0x1234567800000000); |
| 209 assertEquals(copy.getOptionalSfixed32(), -1234); | 207 assertEquals(copy.getOptionalSfixed32(), -1234); |
| 210 assertEquals(copy.getOptionalSfixed64(), -0x1234567800000000); | 208 assertEquals(copy.getOptionalSfixed64(), -0x1234567800000000); |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 265 [-0x8000000000000000]); | 263 [-0x8000000000000000]); |
| 266 assertElementsEquals(copy.getPackedRepeatedFixed32List(), [1234]); | 264 assertElementsEquals(copy.getPackedRepeatedFixed32List(), [1234]); |
| 267 assertElementsEquals(copy.getPackedRepeatedFixed64List(), | 265 assertElementsEquals(copy.getPackedRepeatedFixed64List(), |
| 268 [0x1234567800000000]); | 266 [0x1234567800000000]); |
| 269 assertElementsEquals(copy.getPackedRepeatedSfixed32List(), [-1234]); | 267 assertElementsEquals(copy.getPackedRepeatedSfixed32List(), [-1234]); |
| 270 assertElementsEquals(copy.getPackedRepeatedSfixed64List(), | 268 assertElementsEquals(copy.getPackedRepeatedSfixed64List(), |
| 271 [-0x1234567800000000]); | 269 [-0x1234567800000000]); |
| 272 assertElementsEquals(copy.getPackedRepeatedFloatList(), [1.5]); | 270 assertElementsEquals(copy.getPackedRepeatedFloatList(), [1.5]); |
| 273 assertElementsEquals(copy.getPackedRepeatedDoubleList(), [-1.5]); | 271 assertElementsEquals(copy.getPackedRepeatedDoubleList(), [-1.5]); |
| 274 | 272 |
| 275 | |
| 276 // Check last so we get more granular errors first. | |
| 277 assertTrue(jspb.Message.equals(original, copy)); | |
| 278 } | 273 } |
| 279 | 274 |
| 280 | 275 |
| 281 /** | 276 /** |
| 282 * Helper: verify that all expected extensions are present. | 277 * Helper: verify that all expected extensions are present. |
| 283 * @param {!proto.jspb.test.TestExtendable} msg | 278 * @param {!proto.jspb.test.TestExtendable} msg |
| 284 */ | 279 */ |
| 285 function checkExtensions(msg) { | 280 function checkExtensions(msg) { |
| 286 assertEquals(-42, | 281 assertEquals(-42, |
| 287 msg.getExtension(proto.jspb.test.extendOptionalInt32)); | 282 msg.getExtension(proto.jspb.test.extendOptionalInt32)); |
| (...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 494 assertTrue(bytesCompare(msg.getRepeatedBytesList_asU8()[0], BYTES)); | 489 assertTrue(bytesCompare(msg.getRepeatedBytesList_asU8()[0], BYTES)); |
| 495 assertTrue(bytesCompare(msg.getRepeatedBytesList_asU8()[1], BYTES)); | 490 assertTrue(bytesCompare(msg.getRepeatedBytesList_asU8()[1], BYTES)); |
| 496 } | 491 } |
| 497 | 492 |
| 498 msg.setRepeatedBytesList([BYTES, BYTES]); | 493 msg.setRepeatedBytesList([BYTES, BYTES]); |
| 499 assertGetters(); | 494 assertGetters(); |
| 500 | 495 |
| 501 msg.setRepeatedBytesList([BYTES_B64, BYTES_B64]); | 496 msg.setRepeatedBytesList([BYTES_B64, BYTES_B64]); |
| 502 assertGetters(); | 497 assertGetters(); |
| 503 | 498 |
| 504 msg.setRepeatedBytesList([]); | 499 msg.setRepeatedBytesList(null); |
| 505 assertEquals(0, msg.getRepeatedBytesList().length); | 500 assertEquals(0, msg.getRepeatedBytesList().length); |
| 506 assertEquals(0, msg.getRepeatedBytesList_asB64().length); | 501 assertEquals(0, msg.getRepeatedBytesList_asB64().length); |
| 507 assertEquals(0, msg.getRepeatedBytesList_asU8().length); | 502 assertEquals(0, msg.getRepeatedBytesList_asU8().length); |
| 508 }); | 503 }); |
| 509 | 504 |
| 510 /** | 505 /** |
| 511 * Helper: fill all extension values. | 506 * Helper: fill all extension values. |
| 512 * @param {proto.jspb.test.TestExtendable} msg | 507 * @param {proto.jspb.test.TestExtendable} msg |
| 513 */ | 508 */ |
| 514 function fillExtensions(msg) { | 509 function fillExtensions(msg) { |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 623 /** | 618 /** |
| 624 * Tests extension serialization and deserialization. | 619 * Tests extension serialization and deserialization. |
| 625 */ | 620 */ |
| 626 it('testExtensions', function() { | 621 it('testExtensions', function() { |
| 627 var msg = new proto.jspb.test.TestExtendable(); | 622 var msg = new proto.jspb.test.TestExtendable(); |
| 628 fillExtensions(msg); | 623 fillExtensions(msg); |
| 629 var encoded = msg.serializeBinary(); | 624 var encoded = msg.serializeBinary(); |
| 630 var decoded = proto.jspb.test.TestExtendable.deserializeBinary(encoded); | 625 var decoded = proto.jspb.test.TestExtendable.deserializeBinary(encoded); |
| 631 checkExtensions(decoded); | 626 checkExtensions(decoded); |
| 632 }); | 627 }); |
| 633 | |
| 634 /** | |
| 635 * Tests that unknown extensions don't cause deserialization failure. | |
| 636 */ | |
| 637 it('testUnknownExtension', function() { | |
| 638 var msg = new proto.jspb.test.TestExtendable(); | |
| 639 fillExtensions(msg); | |
| 640 var writer = new jspb.BinaryWriter(); | |
| 641 writer.writeBool((1 << 29) - 1, true); | |
| 642 proto.jspb.test.TestExtendable.serializeBinaryToWriter(msg, writer); | |
| 643 var encoded = writer.getResultBuffer(); | |
| 644 var decoded = proto.jspb.test.TestExtendable.deserializeBinary(encoded); | |
| 645 checkExtensions(decoded); | |
| 646 }); | |
| 647 | |
| 648 it('testAnyWellKnownType', function() { | |
| 649 var any = new proto.google.protobuf.Any(); | |
| 650 var msg = new proto.jspb.test.TestAllTypes(); | |
| 651 | |
| 652 fillAllFields(msg); | |
| 653 | |
| 654 any.pack(msg.serializeBinary(), 'jspb.test.TestAllTypes'); | |
| 655 | |
| 656 assertEquals('type.googleapis.com/jspb.test.TestAllTypes', | |
| 657 any.getTypeUrl()); | |
| 658 | |
| 659 var msg2 = any.unpack( | |
| 660 proto.jspb.test.TestAllTypes.deserializeBinary, | |
| 661 'jspb.test.TestAllTypes'); | |
| 662 | |
| 663 checkAllFields(msg, msg2); | |
| 664 }); | |
| 665 }); | 628 }); |
| OLD | NEW |