summaryrefslogtreecommitdiff
path: root/encoding/json/+test/lexer.ha
diff options
context:
space:
mode:
authorLassi Pulkkinen <lassi@pulk.fi>2024-10-31 03:11:21 +0200
committerLassi Pulkkinen <lassi@pulk.fi>2024-10-31 03:51:35 +0200
commitae44478b30d890fe0fb04022f44d474dcdcc3f9d (patch)
tree5f462459ae4b47d22114eed717d1382d08cf4dfe /encoding/json/+test/lexer.ha
Initial commit (import old repo)HEADmain
Diffstat (limited to 'encoding/json/+test/lexer.ha')
-rw-r--r--encoding/json/+test/lexer.ha62
1 files changed, 62 insertions, 0 deletions
diff --git a/encoding/json/+test/lexer.ha b/encoding/json/+test/lexer.ha
new file mode 100644
index 0000000..b4c098e
--- /dev/null
+++ b/encoding/json/+test/lexer.ha
@@ -0,0 +1,62 @@
+use io;
+use memio;
+use strings;
+
+@test fn lex() void = {
+ const cases: [_](str, []token) = [
+ ("true", [true]),
+ ("false", [false]),
+ ("null", [_null]),
+ ("1234", [1234.0]),
+ ("12.34", [12.34]),
+ ("12.34e5", [12.34e5]),
+ ("12.34E5", [12.34e5]),
+ ("12.34e+5", [12.34e5]),
+ ("12.34e-5", [12.34e-5]),
+ ("12e5", [12.0e5]),
+ ("-1234", [-1234.0]),
+ (`"hello world"`, ["hello world"]),
+ (`"\"\\\/\b\f\n\r\t\u0020"`, ["\"\\/\b\f\n\r\t\u0020"]),
+ ("[ null, null ]", [arraystart, _null, comma, _null, arrayend]),
+ ];
+
+ for (let i = 0z; i < len(cases); i += 1) {
+ const src = strings::toutf8(cases[i].0);
+ const src = memio::fixed(src);
+ const lexer = newlexer(&src);
+ defer close(&lexer);
+
+ for (let j = 0z; j < len(cases[i].1); j += 1) {
+ const want = cases[i].1[j];
+ const have = lex(&lexer)! as token;
+ assert(tokeq(want, have));
+ };
+
+ assert(lex(&lexer) is io::EOF);
+ };
+};
+
+fn tokeq(want: token, have: token) bool = {
+ match (want) {
+ case _null =>
+ return have is _null;
+ case comma =>
+ return have is comma;
+ case colon =>
+ return have is colon;
+ case arraystart =>
+ return have is arraystart;
+ case arrayend =>
+ return have is arrayend;
+ case objstart =>
+ return have is objstart;
+ case objend =>
+ return have is objend;
+ case let b: bool =>
+ return have as bool == b;
+ case let f: f64 =>
+ return have as f64 == f;
+ case let s: str =>
+ return have as str == s;
+ };
+};