hare

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

commit 0ac2780a5a11f54461a4fba5c460582f2e1c555d
parent 48f7a7299b144016d06257942f876f5488bd7bd2
Author: Byron Torres <b@torresjrjr.com>
Date:   Tue,  5 Mar 2024 23:13:51 +0000

test: add HARETEST_INCLUDE, require(); slow tests

Signed-off-by: Byron Torres <b@torresjrjr.com>

Diffstat:
Mcrypto/argon2/+test.ha | 4+---
Mcrypto/sha1/+test.ha | 4+---
Mcrypto/sha256/+test.ha | 4+---
Mtest/util+test.ha | 26++++++++++++++++++++++++++
Mtest/util.ha | 7+++++++
5 files changed, 36 insertions(+), 9 deletions(-)

diff --git a/crypto/argon2/+test.ha b/crypto/argon2/+test.ha @@ -236,9 +236,7 @@ type tcase = struct { }; @test fn samples_slow() void = { - if (os::tryenv("STDLIB_SLOW_TESTS", "") == "") { - test::skip("Enable with STDLIB_SLOW_TESTS=1"); - }; + test::require("slow"); const pass = strings::toutf8("trustno1"); const salt = strings::toutf8("abcdefgh"); diff --git a/crypto/sha1/+test.ha b/crypto/sha1/+test.ha @@ -44,9 +44,7 @@ use test; }; @test fn sha1_1gb() void = { - if (os::tryenv("STDLIB_SLOW_TESTS", "") == "") { - test::skip("Enable with STDLIB_SLOW_TESTS=1"); - }; + test::require("slow"); const input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmno"; const expected = "7789f0c9ef7bfc40d93311143dfbe69e2017f592"; diff --git a/crypto/sha256/+test.ha b/crypto/sha256/+test.ha @@ -41,9 +41,7 @@ use test; }; @test fn sha256_1gb() void = { - if (os::tryenv("STDLIB_SLOW_TESTS", "") == "") { - test::skip("Enable with STDLIB_SLOW_TESTS=1"); - }; + test::require("slow"); let sha = sha256(); const input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmno"; diff --git a/test/util+test.ha b/test/util+test.ha @@ -1,7 +1,10 @@ // SPDX-License-Identifier: MPL-2.0 // (c) Hare authors <https://harelang.org> +use fmt; +use os; use rt; +use strings; let want_abort = false; @@ -25,3 +28,26 @@ export fn skip(reason: str) never = { }; rt::longjmp(&jmpbuf, status::SKIP); }; + +// Check the $HARETEST_INCLUDE space-delimited environment variable for +// keywords. If all the keywords are present, return void. Otherwise, skip the +// currently running test. +export fn require(keywords: str...) void = { + for :keywords (let i = 0z; i < len(keywords); i += 1) { + let keyword = keywords[i]; + let tokr = strings::tokenize(os::tryenv("HARETEST_INCLUDE", ""), " "); + for (true) { + match (strings::next_token(&tokr)) { + case let tok: str => + if (tok == keyword) { + continue :keywords; + }; + case void => + skip(fmt::asprintf( + "Requires HARETEST_INCLUDE='{}'", + strings::join(" ", keywords...), + )); + }; + }; + }; +}; diff --git a/test/util.ha b/test/util.ha @@ -11,3 +11,10 @@ export fn expectabort() void = { export fn skip(reason: str) never = { abort("Attempted to call test::skip outside of @test function"); }; + +// Check the $HARETEST_INCLUDE space-delimited environment variable for +// keywords. If all the keywords are present, return void. Otherwise, skip the +// currently running test. +export fn require(keyword: str) void = { + abort("Attempted to call test::require outside of @test function"); +};