~sircmpwn/hare

ref: c80c71f920b0e94370338341fef35b7f6fd4f07c hare/shlex/+test.ha -rw-r--r-- 2.0 KiB
c80c71f9Sebastian shlex: return number of bytes written for quote 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
77
78
// License: MPL-2.0
// (c) 2021 Alexey Yerin <yyp@disroot.org>
use io;
use strings;
use strio;

@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);
};

fn testquote(sink: *strio::stream, s: str, expected: str) void = {
	assert(quote(sink, s)! == len(expected));
	assert(strio::string(sink) == expected);
	strio::reset(sink);
};

@test fn quote() void = {
	const sink = strio::dynamic();
	defer io::close(&sink)!;
	testquote(&sink, `hello`, `hello`);
	testquote(&sink, `hello world`, `"hello world"`);
	testquote(&sink, `'hello' "world"`, `"'hello' "'"'"world"'"'""`);
};