Files
test-repo/tests/refinements/string.js
T
Christopher Chedeau cf45afba61 Add testing
- This brings in the flow test suite that contains a ton of JavaScript parsing edge cases
- This creates snapshot tests using the pretty printer for all of them
- If uncomment `RUN_AST_TESTS` line in `tests/run_specs.js`, it checks ast(pretty_print(x)) == ast(x). Right now, "178 failed, 197 passed, 375 of 377 total". So half of the tests are not passing, most of them are crashes and many of the rest are subtle issues.
2016-12-23 19:51:53 +00:00

134 lines
2.2 KiB
JavaScript

// @flow
type Mode = "a" | "b" | "c";
let tests = [
function(x: string) {
if (x === 'foo') {
(x: void); // error
}
(x: 'foo'); // error
},
function(x: string) {
if (x !== 'foo') {
(x: 'foo'); // error
}
(x: void); // error
},
function(x: 'bar'): 'foo' {
if (x === 'foo') {
return x; // unreachable, no error
}
return 'foo';
},
function(x: 'foo'): string {
if (x === 'bar') {
return x;
}
return x;
},
function(x: 'foo') {
if (x !== 'bar') {
(x: 'foo');
}
(x: 'foo');
},
function(x: 'foo'): string {
if (x === 'foo') {
return x;
}
return x;
},
function(x: 'foo' | 'bar') {
if (x === 'foo') {
(x: 'foo');
(x: void); // error
}
if (x === 'bar') {
(x: 'bar');
(x: void); // error
}
},
function(x: { foo: string }): 'foo' {
if (x.foo === 'foo') {
return x.foo;
}
return x.foo; // error
},
function(
x: { kind: 'foo', foo: string } | { kind: 'bar', bar: string }
): string {
if (x.kind === 'foo') {
return x.foo;
} else {
return x.bar;
}
},
function(str: string, obj: { foo: string }) {
if (str === obj.bar) { // ok, typos allowed in conditionals
}
},
function(str: string, obj: {[key: string]: string}) {
if (str === obj.bar) { // ok
}
},
function(str: string): Mode {
var ch = str[0];
if (ch !== "a" && ch !== "b" && ch !== "c") {
throw new Error("Wrong string passed");
}
return ch;
},
function(s: string): ?Mode {
if (s === "a") {
return s;
} else if (s === "d") {
return s; // error
}
},
function(mode: Mode) {
switch (mode) {
case "a":
(mode: "a");
break;
case "b":
case "c":
(mode: "b" | "c");
break;
}
},
function(x: string): "" {
if (x) {
return x; // error
} else {
return x; // no error, inferred to be ""
}
},
// Simple template literals are ok
function(x: string): 'foo' {
if (x === `foo`) {
return x;
}
if (`foo` === x) {
return x;
}
return 'foo';
},
];