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 16 matching lines...) Expand all Loading... |
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.setTestOnly(); | 33 goog.setTestOnly(); |
34 | 34 |
35 goog.require('goog.json'); | 35 goog.require('goog.json'); |
36 goog.require('goog.testing.asserts'); | 36 goog.require('goog.testing.asserts'); |
| 37 |
| 38 // CommonJS-LoadFromFile: google-protobuf jspb |
37 goog.require('jspb.Message'); | 39 goog.require('jspb.Message'); |
| 40 |
| 41 // CommonJS-LoadFromFile: test5_pb proto.jspb.exttest.beta |
38 goog.require('proto.jspb.exttest.beta.floatingStrField'); | 42 goog.require('proto.jspb.exttest.beta.floatingStrField'); |
| 43 |
| 44 // CommonJS-LoadFromFile: test3_pb proto.jspb.exttest |
39 goog.require('proto.jspb.exttest.floatingMsgField'); | 45 goog.require('proto.jspb.exttest.floatingMsgField'); |
| 46 |
| 47 // CommonJS-LoadFromFile: test4_pb proto.jspb.exttest |
40 goog.require('proto.jspb.exttest.floatingMsgFieldTwo'); | 48 goog.require('proto.jspb.exttest.floatingMsgFieldTwo'); |
| 49 |
| 50 // CommonJS-LoadFromFile: test_pb proto.jspb.test |
41 goog.require('proto.jspb.test.CloneExtension'); | 51 goog.require('proto.jspb.test.CloneExtension'); |
42 goog.require('proto.jspb.test.Complex'); | 52 goog.require('proto.jspb.test.Complex'); |
43 goog.require('proto.jspb.test.DefaultValues'); | 53 goog.require('proto.jspb.test.DefaultValues'); |
44 goog.require('proto.jspb.test.Empty'); | 54 goog.require('proto.jspb.test.Empty'); |
45 goog.require('proto.jspb.test.EnumContainer'); | 55 goog.require('proto.jspb.test.EnumContainer'); |
46 goog.require('proto.jspb.test.ExtensionMessage'); | |
47 goog.require('proto.jspb.test.floatingMsgField'); | 56 goog.require('proto.jspb.test.floatingMsgField'); |
| 57 goog.require('proto.jspb.test.FloatingPointFields'); |
48 goog.require('proto.jspb.test.floatingStrField'); | 58 goog.require('proto.jspb.test.floatingStrField'); |
49 goog.require('proto.jspb.test.HasExtensions'); | 59 goog.require('proto.jspb.test.HasExtensions'); |
50 goog.require('proto.jspb.test.IndirectExtension'); | 60 goog.require('proto.jspb.test.IndirectExtension'); |
51 goog.require('proto.jspb.test.IsExtension'); | 61 goog.require('proto.jspb.test.IsExtension'); |
52 goog.require('proto.jspb.test.OptionalFields'); | 62 goog.require('proto.jspb.test.OptionalFields'); |
53 goog.require('proto.jspb.test.OuterEnum'); | 63 goog.require('proto.jspb.test.OuterEnum'); |
54 goog.require('proto.jspb.test.simple1'); | 64 goog.require('proto.jspb.test.OuterMessage.Complex'); |
55 goog.require('proto.jspb.test.Simple1'); | 65 goog.require('proto.jspb.test.Simple1'); |
56 goog.require('proto.jspb.test.Simple2'); | 66 goog.require('proto.jspb.test.Simple2'); |
57 goog.require('proto.jspb.test.SpecialCases'); | 67 goog.require('proto.jspb.test.SpecialCases'); |
58 goog.require('proto.jspb.test.TestClone'); | 68 goog.require('proto.jspb.test.TestClone'); |
59 goog.require('proto.jspb.test.TestExtensionsMessage'); | |
60 goog.require('proto.jspb.test.TestGroup'); | 69 goog.require('proto.jspb.test.TestGroup'); |
61 goog.require('proto.jspb.test.TestGroup1'); | 70 goog.require('proto.jspb.test.TestGroup1'); |
62 goog.require('proto.jspb.test.TestMessageWithOneof'); | 71 goog.require('proto.jspb.test.TestMessageWithOneof'); |
63 goog.require('proto.jspb.test.TestReservedNames'); | 72 goog.require('proto.jspb.test.TestReservedNames'); |
64 goog.require('proto.jspb.test.TestReservedNamesExtension'); | 73 goog.require('proto.jspb.test.TestReservedNamesExtension'); |
65 | 74 |
| 75 // CommonJS-LoadFromFile: test2_pb proto.jspb.test |
| 76 goog.require('proto.jspb.test.ExtensionMessage'); |
| 77 goog.require('proto.jspb.test.TestExtensionsMessage'); |
66 | 78 |
67 | 79 |
68 | 80 |
| 81 |
69 describe('Message test suite', function() { | 82 describe('Message test suite', function() { |
70 it('testEmptyProto', function() { | 83 it('testEmptyProto', function() { |
71 var empty1 = new proto.jspb.test.Empty([]); | 84 var empty1 = new proto.jspb.test.Empty([]); |
72 var empty2 = new proto.jspb.test.Empty([]); | 85 var empty2 = new proto.jspb.test.Empty([]); |
73 assertObjectEquals({}, empty1.toObject()); | 86 assertObjectEquals({}, empty1.toObject()); |
74 assertObjectEquals('Message should not be corrupted:', empty2, empty1); | 87 assertObjectEquals('Message should not be corrupted:', empty2, empty1); |
75 }); | 88 }); |
76 | 89 |
77 it('testTopLevelEnum', function() { | 90 it('testTopLevelEnum', function() { |
78 var response = new proto.jspb.test.EnumContainer([]); | 91 var response = new proto.jspb.test.EnumContainer([]); |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
135 aNestedMessage: { | 148 aNestedMessage: { |
136 anInt: undefined | 149 anInt: undefined |
137 }, | 150 }, |
138 // Note: JsPb converts undefined repeated fields to empty arrays. | 151 // Note: JsPb converts undefined repeated fields to empty arrays. |
139 aRepeatedMessageList: [], | 152 aRepeatedMessageList: [], |
140 aRepeatedStringList: [] | 153 aRepeatedStringList: [] |
141 }, result); | 154 }, result); |
142 | 155 |
143 }); | 156 }); |
144 | 157 |
| 158 it('testNestedComplexMessage', function() { |
| 159 // Instantiate the message and set a unique field, just to ensure that we |
| 160 // are not getting jspb.test.Complex instead. |
| 161 var msg = new proto.jspb.test.OuterMessage.Complex(); |
| 162 msg.setInnerComplexField(5); |
| 163 }); |
| 164 |
145 it('testSpecialCases', function() { | 165 it('testSpecialCases', function() { |
146 // Note: Some property names are reserved in JavaScript. | 166 // Note: Some property names are reserved in JavaScript. |
147 // These names are converted to the Js property named pb_<reserved_name>. | 167 // These names are converted to the Js property named pb_<reserved_name>. |
148 var special = | 168 var special = |
149 new proto.jspb.test.SpecialCases(['normal', 'default', 'function', | 169 new proto.jspb.test.SpecialCases(['normal', 'default', 'function', |
150 'var']); | 170 'var']); |
151 var result = special.toObject(); | 171 var result = special.toObject(); |
152 assertObjectEquals({ | 172 assertObjectEquals({ |
153 normal: 'normal', | 173 normal: 'normal', |
154 pb_default: 'default', | 174 pb_default: 'default', |
(...skipping 364 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
519 proto.jspb.test.IndirectExtension.repeatedStrList)); | 539 proto.jspb.test.IndirectExtension.repeatedStrList)); |
520 assertObjectEquals([s1, s2], extendable.getExtension( | 540 assertObjectEquals([s1, s2], extendable.getExtension( |
521 proto.jspb.test.IndirectExtension.repeatedSimpleList)); | 541 proto.jspb.test.IndirectExtension.repeatedSimpleList)); |
522 // Not supported yet, but it should work... | 542 // Not supported yet, but it should work... |
523 extendable.setExtension(proto.jspb.test.IndirectExtension.simple, null); | 543 extendable.setExtension(proto.jspb.test.IndirectExtension.simple, null); |
524 assertNull( | 544 assertNull( |
525 extendable.getExtension(proto.jspb.test.IndirectExtension.simple)); | 545 extendable.getExtension(proto.jspb.test.IndirectExtension.simple)); |
526 extendable.setExtension(proto.jspb.test.IndirectExtension.str, null); | 546 extendable.setExtension(proto.jspb.test.IndirectExtension.str, null); |
527 assertNull(extendable.getExtension(proto.jspb.test.IndirectExtension.str)); | 547 assertNull(extendable.getExtension(proto.jspb.test.IndirectExtension.str)); |
528 | 548 |
529 // These assertions will only work properly in uncompiled mode. | |
530 // Extension fields defined on proto2 Descriptor messages are filtered out. | |
531 | |
532 // TODO(haberman): codegen changes to properly ignore descriptor.proto | |
533 // extensions need to be merged from google3. | |
534 // assertUndefined(proto.jspb.test.IsExtension['simpleOption']); | |
535 | 549 |
536 // Extension fields with jspb.ignore = true are ignored. | 550 // Extension fields with jspb.ignore = true are ignored. |
537 assertUndefined(proto.jspb.test.IndirectExtension['ignored']); | 551 assertUndefined(proto.jspb.test.IndirectExtension['ignored']); |
538 assertUndefined(proto.jspb.test.HasExtensions['ignoredFloating']); | 552 assertUndefined(proto.jspb.test.HasExtensions['ignoredFloating']); |
539 }); | 553 }); |
540 | 554 |
541 it('testFloatingExtensions', function() { | 555 it('testFloatingExtensions', function() { |
542 // From an autogenerated container. | 556 // From an autogenerated container. |
543 var extendable = new proto.jspb.test.HasExtensions(['v1', 'v2', 'v3']); | 557 var extendable = new proto.jspb.test.HasExtensions(['v1', 'v2', 'v3']); |
544 var extension = new proto.jspb.test.Simple1(['foo', ['s1', 's2']]); | 558 var extension = new proto.jspb.test.Simple1(['foo', ['s1', 's2']]); |
(...skipping 337 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
882 | 896 |
883 message = | 897 message = |
884 new proto.jspb.test.TestMessageWithOneof(new Array(10).concat(890)); | 898 new proto.jspb.test.TestMessageWithOneof(new Array(10).concat(890)); |
885 assertEquals(1234, message.getAone()); | 899 assertEquals(1234, message.getAone()); |
886 assertEquals(890, message.getAtwo()); | 900 assertEquals(890, message.getAtwo()); |
887 assertEquals( | 901 assertEquals( |
888 proto.jspb.test.TestMessageWithOneof.DefaultOneofACase.ATWO, | 902 proto.jspb.test.TestMessageWithOneof.DefaultOneofACase.ATWO, |
889 message.getDefaultOneofACase()); | 903 message.getDefaultOneofACase()); |
890 | 904 |
891 message = | 905 message = |
892 new proto.jspb.test.TestMessageWithOneof(new Array(9).concat(567,890)); | 906 new proto.jspb.test.TestMessageWithOneof(new Array(9).concat(567, 890)); |
893 assertEquals(1234, message.getAone()); | 907 assertEquals(1234, message.getAone()); |
894 assertEquals(890, message.getAtwo()); | 908 assertEquals(890, message.getAtwo()); |
895 assertEquals( | 909 assertEquals( |
896 proto.jspb.test.TestMessageWithOneof.DefaultOneofACase.ATWO, | 910 proto.jspb.test.TestMessageWithOneof.DefaultOneofACase.ATWO, |
897 message.getDefaultOneofACase()); | 911 message.getDefaultOneofACase()); |
898 }); | 912 }); |
899 | 913 |
900 it('testInitializeMessageWithOneofDefaults_defaultNotSetOnFirstField', | 914 it('testInitializeMessageWithOneofDefaults_defaultNotSetOnFirstField', |
901 function() { | 915 function() { |
902 var message; | 916 var message; |
903 | 917 |
904 message = | 918 message = |
905 new proto.jspb.test.TestMessageWithOneof(new Array(11).concat(567)); | 919 new proto.jspb.test.TestMessageWithOneof(new Array(11).concat(567)); |
906 assertEquals(567, message.getBone()); | 920 assertEquals(567, message.getBone()); |
907 assertEquals(1234, message.getBtwo()); | 921 assertEquals(1234, message.getBtwo()); |
908 assertEquals( | 922 assertEquals( |
909 proto.jspb.test.TestMessageWithOneof.DefaultOneofBCase.BONE, | 923 proto.jspb.test.TestMessageWithOneof.DefaultOneofBCase.BONE, |
910 message.getDefaultOneofBCase()); | 924 message.getDefaultOneofBCase()); |
911 | 925 |
912 message = | 926 message = |
913 new proto.jspb.test.TestMessageWithOneof(new Array(12).concat(890)); | 927 new proto.jspb.test.TestMessageWithOneof(new Array(12).concat(890)); |
914 assertUndefined(message.getBone()); | 928 assertUndefined(message.getBone()); |
915 assertEquals(890, message.getBtwo()); | 929 assertEquals(890, message.getBtwo()); |
916 assertEquals( | 930 assertEquals( |
917 proto.jspb.test.TestMessageWithOneof.DefaultOneofBCase.BTWO, | 931 proto.jspb.test.TestMessageWithOneof.DefaultOneofBCase.BTWO, |
918 message.getDefaultOneofBCase()); | 932 message.getDefaultOneofBCase()); |
919 | 933 |
920 message = new proto.jspb.test.TestMessageWithOneof( | 934 message = new proto.jspb.test.TestMessageWithOneof( |
921 new Array(11).concat(567,890)); | 935 new Array(11).concat(567, 890)); |
922 assertUndefined(message.getBone()); | 936 assertUndefined(message.getBone()); |
923 assertEquals(890, message.getBtwo()); | 937 assertEquals(890, message.getBtwo()); |
924 assertEquals( | 938 assertEquals( |
925 proto.jspb.test.TestMessageWithOneof.DefaultOneofBCase.BTWO, | 939 proto.jspb.test.TestMessageWithOneof.DefaultOneofBCase.BTWO, |
926 message.getDefaultOneofBCase()); | 940 message.getDefaultOneofBCase()); |
927 }); | 941 }); |
928 | 942 |
929 it('testOneofContainingAnotherMessage', function() { | 943 it('testOneofContainingAnotherMessage', function() { |
930 var message = new proto.jspb.test.TestMessageWithOneof; | 944 var message = new proto.jspb.test.TestMessageWithOneof; |
931 assertEquals( | 945 assertEquals( |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
964 assertUndefined(array[4]); | 978 assertUndefined(array[4]); |
965 array[4] = 'y'; | 979 array[4] = 'y'; |
966 | 980 |
967 assertEquals( | 981 assertEquals( |
968 proto.jspb.test.TestMessageWithOneof.PartialOneofCase.PTHREE, | 982 proto.jspb.test.TestMessageWithOneof.PartialOneofCase.PTHREE, |
969 message.getPartialOneofCase()); | 983 message.getPartialOneofCase()); |
970 assertUndefined(array[2]); | 984 assertUndefined(array[2]); |
971 assertEquals('y', array[4]); | 985 assertEquals('y', array[4]); |
972 }); | 986 }); |
973 | 987 |
| 988 it('testFloatingPointFieldsSupportNan', function() { |
| 989 var assertNan = function(x) { |
| 990 assertTrue('Expected ' + x + ' (' + goog.typeOf(x) + ') to be NaN.', |
| 991 goog.isNumber(x) && isNaN(x)); |
| 992 }; |
| 993 |
| 994 var message = new proto.jspb.test.FloatingPointFields([ |
| 995 'NaN', 'NaN', ['NaN', 'NaN'], 'NaN', |
| 996 'NaN', 'NaN', ['NaN', 'NaN'], 'NaN' |
| 997 ]); |
| 998 assertNan(message.getOptionalFloatField()); |
| 999 assertNan(message.getRequiredFloatField()); |
| 1000 assertNan(message.getRepeatedFloatFieldList()[0]); |
| 1001 assertNan(message.getRepeatedFloatFieldList()[1]); |
| 1002 assertNan(message.getDefaultFloatField()); |
| 1003 assertNan(message.getOptionalDoubleField()); |
| 1004 assertNan(message.getRequiredDoubleField()); |
| 1005 assertNan(message.getRepeatedDoubleFieldList()[0]); |
| 1006 assertNan(message.getRepeatedDoubleFieldList()[1]); |
| 1007 assertNan(message.getDefaultDoubleField()); |
| 1008 }); |
| 1009 |
974 }); | 1010 }); |
OLD | NEW |