Skip to content

Commit

Permalink
Handle arrays with properties in assert.deepEqual (#20674)
Browse files Browse the repository at this point in the history
* Handle arrays with properties in assert.deepEqual

* Fix tests
  • Loading branch information
Andy authored Dec 13, 2017
1 parent 6fbeced commit 84eb25c
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 6 deletions.
1 change: 1 addition & 0 deletions Jakefile.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ var harnessSources = harnessCoreSources.concat([
"textStorage.ts",
"moduleResolution.ts",
"tsconfigParsing.ts",
"asserts.ts",
"builder.ts",
"commandLineParsing.ts",
"configurationExtension.ts",
Expand Down
27 changes: 24 additions & 3 deletions src/harness/harness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,30 @@
// this will work in the browser via browserify
var _chai: typeof chai = require("chai");
var assert: typeof _chai.assert = _chai.assert;
// chai's builtin `assert.isFalse` is featureful but slow - we don't use those features,
// so we'll just overwrite it as an alterative to migrating a bunch of code off of chai
assert.isFalse = (expr, msg) => { if (expr as any as boolean !== false) throw new Error(msg); };
{
// chai's builtin `assert.isFalse` is featureful but slow - we don't use those features,
// so we'll just overwrite it as an alterative to migrating a bunch of code off of chai
assert.isFalse = (expr, msg) => { if (expr as any as boolean !== false) throw new Error(msg); };

const assertDeepImpl = assert.deepEqual;
assert.deepEqual = (a, b, msg) => {
if (ts.isArray(a) && ts.isArray(b)) {
assertDeepImpl(arrayExtraKeysObject(a), arrayExtraKeysObject(b), "Array extra keys differ");
}
assertDeepImpl(a, b, msg);

function arrayExtraKeysObject(a: ReadonlyArray<{} | null | undefined>): object {
const obj: { [key: string]: {} | null | undefined } = {};
for (const key in a) {
if (Number.isNaN(Number(key))) {
obj[key] = a[key];
}
}
return obj;
}
};
}

declare var __dirname: string; // Node-specific
var global: NodeJS.Global = <any>Function("return this").call(undefined);

Expand Down
1 change: 1 addition & 0 deletions src/harness/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@
"./unittests/reuseProgramStructure.ts",
"./unittests/moduleResolution.ts",
"./unittests/tsconfigParsing.ts",
"./unittests/asserts.ts",
"./unittests/builder.ts",
"./unittests/commandLineParsing.ts",
"./unittests/configurationExtension.ts",
Expand Down
11 changes: 11 additions & 0 deletions src/harness/unittests/asserts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/// <reference path="..\harness.ts" />

namespace ts {
describe("assert", () => {
it("deepEqual", () => {
assert.throws(() => assert.deepEqual(createNodeArray([createIdentifier("A")]), createNodeArray([createIdentifier("B")])));
assert.throws(() => assert.deepEqual(createNodeArray([], /*hasTrailingComma*/ true), createNodeArray([], /*hasTrailingComma*/ false)));
assert.deepEqual(createNodeArray([createIdentifier("A")], /*hasTrailingComma*/ true), createNodeArray([createIdentifier("A")], /*hasTrailingComma*/ true));
});
});
}
6 changes: 3 additions & 3 deletions src/harness/unittests/reuseProgramStructure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ namespace ts {

const newTexts: NamedSourceText[] = files.concat([{ name: "non-existing-file.ts", text: SourceText.New("", "", `var x = 1`) }]);
const program2 = updateProgram(program1, ["a.ts"], options, noop, newTexts);
assert.deepEqual(emptyArray, program2.getMissingFilePaths());
assert.lengthOf(program2.getMissingFilePaths(), 0);

assert.equal(StructureIsReused.Not, program1.structureIsReused);
});
Expand Down Expand Up @@ -839,12 +839,12 @@ namespace ts {
updateProgramText(files, root, "const x = 1;");
});
assert.equal(program1.structureIsReused, StructureIsReused.Completely);
assert.deepEqual(program2.getSemanticDiagnostics(), emptyArray);
assert.lengthOf(program2.getSemanticDiagnostics(), 0);
});

it("Target changes -> redirect broken", () => {
const program1 = createRedirectProgram();
assert.deepEqual(program1.getSemanticDiagnostics(), emptyArray);
assert.lengthOf(program1.getSemanticDiagnostics(), 0);

const program2 = updateRedirectProgram(program1, files => {
updateProgramText(files, axIndex, "export default class X { private x: number; private y: number; }");
Expand Down

0 comments on commit 84eb25c

Please sign in to comment.