+test.ha (2104B)
1 // SPDX-License-Identifier: MPL-2.0 2 // (c) Hare authors <https://harelang.org> 3 4 use io; 5 use memio; 6 use strings; 7 8 @test fn split() void = { 9 const s = split(`hello\ world`)!; 10 defer strings::freeall(s); 11 assert(len(s) == 1); 12 assert(s[0] == "hello world"); 13 14 const s = split(`'hello\ world'`)!; 15 defer strings::freeall(s); 16 assert(len(s) == 1); 17 assert(s[0] == `hello\ world`); 18 19 const s = split(`"hello\\world"`)!; 20 defer strings::freeall(s); 21 assert(len(s) == 1); 22 assert(s[0] == `hello\world`); 23 24 const s = split(`"hello "'"'"world"'"'`)!; 25 defer strings::freeall(s); 26 assert(len(s) == 1); 27 assert(s[0] == `hello "world"`); 28 29 const s = split("hello '' world")!; 30 defer strings::freeall(s); 31 assert(len(s) == 3); 32 assert(s[0] == "hello"); 33 assert(s[1] == ""); 34 assert(s[2] == "world"); 35 36 const s = split("Empty ''")!; 37 defer strings::freeall(s); 38 assert(len(s) == 2); 39 assert(s[0] == "Empty"); 40 assert(s[1] == ""); 41 42 const s = split(" Leading spaces")!; 43 defer strings::freeall(s); 44 assert(len(s) == 2); 45 assert(s[0] == "Leading"); 46 assert(s[1] == "spaces"); 47 48 const s = split(`with\ backslashes 'single quoted' "double quoted"`)!; 49 defer strings::freeall(s); 50 assert(len(s) == 3); 51 assert(s[0] == "with backslashes"); 52 assert(s[1] == "single quoted"); 53 assert(s[2] == "double quoted"); 54 55 const s = split("'multiple spaces' 42")!; 56 defer strings::freeall(s); 57 assert(len(s) == 2); 58 assert(s[0] == "multiple spaces"); 59 assert(s[1] == "42"); 60 61 // Invalid 62 assert(split(`"dangling double quote`) is syntaxerr); 63 assert(split("'dangling single quote") is syntaxerr); 64 assert(split(`unterminated\ backslash \`) is syntaxerr); 65 }; 66 67 fn testquote(sink: *memio::stream, s: str, expected: str) void = { 68 assert(quote(sink, s)! == len(expected)); 69 assert(memio::string(sink)! == expected); 70 memio::reset(sink); 71 }; 72 73 @test fn quote() void = { 74 const sink = memio::dynamic(); 75 defer io::close(&sink)!; 76 testquote(&sink, `hello`, `hello`); 77 testquote(&sink, `hello world`, `'hello world'`); 78 testquote(&sink, `'hello' "world"`, `''"'"'hello'"'"' "world"'`); 79 testquote(&sink, `hello\world`, `'hello\world'`); 80 };