proto2_test.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // Copyright 2012 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. goog.provide('goog.testing.proto2Test');
  15. goog.setTestOnly('goog.testing.proto2Test');
  16. goog.require('goog.testing.TestCase');
  17. goog.require('goog.testing.jsunit');
  18. goog.require('goog.testing.proto2');
  19. goog.require('proto2.TestAllTypes');
  20. function setUp() {
  21. // TODO(b/25875505): Fix unreported assertions (go/failonunreportedasserts).
  22. goog.testing.TestCase.getActiveTestCase().failOnUnreportedAsserts = false;
  23. }
  24. function testAssertEquals() {
  25. var assertProto2Equals = goog.testing.proto2.assertEquals;
  26. assertProto2Equals(new proto2.TestAllTypes, new proto2.TestAllTypes);
  27. assertProto2Equals(new proto2.TestAllTypes, new proto2.TestAllTypes, 'oops');
  28. var ex = assertThrows(
  29. goog.partial(
  30. assertProto2Equals, new proto2.TestAllTypes,
  31. new proto2.TestAllTypes.NestedMessage));
  32. assertEquals(
  33. 'Message type mismatch: TestAllTypes != TestAllTypes.NestedMessage',
  34. ex.message);
  35. var message = new proto2.TestAllTypes;
  36. message.setOptionalInt32(1);
  37. ex = assertThrows(
  38. goog.partial(assertProto2Equals, new proto2.TestAllTypes, message));
  39. assertEquals('optional_int32 should not be present', ex.message);
  40. ex = assertThrows(
  41. goog.partial(
  42. assertProto2Equals, new proto2.TestAllTypes, message, 'oops'));
  43. assertEquals('oops\noptional_int32 should not be present', ex.message);
  44. }
  45. function testFindDifferences_EmptyMessages() {
  46. assertEquals(
  47. '', goog.testing.proto2.findDifferences_(
  48. new proto2.TestAllTypes, new proto2.TestAllTypes, ''));
  49. }
  50. function testFindDifferences_FieldNotPresent() {
  51. var message = new proto2.TestAllTypes;
  52. message.setOptionalInt32(0);
  53. var empty = new proto2.TestAllTypes;
  54. assertEquals(
  55. 'optional_int32 should not be present',
  56. goog.testing.proto2.findDifferences_(empty, message, ''));
  57. assertEquals(
  58. 'optional_int32 should be present',
  59. goog.testing.proto2.findDifferences_(message, empty, ''));
  60. assertEquals(
  61. 'path/optional_int32 should be present',
  62. goog.testing.proto2.findDifferences_(message, empty, 'path'));
  63. }
  64. function testFindDifferences_IntFieldDiffers() {
  65. var message1 = new proto2.TestAllTypes;
  66. message1.setOptionalInt32(1);
  67. var message2 = new proto2.TestAllTypes;
  68. message2.setOptionalInt32(2);
  69. assertEquals(
  70. 'optional_int32 should be 1, but was 2',
  71. goog.testing.proto2.findDifferences_(message1, message2, ''));
  72. }
  73. function testFindDifferences_NestedIntFieldDiffers() {
  74. var message1 = new proto2.TestAllTypes;
  75. var nested1 = new proto2.TestAllTypes.NestedMessage();
  76. nested1.setB(1);
  77. message1.setOptionalNestedMessage(nested1);
  78. var message2 = new proto2.TestAllTypes;
  79. var nested2 = new proto2.TestAllTypes.NestedMessage();
  80. nested2.setB(2);
  81. message2.setOptionalNestedMessage(nested2);
  82. assertEquals(
  83. 'optional_nested_message/b should be 1, but was 2',
  84. goog.testing.proto2.findDifferences_(message1, message2, ''));
  85. }
  86. function testFindDifferences_RepeatedFieldLengthDiffers() {
  87. var message1 = new proto2.TestAllTypes;
  88. message1.addRepeatedInt32(1);
  89. var message2 = new proto2.TestAllTypes;
  90. message2.addRepeatedInt32(1);
  91. message2.addRepeatedInt32(2);
  92. assertEquals(
  93. 'repeated_int32 should have 1 items, but has 2',
  94. goog.testing.proto2.findDifferences_(message1, message2, ''));
  95. }
  96. function testFindDifferences_RepeatedFieldItemDiffers() {
  97. var message1 = new proto2.TestAllTypes;
  98. message1.addRepeatedInt32(1);
  99. var message2 = new proto2.TestAllTypes;
  100. message2.addRepeatedInt32(2);
  101. assertEquals(
  102. 'repeated_int32[0] should be 1, but was 2',
  103. goog.testing.proto2.findDifferences_(message1, message2, ''));
  104. }
  105. function testFindDifferences_RepeatedNestedMessageDiffers() {
  106. var message1 = new proto2.TestAllTypes;
  107. var nested1 = new proto2.TestAllTypes.NestedMessage();
  108. nested1.setB(1);
  109. message1.addRepeatedNestedMessage(nested1);
  110. var message2 = new proto2.TestAllTypes;
  111. var nested2 = new proto2.TestAllTypes.NestedMessage();
  112. nested2.setB(2);
  113. message2.addRepeatedNestedMessage(nested2);
  114. assertEquals(
  115. 'repeated_nested_message[0]/b should be 1, but was 2',
  116. goog.testing.proto2.findDifferences_(message1, message2, ''));
  117. }
  118. function testFromObject() {
  119. var nested = new proto2.TestAllTypes.NestedMessage();
  120. nested.setB(1);
  121. var message = new proto2.TestAllTypes;
  122. message.addRepeatedNestedMessage(nested);
  123. message.setOptionalInt32(2);
  124. // Successfully deserializes simple as well as message fields.
  125. assertObjectEquals(
  126. message,
  127. goog.testing.proto2.fromObject(
  128. proto2.TestAllTypes,
  129. {'optional_int32': 2, 'repeated_nested_message': [{'b': 1}]}));
  130. // Fails if the field name is not recognized.
  131. assertThrows(function() {
  132. goog.testing.proto2.fromObject(proto2.TestAllTypes, {'unknown': 1});
  133. });
  134. // Fails if the value type is wrong in the JSON object.
  135. assertThrows(function() {
  136. goog.testing.proto2.fromObject(
  137. proto2.TestAllTypes, {'optional_int32': '1'});
  138. });
  139. }