| 1 |
// SPDX-License-Identifier: MPL-2.0 |
| 2 |
// (c) Hare authors <https://harelang.org> |
| 3 |
|
| 4 |
use fmt; |
| 5 |
use os; |
| 6 |
use rt; |
| 7 |
use strings; |
| 8 |
|
| 9 |
let want_abort = false; |
| 10 |
|
| 11 |
// Expect the currently running test to abort. The test will fail if it doesn't |
| 12 |
// abort. |
| 13 |
export fn expectabort() void = { |
| 14 |
if (jmp == null) { |
| 15 |
abort("Attempted to call test::expectabort outside of @test function"); |
| 16 |
}; |
| 17 |
want_abort = true; |
| 18 |
}; |
| 19 |
|
| 20 |
// Skip the currently running test. |
| 21 |
export fn skip(why: str) never = { |
| 22 |
if (jmp == null) { |
| 23 |
abort("Attempted to call test::skip outside of @test function"); |
| 24 |
}; |
| 25 |
reason = abort_reason { |
| 26 |
msg = why, |
| 27 |
... |
| 28 |
}; |
| 29 |
rt::longjmp(&jmp_buf, status::SKIP); |
| 30 |
}; |
| 31 |
|
| 32 |
// Check the $HARETEST_INCLUDE space-delimited environment variable for |
| 33 |
// keywords. If all the keywords are present, return void. Otherwise, skip the |
| 34 |
// currently running test. |
| 35 |
export fn require(keywords: str...) void = { |
| 36 |
for :keywords (let keyword .. keywords) { |
| 37 |
let tokr = strings::tokenize(os::tryenv("HARETEST_INCLUDE", ""), " "); |
| 38 |
for (true) { |
| 39 |
match (strings::next_token(&tokr)) { |
| 40 |
case let tok: str => |
| 41 |
if (tok == keyword) { |
| 42 |
continue :keywords; |
| 43 |
}; |
| 44 |
case done => |
| 45 |
let joined = strings::join(" ", keywords...)!; |
| 46 |
let msg = fmt::asprintf( |
| 47 |
"Requires HARETEST_INCLUDE='{}'", joined)!; |
| 48 |
free(joined); |
| 49 |
skip(msg); |
| 50 |
}; |
| 51 |
}; |
| 52 |
}; |
| 53 |
}; |
| 54 |
|
| 55 |
let _current: nullable *test = null; |
| 56 |
|
| 57 |
// Returns the name of the currently running test. |
| 58 |
export fn current() str = { |
| 59 |
match (_current) { |
| 60 |
case let t: *test => |
| 61 |
return t.name; |
| 62 |
case null => |
| 63 |
abort("Attempted to call test::current outside of @test function"); |
| 64 |
}; |
| 65 |
}; |