-
Notifications
You must be signed in to change notification settings - Fork 14
/
tests.tcl
executable file
·1665 lines (1518 loc) · 49.7 KB
/
tests.tcl
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#! /usr/bin/env tclsh
# Sqawk, an SQL awk.
# Copyright (c) 2015-2018, 2020, 2024 D. Bohdan
# License: MIT
package require fileutil
package require struct
package require tcltest
namespace eval ::sqawk::tests {
variable path [file dirname [file dirname [file normalize $::argv0/___]]]
variable cleanupVars {}
proc make-temp-file content {
set filename [tcltest::makeFile {} [::fileutil::tempfile]]
::fileutil::writeFile $filename $content
return $filename
}
proc init args {
cd $::sqawk::tests::path
set ::sqawk::tests::cleanupVars {}
foreach {varName content} $args {
upvar 1 $varName f
set f [make-temp-file $content]
lappend ::sqawk::tests::cleanupVars $varName
}
}
proc uninit {} {
foreach varName $::sqawk::tests::cleanupVars {
upvar 1 $varName f
tcltest::removeFile $f
unset f
}
}
# Run Sqawk with {*}$args.
proc sqawk-tcl args {
exec [info nameofexecutable] sqawk.tcl {*}$args
}
# Return 1 if $version in the format of X.Y.Z.W is newer than $reference and
# 0 otherwise.
proc newer-or-equal {version reference} {
foreach a [split $version .] b [split $reference .] {
if {$a > $b} {
return 1
} elseif {$a < $b} {
return 0
}
}
return 1
}
# Set the constraints.
set sqliteVersion [sqawk-tcl {select sqlite_version()} << {}]
tcltest::testConstraint printfInSqlite3 \
[newer-or-equal $sqliteVersion 3.8.3]
tcltest::testConstraint jimsh [expr {
![catch { exec jimsh << {} }]
}]
tcltest::testConstraint utf8 [expr {
[encoding system] eq {utf-8}
}]
tcltest::testConstraint sqlite3cli [expr {
![catch { exec sqlite3 -version }]
}]
tcltest::test error-handling-1.1 {Handle broken pipe, read from stdin} \
-setup {
init filename "line 1\nline 2\nline 3" \
script {
fconfigure stdin -buffering line
puts [gets stdin]
exit 0
}
} -body {
sqawk-tcl {select a0 from a} < $filename \
| [info nameofexecutable] $script
} -cleanup {
uninit
} -result {line 1}
tcltest::test error-handling-2.1 {Fail on bad query} -setup {
init
} -body {
sqawk-tcl -1 asdf sqawk-tcl
} -cleanup {
uninit
} -returnCodes 1 -match regexp -result {\s+}
tcltest::test error-handling-2.2 {Fail on missing file} -setup {
init
} -body {
sqawk-tcl -1 {select a0 from a} missing-file
} -cleanup {
uninit
} -returnCodes 1 -match glob -result {*can't find file missing-file*}
proc difference {filename1 filename2} {
set lines1 [split [::fileutil::cat $filename1] \n]
set lines2 [split [::fileutil::cat $filename2] \n]
set lcs [::struct::list longestCommonSubsequence $lines1 $lines2]
return [::struct::list lcsInvert $lcs [llength $lines1] \
[llength $lines2]]
}
tcltest::test fs-1.1 {Global custom field separator} -setup {
init
} -body {
sqawk-tcl -FS , {
select a1, a2 from a
} << a,b\nc,d\ne,f\n
} -cleanup {
uninit
} -result "a b\nc d\ne f"
tcltest::test fs-1.2 {Global custom field separator} -setup {
init
} -body {
sqawk-tcl -FS @ {
select a1, a2 from a
} << a@b\nc@d\ne@f\n
} -cleanup {
uninit
} -result "a b\nc d\ne f"
tcltest::test fs-1.3 {Global custom field separator} -setup {
init
} -body {
sqawk-tcl -FS \\| {
select distinct a1 as title,a2 as artist from a
} << "Yama Yama|Yamasuki\n"
} -cleanup {
uninit
} -result "Yama Yama Yamasuki"
tcltest::test fs-2.1 {Option -1} -setup {
init
} -body {
sqawk-tcl -1 -OFS , {
select a1, a2 from a
} << "a b\nc d\ne f\n"
} -cleanup {
uninit
} -result "a b,\nc d,\ne f,"
tcltest::test fs-3.1 {Bad custom field separator regexp} -setup {
init
} -body {
source -encoding utf-8 sqawk.tcl
set parser [::sqawk::parsers::awk::parser %AUTO% dummy {
FS | RS \n fields auto trim none
}]
} -cleanup {
uninit
} -returnCodes {
error
} -result {Error in constructor:\
splitting on FS regexp | would cause infinite loop}
tcltest::test join-1.1 {JOIN on two files from examples/hp/} -setup {
init filename {}
} -body {
sqawk-tcl {
select a1, b1, a2 from a inner join b on a2 = b2
where b1 < 10000 order by b1
} examples/hp/MD5SUMS examples/hp/du-bytes > $filename
difference examples/hp/results.correct $filename
} -cleanup {
uninit
} -result {}
tcltest::test join-2.1 {JOIN on files from examples/three-files/,\
FS setting} -setup {
init filename {}
} -body {
set dir examples/three-files/
sqawk-tcl -FS , {
select a1, a2, b2, c2 from a inner join b on a1 = b1
inner join c on a1 = c1
} $dir/1 FS=_ FS=, $dir/2 $dir/3 > $filename
unset dir
difference examples/three-files/results.correct $filename
} -cleanup {
uninit
} -result {}
tcltest::test table-1.1 {Custom table names} -setup {
init filename1 "foo 1\nfoo 2\nfoo 3" \
filename2 "bar 4\nbar 5\nbar 6"
} -body {
sqawk-tcl {
select foo2 from foo; select b2 from b
} table=foo $filename1 $filename2
} -cleanup {
uninit
} -result "1\n2\n3\n4\n5\n6"
tcltest::test table-1.2 {Custom table names and prefixes} -setup {
init filename1 "foo 1\nfoo 2\nfoo 3" \
filename2 "bar 4\nbar 5\nbar 6"
} -body {
sqawk-tcl {
select foo.x2 from foo; select baz2 from bar
} table=foo prefix=x $filename1 table=bar prefix=baz $filename2
} -cleanup {
uninit
} -result "1\n2\n3\n4\n5\n6"
tcltest::test table-1.3 {Use the same table for several files} -setup {
init filename1 "a\nb\nc" \
filename2 "x\ny" \
filename3 "z"
} -body {
sqawk-tcl {
select anr, a1 from a
} $filename1 table=a $filename2 table=a $filename3
} -cleanup {
uninit
} -result "1 a\n2 b\n3 c\n4 x\n5 y\n6 z"
tcltest::test header-1.1 {Header row} -setup {
set content {}
append content "name\tposition\toffice\tphone\n"
append content "Smith\tCEO\t10\t555-1234\n"
append content "James\tHead of marketing\t11\t555-1235\n"
append content "McDonald\tDeveloper\t12\t555-1236\tGood at tables\n"
init filename $content
unset content
} -body {
sqawk-tcl {
select name, office from staff
where position = "CEO"
or staff.phone = "555-1234"
or staff.a5 = "Good at tables"
} FS=\t table=staff prefix=a header=1 $filename
} -cleanup {
uninit
} -result "Smith 10\nMcDonald 12"
tcltest::test header-1.2 {Header row with spaces} -setup {
set content {}
append content "id,a column with a long name,\"even worse - quotes!\"\n"
append content "1,foo,!\n"
append content "2,bar,%\n"
append content "3,baz,$\n"
init filename $content
unset content
} -body {
sqawk-tcl {
select "a column with a long name" from a;
select `"even worse - quotes!"` from a
} FS=, header=1 $filename
} -cleanup {
uninit
} -result "foo\nbar\nbaz\n!\n%\n$"
tcltest::test header-3.1 {"columns" per-file option} -setup {
init header3File "001 a\n002 b\n003 c\n"
} -body {
sqawk-tcl {select hello, a2 from a} columns=hello $header3File
} -cleanup {
uninit
} -result "1 a\n2 b\n3 c"
tcltest::test header-3.2 {"columns" per-file option} -setup {
init header3File "001 a\n002 b\n003 c\n"
} -body {
sqawk-tcl {select a1, a2 from a} columns=,,world $header3File
} -cleanup {
uninit
} -result "1 a\n2 b\n3 c"
tcltest::test header-3.3 {"columns" per-file option} -setup {
init header3File "001 a\n002 b\n003 c\n"
} -body {
sqawk-tcl {select "hello world" from a} {columns=hello world} \
$header3File
} -cleanup {
uninit
} -result 1\n2\n3
tcltest::test header-3.4 {"columns" per-file option} -setup {
init header3File "001 a\n002 b\n003 c\n"
} -body {
sqawk-tcl {select world from a} columns=hello,world $header3File
} -cleanup {
uninit
} -result a\nb\nc
tcltest::test header-3.5 {"columns" per-file option} -setup {
init header3File "001 a\n002 b\n003 c\n"
} -body {
sqawk-tcl {select world from a} columns=hello,world,of,tables \
$header3File
} -cleanup {
uninit
} -result a\nb\nc \
tcltest::test header-3.6 {"columns" per-file option} -setup {
init header3File "001 a\n002 b\n003 c\n"
} -body {
sqawk-tcl {select hello from a} header=1 columns=hello,world \
$header3File
} -cleanup {
uninit
} -result 2\n3
tcltest::test header-3.7 {"columns" per-file option} -setup {
init header3File "001 a\n002 b\n003 c\n"
} -body {
sqawk-tcl {select hello, a from a} header=1 columns=hello \
$header3File
} -cleanup {
uninit
} -result "2 b\n3 c"
tcltest::test header-3.8 {"columns" per-file option} -setup {
init header3File "001 a\n002 b\n003 c\n"
} -body {
sqawk-tcl {select a from a} header=1 columns= $header3File
} -cleanup {
uninit
} -result b\nc
tcltest::test field-mapping-1.3 {::sqawk::parsers::awk::parseFieldMap} \
-setup {
init
} -body {
source -encoding utf-8 sqawk.tcl
set result {}
lappend result [::sqawk::parsers::awk::parseFieldMap auto]
lappend result [::sqawk::parsers::awk::parseFieldMap 1,2]
lappend result [::sqawk::parsers::awk::parseFieldMap 1,1-2,3,5-end]
} -cleanup {
unset result
uninit
} -result [list \
auto \
{{1 1} {2 2}} \
{{1 1} {1 2} {3 3} {5 end}} \
]
tcltest::test field-mapping-2.1 {::sqawk::parsers::awk::map} -setup {
init
} -body {
source -encoding utf-8 sqawk.tcl
set result {}
lappend result [::sqawk::parsers::awk::map \
{start AB foo AB bar {}} {{1 99}}]
lappend result [::sqawk::parsers::awk::map \
{start AB foo AB bar {}} {{1 end}}]
lappend result [::sqawk::parsers::awk::map \
{start AB foo AB bar {}} {{1 1}}]
lappend result [::sqawk::parsers::awk::map \
{start AB foo AB bar {}} {{1 2}}]
lappend result [::sqawk::parsers::awk::map \
{start AB foo AB bar {}} {{4 5}}]
lappend result [::sqawk::parsers::awk::map \
{start AB foo AB bar {}} {{1 1} {2 2} {3 3}}]
lappend result [::sqawk::parsers::awk::map \
{start AB foo AB bar {}} {{1 1} {2 2} {3 end}}]
lappend result [::sqawk::parsers::awk::map \
{start AB foo AB bar {}} {{1 1} {2 3} {3 3}}]
} -cleanup {
unset result
uninit
} -result [list \
startABfooABbar \
startABfooABbar \
start \
startABfoo \
{{}} \
{start foo bar} \
{start foo bar} \
{start fooABbar bar} \
]
tcltest::test field-mapping-2.2 {::sqawk::parsers::awk::map} -setup {
init
} -body {
source -encoding utf-8 sqawk.tcl
set result {}
lappend result [::sqawk::parsers::awk::map \
{start AB foo AB bar AB} {{1 99}}]
lappend result [::sqawk::parsers::awk::map \
{start AB foo AB bar AB} {{1 end}}]
lappend result [::sqawk::parsers::awk::map \
{start AB foo AB bar AB} {{1 1}}]
lappend result [::sqawk::parsers::awk::map \
{start AB foo AB bar AB} {{1 2}}]
lappend result [::sqawk::parsers::awk::map \
{start AB foo AB bar AB} {{4 5}}]
lappend result [::sqawk::parsers::awk::map \
{start AB foo AB bar AB} {{1 1} {2 2} {3 3}}]
lappend result [::sqawk::parsers::awk::map \
{start AB foo AB bar AB} {{1 1} {2 2} {3 end}}]
lappend result [::sqawk::parsers::awk::map \
{start AB foo AB bar AB} {{1 1} {2 3} {3 3}}]
} -cleanup {
unset result
uninit
} -result [list \
startABfooABbarAB \
startABfooABbarAB \
start \
startABfoo \
{{}} \
{start foo bar} \
{start foo barAB} \
{start fooABbar bar} \
]
tcltest::test field-mapping-2.3 {::sqawk::parsers::awk::map auto} -setup {
init
} -body {
source -encoding utf-8 sqawk.tcl
set result {}
lappend result [::sqawk::parsers::awk::map \
{start AB foo AB bar {}} {auto}]
lappend result [::sqawk::parsers::awk::map \
{start AB foo AB bar AB} {auto}]
lappend result [::sqawk::parsers::awk::map \
{start AB foo AB bar AB} {{1 1} auto}]
lappend result [::sqawk::parsers::awk::map \
{start AB foo AB bar AB} {{1 1} {2 2} auto}]
} -cleanup {
unset result
uninit
} -result [list \
{start foo bar} \
{start foo bar} \
{start foo bar} \
{start foo bar} \
]
tcltest::test field-mapping-2.3 {::sqawk::parsers::awk::map} -setup {
init
} -body {
source -encoding utf-8 sqawk.tcl
set result {}
lappend result [::sqawk::parsers::awk::map \
{foo { } 1 { } foo { } 2 { } foo { } 3 {}} \
{{1 2} {3 4} {5 6}}]
lappend result [::sqawk::parsers::awk::map \
{bar { } 4 { } bar { } 5 { } bar { } 6 {}} \
{{1 2} {3 4} {5 6}}]
} -cleanup {
unset result
uninit
} -result {{{foo 1} {foo 2} {foo 3}} {{bar 4} {bar 5} {bar 6}}}
tcltest::test field-mapping-2.4 {::sqawk::parsers::awk::map} -setup {
init
} -body {
source -encoding utf-8 sqawk.tcl
set result {}
lappend result [::sqawk::parsers::awk::map \
{foo { } 1 { } foo { } 2 { } foo { } 3 {}} \
{{2 2} {4 4} {6 6}}]
lappend result [::sqawk::parsers::awk::map \
{bar { } 4 { } bar { } 5 { } bar { } 6 {}} \
{{2 2} {4 4} {6 6}}]
} -cleanup {
unset result
uninit
} -result {{1 2 3} {4 5 6}}
variable merge2File [make-temp-file \
"foo 1 foo 2 foo 3\nbar 4 bar 5 bar 6\n"]
tcltest::test field-mapping-3.1 {merge fields} -setup {
init merge2File "foo 1 foo 2 foo 3\nbar 4 bar 5 bar 6\n"
} -body {
sqawk-tcl -OFS - {
select a1, a2, a3 from a
} {fields=1-2,3-4,5-6} $merge2File
} -cleanup {
uninit
} -result "foo 1-foo 2-foo 3\nbar 4-bar 5-bar 6"
tcltest::test field-mapping-3.2 {skip fields} -setup {
init merge2File "foo 1 foo 2 foo 3\nbar 4 bar 5 bar 6\n"
} -body {
sqawk-tcl -OFS - {
select a1, a2 from a
} {fields=3,6} $merge2File
} -cleanup {
uninit
} -result "foo-3\nbar-6"
tcltest::test field-mapping-3.3 {skip and merge fields} -setup {
init merge2File "foo 1 foo 2 foo 3\nbar 4 bar 5 bar 6\n"
} -body {
sqawk-tcl -OFS - {
select a1, a2 from a
} {fields=1-2,5-6} $merge2File
} -cleanup {
uninit
} -result "foo 1-foo 3\nbar 4-bar 6"
tcltest::test chunked-input-1.1 {input chunking in "awk" parser} -setup {
init
} -body {
set s "1 this-is-just-filler never-mind-it\n2 \
lorem-ipsum-dolor-sit-amet-consectetur-adipiscing-elit-vivamus\
consequat-ut-iaculis-vel-porta-ullamcorper-velit\n3 \
interdum-et-malesuada-fames we-have-a-quota-to-meet-you-know\
donec-mollis-ligula-id-enim-suscipit-cursus-nibh-sagittis\
ullamcorper-est-lectus-eget-ex all-right-thats-enough\n"
set times 5000
if {[string length $s] * $times < 1024 * 1024} {
error {generated input too small}
}
set sequence [string repeat $s $times]
sqawk-tcl {
select sum(a1) from a
} << $sequence
} -cleanup {
uninit
unset s sequence times
} -result 30000
tcltest::test format-1.1 {CSV input} -constraints {
utf8
} -setup {
init filename "1,2,\"Hello, World!\"\nΑλαμπουρνέζικα,3,4\n5,6,7"
} -body {
sqawk-tcl -OFS - {
select a1, a2, a3 from a
} format=csv $filename
} -cleanup {
uninit
} -result "1-2-Hello, World!\nΑλαμπουρνέζικα-3-4\n5-6-7"
tcltest::test format-1.2 {CSV input} -constraints {
utf8
} -setup {
init filename "1;2;\"Hello, World!\"\nΑλαμπουρνέζικα;3;4\n5;6;7"
} -body {
sqawk-tcl -OFS - {
select a1, a2, a3 from a
} format=csvalt {csvsep=;} $filename
} -cleanup {
uninit
} -result "1-2-Hello, World!\nΑλαμπουρνέζικα-3-4\n5-6-7"
tcltest::test format-2.1 {Tcl data input} -setup {
init
} -body {
sqawk-tcl -OFS \\| {
select * from a
} format=tcl << {{1 2 3 4 5 } {6 7 8 9 10}}
} -cleanup {
uninit
} -result [format %s\n%s \
{1|5|1 2 3 4 5 |1|2|3|4|5|||||} \
{2|5|6 7 8 9 10|6|7|8|9|10|||||}]
tcltest::test format-2.2 {Tcl data input} -setup {
init filename {{foo 1 bar 2} {foo 3 bar 4 baz 5}}
} -body {
sqawk-tcl -output json {
select foo, bar, baz from a
} format=tcl kv=1 header=1 $filename
} -cleanup {
uninit
} -result {[{"foo":"1","bar":"2","baz":""},{"foo":"3","bar":"4","baz":"5"}]}
tcltest::test format-2.3 {Tcl data input} -setup {
init
} -body {
sqawk-tcl -OFS \\| -NF 3 {
select * from a
} format=tcl kv=1 << {{ b 2} {a 1 }}
} -cleanup {
uninit
} -result "1|2|b a|b|a|\n2|2| b 2|2||\n3|2|a 1 ||1|"
tcltest::test format-2.4 {Tcl data input} -setup {
init
} -body {
sqawk-tcl -OFS \\| {
select * from a
} format=tcl kv=false lines=true \
<< "1 2 3\na b\ntrue false null"
} -cleanup {
uninit
} -match glob -result \
"1|3|1 2 3|1|2|3|*\n2|2|a b|a|b|*\n3|3|true\
false null|true|false|null|*"
tcltest::test format-2.5 {Tcl data input} -setup {
init
} -body {
sqawk-tcl -OFS \\| {
select * from a
} format=tcl kv=on lines=true header=1 \
<< "k1 1 k2 2 k3 3\n \n\nk1 a k2 b"
} -cleanup {
uninit
} -match regexp -result \
[string map [list | \\| %NL% \n] \
{1|3|k1 1 k2 2 k3 3|1|2|3|+%NL%2|3|k1 a k2 b|a|b|+}]
tcltest::test format-2.6 {Tcl data input} -setup {
init
} -body {
sqawk-tcl -OFS \\| -NF 2 {
select * from a
} format=tcl kv=0 lines=1 << "100\n\n \t\n101"
} -cleanup {
uninit
} -match glob -result 1|1|100|100|\n2|1|101|101|
tcltest::test format-2.7 {Tcl data input} -setup {
init
} -body {
sqawk-tcl -OFS \\| -NF 2 {
select * from a
} format=tcl kv=0 lines=1 << "100\n\n \t\n101\n \n"
} -cleanup {
uninit
} -match glob -result 1|1|100|100|\n2|1|101|101|
tcltest::test format-2.8 {Tcl data input} -setup {
init
} -body {
sqawk-tcl -OFS \\| -NF 2 {
select * from a
} format=tcl kv=0 lines=1 << {}
} -cleanup {
uninit
} -result {}
tcltest::test format-2.9 {Tcl data input} -setup {
init
} -body {
sqawk-tcl -OFS \\| -NF 2 {
select * from a
} format=tcl kv=0 lines=1 << "100\r\n\r\n \t\r\n101\r\n \r\n"
} -cleanup {
uninit
} -match glob -result 1|1|100|100|\n2|1|101|101|
tcltest::test format-2.10 {Tcl data input} -setup {
init
} -body {
sqawk-tcl -OFS \\| -NF 2 {
select * from a
} format=tcl kv=1 lines=1 header=1 \
<< "k 100\r\n\r\n \t\r\nk 101\r\n \r\n"
} -cleanup {
uninit
} -result "1|1|k 100|100|\n2|1|k 101|101|"
tcltest::test format-3.1 {JSON data input} -setup {
init
} -body {
sqawk-tcl -OFS \\| {
select * from a
} format=json kv=0 << {[[1, 2, 3, 4, 5 ],[6, 7, 8, 9, 10]]}
} -cleanup {
uninit
} -result [format %s\n%s \
{1|5|1 2 3 4 5|1|2|3|4|5|||||} \
{2|5|6 7 8 9 10|6|7|8|9|10|||||}]
tcltest::test format-3.2 {JSON data input} -setup {
init filename {[{"foo":1,"bar":2},{"foo":3,"bar":4,"baz":5}]}
} -body {
sqawk-tcl -output json {
select foo, bar, baz from a
} format=json header=1 $filename
} -cleanup {
uninit
} -result {[{"foo":"1","bar":"2","baz":""},{"foo":"3","bar":"4","baz":"5"}]}
tcltest::test format-3.3 {JSON data input} -setup {
init
} -body {
sqawk-tcl -OFS \\| -NF 3 {
select * from a
} format=json << {[{"b": 2}, {"a": 1 }]}
} -cleanup {
uninit
} -result "1|2|b a|b|a|\n2|2|b 2|2||\n3|2|a 1||1|"
tcltest::test format-3.4 {JSON data input} -setup {
init
} -body {
sqawk-tcl -OFS \\| {
select * from a
} format=json kv=false lines=true \
<< [format {[1,2,3]%1$s["a","b"]%1$s[true,false,null]} \n]
} -cleanup {
uninit
} -match glob -result \
"1|3|1 2 3|1|2|3|*\n2|2|a b|a|b|*\n3|3|true\
false null|true|false|null|*"
tcltest::test format-3.5 {JSON data input} -setup {
init
} -body {
sqawk-tcl -OFS \\| {
select * from a
} format=json kv=on lines=true header=1 \
<< [format {{"k1":1,"k2":2,"k3":3}%1$s \
%1$s%1$s{"k1":"a","k2":"b"}} \n]
} -cleanup {
uninit
} -match regexp -result \
[string map [list | \\| %NL% \n] \
{1|3|k1 1 k2 2 k3 3|1|2|3|+%NL%2|3|k1 a k2 b|a|b|+}]
tcltest::test format-3.6 {JSON data input} -setup {
init
} -body {
sqawk-tcl -OFS \\| -NF 2 {
select * from a
} format=json kv=0 lines=1 << "\[100\]\n\n \t\n\[101\]"
} -cleanup {
uninit
} -match glob -result 1|1|100|100|\n2|1|101|101|
tcltest::test format-3.7 {JSON data input} -setup {
init
} -body {
sqawk-tcl -OFS \\| -NF 2 {
select * from a
} format=json kv=0 lines=1 << "\[100\]\n\n \t\n\[101\]\n \n"
} -cleanup {
uninit
} -match glob -result 1|1|100|100|\n2|1|101|101|
tcltest::test format-3.8 {JSON data input} -setup {
init
} -body {
sqawk-tcl -OFS \\| -NF 2 {
select * from a
} format=json kv=0 lines=1 << {}
} -cleanup {
uninit
} -result {}
tcltest::test format-3.9 {JSON data input} -setup {
init
} -body {
sqawk-tcl -OFS \\| -NF 2 {
select * from a
} format=json kv=0 lines=1 << "\[100\]\r\n\r\n \t\r\n\[101\]\r\n \r\n"
} -cleanup {
uninit
} -match glob -result 1|1|100|100|\n2|1|101|101|
tcltest::test format-3.10 {JSON data input} -setup {
init
} -body {
sqawk-tcl -OFS \\| -NF 2 {
select * from a
} format=json kv=1 lines=1 header=1 \
<< "{\"k\":100}\r\n\r\n \t\r\n{\"k\":101}\r\n \r\n"
} -cleanup {
uninit
} -result "1|1|k 100|100|\n2|1|k 101|101|"
tcltest::test output-1.1 {Default output format} -setup {
init filename "line 1\nline 2\nline 3"
} -body {
sqawk-tcl -output awk {select a0 from a} $filename
} -cleanup {
uninit
} -result "line 1\nline 2\nline 3"
variable output2File [make-temp-file "a,b\n1,2"]
tcltest::test output-2.1 {CSV output} -setup {
init output2File a,b\n1,2
} -body {
sqawk-tcl -output awk {select a1 from a} $output2File
} -cleanup {
uninit
} -result a,b\n1,2
tcltest::test output-2.2 {CSV output} -setup {
init output2File a,b\n1,2
} -body {
sqawk-tcl -output csv {select a1 from a} $output2File
} -cleanup {
uninit
} -result \"a,b\"\n\"1,2\"
tcltest::test output-3.1 {Tcl output} -setup {
init output3File "1\t2\tHello, World!\t "
} -body {
sqawk-tcl \
-FS \t \
-output tcl \
{select a1,a2,a3,a4 from a} $output3File
} -cleanup {
uninit
} -result {{1 2 {Hello, World!} { }}}
tcltest::test output-3.2 {Tcl output} -setup {
init output3File "1\t2\tHello, World!\t "
} -body {
sqawk-tcl \
-FS \t \
-output tcl,kv=1 \
{select a1,a2,a3,a4 from a} $output3File
} -cleanup {
uninit
} -result {{a1 1 a2 2 a3 {Hello, World!} a4 { }}}
tcltest::test output-3.3 {Tcl output} -setup {
init output3File "1\t2\nHello, World!\t "
} -body {
sqawk-tcl \
-FS \t \
-output tcl,kv=1,pretty=yes \
{select a1,a2 from a} $output3File
} -cleanup {
uninit
} -result "{a1 1 a2 2}\n{a1 {Hello, World!} a2 { }}"
tcltest::test output-4.1 {Table output} -constraints {
utf8
} -setup {
init output4File a,b,c\nd,e,f\ng,h,i
} -body {
sqawk-tcl \
-FS , \
-output table \
{select a1,a2,a3 from a} $output4File
} -cleanup {
uninit
} -result ┌─┬─┬─┐\n│a│b│c│\n├─┼─┼─┤\n│d│e│f│\n├─┼─┼─┤\n│g│h│i│\n└─┴─┴─┘
tcltest::test output-4.2 {Table output} -constraints {
utf8
} -setup {
init output4File a,b,c\nd,e,f\ng,h,i
} -body {
sqawk-tcl \
-FS , \
-output {table,alignments=left center right} \
{select a1,a2,a3 from a} $output4File
} -cleanup {
uninit
} -result ┌─┬─┬─┐\n│a│b│c│\n├─┼─┼─┤\n│d│e│f│\n├─┼─┼─┤\n│g│h│i│\n└─┴─┴─┘
tcltest::test output-4.3 {Table output} -constraints {
utf8
} -setup {
init output4File a,b,c\nd,e,f\ng,h,i
} -body {
sqawk-tcl \
-FS , \
-output {table,alignments=l c r} \
{select a1,a2,a3 from a} $output4File
} -cleanup {
uninit
} -result ┌─┬─┬─┐\n│a│b│c│\n├─┼─┼─┤\n│d│e│f│\n├─┼─┼─┤\n│g│h│i│\n└─┴─┴─┘
tcltest::test output-4.4 {Table output} -constraints {
utf8
} -setup {
init output4File a,b,c\nd,e,f\ng,h,i
} -body {
sqawk-tcl \
-FS , \
-output {table,align=left center right} \
{select a1,a2,a3 from a} $output4File
} -cleanup {
uninit
} -result ┌─┬─┬─┐\n│a│b│c│\n├─┼─┼─┤\n│d│e│f│\n├─┼─┼─┤\n│g│h│i│\n└─┴─┴─┘
tcltest::test output-4.5 {Table output} -setup {
init output4File a,b,c\nd,e,f\ng,h,i
} -body {
sqawk-tcl \
-FS , \
-output {table,align=l c r,alignments=l c r} \
{select a1,a2,a3 from a} $output4File
} -cleanup {
uninit
} -returnCodes 1 \
-result {error: can't use synonym options "align" and "alignments"\
together*} \
-match glob
tcltest::test output-5.1 {JSON output} -setup {
init output4File a,b,c\nd,e,f\ng,h,i
} -body {
sqawk-tcl \
-FS , \
-output json \
{select a1,a2,a3 from a} $output4File
} -cleanup {
uninit
} -result [format {[%s,%s,%s]} \
{{"a1":"a","a2":"b","a3":"c"}} \
{{"a1":"d","a2":"e","a3":"f"}} \
{{"a1":"g","a2":"h","a3":"i"}}]
tcltest::test output-5.2 {JSON output} -setup {
init output4File a,b,c\nd,e,f\ng,h,i
} -body {
sqawk-tcl \
-FS , \
-output json,kv=0 \
{select a1,a2,a3 from a} $output4File
} -cleanup {
uninit
} -result {[["a","b","c"],["d","e","f"],["g","h","i"]]}
tcltest::test output-5.3 {JSON output} -setup {
init output4File a,b\nc,d\ne,f
} -body {
sqawk-tcl \
-FS , \
-output json,kv=true,pretty=true \
{select a1,a2,a3 from a} $output4File
} -cleanup {
uninit
} -result \
{[{
"a1" : "a",
"a2" : "b",
"a3" : ""
},{
"a1" : "c",
"a2" : "d",
"a3" : ""
},{
"a1" : "e",
"a2" : "f",
"a3" : ""
}]}
tcltest::test trim-1.1 {trim option} -setup {
init trim1File " a \n"
} -body {
sqawk-tcl {select a1 from a} $trim1File
} -cleanup {
uninit
} -result {}
tcltest::test trim-1.2 {trim option} -setup {
init trim1File " a \n"
} -body {
sqawk-tcl {select a1 from a} trim=left $trim1File
} -cleanup {
uninit
} -result a
tcltest::test trim-1.3 {trim option} -setup {
init trim1File " a \n"
} -body {
sqawk-tcl {select a1 from a} trim=both $trim1File
} -cleanup {
uninit
} -result a
tcltest::test a0-1.1 {Verbatim reproduction of input in a0} -setup {
init filename "test:\n\ttclsh tests.tcl\n\"\{"
} -body {
sqawk-tcl {select a0 from a} $filename
} -cleanup {
uninit
} -result "test:\n\ttclsh tests.tcl\n\"\{"
tcltest::test a0-1.2 {Explicitly enable a0} -setup {
init filename "test:\n\ttclsh tests.tcl\n\"\{"
} -body {
sqawk-tcl {select a0 from a} F0=yes $filename
} -cleanup {
uninit
} -result "test:\n\ttclsh tests.tcl\n\"\{"
tcltest::test a0-1.3 {Disable a0 and try to select it} -setup {
init filename "test:\n\ttclsh tests.tcl\n\"\{"
} -body {
sqawk-tcl {select a0 from a} F0=off $filename
} -cleanup {
uninit
} -returnCodes 1 -match glob -result {error: no such column: a0*}
tcltest::test a0-1.4 {Disable a0 and do unrelated things} -setup {
init filename "1 2 3\n4 5 6"
} -body {
sqawk-tcl {select a1, a2 from a} F0=off $filename
} -cleanup {
uninit
} -result "1 2\n4 5"