Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve handling of non-coercable objects in assertEqual #1385

Merged
merged 1 commit into from
Dec 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions js/testing/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,29 @@
limitations under the License.
*/

// TODO(ry) Use unknown here for parameters types.
// tslint:disable-next-line:no-any
export function assertEqual(actual: any, expected: any, msg?: string) {
if (!msg) {
msg = `actual: ${actual} expected: ${expected}`;
}
export function assertEqual(actual: unknown, expected: unknown, msg?: string) {
if (!equal(actual, expected)) {
let actualString: string;
let expectedString: string;
try {
actualString = String(actual);
} catch (e) {
actualString = "[Cannot display]";
}
try {
expectedString = String(expected);
kitsonk marked this conversation as resolved.
Show resolved Hide resolved
} catch (e) {
expectedString = "[Cannot display]";
}
console.error(
"assertEqual failed. actual =",
actual,
actualString,
"expected =",
expected
expectedString
);
if (!msg) {
msg = `actual: ${actualString} expected: ${expectedString}`;
}
throw new Error(msg);
}
}
Expand Down
34 changes: 33 additions & 1 deletion js/testing/util_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { test } from "./testing.ts";
import { assert } from "./util.ts";
import * as util from "./util.ts";

test(async function util_equal() {
test(function util_equal() {
assert(util.equal("world", "world"));
assert(!util.equal("hello", "world"));
assert(util.equal(5, 5));
Expand All @@ -38,3 +38,35 @@ test(async function util_equal() {
)
);
});

test(function util_assertEqual() {
const a = Object.create(null);
a.b = "foo";
util.assertEqual(a, a);
});

test(function util_assertEqualActualUncoercable() {
let didThrow = false;
const a = Object.create(null);
try {
util.assertEqual(a, "bar");
} catch (e) {
didThrow = true;
console.log(e.message);
assert(e.message === "actual: [Cannot display] expected: bar");
}
assert(didThrow);
});

test(function util_assertEqualExpectedUncoercable() {
let didThrow = false;
const a = Object.create(null);
try {
util.assertEqual("bar", a);
} catch (e) {
didThrow = true;
console.log(e.message);
assert(e.message === "actual: bar expected: [Cannot display]");
}
assert(didThrow);
});