| 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 |
|
| 15 |
goog.provide('goog.testing.proto2Test'); |
| 16 |
goog.setTestOnly('goog.testing.proto2Test'); |
| 17 |
|
| 18 |
goog.require('goog.testing.TestCase'); |
| 19 |
goog.require('goog.testing.jsunit'); |
| 20 |
goog.require('goog.testing.proto2'); |
| 21 |
goog.require('proto2.TestAllTypes'); |
| 22 |
|
| 23 |
function setUp() { |
| 24 |
// TODO(b/25875505): Fix unreported assertions (go/failonunreportedasserts). |
| 25 |
goog.testing.TestCase.getActiveTestCase().failOnUnreportedAsserts = false; |
| 26 |
} |
| 27 |
|
| 28 |
function testAssertEquals() { |
| 29 |
var assertProto2Equals = goog.testing.proto2.assertEquals; |
| 30 |
assertProto2Equals(new proto2.TestAllTypes, new proto2.TestAllTypes); |
| 31 |
assertProto2Equals(new proto2.TestAllTypes, new proto2.TestAllTypes, 'oops'); |
| 32 |
|
| 33 |
var ex = assertThrows( |
| 34 |
goog.partial( |
| 35 |
assertProto2Equals, new proto2.TestAllTypes, |
| 36 |
new proto2.TestAllTypes.NestedMessage)); |
| 37 |
assertEquals( |
| 38 |
'Message type mismatch: TestAllTypes != TestAllTypes.NestedMessage', |
| 39 |
ex.message); |
| 40 |
|
| 41 |
var message = new proto2.TestAllTypes; |
| 42 |
message.setOptionalInt32(1); |
| 43 |
ex = assertThrows( |
| 44 |
goog.partial(assertProto2Equals, new proto2.TestAllTypes, message)); |
| 45 |
assertEquals('optional_int32 should not be present', ex.message); |
| 46 |
|
| 47 |
ex = assertThrows( |
| 48 |
goog.partial( |
| 49 |
assertProto2Equals, new proto2.TestAllTypes, message, 'oops')); |
| 50 |
assertEquals('oops\noptional_int32 should not be present', ex.message); |
| 51 |
} |
| 52 |
|
| 53 |
function testFindDifferences_EmptyMessages() { |
| 54 |
assertEquals( |
| 55 |
'', goog.testing.proto2.findDifferences_( |
| 56 |
new proto2.TestAllTypes, new proto2.TestAllTypes, '')); |
| 57 |
} |
| 58 |
|
| 59 |
function testFindDifferences_FieldNotPresent() { |
| 60 |
var message = new proto2.TestAllTypes; |
| 61 |
message.setOptionalInt32(0); |
| 62 |
var empty = new proto2.TestAllTypes; |
| 63 |
assertEquals( |
| 64 |
'optional_int32 should not be present', |
| 65 |
goog.testing.proto2.findDifferences_(empty, message, '')); |
| 66 |
assertEquals( |
| 67 |
'optional_int32 should be present', |
| 68 |
goog.testing.proto2.findDifferences_(message, empty, '')); |
| 69 |
assertEquals( |
| 70 |
'path/optional_int32 should be present', |
| 71 |
goog.testing.proto2.findDifferences_(message, empty, 'path')); |
| 72 |
} |
| 73 |
|
| 74 |
function testFindDifferences_IntFieldDiffers() { |
| 75 |
var message1 = new proto2.TestAllTypes; |
| 76 |
message1.setOptionalInt32(1); |
| 77 |
var message2 = new proto2.TestAllTypes; |
| 78 |
message2.setOptionalInt32(2); |
| 79 |
assertEquals( |
| 80 |
'optional_int32 should be 1, but was 2', |
| 81 |
goog.testing.proto2.findDifferences_(message1, message2, '')); |
| 82 |
} |
| 83 |
|
| 84 |
function testFindDifferences_NestedIntFieldDiffers() { |
| 85 |
var message1 = new proto2.TestAllTypes; |
| 86 |
var nested1 = new proto2.TestAllTypes.NestedMessage(); |
| 87 |
nested1.setB(1); |
| 88 |
message1.setOptionalNestedMessage(nested1); |
| 89 |
var message2 = new proto2.TestAllTypes; |
| 90 |
var nested2 = new proto2.TestAllTypes.NestedMessage(); |
| 91 |
nested2.setB(2); |
| 92 |
message2.setOptionalNestedMessage(nested2); |
| 93 |
assertEquals( |
| 94 |
'optional_nested_message/b should be 1, but was 2', |
| 95 |
goog.testing.proto2.findDifferences_(message1, message2, '')); |
| 96 |
} |
| 97 |
|
| 98 |
function testFindDifferences_RepeatedFieldLengthDiffers() { |
| 99 |
var message1 = new proto2.TestAllTypes; |
| 100 |
message1.addRepeatedInt32(1); |
| 101 |
var message2 = new proto2.TestAllTypes; |
| 102 |
message2.addRepeatedInt32(1); |
| 103 |
message2.addRepeatedInt32(2); |
| 104 |
assertEquals( |
| 105 |
'repeated_int32 should have 1 items, but has 2', |
| 106 |
goog.testing.proto2.findDifferences_(message1, message2, '')); |
| 107 |
} |
| 108 |
|
| 109 |
function testFindDifferences_RepeatedFieldItemDiffers() { |
| 110 |
var message1 = new proto2.TestAllTypes; |
| 111 |
message1.addRepeatedInt32(1); |
| 112 |
var message2 = new proto2.TestAllTypes; |
| 113 |
message2.addRepeatedInt32(2); |
| 114 |
assertEquals( |
| 115 |
'repeated_int32[0] should be 1, but was 2', |
| 116 |
goog.testing.proto2.findDifferences_(message1, message2, '')); |
| 117 |
} |
| 118 |
|
| 119 |
function testFindDifferences_RepeatedNestedMessageDiffers() { |
| 120 |
var message1 = new proto2.TestAllTypes; |
| 121 |
var nested1 = new proto2.TestAllTypes.NestedMessage(); |
| 122 |
nested1.setB(1); |
| 123 |
message1.addRepeatedNestedMessage(nested1); |
| 124 |
var message2 = new proto2.TestAllTypes; |
| 125 |
var nested2 = new proto2.TestAllTypes.NestedMessage(); |
| 126 |
nested2.setB(2); |
| 127 |
message2.addRepeatedNestedMessage(nested2); |
| 128 |
assertEquals( |
| 129 |
'repeated_nested_message[0]/b should be 1, but was 2', |
| 130 |
goog.testing.proto2.findDifferences_(message1, message2, '')); |
| 131 |
} |
| 132 |
|
| 133 |
function testFromObject() { |
| 134 |
var nested = new proto2.TestAllTypes.NestedMessage(); |
| 135 |
nested.setB(1); |
| 136 |
var message = new proto2.TestAllTypes; |
| 137 |
message.addRepeatedNestedMessage(nested); |
| 138 |
message.setOptionalInt32(2); |
| 139 |
// Successfully deserializes simple as well as message fields. |
| 140 |
assertObjectEquals( |
| 141 |
message, |
| 142 |
goog.testing.proto2.fromObject( |
| 143 |
proto2.TestAllTypes, |
| 144 |
{'optional_int32': 2, 'repeated_nested_message': [{'b': 1}]})); |
| 145 |
// Fails if the field name is not recognized. |
| 146 |
assertThrows(function() { |
| 147 |
goog.testing.proto2.fromObject(proto2.TestAllTypes, {'unknown': 1}); |
| 148 |
}); |
| 149 |
// Fails if the value type is wrong in the JSON object. |
| 150 |
assertThrows(function() { |
| 151 |
goog.testing.proto2.fromObject( |
| 152 |
proto2.TestAllTypes, {'optional_int32': '1'}); |
| 153 |
}); |
| 154 |
} |