debug_test.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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.setTestOnly();
  31. goog.require('goog.testing.asserts');
  32. // CommonJS-LoadFromFile: google-protobuf
  33. goog.require('jspb.debug');
  34. // CommonJS-LoadFromFile: test_pb
  35. goog.require('proto.jspb.test.HasExtensions');
  36. goog.require('proto.jspb.test.IsExtension');
  37. goog.require('proto.jspb.test.MapValueMessageNoBinary');
  38. goog.require('proto.jspb.test.Simple1');
  39. goog.require('proto.jspb.test.TestMapFieldsNoBinary');
  40. // CommonJS-LoadFromFile: testbinary_pb
  41. goog.require('proto.jspb.test.TestAllTypes');
  42. describe('debugTest', function() {
  43. it('testSimple1', function() {
  44. if (COMPILED) {
  45. return;
  46. }
  47. var message = new proto.jspb.test.Simple1();
  48. message.setAString('foo');
  49. assertObjectEquals({
  50. $name: 'proto.jspb.test.Simple1',
  51. 'aString': 'foo',
  52. 'aRepeatedStringList': []
  53. }, jspb.debug.dump(message));
  54. message.setABoolean(true);
  55. message.setARepeatedStringList(['1', '2']);
  56. assertObjectEquals({
  57. $name: 'proto.jspb.test.Simple1',
  58. 'aString': 'foo',
  59. 'aRepeatedStringList': ['1', '2'],
  60. 'aBoolean': true
  61. }, jspb.debug.dump(message));
  62. message.clearAString();
  63. assertObjectEquals({
  64. $name: 'proto.jspb.test.Simple1',
  65. 'aRepeatedStringList': ['1', '2'],
  66. 'aBoolean': true
  67. }, jspb.debug.dump(message));
  68. });
  69. it('testBytes', function() {
  70. if (COMPILED || typeof Uint8Array == 'undefined') {
  71. return;
  72. }
  73. var message = new proto.jspb.test.TestAllTypes();
  74. var bytes = new Uint8Array(4);
  75. message.setOptionalBytes(bytes);
  76. assertEquals(jspb.debug.dump(message)['optionalBytes'], bytes);
  77. });
  78. it('testExtensions', function() {
  79. if (COMPILED) {
  80. return;
  81. }
  82. var extension = new proto.jspb.test.IsExtension();
  83. extension.setExt1('ext1field');
  84. var extendable = new proto.jspb.test.HasExtensions();
  85. extendable.setStr1('v1');
  86. extendable.setStr2('v2');
  87. extendable.setStr3('v3');
  88. extendable.setExtension(proto.jspb.test.IsExtension.extField, extension);
  89. assertObjectEquals({
  90. '$name': 'proto.jspb.test.HasExtensions',
  91. 'str1': 'v1',
  92. 'str2': 'v2',
  93. 'str3': 'v3',
  94. '$extensions': {
  95. 'extField': {
  96. '$name': 'proto.jspb.test.IsExtension',
  97. 'ext1': 'ext1field'
  98. },
  99. 'repeatedSimpleList': []
  100. }
  101. }, jspb.debug.dump(extendable));
  102. });
  103. it('testMapsBasicTypes', function() {
  104. if (COMPILED) {
  105. return;
  106. }
  107. var message = new proto.jspb.test.TestMapFieldsNoBinary();
  108. message.getMapBoolStringMap().set(true, 'bool_string_value1');
  109. message.getMapBoolStringMap().set(false, 'bool_string_value2');
  110. message.getMapStringInt32Map().set('key', 111);
  111. assertObjectEquals({
  112. '$name': 'proto.jspb.test.TestMapFieldsNoBinary',
  113. 'mapBoolStringMap': {
  114. true: 'bool_string_value1',
  115. false: 'bool_string_value2'
  116. },
  117. 'mapInt32StringMap': {},
  118. 'mapInt64StringMap': {},
  119. 'mapStringBoolMap': {},
  120. 'mapStringDoubleMap': {},
  121. 'mapStringEnumMap': {},
  122. 'mapStringInt32Map': {
  123. 'key': 111
  124. },
  125. 'mapStringInt64Map': {},
  126. 'mapStringMsgMap': {},
  127. 'mapStringStringMap': {},
  128. 'mapStringTestmapfieldsMap': {}
  129. }, jspb.debug.dump(message));
  130. });
  131. it('testMapsMessageValues', function() {
  132. if (COMPILED) {
  133. return;
  134. }
  135. var value1 = new proto.jspb.test.MapValueMessageNoBinary();
  136. value1.setFoo(1111);
  137. var value2 = new proto.jspb.test.MapValueMessageNoBinary();
  138. value2.setFoo(2222);
  139. var message = new proto.jspb.test.TestMapFieldsNoBinary();
  140. message.getMapStringMsgMap().set('key1', value1);
  141. message.getMapStringMsgMap().set('key2', value2);
  142. assertObjectEquals({
  143. '$name': 'proto.jspb.test.TestMapFieldsNoBinary',
  144. 'mapBoolStringMap': {},
  145. 'mapInt32StringMap': {},
  146. 'mapInt64StringMap': {},
  147. 'mapStringBoolMap': {},
  148. 'mapStringDoubleMap': {},
  149. 'mapStringEnumMap': {},
  150. 'mapStringInt32Map': {},
  151. 'mapStringInt64Map': {},
  152. 'mapStringMsgMap': {
  153. 'key1': {
  154. '$name': 'proto.jspb.test.MapValueMessageNoBinary',
  155. 'foo': 1111
  156. },
  157. 'key2': {
  158. '$name': 'proto.jspb.test.MapValueMessageNoBinary',
  159. 'foo': 2222
  160. }
  161. },
  162. 'mapStringStringMap': {},
  163. 'mapStringTestmapfieldsMap': {}
  164. }, jspb.debug.dump(message));
  165. });
  166. });