mirror of
https://github.com/RGBCube/serenity
synced 2025-05-28 10:05:10 +00:00
LibJS: Improve UpdateExpression::execute()
- Let undefined variables throw a ReferenceError by using Identifier::execute() rather than doing variable lookup manually and ASSERT()ing - Coerce value to number rather than ASSERT()ing - Make code DRY - Add tests
This commit is contained in:
parent
b2305cb67d
commit
a1b820b11c
2 changed files with 63 additions and 11 deletions
54
Libraries/LibJS/Tests/update-expressions-basic.js
Normal file
54
Libraries/LibJS/Tests/update-expressions-basic.js
Normal file
|
@ -0,0 +1,54 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assertThrowsError(() => {
|
||||
++x;
|
||||
}, {
|
||||
error: ReferenceError,
|
||||
message: "'x' not known"
|
||||
});
|
||||
|
||||
var n = 0;
|
||||
assert(++n === 1);
|
||||
assert(n === 1);
|
||||
|
||||
var n = 0;
|
||||
assert(n++ === 0);
|
||||
assert(n === 1);
|
||||
|
||||
var n = 0;
|
||||
assert(--n === -1);
|
||||
assert(n === -1);
|
||||
|
||||
var n = 0;
|
||||
assert(n-- === 0);
|
||||
assert(n === -1);
|
||||
|
||||
var a = [];
|
||||
assert(a++ === 0);
|
||||
assert(a === 1);
|
||||
|
||||
var b = true;
|
||||
assert(b-- === 1);
|
||||
assert(b === 0);
|
||||
|
||||
var s = "foo";
|
||||
assert(isNaN(++s));
|
||||
assert(isNaN(s));
|
||||
|
||||
var s = "foo";
|
||||
assert(isNaN(s++));
|
||||
assert(isNaN(s));
|
||||
|
||||
var s = "foo";
|
||||
assert(isNaN(--s));
|
||||
assert(isNaN(s));
|
||||
|
||||
var s = "foo";
|
||||
assert(isNaN(s--));
|
||||
assert(isNaN(s));
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue