proto3_test.js 11 KB

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