~sircmpwn/hare

ref: 732383144a66c247b6c732b1ce7f46342d340019 hare/shlex/+test.ha -rw-r--r-- 1.9 KiB
73238314Alexey Yerin cmd/harec: display error even if it includes invalid UTF-8 4 months ago
                                                                                
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
66
67
68
69
70
71
72
73
74
75
76
// License: MPL-2.0
// (c) 2021 Alexey Yerin <yyp@disroot.org>
use strings;

@test fn split() void = {
	const s = split(`hello\ world`)!;
	defer strings::freeall(s);
	assert(len(s) == 1);
	assert(s[0] == "hello world");

	const s = split(`'hello\ world'`)!;
	defer strings::freeall(s);
	assert(len(s) == 1);
	assert(s[0] == `hello\ world`);

	const s = split(`"hello\\world"`)!;
	defer strings::freeall(s);
	assert(len(s) == 1);
	assert(s[0] == `hello\world`);

	const s = split(`"hello "'"'"world"'"'`)!;
	defer strings::freeall(s);
	assert(len(s) == 1);
	assert(s[0] == `hello "world"`);

	const s = split("hello '' world")!;
	defer strings::freeall(s);
	assert(len(s) == 3);
	assert(s[0] == "hello");
	assert(s[1] == "");
	assert(s[2] == "world");

	const s = split("Empty ''")!;
	defer strings::freeall(s);
	assert(len(s) == 2);
	assert(s[0] == "Empty");
	assert(s[1] == "");

	const s = split("   Leading spaces")!;
	defer strings::freeall(s);
	assert(len(s) == 2);
	assert(s[0] == "Leading");
	assert(s[1] == "spaces");

	const s = split(`with\ backslashes 'single quoted' "double quoted"`)!;
	defer strings::freeall(s);
	assert(len(s) == 3);
	assert(s[0] == "with backslashes");
	assert(s[1] == "single quoted");
	assert(s[2] == "double quoted");

	const s = split("'multiple spaces'      42")!;
	defer strings::freeall(s);
	assert(len(s) == 2);
	assert(s[0] == "multiple spaces");
	assert(s[1] == "42");

	// Invalid
	assert(split(`"dangling double quote`) is syntaxerr);
	assert(split("'dangling single quote") is syntaxerr);
	assert(split(`unterminated\ backslash \`) is syntaxerr);
};

@test fn quote() void = {
	const bare = quotestr(`hello`);
	defer free(bare);
	assert(bare == `hello`);

	const spaces = quotestr(`hello world`);
	defer free(spaces);
	assert(spaces == `"hello world"`);

	const quotes = quotestr(`'hello' "world"`);
	defer free(quotes);
	assert(quotes == `"'hello' "'"'"world"'"'""`);
};