summaryrefslogtreecommitdiff
path: root/test/shellA.test
blob: 7c2cabff0ce9034f2d90268354cb41c40acb9a32 (plain) (blame)
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
# 2025-02-24
#
# The author disclaims copyright to this source code.  In place of
# a legal notice, here is a blessing:
#
#    May you do good and not evil.
#    May you find forgiveness for yourself and forgive others.
#    May you share freely, never taking more than you give.
#
#***********************************************************************
# TESTRUNNER: shell
#
# Test cases for the command-line shell - focusing on .mode and
# especially control-character escaping and the --escape option.
#
#

set testdir [file dirname $argv0]
source $testdir/tester.tcl
set CLI [test_cli_invocation]

do_execsql_test shellA-1.0 {
  CREATE TABLE t1(a INT, x TEXT);
  INSERT INTO t1 VALUES
   (1, 'line with '' single quote'),
   (2, concat(char(0x1b),'[31mVT-100 codes',char(0x1b),'[0m')),
   (3, NULL),
   (4, 1234),
   (5, 568.25),
   (6, unistr('new\u000aline')),
   (7, unistr('carriage\u000dreturn')),
   (8, 'last line');
} {}

# Initial verification that the database created correctly
# and that our calls to the CLI are working.
#
do_test_with_ansi_output shellA-1.2 {
  exec {*}$CLI -noinit test.db {.mode box -quote off --escape symbol} {SELECT * FROM t1;}
} {
╭───┬──────────────────────────╮
│ a │            x             │
╞═══╪══════════════════════════╡
│ 1 │ line with ' single quote │
├───┼──────────────────────────┤
│ 2 │ ␛[31mVT-100 codes␛[0m    │
├───┼──────────────────────────┤
│ 3 │                          │
├───┼──────────────────────────┤
│ 4 │ 1234                     │
├───┼──────────────────────────┤
│ 5 │ 568.25                   │
├───┼──────────────────────────┤
│ 6 │ new                      │
│   │ line                     │
├───┼──────────────────────────┤
│ 7 │ carriage␍return          │
├───┼──────────────────────────┤
│ 8 │ last line                │
╰───┴──────────────────────────╯
}

# ".mode list"
#
do_test shellA-1.3 {
  exec {*}$CLI -noinit -list test.db {SELECT x FROM t1 WHERE a=2;}
} {
^[[31mVT-100 codes^[[0m
}
do_test_with_ansi_output shellA-1.4 {
  exec {*}$CLI -noinit -list test.db --escape symbol {SELECT x FROM t1 WHERE a=2;}
} {
␛[31mVT-100 codes␛[0m
}
do_test shellA-1.5 {
  exec {*}$CLI -noinit -list test.db --escape ascii {SELECT x FROM t1 WHERE a=2;}
} {
^[[31mVT-100 codes^[[0m
}
do_test_with_ansi_output shellA-1.6 {
  exec {*}$CLI -noinit test.db {.mode list --escape symbol} {SELECT x FROM t1 WHERE a=2;}
} {
␛[31mVT-100 codes␛[0m
}
do_test shellA-1.7 {
  exec {*}$CLI -noinit test.db {.mode list --escape ascii} {SELECT x FROM t1 WHERE a=2;}
} {
^[[31mVT-100 codes^[[0m
}
do_test shellA-1.8 {
  file delete -force out.txt
  exec {*}$CLI -noinit test.db {.mode list --escape off} {SELECT x FROM t1 WHERE a=7;} \
     >out.txt
  set fd [open out.txt rb]
  set res [read $fd]
  close $fd
  string trim $res
} "carriage\rreturn"
do_test shellA-1.9 {
  set rc [catch {
     exec {*}$CLI -noinit test.db {.mode nosuchmode --escape xyz}
  } msg]
  lappend rc $msg
} {1 {argv[3]: .mode nosuchmode --escape xyz
argv[3]:       ^--- unknown mode
argv[3]: Use ".help .mode" for more info}}
do_test shellA-1.10 {
  set rc [catch {
     exec {*}$CLI --noinit --escape abc test.db .q
  } msg]
  lappend rc $msg
} {1 {unknown control character escape mode "abc" - choices: auto off ascii symbol}}

# ".mode quote"
#
do_test shellA-2.1 {
 exec {*}$CLI -noinit test.db --quote {SELECT a, x FROM t1 WHERE a IN (1,2,6,7,8)}
} {
1,'line with '' single quote'
2,unistr('\u001b[31mVT-100 codes\u001b[0m')
6,unistr('new\u000aline')
7,unistr('carriage\u000dreturn')
8,'last line'
}
do_test shellA-2.2 {
  exec {*}$CLI -noinit test.db --quote {.mode -v}
} {/*.mode quote* --escape auto*/}
do_test shellA-2.3 {
  exec {*}$CLI -noinit test.db --quote --escape SYMBOL {.mode}
} {.mode quote --escape symbol}
do_test shellA-2.4 {
  exec {*}$CLI -noinit test.db --quote --escape OFF {.mode}
} {.mode quote --escape off}


# ".mode line"
#
do_test_with_ansi_output shellA-3.1 {
 exec {*}$CLI -noinit test.db --line --escape symbol \
    {SELECT a, x FROM t1 WHERE a IN (1,2,6,7,8)}
} {
    a: 1
    x: line with ' single quote

    a: 2
    x: ␛[31mVT-100 codes␛[0m

    a: 6
    x: new
       line

    a: 7
    x: carriage␍return

    a: 8
    x: last line
}
do_test shellA-3.2 {
 exec {*}$CLI -noinit test.db --line --escape ascii \
    {SELECT a, x FROM t1 WHERE a IN (1,2,6,7,8)}
} {
    a: 1
    x: line with ' single quote

    a: 2
    x: ^[[31mVT-100 codes^[[0m

    a: 6
    x: new
       line

    a: 7
    x: carriage^Mreturn

    a: 8
    x: last line
}

# ".mode box"
#
do_test_with_ansi_output shellA-4.1 {
 exec {*}$CLI -noinit test.db --box --escape ascii \
    {SELECT a, x FROM t1 WHERE a IN (1,2,6,7,8)}
} {
╭───┬──────────────────────────╮
│ a │            x             │
╞═══╪══════════════════════════╡
│ 1 │ line with ' single quote │
├───┼──────────────────────────┤
│ 2 │ ^[[31mVT-100 codes^[[0m  │
├───┼──────────────────────────┤
│ 6 │ new                      │
│   │ line                     │
├───┼──────────────────────────┤
│ 7 │ carriage^Mreturn         │
├───┼──────────────────────────┤
│ 8 │ last line                │
╰───┴──────────────────────────╯
}
do_test_with_ansi_output shellA-4.1b {
 exec {*}$CLI -noinit test.db --box --escape ascii \
    {.mode -border off} \
    {SELECT a, x FROM t1 WHERE a IN (1,2,6,7,8)}
} {
 a │            x
═══╪══════════════════════════
 1 │ line with ' single quote
───┼──────────────────────────
 2 │ ^[[31mVT-100 codes^[[0m
───┼──────────────────────────
 6 │ new
   │ line
───┼──────────────────────────
 7 │ carriage^Mreturn
───┼──────────────────────────
 8 │ last line
}
do_test_with_ansi_output shellA-4.2 {
 exec {*}$CLI -noinit test.db {.mode qbox} {SELECT a, x FROM t1 WHERE a IN (1,2,6,7,8)}
} {
╭───┬───────────────────────────────────────────╮
│ a │                     x                     │
╞═══╪═══════════════════════════════════════════╡
│ 1 │ 'line with '' single quote'               │
│ 2 │ unistr('\u001b[31mVT-100 codes\u001b[0m') │
│ 6 │ unistr('new\u000aline')                   │
│ 7 │ unistr('carriage\u000dreturn')            │
│ 8 │ 'last line'                               │
╰───┴───────────────────────────────────────────╯
}
do_test_with_ansi_output shellA-4.2b {
 exec {*}$CLI -noinit test.db {.mode qbox -border off} \
    {SELECT a, x FROM t1 WHERE a IN (1,2,6,7,8)}
} {
 a │                     x
═══╪═══════════════════════════════════════════
 1 │ 'line with '' single quote'
 2 │ unistr('\u001b[31mVT-100 codes\u001b[0m')
 6 │ unistr('new\u000aline')
 7 │ unistr('carriage\u000dreturn')
 8 │ 'last line'
}

# ".mode insert"
#
do_test shellA-5.1 {
 exec {*}$CLI -noinit test.db {.mode insert t1 --escape ascii} \
    {SELECT a, x FROM t1 WHERE a IN (1,2,6,7,8)}
} {
INSERT INTO t1 VALUES(1,'line with '' single quote');
INSERT INTO t1 VALUES(2,unistr('\u001b[31mVT-100 codes\u001b[0m'));
INSERT INTO t1 VALUES(6,unistr('new\u000aline'));
INSERT INTO t1 VALUES(7,unistr('carriage\u000dreturn'));
INSERT INTO t1 VALUES(8,'last line');
}
do_test shellA-5.2 {
 exec {*}$CLI -noinit test.db {.mode insert t1 --escape symbol} \
    {SELECT a, x FROM t1 WHERE a IN (1,2,6,7,8)}
} {
INSERT INTO t1 VALUES(1,'line with '' single quote');
INSERT INTO t1 VALUES(2,unistr('\u001b[31mVT-100 codes\u001b[0m'));
INSERT INTO t1 VALUES(6,unistr('new\u000aline'));
INSERT INTO t1 VALUES(7,unistr('carriage\u000dreturn'));
INSERT INTO t1 VALUES(8,'last line');
}
do_test shellA-5.3 {
  file delete -force out.txt
  exec {*}$CLI -noinit test.db {.mode insert t1 --escape off} \
    {SELECT a, x FROM t1 WHERE a IN (1,2,6,7,8)} >out.txt
  set fd [open out.txt rb]
  set res [read $fd]
  close $fd
  string trim [string map [list \r\n \n] $res]
} "
INSERT INTO t1 VALUES(1,'line with '' single quote');
INSERT INTO t1 VALUES(2,'\033\13331mVT-100 codes\033\1330m');
INSERT INTO t1 VALUES(6,'new
line');
INSERT INTO t1 VALUES(7,'carriage\rreturn');
INSERT INTO t1 VALUES(8,'last line');
"

# ".mode split"
#
do_test shellA-6.1 {
  db eval {
    CREATE TABLE t2(x);
    INSERT INTO t2(x) VALUES
      ('one'), ('two'), ('three'), ('four'), ('five'),
      ('six'), ('seven'), ('eight'), ('nine'), ('ten'),
      ('eleven'), ('twelve'), ('thirteen'), ('fourteen');
  }
  exec {*}$CLI -noinit test.db \
      {.print} \
      {.mode split -screenwidth 30} \
      {SELECT x FROM t2}
} {
one    five   nine    thirteen
two    six    ten     fourteen
three  seven  eleven
four   eight  twelve}
# 3456789 123456789 123456789

do_test shellA-6.2 {
  exec {*}$CLI -noinit test.db \
      {.print} \
      {.mode split -screenwidth 30} \
      {SELECT x FROM t2} \
      {.mode column -titles off} \
      {SELECT x FROM t2}
} {
one    five   nine    thirteen
two    six    ten     fourteen
three  seven  eleven
four   eight  twelve
one
two
three
four
five
six
seven
eight
nine
ten
eleven
twelve
thirteen
fourteen}

do_test shellA-6.3 {
  exec {*}$CLI -noinit test.db \
      {.print} \
      {.mode table} \
      {.mode --once split -screenwidth 30} \
      {SELECT x FROM t2} \
      {SELECT x FROM t2}
} {
one    five   nine    thirteen
two    six    ten     fourteen
three  seven  eleven
four   eight  twelve
+----------+
|    x     |
+----------+
| one      |
| two      |
| three    |
| four     |
| five     |
| six      |
| seven    |
| eight    |
| nine     |
| ten      |
| eleven   |
| twelve   |
| thirteen |
| fourteen |
+----------+}

finish_test