File: %2Btest.ha

package info (click to toggle)
hare 0.25.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,948 kB
  • sloc: asm: 1,264; makefile: 123; sh: 114; lisp: 101
file content (53 lines) | stat: -rw-r--r-- 1,186 bytes parent folder | download | duplicates (2)
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
// SPDX-License-Identifier: MIT
// (c) Hare authors <https://harelang.org>

use os;
use strings;

fn streq(a: []str, b: []str) bool = {
	if (len(a) != len(b)) {
		return false;
	};
	for (let i = 0z; i < len(a); i += 1) {
		if (a[i] != b[i]) {
			return false;
		};
	};
	return true;
};

@test fn wordexp() void = {
	os::setenv("TESTVAR", "test value")!;
	os::unsetenv("UNSET")!;
	static const cases: [_](str, []str) = [
		(``, []),
		(" \t\n", []),
		(`hello world`, ["hello", "world"]),
		(`hello $TESTVAR`, ["hello", "test", "value"]),
		(`hello "$TESTVAR"`, ["hello", "test value"]),
		(`hello $(echo world)`, ["hello", "world"]),
		(`hello $((2+2))`, ["hello", "4"]),
		(`hello $UNSET`, ["hello"]),
	];

	for (let i = 0z; i < len(cases); i += 1) {
		const (in, out) = cases[i];
		const words = wordexp(in)!;
		defer strings::freeall(words);
		assert(streq(words, out));
	};
};

@test fn wordexp_error() void = {
	os::unsetenv("UNSET")!;
	static const cases: [_](str, flag) = [
		(`hello $UNSET`, flag::UNDEF),
		(`hello $(`, 0),
	];

	for (let i = 0z; i < len(cases); i += 1) {
		const (in, flag) = cases[i];
		const result = wordexp(in, flag);
		assert(result is sh_error);
	};
};