Debian Codegraph @

closure-library/closure/goog/testing/proto2/proto2.js

raw
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 /**
16 * @fileoverview Test helpers to compare goog.proto2.Messages.
17 *
18 */
19
20 goog.setTestOnly('goog.testing.proto2');
21 goog.provide('goog.testing.proto2');
22
23 goog.require('goog.proto2.Message');
24 goog.require('goog.proto2.ObjectSerializer');
25 goog.require('goog.testing.asserts');
26
27
28 /**
29 * Compares two goog.proto2.Message instances of the same type.
30 * @param {!goog.proto2.Message} expected First message.
31 * @param {!goog.proto2.Message} actual Second message.
32 * @param {string} path Path to the messages.
33 * @return {string} A string describing where they differ. Empty string if they
34 * are equal.
35 * @private
36 */
37 goog.testing.proto2.findDifferences_ = function(expected, actual, path) {
38 var fields = expected.getDescriptor().getFields();
39 for (var i = 0; i < fields.length; i++) {
40 var field = fields[i];
41 var newPath = (path ? path + '/' : '') + field.getName();
42
43 if (expected.has(field) && !actual.has(field)) {
44 return newPath + ' should be present';
45 }
46 if (!expected.has(field) && actual.has(field)) {
47 return newPath + ' should not be present';
48 }
49
50 if (expected.has(field)) {
51 var isComposite = field.isCompositeType();
52
53 if (field.isRepeated()) {
54 var expectedCount = expected.countOf(field);
55 var actualCount = actual.countOf(field);
56 if (expectedCount != actualCount) {
57 return newPath + ' should have ' + expectedCount + ' items, ' +
58 'but has ' + actualCount;
59 }
60
61 for (var j = 0; j < expectedCount; j++) {
62 var expectedItem = expected.get(field, j);
63 var actualItem = actual.get(field, j);
64 if (isComposite) {
65 var itemDiff = goog.testing.proto2.findDifferences_(
66 /** @type {!goog.proto2.Message} */ (expectedItem),
67 /** @type {!goog.proto2.Message} */ (actualItem),
68 newPath + '[' + j + ']');
69 if (itemDiff) {
70 return itemDiff;
71 }
72 } else {
73 if (expectedItem != actualItem) {
74 return newPath + '[' + j + '] should be ' + expectedItem +
75 ', but was ' + actualItem;
76 }
77 }
78 }
79 } else {
80 var expectedValue = expected.get(field);
81 var actualValue = actual.get(field);
82 if (isComposite) {
83 var diff = goog.testing.proto2.findDifferences_(
84 /** @type {!goog.proto2.Message} */ (expectedValue),
85 /** @type {!goog.proto2.Message} */ (actualValue), newPath);
86 if (diff) {
87 return diff;
88 }
89 } else {
90 if (expectedValue != actualValue) {
91 return newPath + ' should be ' + expectedValue + ', but was ' +
92 actualValue;
93 }
94 }
95 }
96 }
97 }
98
99 return '';
100 };
101
102
103 /**
104 * Compares two goog.proto2.Message objects. Gives more readable output than
105 * assertObjectEquals on mismatch.
106 * @param {!goog.proto2.Message} expected Expected proto2 message.
107 * @param {!goog.proto2.Message} actual Actual proto2 message.
108 * @param {string=} opt_failureMessage Failure message when the values don't
109 * match.
110 */
111 goog.testing.proto2.assertEquals = function(
112 expected, actual, opt_failureMessage) {
113 var failureSummary = opt_failureMessage || '';
114 if (!(expected instanceof goog.proto2.Message) ||
115 !(actual instanceof goog.proto2.Message)) {
116 goog.testing.asserts.raiseException(
117 failureSummary,
118 'Bad arguments were passed to goog.testing.proto2.assertEquals');
119 }
120 if (expected.constructor != actual.constructor) {
121 goog.testing.asserts.raiseException(
122 failureSummary, 'Message type mismatch: ' +
123 expected.getDescriptor().getFullName() + ' != ' +
124 actual.getDescriptor().getFullName());
125 }
126 var diff = goog.testing.proto2.findDifferences_(expected, actual, '');
127 if (diff) {
128 goog.testing.asserts.raiseException(failureSummary, diff);
129 }
130 };
131
132
133 /**
134 * Helper function to quickly build protocol buffer messages from JSON objects.
135 * @param {function(new:MessageType)} messageCtor A constructor that
136 * creates a {@code goog.proto2.Message} subclass instance.
137 * @param {!Object} json JSON object which uses field names as keys.
138 * @return {MessageType} The deserialized protocol buffer.
139 * @template MessageType
140 */
141 goog.testing.proto2.fromObject = function(messageCtor, json) {
142 var serializer = new goog.proto2.ObjectSerializer(
143 goog.proto2.ObjectSerializer.KeyOption.NAME);
144 var message = new messageCtor;
145 serializer.deserializeTo(message, json);
146 return message;
147 };