diff options
author | bptato <nincsnevem662@gmail.com> | 2024-08-15 19:11:49 +0200 |
---|---|---|
committer | bptato <nincsnevem662@gmail.com> | 2024-08-15 19:23:55 +0200 |
commit | 4bf895db711f3d4d229d3f18fbb2145cce2a73af (patch) | |
tree | 2e81c7399de03aebb9dfa166eba6ee809a75cd2e /test | |
parent | 885a3493b6cad4b4247a200928fe61e41883aaba (diff) | |
download | chawan-4bf895db711f3d4d229d3f18fbb2145cce2a73af.tar.gz |
xhr: more progress
* add responseText, response * add net tests -> currently sync XHR only; should find a way to do async tests... * update monoucha -> simplified & updated some related code that no longer worked properly
Diffstat (limited to 'test')
-rw-r--r-- | test/asserts.js | 23 | ||||
l---------[-rw-r--r--] | test/js/asserts.js | 20 | ||||
-rw-r--r-- | test/js/console.html | 5 | ||||
-rwxr-xr-x | test/js/run_js_tests.sh | 6 | ||||
-rwxr-xr-x | test/layout/run_layout_tests.sh | 8 | ||||
-rw-r--r-- | test/net/all.expected | 1 | ||||
l--------- | test/net/asserts.js | 1 | ||||
-rw-r--r-- | test/net/config.toml | 12 | ||||
-rw-r--r-- | test/net/ping | 1 | ||||
-rw-r--r-- | test/net/run.nim | 47 | ||||
-rwxr-xr-x | test/net/run.sh | 17 | ||||
-rw-r--r-- | test/net/xhr.html (renamed from test/js/xhr.html) | 9 |
12 files changed, 116 insertions, 34 deletions
diff --git a/test/asserts.js b/test/asserts.js new file mode 100644 index 00000000..6ef3033a --- /dev/null +++ b/test/asserts.js @@ -0,0 +1,23 @@ +function assert(x, msg) { + const mymsg = msg ? ": " + msg : ""; + if (!x) + throw new TypeError("Assertion failed" + mymsg); +} + +function assert_throws(expr, error) { + try { + eval(expr); + } catch (e) { + if (e instanceof Error) + return; + } + throw new TypeError("Assertion failed"); +} + +function assert_equals(a, b) { + assert(a === b, "Expected " + b + " but got " + a); +} + +function assert_instanceof(a, b) { + assert(a instanceof b, a + " not an instance of " + b); +} diff --git a/test/js/asserts.js b/test/js/asserts.js index e84f2d71..8471bc87 100644..120000 --- a/test/js/asserts.js +++ b/test/js/asserts.js @@ -1,19 +1 @@ -function assert(x, msg) { - const mymsg = msg ? ": " + msg : ""; - if (!x) - throw new TypeError("Assertion failed" + mymsg); -} - -function assert_throws(expr, error) { - try { - eval(expr); - } catch (e) { - if (e instanceof Error) - return; - } - throw new TypeError("Assertion failed"); -} - -function assert_equals(a, b) { - assert(a === b, "Expected " + b + " but got " + a); -} +../asserts.js \ No newline at end of file diff --git a/test/js/console.html b/test/js/console.html deleted file mode 100644 index 67b244a7..00000000 --- a/test/js/console.html +++ /dev/null @@ -1,5 +0,0 @@ -<!doctype html> -<div>Success</div> -<script> -console.log("Hello, world!") -</script> diff --git a/test/js/run_js_tests.sh b/test/js/run_js_tests.sh index ac531a67..3f484449 100755 --- a/test/js/run_js_tests.sh +++ b/test/js/run_js_tests.sh @@ -1,11 +1,11 @@ #!/bin/sh -if ! test "$CHA_TEST_BIN" -then test -f ../../cha && CHA_TEST_BIN=../../cha || CHA_TEST_BIN=cha +if ! test "$CHA" +then test -f ../../cha && CHA=../../cha || CHA=cha fi failed=0 for h in *.html do printf '%s\r' "$h" - if ! "$CHA_TEST_BIN" -C config.toml "$h" | diff all.expected - + if ! "$CHA" -dC config.toml "$h" | diff all.expected - then failed=$(($failed+1)) printf 'FAIL: %s\n' "$h" fi diff --git a/test/layout/run_layout_tests.sh b/test/layout/run_layout_tests.sh index 20b85ce1..429d2ba1 100755 --- a/test/layout/run_layout_tests.sh +++ b/test/layout/run_layout_tests.sh @@ -1,6 +1,6 @@ #!/bin/sh -if test -z "$CHA_TEST_BIN" -then test -f ../../cha && CHA_TEST_BIN=../../cha || CHA_TEST_BIN=cha +if test -z "$CHA" +then test -f ../../cha && CHA=../../cha || CHA=cha fi failed=0 for h in *.html @@ -8,12 +8,12 @@ do printf '%s\r' "$h" expected="$(basename "$h" .html).expected" color_expected="$(basename "$h" .html).color.expected" if test -f "$expected" - then if ! "$CHA_TEST_BIN" -C config.toml "$h" | diff "$expected" - + then if ! "$CHA" -C config.toml "$h" | diff "$expected" - then failed=$(($failed+1)) printf 'FAIL: %s\n' "$h" fi elif test -f "$color_expected" - then if ! "$CHA_TEST_BIN" -C config.color.toml "$h" | diff "$color_expected" - + then if ! "$CHA" -C config.color.toml "$h" | diff "$color_expected" - then failed=$(($failed+1)) printf 'FAIL: %s\n' "$h" fi diff --git a/test/net/all.expected b/test/net/all.expected new file mode 100644 index 00000000..35821117 --- /dev/null +++ b/test/net/all.expected @@ -0,0 +1 @@ +Success diff --git a/test/net/asserts.js b/test/net/asserts.js new file mode 120000 index 00000000..8471bc87 --- /dev/null +++ b/test/net/asserts.js @@ -0,0 +1 @@ +../asserts.js \ No newline at end of file diff --git a/test/net/config.toml b/test/net/config.toml new file mode 100644 index 00000000..ce723641 --- /dev/null +++ b/test/net/config.toml @@ -0,0 +1,12 @@ +[buffer] +scripting = true + +[display] +columns = 80 +lines = 24 +pixels-per-column = 9 +pixels-per-line = 18 +force-columns = true +force-lines = true +force-pixels-per-column = true +force-pixels-per-line = true diff --git a/test/net/ping b/test/net/ping new file mode 100644 index 00000000..8e554694 --- /dev/null +++ b/test/net/ping @@ -0,0 +1 @@ +pong diff --git a/test/net/run.nim b/test/net/run.nim new file mode 100644 index 00000000..d27bbef3 --- /dev/null +++ b/test/net/run.nim @@ -0,0 +1,47 @@ +import std/asyncdispatch +import std/asynchttpserver +import std/os +import std/posix + +import utils/twtstr + +proc cb(req: Request) {.async.} = + const headers = {"Content-type": "text/html; charset=utf-8"} + if req.url.path == "/stop": + await req.respond(Http200, "", headers.newHttpHeaders()) + quit(0) + let s = readFile(req.url.path.after('/')) + #echo (req.reqMethod, req.url.path, req.headers) + await req.respond(Http200, s, headers.newHttpHeaders()) + +proc runServer(server: AsyncHttpServer) {.async.} = + while true: + if server.shouldAcceptRequest(): + await server.acceptRequest(cb) + else: + # too many concurrent connections, `maxFDs` exceeded + # wait 500ms for FDs to be closed + await sleepAsync(500) + +proc main() {.async.} = + var server = newAsyncHttpServer() + if paramCount() >= 1 and paramStr(1) == "-x": + server.listen(Port(8000)) + await server.runServer() + quit(0) + server.listen(Port(0)) + let port = server.getPort() + case fork() + of 0: + let cmd = getAppFileName().beforeLast('/') & "/run.sh " & $uint16(port) + discard execl("/bin/sh", "sh", "-c", cstring(cmd), nil) + quit(1) + of -1: + stderr.write("Failed to start run.sh") + quit(1) + else: + await server.runServer() + var x: cint + quit(WEXITSTATUS(wait(addr x))) + +waitFor main() diff --git a/test/net/run.sh b/test/net/run.sh new file mode 100755 index 00000000..1ea90e3a --- /dev/null +++ b/test/net/run.sh @@ -0,0 +1,17 @@ +#!/bin/sh + +if ! test "$CHA" +then test -f ../../cha && CHA=../../cha || CHA=cha +fi + +failed=0 +for h in *.html +do printf '%s\r' "$h" + if ! "$CHA" -dC config.toml "http://localhost:$1/$h" | diff all.expected - + then failed=$(($failed+1)) + printf 'FAIL: %s\n' "$h" + fi +done +printf '\n' +$CHA -d "http://localhost:$1/stop" >/dev/null +exit "$failed" diff --git a/test/js/xhr.html b/test/net/xhr.html index 4bd66b95..2d034772 100644 --- a/test/js/xhr.html +++ b/test/net/xhr.html @@ -6,12 +6,15 @@ const x = new XMLHttpRequest(); assert(x.onreadystatechange === null); function myFunction() { - console.log("change"); + ; } x.onreadystatechange = myFunction; assert(myFunction === x.onreadystatechange); assert(x.readyState === XMLHttpRequest.UNSENT); -x.open("GET", ""); -document.getElementById("x").textContent = "Success"; +x.open("GET", "ping", false); +assert_throws("x.responseType = 'document'"); x.send(); +assert_equals(x.readyState, XMLHttpRequest.DONE); +assert_equals(x.responseText.trim(), "pong"); +document.getElementById("x").textContent = "Success"; </script> |