Skip to content
This repository has been archived by the owner on Aug 1, 2024. It is now read-only.

Commit

Permalink
Update I18N test with assert that works better with null/empty actual…
Browse files Browse the repository at this point in the history
… values. ignores white space differences.

RELNOTES: Update I18N test with assert that works better with null/empty actual values.

PiperOrigin-RevId: 494955128
Change-Id: I87fc665aeb54b857cbbcea6dfbd695d7e0acbcdd
  • Loading branch information
Closure Team authored and copybara-github committed Dec 13, 2022
1 parent 5fa11c8 commit 11ed104
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 30 deletions.
8 changes: 5 additions & 3 deletions closure/goog/testing/i18n/asserts.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ goog.testing.i18n.asserts.assertI18nEquals = function(a, b, opt_c) {
let actual;
let msg; // The comment to be added, if any
// If there are 3 arguments, the first is a comment.
if (opt_c) {
if (arguments.length === 3) {
msg = a;
expected = b;
actual = opt_c;
Expand All @@ -87,8 +87,10 @@ goog.testing.i18n.asserts.assertI18nEquals = function(a, b, opt_c) {

// Compare with all horizontal white space characters removed, making
// this less brittle.
let wsFixedActual = actual.replace(
goog.testing.i18n.asserts.HORIZONTAL_WHITE_SPACE_REGEX, '');
let wsFixedActual = actual ?
actual.replace(
goog.testing.i18n.asserts.HORIZONTAL_WHITE_SPACE_REGEX, '') :
actual;

// Now, check if the expected string and the actual result differ only
// in whitespace by stripping white space characters from each.
Expand Down
77 changes: 50 additions & 27 deletions closure/goog/testing/i18n/asserts_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,43 +11,72 @@
goog.module('goog.testing.i18n.assertsTest');
goog.setTestOnly();

const ExpectedFailures = goog.require('goog.testing.ExpectedFailures');
const asserts = goog.require('goog.testing.i18n.asserts');
const testSuite = goog.require('goog.testing.testSuite');

// Add this mapping for testing only
asserts.addI18nMapping('mappedValue', 'newValue');
asserts.addI18nMapping('X\u0020Y', 'AB');

let expectedFailures;

testSuite({
setUpPage() {
expectedFailures = new ExpectedFailures();
},

tearDown() {
expectedFailures.handleTearDown();
},

/** @suppress {checkTypes} suppression added to enable type checking */
testEdgeCases() {
// Pass
asserts.assertI18nEquals(null, null);
asserts.assertI18nEquals('', '');

// Fail
expectedFailures.expectFailureFor(true);
try {
assertThrowsJsUnitException(() => {
asserts.assertI18nEquals(null, '');
});
assertThrowsJsUnitException(() => {
asserts.assertI18nEquals(null, 'test');
});
assertThrowsJsUnitException(() => {
asserts.assertI18nEquals('', null);
});
assertThrowsJsUnitException(() => {
asserts.assertI18nEquals('', 'test');
});
assertThrowsJsUnitException(() => {
asserts.assertI18nEquals('test', null);
});
assertThrowsJsUnitException(() => {
asserts.assertI18nEquals('test', '');
} catch (e) {
expectedFailures.handleException(e);
}
});
},

/** @suppress {checkTypes} suppression added to enable type checking */
testEdgeCases_withComments() {
// Pass
asserts.assertI18nEquals('Expect null values to match.', null, null);
asserts.assertI18nEquals('Expect empty values to match.', '', '');

// Fail
assertThrowsJsUnitException(() => {
asserts.assertI18nEquals(
'Expect null and empty values to not match.', null, '');
});
assertThrowsJsUnitException(() => {
asserts.assertI18nEquals(
'Expect null and non-empty values to not match.', null, 'test');
});
assertThrowsJsUnitException(() => {
asserts.assertI18nEquals(
'Expect empty and null values to not match.', '', null);
});
assertThrowsJsUnitException(() => {
asserts.assertI18nEquals(
'Expect empty and non-empty values to not match.', '', 'test');
});
assertThrowsJsUnitException(() => {
asserts.assertI18nEquals(
'Expect non-empty and null values to not match.', 'test', null);
});
assertThrowsJsUnitException(() => {
asserts.assertI18nEquals(
'Expect non-empty and empty values to not match.', 'test', '');
});
},

testContains() {
Expand All @@ -57,12 +86,9 @@ testSuite({
asserts.assertI18nContains('mappedValue', '** newValue');

// Negative testing
expectedFailures.expectFailureFor(true);
try {
asserts.assertI18nContains('mappedValue', '** dummy');
} catch (e) {
expectedFailures.handleException(e);
}
assertThrowsJsUnitException(() => {
asserts.assertI18nEquals('mappedValue', '** dummy');
});
},

testMappingWorks() {
Expand All @@ -72,12 +98,9 @@ testSuite({
asserts.assertI18nEquals('mappedValue', 'newValue');

// Negative testing
expectedFailures.expectFailureFor(true);
try {
assertThrowsJsUnitException(() => {
asserts.assertI18nEquals('unmappedValue', 'newValue');
} catch (e) {
expectedFailures.handleException(e);
}
});
},

testWhiteSpaceStringWorks() {
Expand Down

0 comments on commit 11ed104

Please sign in to comment.