-
Notifications
You must be signed in to change notification settings - Fork 6
/
test
executable file
·278 lines (215 loc) · 6.86 KB
/
test
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#!/usr/bin/env bats
com=./opy
@test "pattern" {
result=$(seq 10 | $com 'F1%2==0' | xargs )
[ "$result" == '2 4 6 8 10' ]
}
@test "list mode" {
result=$(echo 1 2 3 a b c | $com '[ F2, F3*3, F5+"aaa"]')
[ "$result" = "2 9 baaa" ]
}
@test "pattern and list mode" {
result=$(seq 4 | $com 'F1%2==0:[F1, ":even"]' | xargs)
[ "$result" = "2 :even 4 :even" ]
}
@test "module import" {
result=$( echo 3.141592 | $com -m math '[round(math.sin(F1/4),3)]' )
[ "$result" = "0.707" ]
}
@test "module multiple import" {
result=$( $com -m math,numpy 'B:[math.pi,numpy.pi]' )
[ "$result" = "3.141592653589793 3.141592653589793" ]
}
@test "action" {
result=$( seq 10 | $com '{print(F1,end="")}' )
[ "$result" = "12345678910" ]
}
@test "action2" {
result=$( seq 3 | $com '{r=1;print(r)}' | xargs )
[ "$result" = "1 1 1" ]
}
@test "pattern and action" {
result=$( seq 10 | $com 'F1%2==0:{F1= str(F1)+" " ; print(F1,end="")}' )
[ "$result" = "2 4 6 8 10 " ]
}
@test "spaces after the colon" {
result=$( seq 10 | $com 'F1%2==0: {F1= str(F1)+" " ; print(F1,end="")}' )
[ "$result" = "2 4 6 8 10 " ]
}
@test "multiple pattern" {
result=$( seq 10 | $com 'F1%2==0;F1%2==1' | xargs )
[ "$result" = "1 2 3 4 5 6 7 8 9 10" ]
}
@test "multiple lists" {
result=$( seq 2 | $com '[F1];[F1]' | xargs )
[ "$result" = "1 1 2 2" ]
}
@test "multiple actions" {
result=$( seq 2 | $com '{print(F1)};{print(F1)}' | xargs )
[ "$result" = "1 1 2 2" ]
}
@test "multiple pattern/lists" {
result=$( seq 2 | $com 'F1%2==0:[F1, ":even"];F1%2==1:[F1, ":odd"]' | xargs)
[ "$result" = "1 :odd 2 :even" ]
}
@test "multiple pattern/actions" {
result=$( seq 2 | $com 'F1%2==0:{ print(F1, ":even") } ;F1%2==1:{ print(F1, ":odd")}' |xargs)
[ "$result" = "1 :odd 2 :even" ]
}
@test "multiple actions/list actions" {
result=$(echo a | $com 'B:[2]; B:{ print(1) }; {print(3)}; [4]' | xargs)
[ "$result" = "2 1 3 4" ]
}
@test "begin pattern" {
result=$( seq 2 | $com 'B:{a="hoge"};F1%2==1:{ print(F1, a)}' |xargs)
[ "$result" = "1 hoge" ]
}
@test "end pattern" {
result=$( seq 2 | $com 'B:{a=0};{a+=F1};END:{print(a)}' )
[ "$result" = "3" ]
}
@test "close stdin" {
result=$( $com 'B:{print(1+1)}' )
[ "$result" = "2" ]
}
@test "list at begin" {
result=$( $com 'B:[1+1]' )
[ "$result" = "2" ]
}
@test "list at end" {
result=$( seq 2 | $com 'B:{a=0};{a+=F1};END:[a]' )
[ "$result" = "3" ]
}
@test "space" {
result=$( seq 2 | $com 'B:{a=0}; {a+=F1}; END:[a]' )
[ "$result" = "3" ]
}
@test "NF" {
result=$( seq 10 | xargs -n 3 | $com '[NF]' | xargs )
[ "$result" = "3 3 3 1" ]
}
@test "NR" {
result=$( yes | head -n 3 | $com '[NR]' | xargs )
[ "$result" = "1 2 3" ]
}
@test "dynamic module load" {
result=$(echo 10 | $com '[round(math.sin(F1),3)]' )
[ "$result" = "-0.544" ]
}
@test "regex" {
result=$( seq 10 | $com 'r_("[24680]$")' | xargs )
[ "$result" = "2 4 6 8 10" ]
}
@test "field regex" {
result=$( seq 10 | xargs -n 2 | $com 'r_("[48]$",F2)' | xargs )
[ "$result" = "3 4 7 8" ]
}
@test "file and FNR" {
result=$( $com 'FNR==1' ./opy ./test | xargs)
[ "$result" = "#!/usr/bin/env python3 #!/usr/bin/env bats" ]
}
@test "FILENAME" {
result=$( seq 10 | $com 'FNR==1:[FILENAME]' ./opy "-" | xargs)
[ "$result" = "./opy -" ]
}
@test "string mode" {
result=$( seq 10 | $com -s 'F1=="3"' )
[ "$result" = "3" ]
}
@test "begin and end without input" {
result=$( $com 'B:{a,b=1,2};E:[a,b]' )
[ "$result" = "1 2" ]
}
@test "use of dictionary" {
result=$( echo a | $com 'B:{a={}};{a[F1]=F1};E:[a["a"]]' )
[ "$result" = "a" ]
}
@test "scope of F1" {
result=$( $com -m math,numpy 'B:[math.pi,numpy.pi]' )
[ "$result" = "3.141592653589793 3.141592653589793" ]
}
@test "Fn scope in a list" {
result=$( echo 1 | $com '{ print(" ".join([ str(x)*F1 for x in range(3) ])) }' )
[ "$result" = "0 1 2" ]
}
@test "scope problem in a closure" {
result=$(echo 3 | $com '{a=F1;print(" ".join([ str(x)*a for x in range(3) ]))}' )
[ "$result" = "000 111 222" ]
}
@test "general purpose dictionary" {
result=$( seq 3 | $com '{D[NR]="a"*F1};E:[dict(D)]' )
[ "$result" = "{1: 'a', 2: 'aa', 3: 'aaa'}" ]
}
@test "general purpose list" {
result=$( seq 3 | $com '{L.append("a"*F1)};E:[L]' )
[ "$result" = "['a', 'aa', 'aaa']" ]
}
@test "general purpose key values" {
result=$( echo -e 'a 2\nb b\na c\nb 10.0' | $com '{K[F1].append(F2)};E:{p_(K)}' | xargs )
[ "$result" = "a [2, c] b [b, 10.0]" ]
}
@test "print dictionary by p_" {
result=$( seq 3 | $com '{D[NR]="a"*F1};E:{p_(D,"@")}' )
[ "$result" = "1 a@2 aa@3 aaa@" ]
}
@test "print list by p_" {
result=$( seq 3 | $com -o @ '{L.append(F1)};E:{p_(L)}' )
[ "$result" = "1@2@3" ]
}
@test "parse hex and others" {
result=$( echo 0xa0 0b11 -0o11 | $com '[*F[1:]]' )
[ "$result" = "160 3 -9" ]
}
@test "outer values" {
result=$( a=3.14; b="aho aho" ; $com -v "x=$a" -v "y=$b" 'B:[x*2, y*2]' )
[ "$result" = "6.28 aho ahoaho aho" ]
}
@test "outer values on string mode" {
result=$( a=3.14; b="aho aho" ; $com -s -v "x=$a" -v "y=$b" 'B:[x*2, y*2]' )
[ "$result" = "3.143.14 aho ahoaho aho" ]
}
@test "null separator" {
result=$( echo 123 | $com -i "" '[F2*2]' )
[ "$result" = "4" ]
}
@test "null separator on string mode" {
result=$( echo 123 | $com -si "" '[F2*2]' )
[ "$result" = "22" ]
}
@test "csv line mode" {
result=$( echo -ne '1,2,3\n"あ","い"",","う"\n"やや,""こし"",や〜","やや,""こし"",や〜",","\n"もう,"",","いや,"",や"\n' | $com -c -o '|' 'NR' | tr '\n' @ )
[ "$result" = '1|2|3@あ|い",|う@やや,"こし",や〜|やや,"こし",や〜|,@もう,",|いや,",や@' ]
}
@test "csv line mode string" {
result=$( echo -ne '1,2,3' | $com -cs '[F1+F2+F3]')
[ "$result" = '123' ]
}
@test "csv line mode num" {
result=$( echo -ne '1,2,3' | $com -c '[F1+F2+F3]')
[ "$result" = '6' ]
}
@test "csv output mode" {
result=$( echo -ne '1,2,3\n"あ","い"",","う"\n"やや,""こし"",や〜","やや,""こし"",や〜",","\n"もう,"",","いや,"",や"\n' | $com -c -o '|' 4)
result2=$( echo -ne '1,2,3\n"あ","い"",","う"\n"やや,""こし"",や〜","やや,""こし"",や〜",","\n"もう,"",","いや,"",や"\n' | $com -cC 4 | $com -c -o '|' 4)
[ "$result" = "$result2" ]
}
@test "json" {
result=$( echo '{"hoge":["a","b"]}' | $com -t json '[e for e in T["hoge"]]' | tr '\n' @)
[ "$result" = "a@b@" ]
}
@test "yaml" {
result=$( echo -e 'aho:\n boke: ["a","b"]' | $com -t yaml '[*T["aho"]["boke"]]' | tr '\n' @)
[ "$result" = "a@b@" ]
}
@test "xml" {
result=$( echo -e '<?xml version="1.0" encoding="utf-8"?>\n<hoge>aho</hoge>' | $com -t xml '[e.text for e in T.iter("hoge")]')
[ "$result" = "aho" ]
}
@test "-t csv" {
result=$( echo -e 'z\na,b,"c\nd",e,f' | $com -t csv '[T[1][2]]')
[ "$result" = c$'\n'd ]
}
@test "-t xlsx" {
result=$( $com -t xlsx '{a = T["Sheet1"].values};[a[1][0]]' ./testdata/test.xlsx)
[ "$result" = "危険シェル芸" ]
}