From 871aee14161493110c35779b1c97bd199eb43e45 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Wed, 6 Jul 2016 21:01:00 -0700 Subject: [PATCH] Adding new tests --- .../types/literal/booleanLiteralTypes1.ts | 95 ++++++++++++ .../types/literal/booleanLiteralTypes2.ts | 97 ++++++++++++ .../types/literal/enumLiteralTypes1.ts | 113 ++++++++++++++ .../types/literal/enumLiteralTypes2.ts | 115 ++++++++++++++ .../types/literal/enumLiteralTypes3.ts | 119 +++++++++++++++ .../types/literal/literalTypes1.ts | 90 +++++++++++ .../types/literal/numericLiteralTypes1.ts | 139 +++++++++++++++++ .../types/literal/numericLiteralTypes2.ts | 141 ++++++++++++++++++ .../types/literal/numericLiteralTypes3.ts | 100 +++++++++++++ 9 files changed, 1009 insertions(+) create mode 100644 tests/cases/conformance/types/literal/booleanLiteralTypes1.ts create mode 100644 tests/cases/conformance/types/literal/booleanLiteralTypes2.ts create mode 100644 tests/cases/conformance/types/literal/enumLiteralTypes1.ts create mode 100644 tests/cases/conformance/types/literal/enumLiteralTypes2.ts create mode 100644 tests/cases/conformance/types/literal/enumLiteralTypes3.ts create mode 100644 tests/cases/conformance/types/literal/literalTypes1.ts create mode 100644 tests/cases/conformance/types/literal/numericLiteralTypes1.ts create mode 100644 tests/cases/conformance/types/literal/numericLiteralTypes2.ts create mode 100644 tests/cases/conformance/types/literal/numericLiteralTypes3.ts diff --git a/tests/cases/conformance/types/literal/booleanLiteralTypes1.ts b/tests/cases/conformance/types/literal/booleanLiteralTypes1.ts new file mode 100644 index 00000000000..e8ccbde080b --- /dev/null +++ b/tests/cases/conformance/types/literal/booleanLiteralTypes1.ts @@ -0,0 +1,95 @@ +type A1 = true | false; +type A2 = false | true; + +function f1() { + var a: A1; + var a: A2; + var a: true | false; + var a: false | true; +} + +function f2(a: true | false, b: boolean) { + a = b; + b = a; +} + +function f3(a: true | false, b: true | false) { + var x = a || b; + var x = a && b; + var x = !a; +} + +function f4(t: true, f: false) { + var x1 = t && f; + var x2 = f && t; + var x3 = t || f; + var x4 = f || t; + var x5 = !t; + var x6 = !f; +} + +declare function g(x: true): string; +declare function g(x: false): boolean; +declare function g(x: boolean): number; + +function f5(b: boolean) { + var z1 = g(true); + var z2 = g(false); + var z3 = g(b); +} + +function assertNever(x: never): never { + throw new Error("Unexpected value"); +} + +function f10(x: true | false) { + switch (x) { + case true: return "true"; + case false: return "false"; + } +} + +function f11(x: true | false) { + switch (x) { + case true: return "true"; + case false: return "false"; + } + return assertNever(x); +} + +function f12(x: true | false) { + if (x) { + x; + } + else { + x; + } +} + +function f13(x: true | false) { + if (x === true) { + x; + } + else { + x; + } +} + +type Item = + { kind: true, a: string } | + { kind: false, b: string }; + +function f20(x: Item) { + switch (x.kind) { + case true: return x.a; + case false: return x.b; + } +} + +function f21(x: Item) { + switch (x.kind) { + case true: return x.a; + case false: return x.b; + } + return assertNever(x); +} \ No newline at end of file diff --git a/tests/cases/conformance/types/literal/booleanLiteralTypes2.ts b/tests/cases/conformance/types/literal/booleanLiteralTypes2.ts new file mode 100644 index 00000000000..e3ba292abdb --- /dev/null +++ b/tests/cases/conformance/types/literal/booleanLiteralTypes2.ts @@ -0,0 +1,97 @@ +// @strictNullChecks: true + +type A1 = true | false; +type A2 = false | true; + +function f1() { + var a: A1; + var a: A2; + var a: true | false; + var a: false | true; +} + +function f2(a: true | false, b: boolean) { + a = b; + b = a; +} + +function f3(a: true | false, b: true | false) { + var x = a || b; + var x = a && b; + var x = !a; +} + +function f4(t: true, f: false) { + var x1 = t && f; + var x2 = f && t; + var x3 = t || f; + var x4 = f || t; + var x5 = !t; + var x6 = !f; +} + +declare function g(x: true): string; +declare function g(x: false): boolean; +declare function g(x: boolean): number; + +function f5(b: boolean) { + var z1 = g(true); + var z2 = g(false); + var z3 = g(b); +} + +function assertNever(x: never): never { + throw new Error("Unexpected value"); +} + +function f10(x: true | false) { + switch (x) { + case true: return "true"; + case false: return "false"; + } +} + +function f11(x: true | false) { + switch (x) { + case true: return "true"; + case false: return "false"; + } + return assertNever(x); +} + +function f12(x: true | false) { + if (x) { + x; + } + else { + x; + } +} + +function f13(x: true | false) { + if (x === true) { + x; + } + else { + x; + } +} + +type Item = + { kind: true, a: string } | + { kind: false, b: string }; + +function f20(x: Item) { + switch (x.kind) { + case true: return x.a; + case false: return x.b; + } +} + +function f21(x: Item) { + switch (x.kind) { + case true: return x.a; + case false: return x.b; + } + return assertNever(x); +} \ No newline at end of file diff --git a/tests/cases/conformance/types/literal/enumLiteralTypes1.ts b/tests/cases/conformance/types/literal/enumLiteralTypes1.ts new file mode 100644 index 00000000000..9794b5082b8 --- /dev/null +++ b/tests/cases/conformance/types/literal/enumLiteralTypes1.ts @@ -0,0 +1,113 @@ +const enum Choice { Unknown, Yes, No }; + +type YesNo = Choice.Yes | Choice.No; +type NoYes = Choice.No | Choice.Yes; +type UnknownYesNo = Choice.Unknown | Choice.Yes | Choice.No; + +function f1() { + var a: YesNo; + var a: NoYes; + var a: Choice.Yes | Choice.No; + var a: Choice.No | Choice.Yes; +} + +function f2(a: YesNo, b: UnknownYesNo, c: Choice) { + b = a; + c = a; + c = b; +} + +function f3(a: Choice.Yes, b: YesNo) { + var x = a + b; + var x = a - b; + var x = a * b; + var x = a / b; + var x = a % b; + var x = a | b; + var x = a & b; + var x = a ^ b; + var x = -b; + var x = ~b; + var y = a == b; + var y = a != b; + var y = a === b; + var y = a !== b; + var y = a > b; + var y = a < b; + var y = a >= b; + var y = a <= b; + var y = !b; +} + +function f4(a: Choice.Yes, b: YesNo) { + a++; + b++; +} + +declare function g(x: Choice.Yes): string; +declare function g(x: Choice.No): boolean; +declare function g(x: Choice): number; + +function f5(a: YesNo, b: UnknownYesNo, c: Choice) { + var z1 = g(Choice.Yes); + var z2 = g(Choice.No); + var z3 = g(a); + var z4 = g(b); + var z5 = g(c); +} + +function assertNever(x: never): never { + throw new Error("Unexpected value"); +} + +function f10(x: YesNo) { + switch (x) { + case Choice.Yes: return "true"; + case Choice.No: return "false"; + } +} + +function f11(x: YesNo) { + switch (x) { + case Choice.Yes: return "true"; + case Choice.No: return "false"; + } + return assertNever(x); +} + +function f12(x: UnknownYesNo) { + if (x) { + x; + } + else { + x; + } +} + +function f13(x: UnknownYesNo) { + if (x === Choice.Yes) { + x; + } + else { + x; + } +} + +type Item = + { kind: Choice.Yes, a: string } | + { kind: Choice.No, b: string }; + +function f20(x: Item) { + switch (x.kind) { + case Choice.Yes: return x.a; + case Choice.No: return x.b; + } +} + +function f21(x: Item) { + switch (x.kind) { + case Choice.Yes: return x.a; + case Choice.No: return x.b; + } + return assertNever(x); +} \ No newline at end of file diff --git a/tests/cases/conformance/types/literal/enumLiteralTypes2.ts b/tests/cases/conformance/types/literal/enumLiteralTypes2.ts new file mode 100644 index 00000000000..26dd11760cb --- /dev/null +++ b/tests/cases/conformance/types/literal/enumLiteralTypes2.ts @@ -0,0 +1,115 @@ +// @strictNullChecks: true + +const enum Choice { Unknown, Yes, No }; + +type YesNo = Choice.Yes | Choice.No; +type NoYes = Choice.No | Choice.Yes; +type UnknownYesNo = Choice.Unknown | Choice.Yes | Choice.No; + +function f1() { + var a: YesNo; + var a: NoYes; + var a: Choice.Yes | Choice.No; + var a: Choice.No | Choice.Yes; +} + +function f2(a: YesNo, b: UnknownYesNo, c: Choice) { + b = a; + c = a; + c = b; +} + +function f3(a: Choice.Yes, b: UnknownYesNo) { + var x = a + b; + var x = a - b; + var x = a * b; + var x = a / b; + var x = a % b; + var x = a | b; + var x = a & b; + var x = a ^ b; + var x = -b; + var x = ~b; + var y = a == b; + var y = a != b; + var y = a === b; + var y = a !== b; + var y = a > b; + var y = a < b; + var y = a >= b; + var y = a <= b; + var y = !b; +} + +function f4(a: Choice.Yes, b: UnknownYesNo) { + a++; + b++; +} + +declare function g(x: Choice.Yes): string; +declare function g(x: Choice.No): boolean; +declare function g(x: Choice): number; + +function f5(a: YesNo, b: UnknownYesNo, c: Choice) { + var z1 = g(Choice.Yes); + var z2 = g(Choice.No); + var z3 = g(a); + var z4 = g(b); + var z5 = g(c); +} + +function assertNever(x: never): never { + throw new Error("Unexpected value"); +} + +function f10(x: YesNo) { + switch (x) { + case Choice.Yes: return "true"; + case Choice.No: return "false"; + } +} + +function f11(x: YesNo) { + switch (x) { + case Choice.Yes: return "true"; + case Choice.No: return "false"; + } + return assertNever(x); +} + +function f12(x: UnknownYesNo) { + if (x) { + x; + } + else { + x; + } +} + +function f13(x: UnknownYesNo) { + if (x === Choice.Yes) { + x; + } + else { + x; + } +} + +type Item = + { kind: Choice.Yes, a: string } | + { kind: Choice.No, b: string }; + +function f20(x: Item) { + switch (x.kind) { + case Choice.Yes: return x.a; + case Choice.No: return x.b; + } +} + +function f21(x: Item) { + switch (x.kind) { + case Choice.Yes: return x.a; + case Choice.No: return x.b; + } + return assertNever(x); +} \ No newline at end of file diff --git a/tests/cases/conformance/types/literal/enumLiteralTypes3.ts b/tests/cases/conformance/types/literal/enumLiteralTypes3.ts new file mode 100644 index 00000000000..11e6b7d3c92 --- /dev/null +++ b/tests/cases/conformance/types/literal/enumLiteralTypes3.ts @@ -0,0 +1,119 @@ +const enum Choice { Unknown, Yes, No }; + +type Yes = Choice.Yes; +type YesNo = Choice.Yes | Choice.No; +type NoYes = Choice.No | Choice.Yes; +type UnknownYesNo = Choice.Unknown | Choice.Yes | Choice.No; + +function f1(a: Yes, b: YesNo, c: UnknownYesNo, d: Choice) { + a = a; + a = b; + a = c; + a = d; +} + +function f2(a: Yes, b: YesNo, c: UnknownYesNo, d: Choice) { + b = a; + b = b; + b = c; + b = d; +} + +function f3(a: Yes, b: YesNo, c: UnknownYesNo, d: Choice) { + c = a; + c = b; + c = c; + c = d; +} + +function f4(a: Yes, b: YesNo, c: UnknownYesNo, d: Choice) { + d = a; + d = b; + d = c; + d = d; +} + +function f5(a: Yes, b: YesNo, c: UnknownYesNo, d: Choice) { + a = Choice.Unknown; + a = Choice.Yes; + a = Choice.No; + b = Choice.Unknown; + b = Choice.Yes; + b = Choice.No; + c = Choice.Unknown; + c = Choice.Yes; + c = Choice.No; + d = Choice.Unknown; + d = Choice.Yes; + d = Choice.No; +} + +function f6(a: Yes, b: YesNo, c: UnknownYesNo, d: Choice) { + a === Choice.Unknown; + a === Choice.Yes; + a === Choice.No; + b === Choice.Unknown; + b === Choice.Yes; + b === Choice.No; + c === Choice.Unknown; + c === Choice.Yes; + c === Choice.No; + d === Choice.Unknown; + d === Choice.Yes; + d === Choice.No; +} + +function f7(a: Yes, b: YesNo, c: UnknownYesNo, d: Choice) { + a === a; + a === b; + a === c; + a === d; + b === a; + b === b; + b === c; + b === d; + c === a; + c === b; + c === c; + c === d; + d === a; + d === b; + d === c; + d === d; +} + +function f10(x: Yes): Yes { + switch (x) { + case Choice.Unknown: return x; + case Choice.Yes: return x; + case Choice.No: return x; + } + return x; +} + +function f11(x: YesNo): YesNo { + switch (x) { + case Choice.Unknown: return x; + case Choice.Yes: return x; + case Choice.No: return x; + } + return x; +} + +function f12(x: UnknownYesNo): UnknownYesNo { + switch (x) { + case Choice.Unknown: return x; + case Choice.Yes: return x; + case Choice.No: return x; + } + return x; +} + +function f13(x: Choice): Choice { + switch (x) { + case Choice.Unknown: return x; + case Choice.Yes: return x; + case Choice.No: return x; + } + return x; +} \ No newline at end of file diff --git a/tests/cases/conformance/types/literal/literalTypes1.ts b/tests/cases/conformance/types/literal/literalTypes1.ts new file mode 100644 index 00000000000..b793bba2d34 --- /dev/null +++ b/tests/cases/conformance/types/literal/literalTypes1.ts @@ -0,0 +1,90 @@ +// @strictNullChecks: true + +let zero: 0 = 0; +let one: 1 = 1; +let two: 2 = 2; +let oneOrTwo: 1 | 2 = <1 | 2>1; + +function f1(x: 0 | 1 | 2) { + switch (x) { + case zero: + x; + break; + case one: + x; + break; + case two: + x; + break; + default: + x; + } +} + +function f2(x: 0 | 1 | 2) { + switch (x) { + case zero: + x; + break; + case oneOrTwo: + x; + break; + default: + x; + } +} + +type Falsy = false | 0 | "" | null | undefined; + +function f3(x: Falsy) { + if (x) { + x; + } + else { + x; + } +} + +function f4(x: 0 | 1 | true | string) { + switch (x) { + case 0: + x; + break; + case 1: + x; + break; + case "abc": + case "def": + x; + break; + case null: + x; + break; + case undefined: + x; + break; + default: + x; + } +} + +function f5(x: string | number | boolean) { + switch (x) { + case "abc": + x; + break; + case 0: + case 1: + x; + break; + case true: + x; + break; + case "hello": + case 123: + x; + break; + default: + x; + } +} \ No newline at end of file diff --git a/tests/cases/conformance/types/literal/numericLiteralTypes1.ts b/tests/cases/conformance/types/literal/numericLiteralTypes1.ts new file mode 100644 index 00000000000..76d8265a841 --- /dev/null +++ b/tests/cases/conformance/types/literal/numericLiteralTypes1.ts @@ -0,0 +1,139 @@ +type A1 = 1; +type A2 = 1.0; +type A3 = 1e0; +type A4 = 10e-1; +type A5 = 1 | 1.0 | 1e0 | 10e-1; + +function f1() { + var a: A1 = 1; + var a: A2 = 1; + var a: A3 = 1; + var a: A4 = 1; + var a: A5 = 1; +} + +type B1 = -1 | 0 | 1; +type B2 = 1 | 0 | -1; +type B3 = 0 | -1 | 1; + +function f2() { + var b: B1 = -1; + var b: B2 = 0; + var b: B3 = 1; +} + +function f3(a: 1, b: 0 | 1 | 2) { + var x = a + b; + var x = a - b; + var x = a * b; + var x = a / b; + var x = a % b; + var x = a | b; + var x = a & b; + var x = a ^ b; + var x = -b; + var x = ~b; + var y = a == b; + var y = a != b; + var y = a === b; + var y = a !== b; + var y = a > b; + var y = a < b; + var y = a >= b; + var y = a <= b; + var y = !b; +} + +function f4(a: 1, b: 0 | 1 | 2) { + a++; + b++; +} + +declare function g(x: 0): string; +declare function g(x: 1): boolean; +declare function g(x: number): number; + +function f5(a: 1, b: 0 | 1 | 2) { + var z1 = g(0); + var z2 = g(1); + var z3 = g(2); + var z4 = g(a); + var z5 = g(b); +} + +function assertNever(x: never): never { + throw new Error("Unexpected value"); +} + +type Tag = 0 | 1 | 2; + +function f10(x: Tag) { + switch (x) { + case 0: return "a"; + case 1: return "b"; + case 2: return "c"; + } +} + +function f11(x: Tag) { + switch (x) { + case 0: return "a"; + case 1: return "b"; + case 2: return "c"; + } + return assertNever(x); +} + +function f12(x: Tag) { + if (x) { + x; + } + else { + x; + } +} + +function f13(x: Tag) { + if (x === 0 || x === 2) { + x; + } + else { + x; + } +} + +function f14(x: 0 | 1 | 2, y: string) { + var a = x && y; + var b = x || y; +} + +function f15(x: 0 | false, y: 1 | "one") { + var a = x && y; + var b = y && x; + var c = x || y; + var d = y || x; + var e = !x; + var f = !y; +} + +type Item = + { kind: 0, a: string } | + { kind: 1, b: string } | + { kind: 2, c: string }; + +function f20(x: Item) { + switch (x.kind) { + case 0: return x.a; + case 1: return x.b; + case 2: return x.c; + } +} + +function f21(x: Item) { + switch (x.kind) { + case 0: return x.a; + case 1: return x.b; + case 2: return x.c; + } + return assertNever(x); +} \ No newline at end of file diff --git a/tests/cases/conformance/types/literal/numericLiteralTypes2.ts b/tests/cases/conformance/types/literal/numericLiteralTypes2.ts new file mode 100644 index 00000000000..3ab0fd5631f --- /dev/null +++ b/tests/cases/conformance/types/literal/numericLiteralTypes2.ts @@ -0,0 +1,141 @@ +// @strictNullChecks: true + +type A1 = 1; +type A2 = 1.0; +type A3 = 1e0; +type A4 = 10e-1; +type A5 = 1 | 1.0 | 1e0 | 10e-1; + +function f1() { + var a: A1 = 1; + var a: A2 = 1; + var a: A3 = 1; + var a: A4 = 1; + var a: A5 = 1; +} + +type B1 = -1 | 0 | 1; +type B2 = 1 | 0 | -1; +type B3 = 0 | -1 | 1; + +function f2() { + var b: B1 = -1; + var b: B2 = 0; + var b: B3 = 1; +} + +function f3(a: 1, b: 0 | 1 | 2) { + var x = a + b; + var x = a - b; + var x = a * b; + var x = a / b; + var x = a % b; + var x = a | b; + var x = a & b; + var x = a ^ b; + var x = -b; + var x = ~b; + var y = a == b; + var y = a != b; + var y = a === b; + var y = a !== b; + var y = a > b; + var y = a < b; + var y = a >= b; + var y = a <= b; + var y = !b; +} + +function f4(a: 1, b: 0 | 1 | 2) { + a++; + b++; +} + +declare function g(x: 0): string; +declare function g(x: 1): boolean; +declare function g(x: number): number; + +function f5(a: 1, b: 0 | 1 | 2) { + var z1 = g(0); + var z2 = g(1); + var z3 = g(2); + var z4 = g(a); + var z5 = g(b); +} + +function assertNever(x: never): never { + throw new Error("Unexpected value"); +} + +type Tag = 0 | 1 | 2; + +function f10(x: Tag) { + switch (x) { + case 0: return "a"; + case 1: return "b"; + case 2: return "c"; + } +} + +function f11(x: Tag) { + switch (x) { + case 0: return "a"; + case 1: return "b"; + case 2: return "c"; + } + return assertNever(x); +} + +function f12(x: Tag) { + if (x) { + x; + } + else { + x; + } +} + +function f13(x: Tag) { + if (x === 0 || x === 2) { + x; + } + else { + x; + } +} + +function f14(x: 0 | 1 | 2, y: string) { + var a = x && y; + var b = x || y; +} + +function f15(x: 0 | false, y: 1 | "one") { + var a = x && y; + var b = y && x; + var c = x || y; + var d = y || x; + var e = !x; + var f = !y; +} + +type Item = + { kind: 0, a: string } | + { kind: 1, b: string } | + { kind: 2, c: string }; + +function f20(x: Item) { + switch (x.kind) { + case 0: return x.a; + case 1: return x.b; + case 2: return x.c; + } +} + +function f21(x: Item) { + switch (x.kind) { + case 0: return x.a; + case 1: return x.b; + case 2: return x.c; + } + return assertNever(x); +} \ No newline at end of file diff --git a/tests/cases/conformance/types/literal/numericLiteralTypes3.ts b/tests/cases/conformance/types/literal/numericLiteralTypes3.ts new file mode 100644 index 00000000000..5bdbbc0d20e --- /dev/null +++ b/tests/cases/conformance/types/literal/numericLiteralTypes3.ts @@ -0,0 +1,100 @@ +type A = 1; +type B = 2 | 3; +type C = 1 | 2 | 3; +type D = 0 | 1 | 2; + +function f1(a: A, b: B, c: C, d: D) { + a = a; + a = b; + a = c; + a = d; +} + +function f2(a: A, b: B, c: C, d: D) { + b = a; + b = b; + b = c; + b = d; +} + +function f3(a: A, b: B, c: C, d: D) { + c = a; + c = b; + c = c; + c = d; +} + +function f4(a: A, b: B, c: C, d: D) { + d = a; + d = b; + d = c; + d = d; +} + +function f5(a: A, b: B, c: C, d: D) { + a = 0; + a = 1; + a = 2; + a = 3; + b = 0; + b = 1; + b = 2; + b = 3; + c = 0; + c = 1; + c = 2; + c = 3; + d = 0; + d = 1; + d = 2; + d = 3; +} + +function f6(a: A, b: B, c: C, d: D) { + a === 0; + a === 1; + a === 2; + a === 3; + b === 0; + b === 1; + b === 2; + b === 3; + c === 0; + c === 1; + c === 2; + c === 3; + d === 0; + d === 1; + d === 2; + d === 3; +} + +function f7(a: A, b: B, c: C, d: D) { + a === a; + a === b; + a === c; + a === d; + b === a; + b === b; + b === c; + b === d; + c === a; + c === b; + c === c; + c === d; + d === a; + d === b; + d === c; + d === d; +} + +function f8(x: 0 | 2 | 4) { + switch (x) { + case 0: return; + case 1: return; + case 2: return; + case 3: return; + case 4: return; + case 5: return; + } +} \ No newline at end of file