File: util%2Btest.ha

package info (click to toggle)
hare 0.26.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,352 kB
  • sloc: asm: 1,374; makefile: 123; sh: 117; lisp: 101
file content (65 lines) | stat: -rw-r--r-- 1,581 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// SPDX-License-Identifier: MPL-2.0
// (c) Hare authors <https://harelang.org>

use fmt;
use os;
use rt;
use strings;

let want_abort = false;

// Expect the currently running test to abort. The test will fail if it doesn't
// abort.
export fn expectabort() void = {
	if (jmp == null) {
		abort("Attempted to call test::expectabort outside of @test function");
	};
	want_abort = true;
};

// Skip the currently running test.
export fn skip(why: str) never = {
	if (jmp == null) {
		abort("Attempted to call test::skip outside of @test function");
	};
	reason = abort_reason {
		msg = why,
		...
	};
	rt::longjmp(&jmp_buf, 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 keyword .. keywords) {
		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 done =>
				let joined = strings::join(" ", keywords...)!;
				let msg = fmt::asprintf(
					"Requires HARETEST_INCLUDE='{}'", joined)!;
				free(joined);
				skip(msg);
			};
		};
	};
};

let _current: nullable *test = null;

// Returns the name of the currently running test.
export fn current() str = {
	match (_current) {
	case let t: *test =>
		return t.name;
	case null =>
		abort("Attempted to call test::current outside of @test function");
	};
};