commit 207e0d99d2693f288678b4855b427038db709502
parent 5399f1779bee7067612f09f560cbccf4c387b0c4
Author: Bor Grošelj Simić <bgs@turminal.net>
Date: Fri, 8 Apr 2022 21:36:15 +0200
{strings,bytes}/tokenize.ha: simplify code in @test
Signed-off-by: Bor Grošelj Simić <bgs@turminal.net>
Diffstat:
2 files changed, 27 insertions(+), 128 deletions(-)
diff --git a/bytes/tokenize.ha b/bytes/tokenize.ha
@@ -63,121 +63,48 @@ export fn remaining_tokens(s: *tokenizer) []u8 = {
@test fn tokenize() void = {
const input: [_]u8 = [1, 2, 24, 42, 3, 24, 24, 42, 4, 5];
let t = tokenize(input, [24, 42]);
-
let p = peek_token(&t) as []u8;
let n = next_token(&t) as []u8;
assert(equal(p, n));
assert(equal([1, 2], n));
-
- p = peek_token(&t) as []u8;
- n = next_token(&t) as []u8;
+ let p = peek_token(&t) as []u8;
+ let n = next_token(&t) as []u8;
assert(equal(p, n));
assert(equal([3, 24], n));
-
assert(equal(peek_token(&t) as []u8, peek_token(&t) as []u8));
- match (next_token(&t)) {
- case let b: []u8 =>
- assert(equal([4, 5], b));
- case void =>
- abort();
- };
-
+ assert(equal([4, 5], next_token(&t) as []u8));
assert(peek_token(&t) is void);
assert(next_token(&t) is void);
- const input2: [_]u8 = [24, 42, 1, 24, 42];
- t = tokenize(input2, [24, 42]);
-
+ const input: [_]u8 = [24, 42, 1, 24, 42];
+ t = tokenize(input, [24, 42]);
assert(equal(peek_token(&t) as []u8, peek_token(&t) as []u8));
- match (next_token(&t)) {
- case let b: []u8 =>
- assert(equal([], b));
- case void =>
- abort();
- };
-
+ assert(equal([], next_token(&t) as []u8));
assert(equal(peek_token(&t) as []u8, peek_token(&t) as []u8));
- match (next_token(&t)) {
- case let b: []u8 =>
- assert(equal([1], b));
- case void =>
- abort();
- };
-
+ assert(equal([1], next_token(&t) as []u8));
assert(equal(peek_token(&t) as []u8, peek_token(&t) as []u8));
- match (next_token(&t)) {
- case let b: []u8 =>
- assert(equal([], b));
- case void =>
- abort();
- };
-
+ assert(equal([], next_token(&t) as []u8));
assert(peek_token(&t) is void);
assert(next_token(&t) is void);
- const input3: [_]u8 = [1, 1, 1, 2, 1, 1, 2, 2];
- t = tokenize(input3, [1, 2]);
-
- match (next_token(&t)) {
- case let b: []u8 =>
- assert(equal([1, 1], b));
- case void =>
- abort();
- };
-
- match (next_token(&t)) {
- case let b: []u8 =>
- assert(equal([1], b));
- case void =>
- abort();
- };
-
- match (next_token(&t)) {
- case let b: []u8 =>
- assert(equal([2], b));
- case void =>
- abort();
- };
-
+ const input: [_]u8 = [1, 1, 1, 2, 1, 1, 2, 2];
+ t = tokenize(input, [1, 2]);
+ assert(equal([1, 1], next_token(&t) as []u8));
+ assert(equal([1], next_token(&t) as []u8));
+ assert(equal([2], next_token(&t) as []u8));
assert(next_token(&t) is void);
- const input4: [_]u8 = [1, 2];
- t = tokenize(input4, [1, 2]);
-
- match (next_token(&t)) {
- case let b: []u8 =>
- assert(equal([], b));
- case void =>
- abort();
- };
-
- match (next_token(&t)) {
- case let b: []u8 =>
- assert(equal([], b));
- case void =>
- abort();
- };
-
+ const input: [_]u8 = [1, 2];
+ t = tokenize(input, [1, 2]);
+ assert(equal([], next_token(&t) as []u8));
+ assert(equal([], next_token(&t) as []u8));
assert(peek_token(&t) is void);
assert(next_token(&t) is void);
- const input5: [_]u8 = [24, 42, 1, 24, 42, 2, 3, 4];
- t = tokenize(input5, [24, 42]);
-
- match (next_token(&t)) {
- case let b: []u8 =>
- assert(equal([], b));
- case void =>
- abort();
- };
-
- match (next_token(&t)) {
- case let b: []u8 =>
- assert(equal([1], b));
- case void =>
- abort();
- };
-
+ const input: [_]u8 = [24, 42, 1, 24, 42, 2, 3, 4];
+ t = tokenize(input, [24, 42]);
+ assert(equal([], next_token(&t) as []u8));
+ assert(equal([1], next_token(&t) as []u8));
assert(equal(remaining_tokens(&t), [2, 3, 4]));
assert(equal(peek_token(&t) as []u8, [2, 3, 4]));
assert(equal(remaining_tokens(&t), [2, 3, 4]));
diff --git a/strings/tokenize.ha b/strings/tokenize.ha
@@ -46,51 +46,23 @@ export fn remaining_tokens(s: *tokenizer) str = {
@test fn tokenize() void = {
let tok = tokenize("Hello, my name is drew", " ");
- match (next_token(&tok)) {
- case let s: str =>
- assert(s == "Hello,");
- case void =>
- abort();
- };
-
- match (next_token(&tok)) {
- case let s: str =>
- assert(s == "my");
- case void =>
- abort();
- };
-
- match (peek_token(&tok)) {
- case let s: str =>
- assert(s == "name");
- case void =>
- abort();
- };
-
-
- match (next_token(&tok)) {
- case let s: str =>
- assert(s == "name");
- case void =>
- abort();
- };
-
+ assert(next_token(&tok) as str == "Hello,");
+ assert(next_token(&tok) as str == "my");
+ assert(peek_token(&tok) as str == "name");
+ assert(next_token(&tok) as str == "name");
assert(remaining_tokens(&tok) == "is drew");
assert(peek_token(&tok) as str == "is");
assert(remaining_tokens(&tok) == "is drew");
- tok = tokenize("foo", "foo");
-
+ let tok = tokenize("foo", "foo");
assert(peek_token(&tok) as str == "");
assert(next_token(&tok) as str == "");
-
assert(peek_token(&tok) as str == "");
assert(next_token(&tok) as str == "");
-
assert(peek_token(&tok) is void);
assert(next_token(&tok) is void);
- tok = tokenize("", "foo");
+ let tok = tokenize("", "foo");
assert(peek_token(&tok) is void);
assert(next_token(&tok) is void);
};