hare

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

commit 09e5967363f98cf07ddb8555e9b54a6a5cb28ba0
parent 577fafa83ce9f075efcf5c7ee53a162b9ca30d85
Author: Alexey Yerin <yyp@disroot.org>
Date:   Thu, 14 Oct 2021 23:13:45 +0300

types: add tests for reflection helpers

Signed-off-by: Alexey Yerin <yyp@disroot.org>

Diffstat:
Mscripts/gen-stdlib | 14++++++++++++--
Mstdlib.mk | 3++-
Atypes/util+test.ha | 49+++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 63 insertions(+), 3 deletions(-)

diff --git a/scripts/gen-stdlib b/scripts/gen-stdlib @@ -820,13 +820,23 @@ temp() { gen_ssa temp crypto::random encoding::hex fs io os path strio fmt } -types() { +gensrcs_types() { gen_srcs types \ limits.ha \ classes.ha \ reflect.ha \ util.ha \ - 'arch$(ARCH).ha' + 'arch$(ARCH).ha' \ + $* +} + +types() { + if [ $testing -eq 0 ]; then + gensrcs_types + else + gensrcs_types \ + util+test.ha + fi gen_ssa types } diff --git a/stdlib.mk b/stdlib.mk @@ -2401,7 +2401,8 @@ testlib_types_srcs= \ $(STDLIB)/types/classes.ha \ $(STDLIB)/types/reflect.ha \ $(STDLIB)/types/util.ha \ - $(STDLIB)/types/arch$(ARCH).ha + $(STDLIB)/types/arch$(ARCH).ha \ + $(STDLIB)/types/util+test.ha $(TESTCACHE)/types/types.ssa: $(testlib_types_srcs) $(testlib_rt) @printf 'HAREC \t$@\n' diff --git a/types/util+test.ha b/types/util+test.ha @@ -0,0 +1,49 @@ +type language = enum { + C, + HARE, + PYTHON, + MYRDDIN, +}; + +@test fn strenum() void = { + const l = language::C; + assert(strenum(type(language), &l) == "C"); + + const l = language::HARE; + assert(strenum(type(language), &l) == "HARE"); + + const l = language::PYTHON; + assert(strenum(type(language), &l) == "PYTHON"); + + const l = language::MYRDDIN; + assert(strenum(type(language), &l) == "MYRDDIN"); +}; + +type perm = enum u8 { + NONE = 0, + READ = 1 << 0, + WRITE = 1 << 1, + EXECUTE = 1 << 2, +}; + +@test fn strflag() void = { + const perm = perm::READ; + const res = strflag(type(perm), &perm); + defer free(res); + assert(res == "READ"); + + const perm = perm::READ | perm::WRITE; + const res = strflag(type(perm), &perm); + defer free(res); + assert(res == "READ|WRITE"); + + const perm = perm::WRITE | perm::EXECUTE; + const res = strflag(type(perm), &perm); + defer free(res); + assert(res == "WRITE|EXECUTE"); + + const perm = perm::NONE; + const res = strflag(type(perm), &perm); + defer free(res); + assert(res == "NONE"); +};