hare

[hare] The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

commit d2a731687d2d30f3f9f6b7222ff4f7910305ede9
parent 40f16febd26f41ab6978625f273349d2ea3a0306
Author: Sebastian <sebastian@sebsite.pw>
Date:   Sat, 22 Jul 2023 01:02:08 -0400

hare::parse+test: add and use roundtrip_reparse

For testing things like trailing commas that don't get unparsed by
hare::unparse. 'roundtrip' is still used for most tests, since it
doubles as a nice way to test hare::unparse; 'roundtrip_reparse' is only
used when a second round of parsing is required.

Closes: https://todo.sr.ht/~sircmpwn/hare/373
Signed-off-by: Sebastian <sebastian@sebsite.pw>

Diffstat:
Mhare/parse/+test/expr_test.ha | 7+++++++
Mhare/parse/+test/roundtrip.ha | 23+++++++++++++++++------
Mhare/parse/+test/types.ha | 5+++++
3 files changed, 29 insertions(+), 6 deletions(-)

diff --git a/hare/parse/+test/expr_test.ha b/hare/parse/+test/expr_test.ha @@ -88,6 +88,7 @@ "export fn main() void = test(void, void, void);\n\n" "export fn main() void = test(void, void, void...);\n\n" "export fn main() void = test()()(void);\n"); + roundtrip_reparse("export fn main() void = test(void,);\n"); }; @test fn cast() void = { @@ -162,6 +163,12 @@ '\\'; }; `); + roundtrip_reparse(`export fn main() void = { + struct { x: int = 10, y: int = 20 }; + coords { x: int = 10, y: int = 20 }; + "string " "concatenation"; +}; +`); }; @test fn control() void = { diff --git a/hare/parse/+test/roundtrip.ha b/hare/parse/+test/roundtrip.ha @@ -14,6 +14,22 @@ use strings; use strio; fn roundtrip(src: str) void = { + let unsrc = _roundtrip(src); + defer free(unsrc); + if (unsrc != src) { + fmt::errorfln("=== wanted\n{}", src)!; + fmt::errorfln("=== got\n{}", unsrc)!; + abort(); + }; +}; + +fn roundtrip_reparse(src: str) void = { + let unsrc = _roundtrip(src); + defer free(unsrc); + roundtrip(unsrc); +}; + +fn _roundtrip(src: str) str = { let buf = bufio::fixed(strings::toutf8(src), mode::READ); let lexer = lex::init(&buf, "<test>", lex::flag::COMMENTS); defer lex::finish(&lexer); @@ -29,13 +45,8 @@ fn roundtrip(src: str) void = { }; defer ast::subunit_finish(u); let out = strio::dynamic(); - defer io::close(&out)!; let z = unparse::subunit(&out, u) as size; let unsrc = strio::string(&out); assert(z == len(unsrc)); - if (unsrc != src) { - fmt::errorfln("=== wanted\n{}", src)!; - fmt::errorfln("=== got\n{}", unsrc)!; - abort(); - }; + return unsrc; }; diff --git a/hare/parse/+test/types.ha b/hare/parse/+test/types.ha @@ -21,6 +21,9 @@ export type baz = struct { }, }; "); + roundtrip_reparse("export type foo = struct { x: int, y: int };\n" + "export type bar = union { x: int, y: int };\n" + "export type baz = struct { embedded, struct { x: int, y: int } };\n"); }; @test fn array_slice() void = { @@ -58,6 +61,7 @@ export type baz = enum rune { Q, }; "); + roundtrip_reparse("export type foo = enum { X, Y, Z };\n"); }; @test fn tuple() void = { @@ -68,6 +72,7 @@ export type bar = (a, b::c, d); export type baz = (bat, foo::bar::baz, long_type_name, yet_another_very_long_type_name, this_spans_multiple_lines, for_readability); "); + roundtrip_reparse("export type foo = (int, str,);\n"); }; @test fn tagged_union() void = {