annotate src/test/kotlin/name/blackcap/passman/ShplitterTest.kt @ 25:131e39d96862

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