| 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'); |
| 35 goog.require('jspb.Message'); | 36 goog.require('jspb.Message'); |
| 36 | 37 |
| 37 // CommonJS-LoadFromFile: ../testbinary_pb proto.jspb.test | 38 // CommonJS-LoadFromFile: ../testbinary_pb proto.jspb.test |
| 38 goog.require('proto.jspb.test.ExtendsWithMessage'); | 39 goog.require('proto.jspb.test.ExtendsWithMessage'); |
| 39 goog.require('proto.jspb.test.ForeignEnum'); | 40 goog.require('proto.jspb.test.ForeignEnum'); |
| 40 goog.require('proto.jspb.test.ForeignMessage'); | 41 goog.require('proto.jspb.test.ForeignMessage'); |
| 41 goog.require('proto.jspb.test.TestAllTypes'); | 42 goog.require('proto.jspb.test.TestAllTypes'); |
| 42 goog.require('proto.jspb.test.TestExtendable'); | 43 goog.require('proto.jspb.test.TestExtendable'); |
| 43 goog.require('proto.jspb.test.extendOptionalBool'); | 44 goog.require('proto.jspb.test.extendOptionalBool'); |
| 44 goog.require('proto.jspb.test.extendOptionalBytes'); | 45 goog.require('proto.jspb.test.extendOptionalBytes'); |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 80 goog.require('proto.jspb.test.extendRepeatedInt32List'); | 81 goog.require('proto.jspb.test.extendRepeatedInt32List'); |
| 81 goog.require('proto.jspb.test.extendRepeatedInt64List'); | 82 goog.require('proto.jspb.test.extendRepeatedInt64List'); |
| 82 goog.require('proto.jspb.test.extendRepeatedSfixed32List'); | 83 goog.require('proto.jspb.test.extendRepeatedSfixed32List'); |
| 83 goog.require('proto.jspb.test.extendRepeatedSfixed64List'); | 84 goog.require('proto.jspb.test.extendRepeatedSfixed64List'); |
| 84 goog.require('proto.jspb.test.extendRepeatedSint32List'); | 85 goog.require('proto.jspb.test.extendRepeatedSint32List'); |
| 85 goog.require('proto.jspb.test.extendRepeatedSint64List'); | 86 goog.require('proto.jspb.test.extendRepeatedSint64List'); |
| 86 goog.require('proto.jspb.test.extendRepeatedStringList'); | 87 goog.require('proto.jspb.test.extendRepeatedStringList'); |
| 87 goog.require('proto.jspb.test.extendRepeatedUint32List'); | 88 goog.require('proto.jspb.test.extendRepeatedUint32List'); |
| 88 goog.require('proto.jspb.test.extendRepeatedUint64List'); | 89 goog.require('proto.jspb.test.extendRepeatedUint64List'); |
| 89 | 90 |
| 91 // CommonJS-LoadFromFile: ../node_modules/google-protobuf/google/protobuf/any_pb
proto.google.protobuf |
| 92 goog.require('proto.google.protobuf.Any'); |
| 93 |
| 90 | 94 |
| 91 var suite = {}; | 95 var suite = {}; |
| 92 | 96 |
| 93 var BYTES = new Uint8Array([1, 2, 8, 9]); | 97 var BYTES = new Uint8Array([1, 2, 8, 9]); |
| 94 | 98 |
| 95 var BYTES_B64 = goog.crypt.base64.encodeByteArray(BYTES); | 99 var BYTES_B64 = goog.crypt.base64.encodeByteArray(BYTES); |
| 96 | 100 |
| 97 | 101 |
| 98 /** | 102 /** |
| 99 * Helper: fill all fields on a TestAllTypes message. | 103 * Helper: fill all fields on a TestAllTypes message. |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 187 } | 191 } |
| 188 | 192 |
| 189 | 193 |
| 190 /** | 194 /** |
| 191 * Helper: verify contents of given TestAllTypes message as set by | 195 * Helper: verify contents of given TestAllTypes message as set by |
| 192 * fillAllFields(). | 196 * fillAllFields(). |
| 193 * @param {proto.jspb.test.TestAllTypes} original | 197 * @param {proto.jspb.test.TestAllTypes} original |
| 194 * @param {proto.jspb.test.TestAllTypes} copy | 198 * @param {proto.jspb.test.TestAllTypes} copy |
| 195 */ | 199 */ |
| 196 function checkAllFields(original, copy) { | 200 function checkAllFields(original, copy) { |
| 197 assertTrue(jspb.Message.equals(original, copy)); | |
| 198 | |
| 199 assertEquals(copy.getOptionalInt32(), -42); | 201 assertEquals(copy.getOptionalInt32(), -42); |
| 200 assertEquals(copy.getOptionalInt64(), -0x7fffffff00000000); | 202 assertEquals(copy.getOptionalInt64(), -0x7fffffff00000000); |
| 201 assertEquals(copy.getOptionalUint32(), 0x80000000); | 203 assertEquals(copy.getOptionalUint32(), 0x80000000); |
| 202 assertEquals(copy.getOptionalUint64(), 0xf000000000000000); | 204 assertEquals(copy.getOptionalUint64(), 0xf000000000000000); |
| 203 assertEquals(copy.getOptionalSint32(), -100); | 205 assertEquals(copy.getOptionalSint32(), -100); |
| 204 assertEquals(copy.getOptionalSint64(), -0x8000000000000000); | 206 assertEquals(copy.getOptionalSint64(), -0x8000000000000000); |
| 205 assertEquals(copy.getOptionalFixed32(), 1234); | 207 assertEquals(copy.getOptionalFixed32(), 1234); |
| 206 assertEquals(copy.getOptionalFixed64(), 0x1234567800000000); | 208 assertEquals(copy.getOptionalFixed64(), 0x1234567800000000); |
| 207 assertEquals(copy.getOptionalSfixed32(), -1234); | 209 assertEquals(copy.getOptionalSfixed32(), -1234); |
| 208 assertEquals(copy.getOptionalSfixed64(), -0x1234567800000000); | 210 assertEquals(copy.getOptionalSfixed64(), -0x1234567800000000); |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 263 [-0x8000000000000000]); | 265 [-0x8000000000000000]); |
| 264 assertElementsEquals(copy.getPackedRepeatedFixed32List(), [1234]); | 266 assertElementsEquals(copy.getPackedRepeatedFixed32List(), [1234]); |
| 265 assertElementsEquals(copy.getPackedRepeatedFixed64List(), | 267 assertElementsEquals(copy.getPackedRepeatedFixed64List(), |
| 266 [0x1234567800000000]); | 268 [0x1234567800000000]); |
| 267 assertElementsEquals(copy.getPackedRepeatedSfixed32List(), [-1234]); | 269 assertElementsEquals(copy.getPackedRepeatedSfixed32List(), [-1234]); |
| 268 assertElementsEquals(copy.getPackedRepeatedSfixed64List(), | 270 assertElementsEquals(copy.getPackedRepeatedSfixed64List(), |
| 269 [-0x1234567800000000]); | 271 [-0x1234567800000000]); |
| 270 assertElementsEquals(copy.getPackedRepeatedFloatList(), [1.5]); | 272 assertElementsEquals(copy.getPackedRepeatedFloatList(), [1.5]); |
| 271 assertElementsEquals(copy.getPackedRepeatedDoubleList(), [-1.5]); | 273 assertElementsEquals(copy.getPackedRepeatedDoubleList(), [-1.5]); |
| 272 | 274 |
| 275 |
| 276 // Check last so we get more granular errors first. |
| 277 assertTrue(jspb.Message.equals(original, copy)); |
| 273 } | 278 } |
| 274 | 279 |
| 275 | 280 |
| 276 /** | 281 /** |
| 277 * Helper: verify that all expected extensions are present. | 282 * Helper: verify that all expected extensions are present. |
| 278 * @param {!proto.jspb.test.TestExtendable} msg | 283 * @param {!proto.jspb.test.TestExtendable} msg |
| 279 */ | 284 */ |
| 280 function checkExtensions(msg) { | 285 function checkExtensions(msg) { |
| 281 assertEquals(-42, | 286 assertEquals(-42, |
| 282 msg.getExtension(proto.jspb.test.extendOptionalInt32)); | 287 msg.getExtension(proto.jspb.test.extendOptionalInt32)); |
| (...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 489 assertTrue(bytesCompare(msg.getRepeatedBytesList_asU8()[0], BYTES)); | 494 assertTrue(bytesCompare(msg.getRepeatedBytesList_asU8()[0], BYTES)); |
| 490 assertTrue(bytesCompare(msg.getRepeatedBytesList_asU8()[1], BYTES)); | 495 assertTrue(bytesCompare(msg.getRepeatedBytesList_asU8()[1], BYTES)); |
| 491 } | 496 } |
| 492 | 497 |
| 493 msg.setRepeatedBytesList([BYTES, BYTES]); | 498 msg.setRepeatedBytesList([BYTES, BYTES]); |
| 494 assertGetters(); | 499 assertGetters(); |
| 495 | 500 |
| 496 msg.setRepeatedBytesList([BYTES_B64, BYTES_B64]); | 501 msg.setRepeatedBytesList([BYTES_B64, BYTES_B64]); |
| 497 assertGetters(); | 502 assertGetters(); |
| 498 | 503 |
| 499 msg.setRepeatedBytesList(null); | 504 msg.setRepeatedBytesList([]); |
| 500 assertEquals(0, msg.getRepeatedBytesList().length); | 505 assertEquals(0, msg.getRepeatedBytesList().length); |
| 501 assertEquals(0, msg.getRepeatedBytesList_asB64().length); | 506 assertEquals(0, msg.getRepeatedBytesList_asB64().length); |
| 502 assertEquals(0, msg.getRepeatedBytesList_asU8().length); | 507 assertEquals(0, msg.getRepeatedBytesList_asU8().length); |
| 503 }); | 508 }); |
| 504 | 509 |
| 505 /** | 510 /** |
| 506 * Helper: fill all extension values. | 511 * Helper: fill all extension values. |
| 507 * @param {proto.jspb.test.TestExtendable} msg | 512 * @param {proto.jspb.test.TestExtendable} msg |
| 508 */ | 513 */ |
| 509 function fillExtensions(msg) { | 514 function fillExtensions(msg) { |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 618 /** | 623 /** |
| 619 * Tests extension serialization and deserialization. | 624 * Tests extension serialization and deserialization. |
| 620 */ | 625 */ |
| 621 it('testExtensions', function() { | 626 it('testExtensions', function() { |
| 622 var msg = new proto.jspb.test.TestExtendable(); | 627 var msg = new proto.jspb.test.TestExtendable(); |
| 623 fillExtensions(msg); | 628 fillExtensions(msg); |
| 624 var encoded = msg.serializeBinary(); | 629 var encoded = msg.serializeBinary(); |
| 625 var decoded = proto.jspb.test.TestExtendable.deserializeBinary(encoded); | 630 var decoded = proto.jspb.test.TestExtendable.deserializeBinary(encoded); |
| 626 checkExtensions(decoded); | 631 checkExtensions(decoded); |
| 627 }); | 632 }); |
| 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 }); |
| 628 }); | 665 }); |
| OLD | NEW |