mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 09:37:34 +00:00
LibJS: Convert all remaining non-Array tests to the new system :)
This commit is contained in:
parent
918f4affd5
commit
15de2eda2b
72 changed files with 2394 additions and 1998 deletions
|
@ -1,11 +1,10 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
test("basic functionality", () => {
|
||||
var j = 0;
|
||||
for (var i = 0; i < 9; ++i) {
|
||||
if (i == 3) continue;
|
||||
if (i == 3) {
|
||||
continue;
|
||||
}
|
||||
++j;
|
||||
}
|
||||
assert(j == 8);
|
||||
console.log("PASS");
|
||||
} catch {}
|
||||
expect(j).toBe(8);
|
||||
});
|
||||
|
|
|
@ -1,18 +1,14 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
var number = 0;
|
||||
test("basic functionality", () => {
|
||||
let number = 0;
|
||||
do {
|
||||
number++;
|
||||
} while (number < 9);
|
||||
assert(number === 9);
|
||||
expect(number).toBe(9);
|
||||
});
|
||||
|
||||
number = 0;
|
||||
test("no braces", () => {
|
||||
let number = 0;
|
||||
do number++;
|
||||
while (number < 3);
|
||||
assert(number === 3);
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
||||
expect(number).toBe(3);
|
||||
});
|
||||
|
|
|
@ -1,24 +1,15 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
var a = [];
|
||||
for (var i = 0; i < 3; ++i) {
|
||||
test("basic functionality", () => {
|
||||
let a = [];
|
||||
for (let i = 0; i < 3; ++i) {
|
||||
a.push(i);
|
||||
}
|
||||
assert(a.length === 3);
|
||||
assert(a[0] === 0);
|
||||
assert(a[1] === 1);
|
||||
assert(a[2] === 2);
|
||||
expect(a).toEqual([0, 1, 2]);
|
||||
});
|
||||
|
||||
for (; a.length < 6; ) {
|
||||
test("only condition", () => {
|
||||
let a = [];
|
||||
for (; a.length < 3; ) {
|
||||
a.push("x");
|
||||
}
|
||||
assert(a.length === 6);
|
||||
assert(a[3] === "x");
|
||||
assert(a[4] === "x");
|
||||
assert(a[5] === "x");
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
||||
expect(a).toEqual(["x", "x", "x"]);
|
||||
});
|
||||
|
|
|
@ -1,45 +1,23 @@
|
|||
load("test-common.js");
|
||||
test("using undefined variable in initializer", () => {
|
||||
expect(() => {
|
||||
for (let i = foo; i < 100; ++i) {}
|
||||
}).toThrowWithMessage(ReferenceError, "'foo' is not defined");
|
||||
});
|
||||
|
||||
try {
|
||||
assertThrowsError(
|
||||
() => {
|
||||
for (var i = foo; i < 100; ++i) {
|
||||
assertNotReached();
|
||||
}
|
||||
},
|
||||
{
|
||||
error: ReferenceError,
|
||||
message: "'foo' is not defined",
|
||||
test("using undefined variable in condition", () => {
|
||||
expect(() => {
|
||||
for (let i = 0; i < foo; ++i) {}
|
||||
}).toThrowWithMessage(ReferenceError, "'foo' is not defined");
|
||||
});
|
||||
|
||||
test("using undefined variable in updater", () => {
|
||||
let loopCount = 0;
|
||||
|
||||
expect(() => {
|
||||
for (let i = 0; i < 100; ++foo) {
|
||||
loopCount++;
|
||||
}
|
||||
);
|
||||
}).toThrowWithMessage(ReferenceError, "'foo' is not defined");
|
||||
|
||||
assertThrowsError(
|
||||
() => {
|
||||
for (var i = 0; i < foo; ++i) {
|
||||
assertNotReached();
|
||||
}
|
||||
},
|
||||
{
|
||||
error: ReferenceError,
|
||||
message: "'foo' is not defined",
|
||||
}
|
||||
);
|
||||
|
||||
var loopCount = 0;
|
||||
assertThrowsError(
|
||||
() => {
|
||||
for (var i = 0; i < 100; ++foo) {
|
||||
loopCount++;
|
||||
}
|
||||
},
|
||||
{
|
||||
error: ReferenceError,
|
||||
message: "'foo' is not defined",
|
||||
}
|
||||
);
|
||||
assert(loopCount === 1);
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
||||
expect(loopCount).toBe(1);
|
||||
});
|
||||
|
|
|
@ -1,47 +1,45 @@
|
|||
load("test-common.js");
|
||||
test("iterate through empty string", () => {
|
||||
const a = [];
|
||||
for (const property in "") {
|
||||
a.push(property);
|
||||
}
|
||||
expect(a).toEqual([]);
|
||||
});
|
||||
|
||||
try {
|
||||
assertVisitsAll(visit => {
|
||||
for (const property in "") {
|
||||
visit(property);
|
||||
}
|
||||
}, []);
|
||||
test("iterate through number", () => {
|
||||
const a = [];
|
||||
for (const property in 123) {
|
||||
a.push(property);
|
||||
}
|
||||
expect(a).toEqual([]);
|
||||
});
|
||||
|
||||
assertVisitsAll(visit => {
|
||||
for (const property in 123) {
|
||||
visit(property);
|
||||
}
|
||||
}, []);
|
||||
test("iterate through empty object", () => {
|
||||
const a = [];
|
||||
for (const property in {}) {
|
||||
a.push(property);
|
||||
}
|
||||
expect(a).toEqual([]);
|
||||
});
|
||||
|
||||
assertVisitsAll(visit => {
|
||||
for (const property in {}) {
|
||||
visit(property);
|
||||
}
|
||||
}, []);
|
||||
test("iterate through string", () => {
|
||||
const a = [];
|
||||
for (const property in "hello") {
|
||||
a.push(property);
|
||||
}
|
||||
expect(a).toEqual(["0", "1", "2", "3", "4"]);
|
||||
});
|
||||
|
||||
assertVisitsAll(
|
||||
visit => {
|
||||
for (const property in "hello") {
|
||||
visit(property);
|
||||
}
|
||||
},
|
||||
["0", "1", "2", "3", "4"]
|
||||
);
|
||||
|
||||
assertVisitsAll(
|
||||
visit => {
|
||||
for (const property in { a: 1, b: 2, c: 2 }) {
|
||||
visit(property);
|
||||
}
|
||||
},
|
||||
["a", "b", "c"]
|
||||
);
|
||||
test("iterate through object", () => {
|
||||
const a = [];
|
||||
for (const property in { a: 1, b: 2, c: 2 }) {
|
||||
a.push(property);
|
||||
}
|
||||
expect(a).toEqual(["a", "b", "c"]);
|
||||
});
|
||||
|
||||
test("use already-declared variable", () => {
|
||||
var property;
|
||||
for (property in "abc");
|
||||
assert(property === "2");
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
||||
expect(property).toBe("2");
|
||||
});
|
||||
|
|
|
@ -1,12 +1,5 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
var number = 0;
|
||||
|
||||
for (var i = 0; i < 3; ++i) for (var j = 0; j < 3; ++j) number++;
|
||||
|
||||
assert(number === 9);
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
||||
test("basic functionality", () => {
|
||||
let number = 0;
|
||||
for (let i = 0; i < 3; ++i) for (let j = 0; j < 3; ++j) number++;
|
||||
expect(number).toBe(9);
|
||||
});
|
||||
|
|
|
@ -1,60 +1,47 @@
|
|||
load("test-common.js");
|
||||
describe("correct behavior", () => {
|
||||
test("iterate through array", () => {
|
||||
const a = [];
|
||||
for (const num of [1, 2, 3]) {
|
||||
a.push(num);
|
||||
}
|
||||
expect(a).toEqual([1, 2, 3]);
|
||||
});
|
||||
|
||||
try {
|
||||
assertThrowsError(
|
||||
() => {
|
||||
test("iterate through string", () => {
|
||||
const a = [];
|
||||
for (const char of "hello") {
|
||||
a.push(char);
|
||||
}
|
||||
expect(a).toEqual(["h", "e", "l", "l", "o"]);
|
||||
});
|
||||
|
||||
test("iterate through string object", () => {
|
||||
const a = [];
|
||||
for (const char of new String("hello")) {
|
||||
a.push(char);
|
||||
}
|
||||
expect(a).toEqual(["h", "e", "l", "l", "o"]);
|
||||
});
|
||||
|
||||
test("use already-declared variable", () => {
|
||||
var char;
|
||||
for (char of "abc");
|
||||
expect(char).toBe("c");
|
||||
});
|
||||
});
|
||||
|
||||
describe("errors", () => {
|
||||
test("right hand side is a primitive", () => {
|
||||
expect(() => {
|
||||
for (const _ of 123) {
|
||||
}
|
||||
},
|
||||
{
|
||||
error: TypeError,
|
||||
message: "for..of right-hand side must be iterable",
|
||||
}
|
||||
);
|
||||
}).toThrowWithMessage(TypeError, "for..of right-hand side must be iterable");
|
||||
});
|
||||
|
||||
assertThrowsError(
|
||||
() => {
|
||||
test("right hand side is an object", () => {
|
||||
expect(() => {
|
||||
for (const _ of { foo: 1, bar: 2 }) {
|
||||
}
|
||||
},
|
||||
{
|
||||
error: TypeError,
|
||||
message: "for..of right-hand side must be iterable",
|
||||
}
|
||||
);
|
||||
|
||||
assertVisitsAll(
|
||||
visit => {
|
||||
for (const num of [1, 2, 3]) {
|
||||
visit(num);
|
||||
}
|
||||
},
|
||||
[1, 2, 3]
|
||||
);
|
||||
|
||||
assertVisitsAll(
|
||||
visit => {
|
||||
for (const char of "hello") {
|
||||
visit(char);
|
||||
}
|
||||
},
|
||||
["h", "e", "l", "l", "o"]
|
||||
);
|
||||
|
||||
assertVisitsAll(
|
||||
visit => {
|
||||
for (const char of new String("hello")) {
|
||||
visit(char);
|
||||
}
|
||||
},
|
||||
["h", "e", "l", "l", "o"]
|
||||
);
|
||||
|
||||
var char;
|
||||
for (char of "abc");
|
||||
assert(char === "c");
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
||||
}).toThrowWithMessage(TypeError, "for..of right-hand side must be iterable");
|
||||
});
|
||||
});
|
||||
|
|
|
@ -1,22 +1,18 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
test("var in for head", () => {
|
||||
for (var v = 5; false; );
|
||||
assert(v == 5);
|
||||
expect(v).toBe(5);
|
||||
});
|
||||
|
||||
const options = { error: ReferenceError };
|
||||
|
||||
assertThrowsError(() => {
|
||||
for (let l = 5; false; );
|
||||
test("let in for head", () => {
|
||||
for (let l = 5; false; );
|
||||
expect(() => {
|
||||
l;
|
||||
}, options);
|
||||
}).toThrowWithMessage(ReferenceError, "'l' is not defined");
|
||||
});
|
||||
|
||||
assertThrowsError(() => {
|
||||
for (const c = 5; false; );
|
||||
test("const in for head", () => {
|
||||
for (const c = 5; false; );
|
||||
expect(() => {
|
||||
c;
|
||||
}, options);
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
||||
}).toThrowWithMessage(ReferenceError, "'c' is not defined");
|
||||
});
|
||||
|
|
|
@ -1,21 +1,19 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
var number = 0;
|
||||
test("basic functionality", () => {
|
||||
let number = 0;
|
||||
while (number < 9) {
|
||||
number++;
|
||||
}
|
||||
assert(number === 9);
|
||||
expect(number).toBe(9);
|
||||
});
|
||||
|
||||
number = 0;
|
||||
test("no braces", () => {
|
||||
let number = 0;
|
||||
while (number < 3) number++;
|
||||
assert(number === 3);
|
||||
expect(number).toBe(3);
|
||||
});
|
||||
|
||||
test("does not loop when initially false", () => {
|
||||
while (false) {
|
||||
assertNotReached();
|
||||
expect().fail();
|
||||
}
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue