js/LibJS: Move test functions to pure javascript.

The addition of assert functions to Userland/js
was done before we had load(..) implemented. Now
that it exists, it seems like the right move the
test helper functions to pure javascript instead
of poluting js with random global functions.
This commit is contained in:
Brian Gianforcaro 2020-04-13 10:31:13 -07:00 committed by Andreas Kling
parent 9477efe970
commit d74ad81402
Notes: sideshowbarker 2024-07-19 07:36:39 +09:00
91 changed files with 193 additions and 19 deletions

View File

@ -1,3 +1,5 @@
load("test-common.js");
try {
assert(Array.length === 1);
assert(Array.prototype.length === 0);

View File

@ -1,3 +1,5 @@
load("test-common.js");
try {
var a = [1, 2, 3];
var value = a.pop();

View File

@ -1,3 +1,5 @@
load("test-common.js");
try {
var a = [1, 2, 3];
var value = a.shift();

View File

@ -1,3 +1,5 @@
load("test-common.js");
try {
var a = [1, 2, 3];
assert(a.toString() === '1,2,3');

View File

@ -1,3 +1,5 @@
load("test-common.js");
try {
assert(Boolean.length === 1);
assert(typeof new Boolean() === "object");

View File

@ -1,3 +1,5 @@
load("test-common.js");
try {
assert(typeof Boolean.prototype === "object");
assert(Boolean.prototype.valueOf() === false);

View File

@ -1,3 +1,5 @@
load("test-common.js");
try {
var foo = true;
assert(foo.toString() === "true");

View File

@ -1,3 +1,5 @@
load("test-common.js");
try {
var foo = true;
assert(foo.valueOf() === true);

View File

@ -1,3 +1,5 @@
load("test-common.js");
try {
var last = 0;
for (var i = 0; i < 100; ++i) {

View File

@ -1,3 +1,5 @@
load("test-common.js");
try {
var d = new Date();
assert(!isNaN(d.getDate()));

View File

@ -1,3 +1,5 @@
load("test-common.js");
try {
var d = new Date();
assert(!isNaN(d.getDay()));

View File

@ -1,3 +1,5 @@
load("test-common.js");
try {
var d = new Date();
assert(!isNaN(d.getFullYear()));

View File

@ -1,3 +1,5 @@
load("test-common.js");
try {
var d = new Date();
assert(!isNaN(d.getHours()));

View File

@ -1,3 +1,5 @@
load("test-common.js");
try {
var d = new Date();
assert(!isNaN(d.getMilliseconds()));

View File

@ -1,3 +1,5 @@
load("test-common.js");
try {
var d = new Date();
assert(!isNaN(d.getMinutes()));

View File

@ -1,3 +1,5 @@
load("test-common.js");
try {
var d = new Date();
assert(!isNaN(d.getMonth()));

View File

@ -1,3 +1,5 @@
load("test-common.js");
try {
var d = new Date();
assert(!isNaN(d.getSeconds()));

View File

@ -1,3 +1,5 @@
load("test-common.js");
try {
var d = new Date();
assert(!isNaN(d.getTime()));

View File

@ -1,3 +1,5 @@
load("test-common.js");
try {
var e;

View File

@ -1,3 +1,5 @@
load("test-common.js");
try {
var changedInstance = new Error("");
changedInstance.name = 'NewCustomError';

View File

@ -1,3 +1,5 @@
load("test-common.js");
try {
assert(Error().toString() === "Error");
assert(Error(undefined).toString() === "Error");

View File

@ -1,3 +1,5 @@
load("test-common.js");
try {
assert(Function.length === 1);
assert(Function.prototype.length === 0);

View File

@ -1,3 +1,5 @@
load("test-common.js");
try {
function Foo(arg) {
this.foo = arg;

View File

@ -1,3 +1,5 @@
load("test-common.js");
try {
function Foo(arg) {
this.foo = arg;

View File

@ -1,3 +1,5 @@
load("test-common.js");
try {
assert((function() {}).toString() === "function () {\n ???\n}");
assert((function(foo) {}).toString() === "function (foo) {\n ???\n}");

View File

@ -1,3 +1,5 @@
load("test-common.js");
try {
assert(Infinity + "" === "Infinity");
assert(-Infinity + "" === "-Infinity");

View File

@ -1,3 +1,5 @@
load("test-common.js");
// Borrowed from LibM/TestMath.cpp :^)
function expectClose(a, b) { assert(Math.abs(a - b) < 0.000001); }

View File

@ -1,3 +1,5 @@
load("test-common.js");
try {
assert(Math.abs('-1') === 1);
assert(Math.abs(-2) === 2);

View File

@ -1,3 +1,5 @@
load("test-common.js");
try {
assert(Math.ceil(0.95) === 1);
assert(Math.ceil(4) === 4);

View File

@ -1,3 +1,5 @@
load("test-common.js");
try {
assert(Math.cos(0) === 1);
assert(Math.cos(null) === 1);

View File

@ -1,3 +1,5 @@
load("test-common.js");
try {
assert(Math.max.length === 2);
assert(Math.max() === -Infinity);

View File

@ -1,3 +1,5 @@
load("test-common.js");
try {
assert(Math.min.length === 2);
assert(Math.min(1) === 1);

View File

@ -1,3 +1,5 @@
load("test-common.js");
try {
assert(Math.sin(0) === 0);
assert(Math.sin(null) === 0);

View File

@ -1,3 +1,5 @@
load("test-common.js");
try {
assert(Math.sqrt(9) === 3);
console.log("PASS");

View File

@ -1,3 +1,5 @@
load("test-common.js");
try {
assert(Math.tan(0) === 0);
assert(Math.tan(null) === 0);

View File

@ -1,3 +1,5 @@
load("test-common.js")
try {
assert(Math.trunc(13.37) === 13);
assert(Math.trunc(42.84) === 42);

View File

@ -1,3 +1,5 @@
load("test-common.js");
try {
var nan = undefined + 1;
assert(nan + "" == "NaN");

View File

@ -1,3 +1,5 @@
load("test-common.js");
try {
assert(Number.EPSILON === 2 ** -52);
assert(Number.EPSILON > 0);

View File

@ -1,3 +1,5 @@
load("test-common.js");
try {
assert(Number.isSafeInteger.length === 1);
assert(Number.isSafeInteger(0) === true);

View File

@ -1,3 +1,5 @@
load("test-common.js");
try {
assert(Number.length === 1);
assert(typeof Number() === "number");

View File

@ -1,3 +1,5 @@
load("test-common.js");
try {
assert(typeof Number.prototype === "object");
assert(Number.prototype.valueOf() === 0);

View File

@ -1,3 +1,5 @@
load("test-common.js");
try {
var o = {};
Object.defineProperty(o, "foo", { value: 1, writable: false, enumerable: false });

View File

@ -1,3 +1,5 @@
load("test-common.js");
try {
var names = Object.getOwnPropertyNames([1, 2, 3]);

View File

@ -1,3 +1,5 @@
load("test-common.js");
try {
var o1 = new Object();
var o2 = {};

View File

@ -1,3 +1,5 @@
load("test-common.js");
try {
assert(Array.prototype.constructor === Array)
assert(Boolean.prototype.constructor === Boolean)

View File

@ -1,3 +1,5 @@
load("test-common.js");
try {
var o = {};
o.foo = 1;

View File

@ -1,3 +1,5 @@
load("test-common.js");
try {
var o = new Object();
Object.prototype.foo = 123;

View File

@ -1,3 +1,5 @@
load("test-common.js");
try {
assert(typeof Object.prototype.toString() === "string");
console.log("PASS");

View File

@ -1,3 +1,5 @@
load("test-common.js");
try {
var s = "foobar"
assert(typeof s === "string");

View File

@ -1,3 +1,5 @@
load("test-common.js");
try {
var s = "hello friends"

View File

@ -1,3 +1,5 @@
load("test-common.js");
try {
assert(typeof Object.getPrototypeOf("") === "object");
assert(Object.getPrototypeOf("").valueOf() === '');

View File

@ -1,3 +1,5 @@
load("test-common.js");
try {
assert(String.prototype.padEnd.length === 1);

View File

@ -1,3 +1,5 @@
load("test-common.js");
try {
assert(String.prototype.padStart.length === 1);

View File

@ -1,3 +1,5 @@
load("test-common.js");
try {
var s = "foobar";
assert(s.startsWith("f") === true);

View File

@ -1,3 +1,5 @@
load("test-common.js");
try {
assert(String.prototype.toLowerCase.length === 0);

View File

@ -1,3 +1,5 @@
load("test-common.js");
try {
assert(String.prototype.toString.length === 0)
assert("".toString() === "");

View File

@ -1,3 +1,5 @@
load("test-common.js");
try {
assert(String.prototype.toUpperCase.length === 0);

View File

@ -1,3 +1,5 @@
load("test-common.js");
try {
var a = [1, 2, 3];

View File

@ -1,3 +1,5 @@
load("test-common.js");
try {
let getNumber = () => 42;
assert(getNumber() === 42);

View File

@ -1,3 +1,5 @@
load("test-common.js");
try {
assert((0 | 0) === 0);
assert((0 | 1) === 1);

View File

@ -1,3 +1,5 @@
load("test-common.js");
try {
var j = 0;
for (var i = 0; i < 9; ++i) {

View File

@ -1,3 +1,5 @@
load("test-common.js");
try {
var number = 0;
do {

View File

@ -1,3 +1,5 @@
load("test-common.js");
try {
i < 3;
} catch (e) {

View File

@ -1,3 +1,5 @@
load("test-common.js");
try {
assert(2 ** 0 === 1);
assert(2 ** 1 === 2);

View File

@ -1,3 +1,5 @@
load("test-common.js");
try {
var a = [];
for (var i = 0; i < 3; ++i) {

View File

@ -1,3 +1,5 @@
load("test-common.js");
try {
var number = 0;

View File

@ -1,3 +1,5 @@
load("test-common.js");
try {
try {
var b = true;

View File

@ -1,3 +1,5 @@
load("test-common.js");
try {
function foo() { }
assert(foo.length === 0);

View File

@ -1,3 +1,5 @@
load("test-common.js");
function foo(a, b) { return a + b; }
try {

View File

@ -1,3 +1,5 @@
load("test-common.js");
try {
assert(typeof this === "object");
assert(this === globalThis);

View File

@ -1,3 +1,5 @@
load("test-common.js");
try {
function Foo() {
this.x = 123;

View File

@ -1,3 +1,5 @@
load("test-common.js");
try {
try {
Math.abs(-20) = 40;

View File

@ -1,3 +1,5 @@
load("test-common.js");
try {
let i = 1;

View File

@ -1,3 +1,5 @@
load("test-common.js");
try {
assert((true && true) === true);
assert((false && false) === false);

View File

@ -1,3 +1,5 @@
load("test-common.js");
try {
let foo = 1;
false && (foo = 2);

View File

@ -1,3 +1,5 @@
load("test-common.js");
try {
assert(10 % 3 === 1);
assert(10.5 % 2.5 === 0.5);

View File

@ -1,3 +1,5 @@
load("test-common.js");
try {
assert(0xff === 255);
assert(0XFF === 255);

View File

@ -1,3 +1,5 @@
load("test-common.js");
try {
var o = { 1: 23, foo: "bar", "hello": "friends" };
assert(o[1] === 23);

View File

@ -1,3 +1,5 @@
load("test-common.js");
try {
var o = {};
o.a = 1;

View File

@ -13,6 +13,7 @@ pass_count=0
fail_count=0
count=0
GLOBIGNORE=test-common.js
for f in *.js; do
result=`$js_program -t $f`
if [ "$result" = "PASS" ]; then

View File

@ -1,3 +1,5 @@
load("test-common.js");
try {
var i = 0;
var three;

View File

@ -1,3 +1,5 @@
load("test-common.js");
try {
var x = 1;

View File

@ -0,0 +1,16 @@
function AssertionError(message) {
var instance = new Error(message);
instance.name = 'AssertionError';
Object.setPrototypeOf(instance, Object.getPrototypeOf(this));
return instance;
}
function assert(value) {
if (!value)
throw new AssertionError("The assertion failed!");
}
function assertNotReached() {
throw new AssertionError("assertNotReached() was reached!");
}

View File

@ -1,3 +1,5 @@
load("test-common.js");
try {
throw 1;
assertNotReached();

View File

@ -1,3 +1,5 @@
load("test-common.js");
try {
assert(+false === 0);
assert(-false === 0);

View File

@ -1,3 +1,5 @@
load("test-common.js");
try {
assert(typeof "foo" === "string");
assert(!(typeof "foo" !== "string"));

View File

@ -1,3 +1,5 @@
load("test-common.js");
try {
var a = 1, b = 2, c = a + b;
assert(a === 1);

View File

@ -1,3 +1,5 @@
load("test-common.js");
try {
function foo() {
i = 3;

View File

@ -1,3 +1,5 @@
load("test-common.js");
try {
const constantValue = 1;

View File

@ -1,3 +1,5 @@
load("test-common.js");
function foo(a) {
return a;
}

View File

@ -372,28 +372,9 @@ void repl(JS::Interpreter& interpreter)
}
}
JS::Value assert_impl(JS::Interpreter& interpreter)
{
if (!interpreter.argument_count())
return interpreter.throw_exception<JS::TypeError>("No arguments specified");
auto assertion_value = interpreter.argument(0).to_boolean();
if (!assertion_value)
return interpreter.throw_exception<JS::Error>("AssertionError", "The assertion failed!");
return JS::Value(assertion_value);
}
JS::Value assert_not_reached(JS::Interpreter& interpreter)
{
return interpreter.throw_exception<JS::Error>("AssertionError", "assertNotReached() was reached!");
}
void enable_test_mode(JS::Interpreter& interpreter)
{
interpreter.global_object().put_native_function("load", ReplObject::load_file);
interpreter.global_object().put_native_function("assert", assert_impl);
interpreter.global_object().put_native_function("assertNotReached", assert_not_reached);
}
int main(int argc, char** argv)