Skip to content

Commit c892458

Browse files
author
Andy Hanson
committed
Handle arrays with properties in assert.deepEqual
1 parent 79a1240 commit c892458

File tree

4 files changed

+37
-3
lines changed

4 files changed

+37
-3
lines changed

Jakefile.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ var harnessSources = harnessCoreSources.concat([
130130
"textStorage.ts",
131131
"moduleResolution.ts",
132132
"tsconfigParsing.ts",
133+
"asserts.ts",
133134
"builder.ts",
134135
"commandLineParsing.ts",
135136
"configurationExtension.ts",

src/harness/harness.ts

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,30 @@
3232
// this will work in the browser via browserify
3333
var _chai: typeof chai = require("chai");
3434
var assert: typeof _chai.assert = _chai.assert;
35-
// chai's builtin `assert.isFalse` is featureful but slow - we don't use those features,
36-
// so we'll just overwrite it as an alterative to migrating a bunch of code off of chai
37-
assert.isFalse = (expr, msg) => { if (expr as any as boolean !== false) throw new Error(msg); };
35+
{
36+
// chai's builtin `assert.isFalse` is featureful but slow - we don't use those features,
37+
// so we'll just overwrite it as an alterative to migrating a bunch of code off of chai
38+
assert.isFalse = (expr, msg) => { if (expr as any as boolean !== false) throw new Error(msg); };
39+
40+
const assertDeepImpl = assert.deepEqual;
41+
assert.deepEqual = (a, b, msg) => {
42+
if (ts.isArray(a) && ts.isArray(b)) {
43+
assertDeepImpl(arrayExtraKeysObject(a), arrayExtraKeysObject(b), "Array extra keys differ");
44+
}
45+
assertDeepImpl(a, b, msg);
46+
47+
function arrayExtraKeysObject(a: ReadonlyArray<{} | null | undefined>): object {
48+
const obj: { [key: string]: {} | null | undefined } = {};
49+
for (const key in a) {
50+
if (Number.isNaN(Number(key))) {
51+
obj[key] = a[key];
52+
}
53+
}
54+
return obj;
55+
}
56+
};
57+
}
58+
3859
declare var __dirname: string; // Node-specific
3960
var global: NodeJS.Global = <any>Function("return this").call(undefined);
4061

src/harness/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@
108108
"./unittests/reuseProgramStructure.ts",
109109
"./unittests/moduleResolution.ts",
110110
"./unittests/tsconfigParsing.ts",
111+
"./unittests/asserts.ts",
111112
"./unittests/builder.ts",
112113
"./unittests/commandLineParsing.ts",
113114
"./unittests/configurationExtension.ts",

src/harness/unittests/asserts.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/// <reference path="..\harness.ts" />
2+
3+
namespace ts {
4+
describe("assert", () => {
5+
it("deepEqual", () => {
6+
assert.throws(() => assert.deepEqual(createNodeArray([createIdentifier("A")]), createNodeArray([createIdentifier("B")])));
7+
assert.throws(() => assert.deepEqual(createNodeArray([], /*hasTrailingComma*/ true), createNodeArray([], /*hasTrailingComma*/ false)));
8+
assert.deepEqual(createNodeArray([createIdentifier("A")], /*hasTrailingComma*/ true), createNodeArray([createIdentifier("A")], /*hasTrailingComma*/ true));
9+
});
10+
});
11+
}

0 commit comments

Comments
 (0)