mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 07:17:35 +00:00
Libraries: Move to Userland/Libraries/
This commit is contained in:
parent
dc28c07fa5
commit
13d7c09125
1857 changed files with 266 additions and 274 deletions
30
Userland/Libraries/LibJS/Tests/loops/break-basic.js
Normal file
30
Userland/Libraries/LibJS/Tests/loops/break-basic.js
Normal file
|
@ -0,0 +1,30 @@
|
|||
test("Toplevel break inside loop", () => {
|
||||
var j = 0;
|
||||
for (var i = 0; i < 9; ++i) {
|
||||
break;
|
||||
++j;
|
||||
}
|
||||
expect(j).toBe(0);
|
||||
});
|
||||
|
||||
test("break inside sub-blocks", () => {
|
||||
var j = 0;
|
||||
for (var i = 0; i < 9; ++i) {
|
||||
if (j == 4) {
|
||||
break;
|
||||
}
|
||||
++j;
|
||||
}
|
||||
expect(j).toBe(4);
|
||||
});
|
||||
|
||||
test("break inside curly sub-blocks", () => {
|
||||
var j = 0;
|
||||
for (var i = 0; i < 9; ++i) {
|
||||
if (j == 4) {
|
||||
break;
|
||||
}
|
||||
++j;
|
||||
}
|
||||
expect(j).toBe(4);
|
||||
});
|
10
Userland/Libraries/LibJS/Tests/loops/continue-basic.js
Normal file
10
Userland/Libraries/LibJS/Tests/loops/continue-basic.js
Normal file
|
@ -0,0 +1,10 @@
|
|||
test("basic functionality", () => {
|
||||
var j = 0;
|
||||
for (var i = 0; i < 9; ++i) {
|
||||
if (i == 3) {
|
||||
continue;
|
||||
}
|
||||
++j;
|
||||
}
|
||||
expect(j).toBe(8);
|
||||
});
|
24
Userland/Libraries/LibJS/Tests/loops/do-while-basic.js
Normal file
24
Userland/Libraries/LibJS/Tests/loops/do-while-basic.js
Normal file
|
@ -0,0 +1,24 @@
|
|||
test("basic functionality", () => {
|
||||
let number = 0;
|
||||
do {
|
||||
number++;
|
||||
} while (number < 9);
|
||||
expect(number).toBe(9);
|
||||
});
|
||||
|
||||
test("no braces", () => {
|
||||
let number = 0;
|
||||
do number++;
|
||||
while (number < 3);
|
||||
expect(number).toBe(3);
|
||||
});
|
||||
|
||||
test("exception in test expression", () => {
|
||||
expect(() => {
|
||||
do {} while (foo);
|
||||
}).toThrow(ReferenceError);
|
||||
});
|
||||
|
||||
test("automatic semicolon insertion", () => {
|
||||
expect("do {} while (false) foo").toEval();
|
||||
});
|
15
Userland/Libraries/LibJS/Tests/loops/for-basic.js
Normal file
15
Userland/Libraries/LibJS/Tests/loops/for-basic.js
Normal file
|
@ -0,0 +1,15 @@
|
|||
test("basic functionality", () => {
|
||||
let a = [];
|
||||
for (let i = 0; i < 3; ++i) {
|
||||
a.push(i);
|
||||
}
|
||||
expect(a).toEqual([0, 1, 2]);
|
||||
});
|
||||
|
||||
test("only condition", () => {
|
||||
let a = [];
|
||||
for (; a.length < 3; ) {
|
||||
a.push("x");
|
||||
}
|
||||
expect(a).toEqual(["x", "x", "x"]);
|
||||
});
|
23
Userland/Libraries/LibJS/Tests/loops/for-head-errors.js
Normal file
23
Userland/Libraries/LibJS/Tests/loops/for-head-errors.js
Normal file
|
@ -0,0 +1,23 @@
|
|||
test("using undefined variable in initializer", () => {
|
||||
expect(() => {
|
||||
for (let i = foo; i < 100; ++i) {}
|
||||
}).toThrowWithMessage(ReferenceError, "'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");
|
||||
|
||||
expect(loopCount).toBe(1);
|
||||
});
|
45
Userland/Libraries/LibJS/Tests/loops/for-in-basic.js
Normal file
45
Userland/Libraries/LibJS/Tests/loops/for-in-basic.js
Normal file
|
@ -0,0 +1,45 @@
|
|||
test("iterate through empty string", () => {
|
||||
const a = [];
|
||||
for (const property in "") {
|
||||
a.push(property);
|
||||
}
|
||||
expect(a).toEqual([]);
|
||||
});
|
||||
|
||||
test("iterate through number", () => {
|
||||
const a = [];
|
||||
for (const property in 123) {
|
||||
a.push(property);
|
||||
}
|
||||
expect(a).toEqual([]);
|
||||
});
|
||||
|
||||
test("iterate through empty object", () => {
|
||||
const a = [];
|
||||
for (const property in {}) {
|
||||
a.push(property);
|
||||
}
|
||||
expect(a).toEqual([]);
|
||||
});
|
||||
|
||||
test("iterate through string", () => {
|
||||
const a = [];
|
||||
for (const property in "hello") {
|
||||
a.push(property);
|
||||
}
|
||||
expect(a).toEqual(["0", "1", "2", "3", "4"]);
|
||||
});
|
||||
|
||||
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");
|
||||
expect(property).toBe("2");
|
||||
});
|
5
Userland/Libraries/LibJS/Tests/loops/for-no-curlies.js
Normal file
5
Userland/Libraries/LibJS/Tests/loops/for-no-curlies.js
Normal file
|
@ -0,0 +1,5 @@
|
|||
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);
|
||||
});
|
100
Userland/Libraries/LibJS/Tests/loops/for-of-basic.js
Normal file
100
Userland/Libraries/LibJS/Tests/loops/for-of-basic.js
Normal file
|
@ -0,0 +1,100 @@
|
|||
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]);
|
||||
});
|
||||
|
||||
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");
|
||||
});
|
||||
|
||||
test("respects custom Symbol.iterator method", () => {
|
||||
const o = {
|
||||
[Symbol.iterator]() {
|
||||
return {
|
||||
i: 0,
|
||||
next() {
|
||||
if (this.i++ == 3) {
|
||||
return { done: true };
|
||||
}
|
||||
return { value: this.i, done: false };
|
||||
},
|
||||
};
|
||||
},
|
||||
};
|
||||
|
||||
const a = [];
|
||||
for (const k of o) {
|
||||
a.push(k);
|
||||
}
|
||||
|
||||
expect(a).toEqual([1, 2, 3]);
|
||||
});
|
||||
|
||||
test("loops through custom iterator if there is an exception thrown part way through", () => {
|
||||
// This tests against the way custom iterators used to be implemented, where the values
|
||||
// were all collected at once before the for-of body was executed, instead of getting
|
||||
// the values one at a time
|
||||
const o = {
|
||||
[Symbol.iterator]() {
|
||||
return {
|
||||
i: 0,
|
||||
next() {
|
||||
if (this.i++ === 3) {
|
||||
throw new Error();
|
||||
}
|
||||
return { value: this.i };
|
||||
},
|
||||
};
|
||||
},
|
||||
};
|
||||
|
||||
const a = [];
|
||||
|
||||
try {
|
||||
for (let k of o) {
|
||||
a.push(k);
|
||||
}
|
||||
expect().fail();
|
||||
} catch (e) {
|
||||
expect(a).toEqual([1, 2, 3]);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("errors", () => {
|
||||
test("right hand side is a primitive", () => {
|
||||
expect(() => {
|
||||
for (const _ of 123) {
|
||||
}
|
||||
}).toThrowWithMessage(TypeError, "123 is not iterable");
|
||||
});
|
||||
|
||||
test("right hand side is an object", () => {
|
||||
expect(() => {
|
||||
for (const _ of { foo: 1, bar: 2 }) {
|
||||
}
|
||||
}).toThrowWithMessage(TypeError, "[object Object] is not iterable");
|
||||
});
|
||||
});
|
18
Userland/Libraries/LibJS/Tests/loops/for-scopes.js
Normal file
18
Userland/Libraries/LibJS/Tests/loops/for-scopes.js
Normal file
|
@ -0,0 +1,18 @@
|
|||
test("var in for head", () => {
|
||||
for (var v = 5; false; );
|
||||
expect(v).toBe(5);
|
||||
});
|
||||
|
||||
test("let in for head", () => {
|
||||
for (let l = 5; false; );
|
||||
expect(() => {
|
||||
l;
|
||||
}).toThrowWithMessage(ReferenceError, "'l' is not defined");
|
||||
});
|
||||
|
||||
test("const in for head", () => {
|
||||
for (const c = 5; false; );
|
||||
expect(() => {
|
||||
c;
|
||||
}).toThrowWithMessage(ReferenceError, "'c' is not defined");
|
||||
});
|
25
Userland/Libraries/LibJS/Tests/loops/while-basic.js
Normal file
25
Userland/Libraries/LibJS/Tests/loops/while-basic.js
Normal file
|
@ -0,0 +1,25 @@
|
|||
test("basic functionality", () => {
|
||||
let number = 0;
|
||||
while (number < 9) {
|
||||
number++;
|
||||
}
|
||||
expect(number).toBe(9);
|
||||
});
|
||||
|
||||
test("no braces", () => {
|
||||
let number = 0;
|
||||
while (number < 3) number++;
|
||||
expect(number).toBe(3);
|
||||
});
|
||||
|
||||
test("does not loop when initially false", () => {
|
||||
while (false) {
|
||||
expect().fail();
|
||||
}
|
||||
});
|
||||
|
||||
test("exception in test expression", () => {
|
||||
expect(() => {
|
||||
while (foo);
|
||||
}).toThrow(ReferenceError);
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue