hare

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

commit 79352cc1dd744ff4464427eecc93ddbf018e2c72
parent 7b9b1e8ed87e03f05d80d6498081a6fe5b7931b4
Author: Drew DeVault <sir@cmpwn.com>
Date:   Sun, 22 Aug 2021 12:30:09 +0200

all: fix tests

Whoops!

Signed-off-by: Drew DeVault <sir@cmpwn.com>

Diffstat:
Mcmd/haredoc/html.ha | 17+++++++++++++++++
Mhare/types/+test.ha | 79+++++++++++++++++++++++++++++++++++++++----------------------------------------
Mhare/types/hash.ha | 30+++++++++++++++---------------
Mhare/unit/process.ha | 10+++++-----
Mhare/unparse/decl.ha | 6+++---
Mhare/unparse/type.ha | 32++++++++++++++++----------------
6 files changed, 95 insertions(+), 79 deletions(-)

diff --git a/cmd/haredoc/html.ha b/cmd/haredoc/html.ha @@ -31,6 +31,23 @@ fn html_escape(out: *io::stream, in: str) (size | io::error) = { return z; }; +@test fn html_escape() void = { + let sink = strio::dynamic(); + defer io::close(sink); + html_escape(sink, "hello world!")!; + assert(strio::string(sink) == "hello world!"); + + let sink = strio::dynamic(); + defer io::close(sink); + html_escape(sink, "\"hello world!\"")!; + assert(strio::string(sink) == "&quot;hello world!&quot;"); + + let sink = strio::dynamic(); + defer io::close(sink); + html_escape(sink, "<hello & 'world'!>")!; + assert(strio::string(sink) == "&lt;hello &amp; &apos;world&apos;!&gt;"); +}; + // Formats output as HTML fn emit_html(ctx: *context) (void | error) = { const decls = ctx.summary; diff --git a/hare/types/+test.ha b/hare/types/+test.ha @@ -21,8 +21,7 @@ fn parse_type(in: str) ast::_type = { let atype = parse_type("int"); defer ast::type_free(atype); let htype = lookup(st, &atype)!; - assert(htype._type is builtin); - assert(htype._type as builtin == builtin::INT); + assert(htype.repr as builtin == builtin::INT); assert(htype.sz == x86_64._int && htype.align == x86_64._int); let type2 = lookup(st, &atype)!; @@ -32,8 +31,8 @@ fn parse_type(in: str) ast::_type = { defer ast::type_free(atype); let htype = lookup(st, &atype)!; assert(htype.sz == x86_64._pointer && htype.align == x86_64._pointer); - let htype = htype._type as pointer; - assert(htype.referent._type as builtin == builtin::INT); + let htype = htype.repr as pointer; + assert(htype.referent.repr as builtin == builtin::INT); }; fn resolve( @@ -58,19 +57,19 @@ fn resolve( let htype = lookup(st, &atype)!; assert(htype.sz == 8); assert(htype.align == 4); - let stype = htype._type as _struct; + let stype = htype.repr as _struct; assert(stype.kind == struct_union::STRUCT); assert(len(stype.fields) == 2); let x = stype.fields[0]; assert(x.name == "x"); assert(x.offs == 0); - assert(x._type._type as builtin == builtin::INT); + assert(x._type.repr as builtin == builtin::INT); let y = stype.fields[1]; assert(y.name == "y"); assert(y.offs == 4); - assert(y._type._type as builtin == builtin::INT); + assert(y._type.repr as builtin == builtin::INT); // Basic union let atype = parse_type("union { x: int, y: int }"); @@ -78,19 +77,19 @@ fn resolve( let htype = lookup(st, &atype)!; assert(htype.sz == 4); assert(htype.align == 4); - let stype = htype._type as _struct; + let stype = htype.repr as _struct; assert(stype.kind == struct_union::UNION); assert(len(stype.fields) == 2); let x = stype.fields[0]; assert(x.name == "x"); assert(x.offs == 0); - assert(x._type._type as builtin == builtin::INT); + assert(x._type.repr as builtin == builtin::INT); let y = stype.fields[1]; assert(y.name == "y"); assert(y.offs == 0); - assert(y._type._type as builtin == builtin::INT); + assert(y._type.repr as builtin == builtin::INT); // Padding let atype = parse_type("struct { w: u8, x: u32, y: u8, z: u64 }"); @@ -98,7 +97,7 @@ fn resolve( let htype = lookup(st, &atype)!; assert(htype.sz == 24); assert(htype.align == 8); - let stype = htype._type as _struct; + let stype = htype.repr as _struct; assert(stype.kind == struct_union::STRUCT); let w = stype.fields[0]; @@ -119,7 +118,7 @@ fn resolve( let atype = parse_type("struct { z: u8, y: u8, x: u8, q: u8 }"); defer ast::type_free(atype); let htype = lookup(st, &atype)!; - let stype = htype._type as _struct; + let stype = htype.repr as _struct; assert(stype.fields[0].name == "q"); assert(stype.fields[1].name == "x"); assert(stype.fields[2].name == "y"); @@ -139,7 +138,7 @@ fn resolve( let htype = lookup(st, &atype)!; assert(htype.sz == 20); assert(htype.align == 4); - let stype = htype._type as _struct; + let stype = htype.repr as _struct; assert(stype.fields[0].name == "p"); assert(stype.fields[0].offs == 16); assert(stype.fields[1].name == "q"); @@ -165,7 +164,7 @@ fn resolve( let htype = lookup(st, &atype)!; assert(htype.sz == 16); assert(htype.align == 4); - let stype = htype._type as _struct; + let stype = htype.repr as _struct; assert(stype.fields[0].name == "p"); assert(stype.fields[0].offs == 12); assert(stype.fields[1].name == "q"); @@ -193,7 +192,7 @@ fn resolve( let htype = lookup(st, &atype)!; assert(htype.sz == 36); assert(htype.align == 4); - let stype = htype._type as _struct; + let stype = htype.repr as _struct; assert(stype.fields[0].name == "x"); assert(stype.fields[0].offs == 8); assert(stype.fields[1].name == "y"); @@ -212,13 +211,13 @@ fn resolve( let htype = lookup(st, &atype)!; assert(htype.sz == 8); assert(htype.align == 4); - let tup = htype._type as tuple; + let tup = htype.repr as tuple; assert(len(tup) == 2); assert(tup[0].offs == 0); - assert(tup[0]._type._type as builtin == builtin::INT); + assert(tup[0]._type.repr as builtin == builtin::INT); assert(tup[1].offs == 4); - assert(tup[1]._type._type as builtin == builtin::INT); + assert(tup[1]._type.repr as builtin == builtin::INT); // Padding let atype = parse_type("(i8, i32, i8, i64)"); @@ -227,7 +226,7 @@ fn resolve( assert(htype.sz == 24); assert(htype.align == 8); - let tup = htype._type as tuple; + let tup = htype.repr as tuple; assert(tup[0].offs == 0); assert(tup[1].offs == 4); assert(tup[2].offs == 8); @@ -244,8 +243,8 @@ fn resolve( let htype = lookup(st, &atype)!; assert(htype.sz == 24); assert(htype.align == 8); - let slice = htype._type as slice; - assert(slice._type as builtin == builtin::INT); + let slice = htype.repr as slice; + assert(slice.repr as builtin == builtin::INT); // Normal array let atype = parse_type("[5]i32"); @@ -253,8 +252,8 @@ fn resolve( let htype = lookup(st, &atype)!; assert(htype.sz == 4 * 5); assert(htype.align == 4); - let array = htype._type as array; - assert(array.member._type as builtin == builtin::I32); + let array = htype.repr as array; + assert(array.member.repr as builtin == builtin::I32); assert(array.length == 5); // Unbounded array @@ -263,8 +262,8 @@ fn resolve( let htype = lookup(st, &atype)!; assert(htype.sz == SIZE_UNDEFINED); assert(htype.align == 4); - let array = htype._type as array; - assert(array.member._type as builtin == builtin::I32); + let array = htype.repr as array; + assert(array.member.repr as builtin == builtin::I32); assert(array.length == SIZE_UNDEFINED); // Contextual array (equivalent to unbounded at this compilation stage) @@ -273,8 +272,8 @@ fn resolve( let htype = lookup(st, &atype)!; assert(htype.sz == SIZE_UNDEFINED); assert(htype.align == 4); - let array = htype._type as array; - assert(array.member._type as builtin == builtin::I32); + let array = htype.repr as array; + assert(array.member.repr as builtin == builtin::I32); assert(array.length == SIZE_UNDEFINED); }; @@ -287,8 +286,8 @@ fn resolve( let htype = lookup(st, &atype)!; assert(htype.sz == SIZE_UNDEFINED); assert(htype.align == SIZE_UNDEFINED); - let f = htype._type as func; - assert(f.result._type as builtin == builtin::VOID); + let f = htype.repr as func; + assert(f.result.repr as builtin == builtin::VOID); assert(f.variadism == variadism::NONE); assert(f.flags == func_flags::NORETURN); assert(len(f.params) == 0); @@ -298,13 +297,13 @@ fn resolve( let htype = lookup(st, &atype)!; assert(htype.sz == SIZE_UNDEFINED); assert(htype.align == SIZE_UNDEFINED); - let f = htype._type as func; - assert(f.result._type as builtin == builtin::INT); + let f = htype.repr as func; + assert(f.result.repr as builtin == builtin::INT); assert(f.variadism == variadism::HARE); assert(f.flags == 0); assert(len(f.params) == 2); - assert(f.params[0]._type as builtin == builtin::INT); - assert(f.params[1]._type as builtin == builtin::STR); + assert(f.params[0].repr as builtin == builtin::INT); + assert(f.params[1].repr as builtin == builtin::STR); }; @test fn tagged() void = { @@ -316,19 +315,19 @@ fn resolve( let htype = lookup(st, &atype)!; assert(htype.sz == st.arch._int * 2); assert(htype.align == st.arch._int); - let t = htype._type as tagged; + let t = htype.repr as tagged; assert(len(t) == 2); - assert(t[0]._type as builtin == builtin::INT); - assert(t[1]._type as builtin == builtin::VOID); + assert(t[0].repr as builtin == builtin::INT); + assert(t[1].repr as builtin == builtin::VOID); let atype = parse_type("(int | (int | str | void))"); defer ast::type_free(atype); let htype = lookup(st, &atype)!; assert(htype.sz == 32); assert(htype.align == 8); - let t = htype._type as tagged; + let t = htype.repr as tagged; assert(len(t) == 3); - assert(t[0]._type as builtin == builtin::INT); - assert(t[1]._type as builtin == builtin::VOID); - assert(t[2]._type as builtin == builtin::STR); + assert(t[0].repr as builtin == builtin::INT); + assert(t[1].repr as builtin == builtin::VOID); + assert(t[2].repr as builtin == builtin::STR); }; diff --git a/hare/types/hash.ha b/hare/types/hash.ha @@ -129,13 +129,13 @@ export fn hash(t: *_type) u32 = { // Test a few samples for ABI compatibility with harec let sample = _type { flags = flags::NONE, - _type = builtin::STR, + repr = builtin::STR, }; assert(hash(&sample) == 3350498318); let sample = _type { flags = flags::NONE, - _type = alias { + repr = alias { id = ["foo", "bar"], ... }, @@ -144,7 +144,7 @@ export fn hash(t: *_type) u32 = { let sample = _type { flags = flags::ERROR, - _type = alias { + repr = alias { id = ["foo", "bar"], ... }, @@ -153,13 +153,13 @@ export fn hash(t: *_type) u32 = { let _int = _type { flags = flags::NONE, - _type = builtin::INT, + repr = builtin::INT, }; assert(hash(&_int) == 1737287038); let sample = _type { flags = flags::NONE, - _type = pointer { + repr = pointer { referent = &_int, flags = pointer_flags::NONE, }, @@ -168,7 +168,7 @@ export fn hash(t: *_type) u32 = { let sample = _type { flags = flags::NONE, - _type = pointer { + repr = pointer { referent = &_int, flags = pointer_flags::NULLABLE, }, @@ -177,7 +177,7 @@ export fn hash(t: *_type) u32 = { let sample = _type { flags = flags::NONE, - _type = _struct { + repr = _struct { kind = struct_union::STRUCT, fields = [ struct_field { @@ -197,7 +197,7 @@ export fn hash(t: *_type) u32 = { let sample = _type { flags = flags::NONE, - _type = [ + repr = [ tuple_value { offs = 0, _type = &_int, @@ -216,7 +216,7 @@ export fn hash(t: *_type) u32 = { let sample = _type { flags = flags::NONE, - _type = array { + repr = array { length = 5, member = &_int, }, @@ -225,16 +225,16 @@ export fn hash(t: *_type) u32 = { let _uint = _type { flags = flags::NONE, - _type = builtin::UINT, + repr = builtin::UINT, }; let _void = _type { flags = flags::NONE, - _type = builtin::VOID, + repr = builtin::VOID, }; let sample = _type { flags = flags::NONE, - _type = [ + repr = [ &_int, &_uint, &_void, @@ -244,7 +244,7 @@ export fn hash(t: *_type) u32 = { let sample = _type { flags = flags::NONE, - _type = _enum { + repr = _enum { storage = builtin::INT, values = [ ("FOO", 0), @@ -257,7 +257,7 @@ export fn hash(t: *_type) u32 = { let sample = _type { flags = flags::NONE, - _type = func { + repr = func { result = &_void, variadism = variadism::C, flags = func_flags::NORETURN, @@ -268,7 +268,7 @@ export fn hash(t: *_type) u32 = { let sample = _type { flags = flags::NONE, - _type = &_void: slice, + repr = &_void: slice, }; assert(hash(&sample) == 3143795781); }; diff --git a/hare/unit/process.ha b/hare/unit/process.ha @@ -150,28 +150,28 @@ fn process_constant(ctx: *context, aexpr: *ast::expr) (*expr | error) = { const aexpr = parse_expr("void"); defer ast::expr_free(aexpr); const expr = process_constant(&ctx, aexpr)!; - assert(expr.result._type as types::builtin == types::builtin::VOID); + assert(expr.result.repr as types::builtin == types::builtin::VOID); const constexpr = expr.expr as constant_expr; assert(constexpr is void); const aexpr = parse_expr("true"); defer ast::expr_free(aexpr); const expr = process_constant(&ctx, aexpr)!; - assert(expr.result._type as types::builtin == types::builtin::BOOL); + assert(expr.result.repr as types::builtin == types::builtin::BOOL); const constexpr = expr.expr as constant_expr; assert(constexpr as bool == true); const aexpr = parse_expr("false"); defer ast::expr_free(aexpr); const expr = process_constant(&ctx, aexpr)!; - assert(expr.result._type as types::builtin == types::builtin::BOOL); + assert(expr.result.repr as types::builtin == types::builtin::BOOL); const constexpr = expr.expr as constant_expr; assert(constexpr as bool == false); const aexpr = parse_expr("null"); defer ast::expr_free(aexpr); const expr = process_constant(&ctx, aexpr)!; - assert(expr.result._type as types::builtin == types::builtin::NULL); + assert(expr.result.repr as types::builtin == types::builtin::NULL); assert(expr.expr is constant_expr); const cases: [_](str, types::builtin, lex::value) = [ @@ -186,7 +186,7 @@ fn process_constant(ctx: *context, aexpr: *ast::expr) (*expr | error) = { const aexpr = parse_expr(case.0); defer ast::expr_free(aexpr); const expr = process_constant(&ctx, aexpr)!; - assert(expr.result._type as types::builtin == case.1); + assert(expr.result.repr as types::builtin == case.1); const constexpr = expr.expr as constant_expr; const lv = constexpr as lex::value; match (case.2) { diff --git a/hare/unparse/decl.ha b/hare/unparse/decl.ha @@ -107,13 +107,13 @@ fn decl_test(d: ast::decl, expected: str) bool = { start = loc, end = loc, flags = 0, - _type = ast::builtin_type::INT, + repr = ast::builtin_type::INT, }; let type_fn = ast::_type { start = loc, end = loc, flags = ast::type_flags::CONST, - _type = ast::func_type { + repr = ast::func_type { result = &type_int, attrs = ast::func_attrs::NORETURN, variadism = ast::variadism::HARE, @@ -191,7 +191,7 @@ fn decl_test(d: ast::decl, expected: str) bool = { }; assert(decl_test(d, "@fini @noreturn @symbol(\"foo\") fn foo(foo: int, bar: int...) int;")); - type_fn._type = ast::func_type { + type_fn.repr = ast::func_type { result = &type_int, attrs = 0, variadism = ast::variadism::NONE, diff --git a/hare/unparse/type.ha b/hare/unparse/type.ha @@ -219,7 +219,7 @@ fn type_test(t: ast::_type, expected: str) bool = { start = loc, end = loc, flags = ast::type_flags::CONST, - _type = ast::alias_type { + repr = ast::alias_type { unwrap = false, ident = ["foo", "bar"], }, @@ -228,7 +228,7 @@ fn type_test(t: ast::_type, expected: str) bool = { start = loc, end = loc, flags = 0, - _type = ast::builtin_type::INT, + repr = ast::builtin_type::INT, }; let expr_void = ast::expr { start = lex::location { ... }, @@ -238,18 +238,18 @@ fn type_test(t: ast::_type, expected: str) bool = { assert(type_test(t, "const foo::bar")); t.flags = 0; - t._type = ast::alias_type { + t.repr = ast::alias_type { unwrap = true, ident = ["baz"], }; assert(type_test(t, "...baz")); t.flags = ast::type_flags::ERROR; - t._type = ast::builtin_type::INT; + t.repr = ast::builtin_type::INT; assert(type_test(t, "int!")); t.flags = ast::type_flags::CONST | ast::type_flags::ERROR; - t._type = ast::enum_type { + t.repr = ast::enum_type { storage = ast::builtin_type::U32, values = [ ast::enum_field { @@ -266,14 +266,14 @@ fn type_test(t: ast::_type, expected: str) bool = { t.flags = 0; - t._type = ast::func_type { + t.repr = ast::func_type { result = &type_int, attrs = 0, variadism = ast::variadism::NONE, params = [], }; assert(type_test(t, "fn() int")); - t._type = ast::func_type { + t.repr = ast::func_type { result = &type_int, attrs = ast::func_attrs::NORETURN, variadism = ast::variadism::C, @@ -286,7 +286,7 @@ fn type_test(t: ast::_type, expected: str) bool = { ], }; assert(type_test(t, "@noreturn fn(_: int, ...) int")); - t._type = ast::func_type { + t.repr = ast::func_type { result = &type_int, attrs = 0, variadism = ast::variadism::HARE, @@ -309,41 +309,41 @@ fn type_test(t: ast::_type, expected: str) bool = { assert(type_test(t, "fn(foo: int, bar: int...) int")); t.flags = 0; - t._type = ast::list_type { + t.repr = ast::list_type { length = ast::len_slice, members = &type_int, }; assert(type_test(t, "[]int")); - t._type = ast::list_type { + t.repr = ast::list_type { length = ast::len_unbounded, members = &type_int, }; assert(type_test(t, "[*]int")); - t._type = ast::list_type { + t.repr = ast::list_type { length = ast::len_contextual, members = &type_int, }; assert(type_test(t, "[_]int")); - t._type = ast::list_type { + t.repr = ast::list_type { length = &expr_void, members = &type_int, }; assert(type_test(t, "[void]int")); - t._type = ast::pointer_type { + t.repr = ast::pointer_type { referent = &type_int, flags = 0, }; assert(type_test(t, "*int")); - t._type = ast::pointer_type { + t.repr = ast::pointer_type { referent = &type_int, flags = ast::pointer_flags::NULLABLE, }; assert(type_test(t, "nullable *int")); - t._type = [&type_int, &type_int]: ast::tagged_type; + t.repr = [&type_int, &type_int]: ast::tagged_type; assert(type_test(t, "(int | int)")); - t._type = [&type_int, &type_int]: ast::tuple_type; + t.repr = [&type_int, &type_int]: ast::tuple_type; assert(type_test(t, "(int, int)")); };