Ver código fonte

LibJS: Add a very basic test runner (shell script) + some tests

Andreas Kling 5 anos atrás
pai
commit
45488401b1

+ 11 - 0
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);
+}

+ 19 - 0
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);
+}

+ 23 - 0
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);
+}

+ 14 - 0
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);
+}

+ 26 - 0
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"