diff options
author | Andreas Kling <kling@serenityos.org> | 2020-03-25 14:26:31 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-03-25 15:32:17 +0100 |
commit | 45488401b108c376cd1cc91414bcf8007e45c13a (patch) | |
tree | c58b02f39fc6a9ef29e2c17900d90793c0e4a598 /Libraries/LibJS | |
parent | a94a150df079273d6d03d765cf5ae0822e2d0aa8 (diff) | |
download | serenity-45488401b108c376cd1cc91414bcf8007e45c13a.zip |
LibJS: Add a very basic test runner (shell script) + some tests
Diffstat (limited to 'Libraries/LibJS')
-rw-r--r-- | Libraries/LibJS/Tests/Object.prototype.hasOwnProperty.js | 11 | ||||
-rw-r--r-- | Libraries/LibJS/Tests/String.prototype.charAt.js | 19 | ||||
-rw-r--r-- | Libraries/LibJS/Tests/basic-array.js | 23 | ||||
-rw-r--r-- | Libraries/LibJS/Tests/basic-typeof.js | 14 | ||||
-rwxr-xr-x | Libraries/LibJS/Tests/run-tests | 26 |
5 files changed, 93 insertions, 0 deletions
diff --git a/Libraries/LibJS/Tests/Object.prototype.hasOwnProperty.js b/Libraries/LibJS/Tests/Object.prototype.hasOwnProperty.js new file mode 100644 index 0000000000..9a47401bf5 --- /dev/null +++ b/Libraries/LibJS/Tests/Object.prototype.hasOwnProperty.js @@ -0,0 +1,11 @@ +function assert(x) { if (!x) throw 1; } + +try { + var o = {}; + o.foo = 1; + assert(o.hasOwnProperty("foo") === true); + assert(o.hasOwnProperty("bar") === false); + console.log("PASS"); +} catch (e) { + console.log("FAIL: " + e); +} diff --git a/Libraries/LibJS/Tests/String.prototype.charAt.js b/Libraries/LibJS/Tests/String.prototype.charAt.js new file mode 100644 index 0000000000..741e89e2ec --- /dev/null +++ b/Libraries/LibJS/Tests/String.prototype.charAt.js @@ -0,0 +1,19 @@ +function assert(x) { if (!x) throw 1; } + +try { + var s = "foobar" + assert(typeof s === "string"); + assert(s.length === 6); + + assert(s.charAt(0) === 'f'); + assert(s.charAt(1) === 'o'); + assert(s.charAt(2) === 'o'); + assert(s.charAt(3) === 'b'); + assert(s.charAt(4) === 'a'); + assert(s.charAt(5) === 'r'); + assert(s.charAt(6) === ''); + + console.log("PASS"); +} catch (e) { + console.log("FAIL: " + e); +} diff --git a/Libraries/LibJS/Tests/basic-array.js b/Libraries/LibJS/Tests/basic-array.js new file mode 100644 index 0000000000..cc06f8925a --- /dev/null +++ b/Libraries/LibJS/Tests/basic-array.js @@ -0,0 +1,23 @@ +function assert(x) { if (!x) throw 1; } + +try { + var a = [1, 2, 3]; + + assert(typeof a === "object"); + assert(a.length === 3); + assert(a[0] === 1); + assert(a[1] === 2); + assert(a[2] === 3); + + a[1] = 5; + assert(a[1] === 5); + assert(a.length === 3); + + a.push(7); + assert(a[3] === 7); + assert(a.length === 4); + + console.log("PASS"); +} catch (e) { + console.log("FAIL: " + e); +} diff --git a/Libraries/LibJS/Tests/basic-typeof.js b/Libraries/LibJS/Tests/basic-typeof.js new file mode 100644 index 0000000000..110d01dcb6 --- /dev/null +++ b/Libraries/LibJS/Tests/basic-typeof.js @@ -0,0 +1,14 @@ +function assert(x) { if (!x) throw 1; } + +try { + assert(typeof "foo" === "string"); + assert(!(typeof "foo" !== "string")); + assert(typeof (1 + 2) === "number"); + assert(typeof {} === "object"); + assert(typeof null === "object"); + assert(typeof undefined === "undefined"); + + console.log("PASS"); +} catch (e) { + console.log("FAIL: " + e); +} diff --git a/Libraries/LibJS/Tests/run-tests b/Libraries/LibJS/Tests/run-tests new file mode 100755 index 0000000000..e93d3a225e --- /dev/null +++ b/Libraries/LibJS/Tests/run-tests @@ -0,0 +1,26 @@ +#!/bin/bash + +if [ "`uname`" = "SerenityOS" ]; then + js_program=/bin/js +else + [ -z "$js_program" ] && js_program="$SERENITY_ROOT/Meta/Lagom/build/js" +fi + +pass_count=0 +fail_count=0 +count=0 + +for f in *.js; do + echo -n $f: + result=`$js_program $f` + if [ "$result" = "PASS" ]; then + let pass_count++ + echo -e "\033[32;1mPASS\033[0m" + else + echo -e "\033[31;1mFAIL\033[0m" + let fail_count++ + fi + let count++ +done + +echo -e "Ran $count tests, Passed: \033[32;1m$pass_count\033[0m, Failed: \033[31;1m$fail_count\033[0m" |