proto3_test.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. goog.require('goog.testing.asserts');
  31. // CommonJS-LoadFromFile: testbinary_pb proto.jspb.test
  32. goog.require('proto.jspb.test.ForeignMessage');
  33. // CommonJS-LoadFromFile: proto3_test_pb proto.jspb.test
  34. goog.require('proto.jspb.test.Proto3Enum');
  35. goog.require('proto.jspb.test.TestProto3');
  36. /**
  37. * Helper: compare a bytes field to a string with codepoints 0--255.
  38. * @param {Uint8Array|string} arr
  39. * @param {string} str
  40. * @return {boolean}
  41. */
  42. function bytesCompare(arr, str) {
  43. if (arr.length != str.length) {
  44. return false;
  45. }
  46. if (typeof arr == 'string') {
  47. for (var i = 0; i < arr.length; i++) {
  48. if (arr.charCodeAt(i) != str.charCodeAt(i)) {
  49. return false;
  50. }
  51. }
  52. return true;
  53. } else {
  54. for (var i = 0; i < arr.length; i++) {
  55. if (arr[i] != str.charCodeAt(i)) {
  56. return false;
  57. }
  58. }
  59. return true;
  60. }
  61. }
  62. describe('proto3Test', function() {
  63. /**
  64. * Test defaults for proto3 message fields.
  65. */
  66. it('testProto3FieldDefaults', function() {
  67. var msg = new proto.jspb.test.TestProto3();
  68. assertEquals(msg.getOptionalInt32(), 0);
  69. assertEquals(msg.getOptionalInt64(), 0);
  70. assertEquals(msg.getOptionalUint32(), 0);
  71. assertEquals(msg.getOptionalUint64(), 0);
  72. assertEquals(msg.getOptionalSint32(), 0);
  73. assertEquals(msg.getOptionalSint64(), 0);
  74. assertEquals(msg.getOptionalFixed32(), 0);
  75. assertEquals(msg.getOptionalFixed64(), 0);
  76. assertEquals(msg.getOptionalSfixed32(), 0);
  77. assertEquals(msg.getOptionalSfixed64(), 0);
  78. assertEquals(msg.getOptionalFloat(), 0);
  79. assertEquals(msg.getOptionalDouble(), 0);
  80. assertEquals(msg.getOptionalString(), '');
  81. // If/when we change bytes fields to return Uint8Array, we'll want to switch
  82. // to this assertion instead:
  83. //assertEquals(msg.getOptionalBytes() instanceof Uint8Array, true);
  84. assertEquals(typeof msg.getOptionalBytes(), 'string');
  85. assertEquals(msg.getOptionalBytes().length, 0);
  86. assertEquals(msg.getOptionalForeignEnum(), proto.jspb.test.Proto3Enum.PROTO3_FOO);
  87. assertEquals(msg.getOptionalForeignMessage(), undefined);
  88. assertEquals(msg.getOptionalForeignMessage(), undefined);
  89. assertEquals(msg.getRepeatedInt32List().length, 0);
  90. assertEquals(msg.getRepeatedInt64List().length, 0);
  91. assertEquals(msg.getRepeatedUint32List().length, 0);
  92. assertEquals(msg.getRepeatedUint64List().length, 0);
  93. assertEquals(msg.getRepeatedSint32List().length, 0);
  94. assertEquals(msg.getRepeatedSint64List().length, 0);
  95. assertEquals(msg.getRepeatedFixed32List().length, 0);
  96. assertEquals(msg.getRepeatedFixed64List().length, 0);
  97. assertEquals(msg.getRepeatedSfixed32List().length, 0);
  98. assertEquals(msg.getRepeatedSfixed64List().length, 0);
  99. assertEquals(msg.getRepeatedFloatList().length, 0);
  100. assertEquals(msg.getRepeatedDoubleList().length, 0);
  101. assertEquals(msg.getRepeatedStringList().length, 0);
  102. assertEquals(msg.getRepeatedBytesList().length, 0);
  103. assertEquals(msg.getRepeatedForeignEnumList().length, 0);
  104. assertEquals(msg.getRepeatedForeignMessageList().length, 0);
  105. });
  106. /**
  107. * Test that all fields can be set and read via a serialization roundtrip.
  108. */
  109. it('testProto3FieldSetGet', function() {
  110. var msg = new proto.jspb.test.TestProto3();
  111. msg.setOptionalInt32(-42);
  112. msg.setOptionalInt64(-0x7fffffff00000000);
  113. msg.setOptionalUint32(0x80000000);
  114. msg.setOptionalUint64(0xf000000000000000);
  115. msg.setOptionalSint32(-100);
  116. msg.setOptionalSint64(-0x8000000000000000);
  117. msg.setOptionalFixed32(1234);
  118. msg.setOptionalFixed64(0x1234567800000000);
  119. msg.setOptionalSfixed32(-1234);
  120. msg.setOptionalSfixed64(-0x1234567800000000);
  121. msg.setOptionalFloat(1.5);
  122. msg.setOptionalDouble(-1.5);
  123. msg.setOptionalBool(true);
  124. msg.setOptionalString('hello world');
  125. msg.setOptionalBytes('bytes');
  126. var submsg = new proto.jspb.test.ForeignMessage();
  127. submsg.setC(16);
  128. msg.setOptionalForeignMessage(submsg);
  129. msg.setOptionalForeignEnum(proto.jspb.test.Proto3Enum.PROTO3_BAR);
  130. msg.setRepeatedInt32List([-42]);
  131. msg.setRepeatedInt64List([-0x7fffffff00000000]);
  132. msg.setRepeatedUint32List([0x80000000]);
  133. msg.setRepeatedUint64List([0xf000000000000000]);
  134. msg.setRepeatedSint32List([-100]);
  135. msg.setRepeatedSint64List([-0x8000000000000000]);
  136. msg.setRepeatedFixed32List([1234]);
  137. msg.setRepeatedFixed64List([0x1234567800000000]);
  138. msg.setRepeatedSfixed32List([-1234]);
  139. msg.setRepeatedSfixed64List([-0x1234567800000000]);
  140. msg.setRepeatedFloatList([1.5]);
  141. msg.setRepeatedDoubleList([-1.5]);
  142. msg.setRepeatedBoolList([true]);
  143. msg.setRepeatedStringList(['hello world']);
  144. msg.setRepeatedBytesList(['bytes']);
  145. submsg = new proto.jspb.test.ForeignMessage();
  146. submsg.setC(1000);
  147. msg.setRepeatedForeignMessageList([submsg]);
  148. msg.setRepeatedForeignEnumList([proto.jspb.test.Proto3Enum.PROTO3_BAR]);
  149. msg.setOneofString('asdf');
  150. var serialized = msg.serializeBinary();
  151. msg = proto.jspb.test.TestProto3.deserializeBinary(serialized);
  152. assertEquals(msg.getOptionalInt32(), -42);
  153. assertEquals(msg.getOptionalInt64(), -0x7fffffff00000000);
  154. assertEquals(msg.getOptionalUint32(), 0x80000000);
  155. assertEquals(msg.getOptionalUint64(), 0xf000000000000000);
  156. assertEquals(msg.getOptionalSint32(), -100);
  157. assertEquals(msg.getOptionalSint64(), -0x8000000000000000);
  158. assertEquals(msg.getOptionalFixed32(), 1234);
  159. assertEquals(msg.getOptionalFixed64(), 0x1234567800000000);
  160. assertEquals(msg.getOptionalSfixed32(), -1234);
  161. assertEquals(msg.getOptionalSfixed64(), -0x1234567800000000);
  162. assertEquals(msg.getOptionalFloat(), 1.5);
  163. assertEquals(msg.getOptionalDouble(), -1.5);
  164. assertEquals(msg.getOptionalBool(), true);
  165. assertEquals(msg.getOptionalString(), 'hello world');
  166. assertEquals(true, bytesCompare(msg.getOptionalBytes(), 'bytes'));
  167. assertEquals(msg.getOptionalForeignMessage().getC(), 16);
  168. assertEquals(msg.getOptionalForeignEnum(),
  169. proto.jspb.test.Proto3Enum.PROTO3_BAR);
  170. assertElementsEquals(msg.getRepeatedInt32List(), [-42]);
  171. assertElementsEquals(msg.getRepeatedInt64List(), [-0x7fffffff00000000]);
  172. assertElementsEquals(msg.getRepeatedUint32List(), [0x80000000]);
  173. assertElementsEquals(msg.getRepeatedUint64List(), [0xf000000000000000]);
  174. assertElementsEquals(msg.getRepeatedSint32List(), [-100]);
  175. assertElementsEquals(msg.getRepeatedSint64List(), [-0x8000000000000000]);
  176. assertElementsEquals(msg.getRepeatedFixed32List(), [1234]);
  177. assertElementsEquals(msg.getRepeatedFixed64List(), [0x1234567800000000]);
  178. assertElementsEquals(msg.getRepeatedSfixed32List(), [-1234]);
  179. assertElementsEquals(msg.getRepeatedSfixed64List(), [-0x1234567800000000]);
  180. assertElementsEquals(msg.getRepeatedFloatList(), [1.5]);
  181. assertElementsEquals(msg.getRepeatedDoubleList(), [-1.5]);
  182. assertElementsEquals(msg.getRepeatedBoolList(), [true]);
  183. assertElementsEquals(msg.getRepeatedStringList(), ['hello world']);
  184. assertEquals(msg.getRepeatedBytesList().length, 1);
  185. assertEquals(true, bytesCompare(msg.getRepeatedBytesList()[0], 'bytes'));
  186. assertEquals(msg.getRepeatedForeignMessageList().length, 1);
  187. assertEquals(msg.getRepeatedForeignMessageList()[0].getC(), 1000);
  188. assertElementsEquals(msg.getRepeatedForeignEnumList(),
  189. [proto.jspb.test.Proto3Enum.PROTO3_BAR]);
  190. assertEquals(msg.getOneofString(), 'asdf');
  191. });
  192. /**
  193. * Test that oneofs continue to have a notion of field presence.
  194. */
  195. it('testOneofs', function() {
  196. var msg = new proto.jspb.test.TestProto3();
  197. assertEquals(msg.getOneofUint32(), undefined);
  198. assertEquals(msg.getOneofForeignMessage(), undefined);
  199. assertEquals(msg.getOneofString(), undefined);
  200. assertEquals(msg.getOneofBytes(), undefined);
  201. msg.setOneofUint32(42);
  202. assertEquals(msg.getOneofUint32(), 42);
  203. assertEquals(msg.getOneofForeignMessage(), undefined);
  204. assertEquals(msg.getOneofString(), undefined);
  205. assertEquals(msg.getOneofBytes(), undefined);
  206. var submsg = new proto.jspb.test.ForeignMessage();
  207. msg.setOneofForeignMessage(submsg);
  208. assertEquals(msg.getOneofUint32(), undefined);
  209. assertEquals(msg.getOneofForeignMessage(), submsg);
  210. assertEquals(msg.getOneofString(), undefined);
  211. assertEquals(msg.getOneofBytes(), undefined);
  212. msg.setOneofString('hello');
  213. assertEquals(msg.getOneofUint32(), undefined);
  214. assertEquals(msg.getOneofForeignMessage(), undefined);
  215. assertEquals(msg.getOneofString(), 'hello');
  216. assertEquals(msg.getOneofBytes(), undefined);
  217. msg.setOneofBytes('\u00FF\u00FF');
  218. assertEquals(msg.getOneofUint32(), undefined);
  219. assertEquals(msg.getOneofForeignMessage(), undefined);
  220. assertEquals(msg.getOneofString(), undefined);
  221. assertEquals(msg.getOneofBytes(), '\u00FF\u00FF');
  222. });
  223. /**
  224. * Test that "default"-valued primitive fields are not emitted on the wire.
  225. */
  226. it('testNoSerializeDefaults', function() {
  227. var msg = new proto.jspb.test.TestProto3();
  228. // Set each primitive to a non-default value, then back to its default, to
  229. // ensure that the serialization is actually checking the value and not just
  230. // whether it has ever been set.
  231. msg.setOptionalInt32(42);
  232. msg.setOptionalInt32(0);
  233. msg.setOptionalDouble(3.14);
  234. msg.setOptionalDouble(0.0);
  235. msg.setOptionalBool(true);
  236. msg.setOptionalBool(false);
  237. msg.setOptionalString('hello world');
  238. msg.setOptionalString('');
  239. msg.setOptionalBytes('\u00FF\u00FF');
  240. msg.setOptionalBytes('');
  241. msg.setOptionalForeignMessage(new proto.jspb.test.ForeignMessage());
  242. msg.setOptionalForeignMessage(null);
  243. msg.setOptionalForeignEnum(proto.jspb.test.Proto3Enum.PROTO3_BAR);
  244. msg.setOptionalForeignEnum(proto.jspb.test.Proto3Enum.PROTO3_FOO);
  245. msg.setOneofUint32(32);
  246. msg.setOneofUint32(null);
  247. var serialized = msg.serializeBinary();
  248. assertEquals(0, serialized.length);
  249. });
  250. });