asserts.js 2.8 KB

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