Mercurial > cgi-bin > hgweb.cgi > PassMan
comparison src/test/kotlin/name/blackcap/passman/ShplitterTest.kt @ 19:7d80cbcb67bb
add shlex-style splitter and tests
author | David Barts <n5jrn@me.com> |
---|---|
date | Sun, 30 Jun 2024 20:37:36 -0700 |
parents | |
children | 4391afcf6bd0 |
comparison
equal
deleted
inserted
replaced
18:8f3ddebb4295 | 19:7d80cbcb67bb |
---|---|
1 package name.blackcap.passman | |
2 | |
3 import org.junit.jupiter.api.Assertions.assertFalse | |
4 import org.junit.jupiter.api.Assertions.assertTrue | |
5 import org.junit.jupiter.api.Test | |
6 | |
7 class ShplitterTest { | |
8 private lateinit var fixture: Shplitter | |
9 | |
10 @Test | |
11 fun complete() { | |
12 val CASES = arrayOf<String>( | |
13 "unquoted", | |
14 "\"double quoted\"", | |
15 "'single quoted'", | |
16 "back\\ slash", | |
17 "in\\terior back slash", | |
18 "\"first\" word double quoted", | |
19 "last word double \"quoted\"", | |
20 "'first' word single quoted", | |
21 "last word single 'quoted'") | |
22 | |
23 for (case in CASES) { | |
24 val fixture = Shplitter() | |
25 fixture.feed(case) | |
26 assertTrue(fixture.complete, "should be complete but is not: ${case}") | |
27 } | |
28 } | |
29 | |
30 @Test | |
31 fun incomplete() { | |
32 val CASES = arrayOf<String>( | |
33 "ends with backslash\\", | |
34 "unclosed double \"quote", | |
35 "ends with double quote\"", | |
36 "unclosed single 'quote", | |
37 "ends with single quote'" | |
38 ) | |
39 for (case in CASES) { | |
40 val fixture = Shplitter() | |
41 fixture.feed(case) | |
42 assertFalse(fixture.complete, "should not be complete but is: ${case}") | |
43 } | |
44 } | |
45 | |
46 // Test cases cribbed from Python shlex source. | |
47 data class TestDatum(val from: String, val to: Array<String>) | |
48 val TEST_DATA_1 = arrayOf<TestDatum>( | |
49 TestDatum("foo bar", arrayOf<String>("foo", "bar")), | |
50 TestDatum(" foo bar", arrayOf<String>("foo", "bar")), | |
51 TestDatum(" foo bar ", arrayOf<String>("foo", "bar")), | |
52 TestDatum("foo\tbar\tbla\tfasel", arrayOf<String>("foo", "bar", "bla", "fasel")), | |
53 TestDatum("x y z\t\txxxx", arrayOf<String>("x", "y", "z", "xxxx")), | |
54 TestDatum("\\x bar", arrayOf<String>("x", "bar")), | |
55 TestDatum("\\ x bar", arrayOf<String>(" x", "bar")), | |
56 TestDatum("\\ bar", arrayOf<String>(" bar")), | |
57 TestDatum("foo \\x bar", arrayOf<String>("foo", "x", "bar")), | |
58 TestDatum("foo \\ x bar", arrayOf<String>("foo", " x", "bar")), | |
59 TestDatum("foo \\ bar", arrayOf<String>("foo", " bar")), | |
60 TestDatum("foo \"bar\" bla", arrayOf<String>("foo", "bar", "bla")), | |
61 TestDatum("\"foo\" \"bar\" \"bla\"", arrayOf<String>("foo", "bar", "bla")), | |
62 TestDatum("\"foo\" bar \"bla\"", arrayOf<String>("foo", "bar", "bla")), | |
63 TestDatum("\"foo\" bar bla", arrayOf<String>("foo", "bar", "bla")), | |
64 TestDatum("foo 'bar' bla", arrayOf<String>("foo", "bar", "bla")), | |
65 TestDatum("'foo' 'bar' 'bla'", arrayOf<String>("foo", "bar", "bla")), | |
66 TestDatum("'foo' bar 'bla'", arrayOf<String>("foo", "bar", "bla")), | |
67 TestDatum("'foo' bar bla", arrayOf<String>("foo", "bar", "bla")), | |
68 TestDatum("blurb foo\"bar\"bar\"fasel\" baz", arrayOf<String>("blurb", "foobarbarfasel", "baz")), | |
69 TestDatum("blurb foo'bar'bar'fasel' baz", arrayOf<String>("blurb", "foobarbarfasel", "baz")), | |
70 TestDatum("\"\"", arrayOf<String>("")), | |
71 TestDatum("''", arrayOf<String>("")), | |
72 TestDatum("foo \"\" bar", arrayOf<String>("foo", "", "bar")), | |
73 TestDatum("foo '' bar", arrayOf<String>("foo", "", "bar")), | |
74 TestDatum("foo \"\" \"\" \"\" bar", arrayOf<String>("foo", "", "", "", "bar")), | |
75 TestDatum("foo '' '' '' bar", arrayOf<String>("foo", "", "", "", "bar")), | |
76 TestDatum("\"foo\\ bar\"", arrayOf<String>("foo bar"))); | |
77 val TEST_DATA_2 = arrayOf<TestDatum>( | |
78 TestDatum("\"foo\\\\ bar\"", arrayOf<String>("foo\\ bar")), | |
79 TestDatum("\"foo\\\\ bar\\\\\"", arrayOf<String>("foo\\ bar\\")), | |
80 TestDatum("\"foo\\\\\" bar\\\"", arrayOf<String>("foo\\", "bar\"")), | |
81 TestDatum("\"foo\\\\ bar\" dfadf", arrayOf<String>("foo\\ bar", "dfadf")), | |
82 TestDatum("\"foo\\\\\\ bar\\\" dfadf\"", arrayOf<String>("foo\\ bar\" dfadf")), | |
83 TestDatum("\"foo\\\\\\x bar\" dfadf", arrayOf<String>("foo\\x bar", "dfadf")), | |
84 TestDatum("\"foo\\x bar\\\" dfadf\"", arrayOf<String>("foox bar\" dfadf")), | |
85 TestDatum("\"foo\\x\"", arrayOf<String>("foox")), | |
86 TestDatum("\"foo\\ \"", arrayOf<String>("foo ")), | |
87 TestDatum("foo\\ xx", arrayOf<String>("foo xx")), | |
88 TestDatum("foo\\ x\\x", arrayOf<String>("foo xx")), | |
89 TestDatum("foo\\ x\\x\\\"\"\"", arrayOf<String>("foo xx\"")), | |
90 TestDatum("\"foo\\ x\\x\"", arrayOf<String>("foo xx")), | |
91 TestDatum("\"foo\\ x\\x\\\\\"", arrayOf<String>("foo xx\\")), | |
92 TestDatum("\"foo\\ x\\x\\\\\"\"foobar\"", arrayOf<String>("foo xx\\foobar")), | |
93 TestDatum("\"foo\\ x\\x\\\\\"\\'\"foobar\"", arrayOf<String>("foo xx\\'foobar")), | |
94 TestDatum("\"foo\\ x\\x\\\\\"\\'\"fo'obar\" 'don'\\''t'", arrayOf<String>("foo xx\\'foobar\" don\\t")), | |
95 TestDatum("'foo\\ bar'", arrayOf<String>("foo\\ bar")), | |
96 TestDatum("'foo\\\\ bar'", arrayOf<String>("foo\\\\ bar")), | |
97 TestDatum("foo\\ bar", arrayOf<String>("foo bar")), | |
98 TestDatum("foo#bar\\nbaz", arrayOf<String>("foo#barnbaz")), | |
99 TestDatum(":-) ;-)", arrayOf<String>(":-)", ":-)")), | |
100 TestDatum("áéíóú", arrayOf<String>("áéíóú")) | |
101 ) | |
102 | |
103 fun runArray(testData: Array<TestDatum>) { | |
104 for (testDatum in testData) { | |
105 val s = Shplitter() | |
106 s.feed(testDatum.from) | |
107 assertTrue(s.complete, "${testDatum.from}: should be complete, is not") | |
108 val split = s.split().toList() | |
109 val expecting = testDatum.to.asList() | |
110 assertTrue(split == expecting, "${testDatum.from}: expected ${expecting}, got $split") | |
111 } | |
112 } | |
113 | |
114 @Test | |
115 fun split() { | |
116 runArray(TEST_DATA_1) | |
117 runArray(TEST_DATA_2) | |
118 } | |
119 } |