hare

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

util+test.ha (1281B)


      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 (rt::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(reason: str) never = {
     22 	if (rt::jmp == null) {
     23 		abort("Attempted to call test::skip outside of @test function");
     24 	};
     25 	rt::reason = rt::abort_reason {
     26 		msg = reason,
     27 		...
     28 	};
     29 	rt::longjmp(&jmpbuf, 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 				skip(fmt::asprintf(
     46 					"Requires HARETEST_INCLUDE='{}'",
     47 					strings::join(" ", keywords...),
     48 				));
     49 			};
     50 		};
     51 	};
     52 };