hare

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

+test.ha (1186B)


      1 // SPDX-License-Identifier: MIT
      2 // (c) Hare authors <https://harelang.org>
      3 
      4 use os;
      5 use strings;
      6 
      7 fn streq(a: []str, b: []str) bool = {
      8 	if (len(a) != len(b)) {
      9 		return false;
     10 	};
     11 	for (let i = 0z; i < len(a); i += 1) {
     12 		if (a[i] != b[i]) {
     13 			return false;
     14 		};
     15 	};
     16 	return true;
     17 };
     18 
     19 @test fn wordexp() void = {
     20 	os::setenv("TESTVAR", "test value")!;
     21 	os::unsetenv("UNSET")!;
     22 	static const cases: [_](str, []str) = [
     23 		(``, []),
     24 		(" \t\n", []),
     25 		(`hello world`, ["hello", "world"]),
     26 		(`hello $TESTVAR`, ["hello", "test", "value"]),
     27 		(`hello "$TESTVAR"`, ["hello", "test value"]),
     28 		(`hello $(echo world)`, ["hello", "world"]),
     29 		(`hello $((2+2))`, ["hello", "4"]),
     30 		(`hello $UNSET`, ["hello"]),
     31 	];
     32 
     33 	for (let i = 0z; i < len(cases); i += 1) {
     34 		const (in, out) = cases[i];
     35 		const words = wordexp(in)!;
     36 		defer strings::freeall(words);
     37 		assert(streq(words, out));
     38 	};
     39 };
     40 
     41 @test fn wordexp_error() void = {
     42 	os::unsetenv("UNSET")!;
     43 	static const cases: [_](str, flag) = [
     44 		(`hello $UNSET`, flag::UNDEF),
     45 		(`hello $(`, 0),
     46 	];
     47 
     48 	for (let i = 0z; i < len(cases); i += 1) {
     49 		const (in, flag) = cases[i];
     50 		const result = wordexp(in, flag);
     51 		assert(result is sh_error);
     52 	};
     53 };