Debian Codegraph @

closure-library/closure/goog/testing/i18n/asserts.js

raw
1 // Copyright 2013 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 Assert functions that account for locale data changes.
17 *
18 * The locale data gets updated from CLDR (http://cldr.unicode.org/),
19 * and CLDR gets an update about twice per year.
20 * So the locale data are expected to change.
21 * This can make unit tests quite fragile:
22 * assertEquals("Dec 31, 2013, 1:23pm", format);
23 * Now imagine that the decision is made to add a dot after abbreviations,
24 * and a comma between date and time.
25 * The previous assert will fail, because the string is now
26 * "Dec. 31 2013, 1:23pm"
27 *
28 * One option is to not unit test the results of the formatters client side,
29 * and just trust that CLDR and closure/i18n takes care of that.
30 * The other option is to be a more flexible when testing.
31 * This is the role of assertI18nEquals, to centralize all the small
32 * differences between hard-coded values in unit tests and the current result.
33 * It allows some decupling, so that the closure/i18n can be updated without
34 * breaking all the clients using it.
35 * For the example above, this will succeed:
36 * assertI18nEquals("Dec 31, 2013, 1:23pm", "Dec. 31, 2013 1:23pm");
37 * It does this by white-listing, no "guessing" involved.
38 *
39 * But I would say that the best practice is the first option: trust the
40 * library, stop unit-testing it.
41 */
42
43 goog.provide('goog.testing.i18n.asserts');
44 goog.setTestOnly('goog.testing.i18n.asserts');
45
46 goog.require('goog.testing.jsunit');
47
48
49 /**
50 * A map of known tests where locale data changed, but the old values are
51 * still tested for by various clients.
52 * @const {!Object<string, string>}
53 * @private
54 */
55 goog.testing.i18n.asserts.EXPECTED_VALUE_MAP_ = {
56 // Data to test the assert itself, old string as key, new string as value
57 };
58
59
60 /**
61 * Asserts that the two values are "almost equal" from i18n perspective
62 * (based on a manually maintained and validated whitelist).
63 * @param {string} expected The expected value.
64 * @param {string} actual The actual value.
65 */
66 goog.testing.i18n.asserts.assertI18nEquals = function(expected, actual) {
67 if (expected == actual) {
68 return;
69 }
70
71 var newExpected = goog.testing.i18n.asserts.EXPECTED_VALUE_MAP_[expected];
72 if (newExpected == actual) {
73 return;
74 }
75
76 assertEquals(expected, actual);
77 };