Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
weswigham committed Jul 5, 2017
1 parent a18fe4d commit 908a6ae
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 18 deletions.
4 changes: 2 additions & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,9 +266,9 @@ namespace ts {
getUnknownType: () => unknownType,
getLiteralType: (literal: string | number) => {
/* tslint:disable:no-null-keyword */
Debug.assert(literal !== undefined && literal !== null);
Debug.assert(literal !== undefined && literal !== null, "Argument to getLiteralType was null or undefined");
/* tslint:enable:no-null-keyword */
Debug.assert(typeof literal === "string" || typeof literal === "number");
Debug.assert(typeof literal === "string" || typeof literal === "number", "Argument to getLiteralType was not a string or number");
return getLiteralType(literal);
},
getFalseType: () => falseType,
Expand Down
28 changes: 12 additions & 16 deletions src/harness/unittests/checkerPublicRelationships.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,39 +87,35 @@ namespace ts {
});

it("can get string literal types", () => {
assert((checker.getLiteralType("foobar") as LiteralType).value === "foobar");
assert(checker.getLiteralType("foobar").value === "foobar");
});

it("can get numeber literal types", () => {
assert((checker.getLiteralType(42) as LiteralType).value === "42");
assert(checker.getLiteralType(42).value === 42);
});

it("doesn't choke on exceptional input to literal type getters", () => {
assert.equal((checker.getLiteralType("") as LiteralType).value, "");
assert.throws(() => checker.getLiteralType(/*content*/ undefined), Error, "Debug Failure. False expression:");
assert.equal(checker.getLiteralType("").value, "");
assert.throws(() => checker.getLiteralType(/*content*/ undefined), Error, "Argument to getLiteralType was null or undefined");
/* tslint:disable:no-null-keyword */
assert.throws(() => checker.getLiteralType(/*content*/ null), Error, "Debug Failure. False expression:");
assert.throws(() => checker.getLiteralType(/*content*/ null), Error, "Argument to getLiteralType was null or undefined");
/* tslint:enable:no-null-keyword */
let hugeStringLiteral = map(new Array(2 ** 16 - 1), () => "a").join();
assert.equal((checker.getLiteralType(hugeStringLiteral) as LiteralType).value, hugeStringLiteral);
assert.equal(checker.getLiteralType(hugeStringLiteral).value, hugeStringLiteral);
hugeStringLiteral = undefined;


assert.throws(() => checker.getLiteralType(/*content*/ undefined), Error, "Debug Failure. False expression:");
/* tslint:disable:no-null-keyword */
assert.throws(() => checker.getLiteralType(/*content*/ null), Error, "Debug Failure. False expression:");
/* tslint:enable:no-null-keyword */

const sanityChecks = ["000", "0b0", "0x0", "0.0", "0e-0", "-010", "-0b10", "-0x10", "-0o10", "-10.0", "-1e-1", "NaN", "Infinity", "-Infinity"];
forEach(sanityChecks, num => {
assert.equal((checker.getLiteralType(num) as LiteralType).value, num, `${num} did not match.`);
assert.equal(checker.getLiteralType(num).value, num, `${num} did not match.`);
});

const insanityChecks = [[0, "0"], [0b0, "0"], [-10, "-10"], [NaN, "NaN"], [Infinity, "Infinity"], [-Infinity, "-Infinity"]];
forEach(insanityChecks, ([num, expected]) => {
assert.equal((checker.getLiteralType(num as any) as LiteralType).value, expected, `${JSON.stringify(num)} should be ${expected}`);
const insanityChecks = [0, 0b0, -10, Infinity, -Infinity];
forEach(insanityChecks, (num) => {
assert.equal(checker.getLiteralType(num).value, num, `${JSON.stringify(num)} should be ${num}`);
});

assert.isNaN(checker.getLiteralType(NaN).value);

const instabilityChecks = [{ foo: 42 }, new Date(42), [42], new Number(42), new String("42")];
forEach(instabilityChecks, (bad) => {
assert.throws(() => checker.getLiteralType(bad as any));
Expand Down

0 comments on commit 908a6ae

Please sign in to comment.