cut: code shrink
[busybox-git.git] / testsuite / cut.tests
blob21cfea809feca761ea252aac03cc879ed4e20b4c
1 #!/bin/sh
3 # Copyright 2007 by Denys Vlasenko <vda.linux@googlemail.com>
4 # Licensed under GPLv2, see file LICENSE in this source tree.
6 . ./testing.sh
8 # testing "test name" "options" "expected result" "file input" "stdin"
9 # file input will be file called "input"
10 # test can create a file "actual" instead of writing to stdout
12 testing "cut '-' (stdin) and multi file handling" \
13 "cut -d' ' -f2 - input" \
14 "over\n""quick\n" \
15 "the quick brown fox\n" \
16 "jumps over the lazy dog\n" \
18 abc="\
19 one:two:three:four:five:six:seven
20 alpha:beta:gamma:delta:epsilon:zeta:eta:theta:iota:kappa:lambda:mu
21 the quick brown fox jumps over the lazy dog
24 testing "cut -b a,a,a" "cut -b 3,3,3 input" "e\np\ne\n" "$abc" ""
26 testing "cut -b overlaps" \
27 "cut -b 1-3,2-5,7-9,9-10 input" \
29 one:to:th
30 alphabeta
31 the qick \n" \
32 "$abc" ""
33 testing "-b encapsulated" \
34 "cut -b 3-8,4-6 input" \
36 e:two:
37 pha:be
38 e quic\n" \
39 "$abc" ""
40 optional LONG_OPTS
41 testing "cut -b --output-delimiter overlaps" \
42 "cut --output-delimiter='^' -b 1-3,2-5,7-9,9-10 input" \
44 one:t^o:th
45 alpha^beta
46 the q^ick \n" \
47 "$abc" ""
48 SKIP=
50 testing "cut high-low error" "cut -b 8-3 input 2>/dev/null || echo err" "err\n" \
51 "$abc" ""
53 testing "cut -b 2-1 error" "cut -b 2-1 input 2>/dev/null || echo err" "err\n" \
54 "$abc" ""
56 testing "cut -c a-b" "cut -c 4-10 input" ":two:th\nha:beta\n quick \n" "$abc" ""
57 testing "cut -c a-" "cut -c 41- input" "\ntheta:iota:kappa:lambda:mu\ndog\n" "$abc" ""
58 testing "cut -c -b" "cut -c -39 input" \
59 "one:two:three:four:five:six:seven\nalpha:beta:gamma:delta:epsilon:zeta:eta\nthe quick brown fox jumps over the lazy\n" \
60 "$abc" ""
61 testing "cut -c a" "cut -c 40 input" "\n:\n \n" "$abc" ""
62 testing "cut -c a,b-c,d" "cut -c 3,5-7,10 input" "etwoh\npa:ba\nequi \n" "$abc" ""
64 testing "cut -f a-" "cut -d ':' -f 5- input" "five:six:seven\nepsilon:zeta:eta:theta:iota:kappa:lambda:mu\nthe quick brown fox jumps over the lazy dog\n" "$abc" ""
66 testing "cut show whole line with no delim" "cut -d ' ' -f 3 input" \
67 "one:two:three:four:five:six:seven\nalpha:beta:gamma:delta:epsilon:zeta:eta:theta:iota:kappa:lambda:mu\nbrown\n" "$abc" ""
69 testing "cut with echo, -c (a-b)" "echo 'ref_categorie=test' | cut -c 1-15 " "ref_categorie=t\n" "" ""
70 testing "cut with echo, -c (a)" "echo 'ref_categorie=test' | cut -c 14" "=\n" "" ""
72 testing "cut with -c (a,b,c)" "cut -c 4,5,20 input" "det\n" "abcdefghijklmnopqrstuvwxyz" ""
74 testing "cut with -b (a,b,c)" "cut -b 4,5,20 input" "det\n" "abcdefghijklmnopqrstuvwxyz" ""
76 input="\
77 406378:Sales:Itorre:Jan
78 031762:Marketing:Nasium:Jim
79 636496:Research:Ancholie:Mel
80 396082:Sales:Jucacion:Ed
82 testing "cut with -d -f(:) -s" "cut -d: -f3 -s input" "Itorre\nNasium\nAncholie\nJucacion\n" "$input" ""
83 testing "cut with -d -f( ) -s" "cut -d' ' -f3 -s input && echo yes" "yes\n" "$input" ""
84 testing "cut with -d -f(a) -s" "cut -da -f3 -s input" "n\nsium:Jim\n\ncion:Ed\n" "$input" ""
85 testing "cut with -d -f(a) -s -n" "cut -da -f3 -s -n input" "n\nsium:Jim\n\ncion:Ed\n" "$input" ""
87 input="\
89 foo bar baz
91 bing bong boop
94 testing "cut with -d -s omits blank lines" "cut -d' ' -f2 -s input" "bar\nbong\n" "$input" ""
96 # substitute for awk
97 optional FEATURE_CUT_REGEX
98 testing "cut -DF unordered" "cut -DF 2,7,5" \
100 said and your
102 is demand. supply
103 forecast :
104 you you better,
106 Em: Took hate
107 " "" \
108 "Bother, said Pooh. It's your husband, and he has a gun.
109 Cheerios are donut seeds.
110 Talk is cheap because supply exceeds demand.
111 Weather forecast for tonight : dark.
112 Apple: you can buy better, but you can't pay more.
113 Subcalifragilisticexpialidocious.
114 Auntie Em: Hate you, hate Kansas. Took the dog. Dorothy."
116 # No delimiter found: print entire line regardless of -F RANGES
117 testing "cut -F1" "cut -d: -F1" \
118 "the_only_field\n" "" \
119 "the_only_field\n"
120 testing "cut -F2" "cut -d: -F2" \
121 "the_only_field\n" "" \
122 "the_only_field\n"
123 # No delimiter found and -s: skip entire line
124 testing "cut -sF1" "cut -d: -sF1" \
125 "" "" \
126 "the_only_field\n"
127 #^^^ the above is probably mishandled by toybox, it prints the line
128 testing "cut -sF2" "cut -d: -sF2" \
129 "" "" \
130 "the_only_field\n"
131 # -D disables special handling of lines with no delimiters, the line is treated as the 1st field
132 testing "cut -DF1" "cut -d: -DF1" \
133 "the_only_field\n" "" \
134 "the_only_field\n"
135 testing "cut -DF2" "cut -d: -DF2" \
136 "\n" "" \
137 "the_only_field\n"
139 optional FEATURE_CUT_REGEX LONG_OPTS
140 testing "cut -F preserves intermediate delimiters" \
141 "cut --output-delimiter=: -F2,4-6,7" \
142 "2:4 5 6:7\n" \
143 "" "1 2 3 4\t\t5 6 7 8\n"
144 SKIP=
146 optional LONG_OPTS
147 testing "cut -f does not preserve intermediate delimiters" \
148 "cut --output-delimiter=: -d' ' -f2,4-6,7" \
149 "2:4:5:6:7\n" \
150 "" "1 2 3 4 5 6 7 8\n"
151 SKIP=
153 testing "cut empty field" "cut -d ':' -f 1-3" \
154 "a::b\n" \
155 "" "a::b\n"
156 testing "cut empty field 2" "cut -d ':' -f 3-5" \
157 "b::c\n" \
158 "" "a::b::c:d\n"
159 testing "cut non-existing field" "cut -d ':' -f1,3" \
160 "1\n" \
161 "" "1:\n"
163 # cut -d$'\n' has a special meaning: "select input lines".
164 # I didn't find any documentation for this feature.
165 testing "cut -dNEWLINE" \
166 "cut -d'
167 ' -f4,2,6-8" \
168 "2\n4\n6\n7\n" \
169 "" "1\n2\n3\n4\n5\n6\n7"
171 optional LONG_OPTS
172 testing "cut -dNEWLINE --output-delimiter" \
173 "cut -d'
174 ' --output-delimiter=@@ -f4,2,6-8" \
175 "2@@4@@6@@7\n" \
176 "" "1\n2\n3\n4\n5\n6\n7"
178 testing "cut -dNEWLINE --output-delimiter 2" \
179 "cut -d'
180 ' --output-delimiter=@@ -f4,2,6-8" \
181 "2@@4@@6@@7\n" \
182 "" "1\n2\n3\n4\n5\n6\n7\n"
184 testing "cut -dNEWLINE --output-delimiter EMPTY_INPUT" \
185 "cut -d'
186 ' --output-delimiter=@@ -f4,2,6-8" \
187 "" \
188 "" ""
189 SKIP=
191 # This seems to work as if delimiter is never found.
192 # We test here that -d '' does *not* operate as if there was no -d
193 # and delimiter has defaulted to TAB:
194 testing "cut -d EMPTY" \
195 "cut -d '' -f2-" \
196 "1 2\t3 4 5\n" \
197 "" "1 2\t3 4 5\n"
198 testing "cut -d EMPTY -s" \
199 "cut -d '' -f2- -s" \
200 "" \
201 "" "1 2\t3 4 5\n"
203 exit $FAILCOUNT