summaryrefslogtreecommitdiff
path: root/Data/BuiltIn/Libraries/lua-stdlib/spec/string_spec.yaml
blob: 2fa47f20340f9a1d74ce4382d75071ad509a45ee (plain)
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
# General Lua Libraries for Lua 5.1, 5.2 & 5.3
# Copyright (C) 2011-2018 stdlib authors

before:
  base_module = 'string'
  this_module = 'std.string'
  global_table = '_G'

  extend_base = {'__concat', '__index',
                 'caps', 'chomp', 'escape_pattern', 'escape_shell',
                 'finds', 'format', 'ltrim',
                 'numbertosi', 'ordinal_suffix', 'pad',
                 'prettytostring', 'rtrim', 'split',
                 'tfind', 'trim', 'wrap'}

  M = require(this_module)
  getmetatable('').__concat = M.__concat
  getmetatable('').__index = M.__index

specify std.string:
- before:
    subject = 'a string \n\n'

- context when required:
  - context by name:
    - it does not touch the global table:
        expect(show_apis {added_to=global_table, by=this_module}).
           to_equal {}
    - it does not touch the core string table:
        expect(show_apis {added_to=base_module, by=this_module}).
           to_equal {}
    - it contains apis from the core string table:
        expect(show_apis {from=base_module, not_in=this_module}).
           to_contain.a_permutation_of(extend_base)

  - context via the std module:
    - it does not touch the global table:
        expect(show_apis {added_to=global_table, by='std'}).
           to_equal {}
    - it does not touch the core string table:
        expect(show_apis {added_to=base_module, by='std'}).
           to_equal {}

- describe ..:
  - it concatenates string arguments:
      target = 'a string \n\n another string'
      expect(subject .. ' another string').to_be(target)
  - it stringifies non-string arguments:
      argument = {'a table'}
      expect(subject .. argument).to_be(subject .. '{1="a table"}')
  - it stringifies nil arguments:
      argument = nil
      expect(subject .. argument).
         to_be(string.format('%s%s', subject, require 'std.normalize'.str(argument)))
  - it does not perturb the original subject:
      original = subject
      newstring = subject .. ' concatenate something'
      expect(subject).to_be(original)


- describe caps:
  - before:
      f = M.caps

  - context with bad arguments:
      badargs.diagnose(f, 'std.string.caps(string)')

  - it capitalises words of a string:
      target = 'A String \n\n'
      expect(f(subject)).to_be(target)
  - it changes only the first letter of each word:
      expect(f 'a stRiNg').to_be 'A StRiNg'
  - it is available as a string metamethod:
      expect(('a stRiNg'):caps()).to_be 'A StRiNg'
  - it does not perturb the original subject:
      original = subject
      newstring = f(subject)
      expect(subject).to_be(original)


- describe chomp:
  - before:
      target = 'a string \n'
      f = M.chomp

  - context with bad arguments:
      badargs.diagnose(f, 'std.string.chomp(string)')

  - it removes a single trailing newline from a string:
      expect(f(subject)).to_be(target)
  - it does not change a string with no trailing newline:
      subject = 'a string '
      expect(f(subject)).to_be(subject)
  - it is available as a string metamethod:
      expect(subject:chomp()).to_be(target)
  - it does not perturb the original subject:
      original = subject
      newstring = f(subject)
      expect(subject).to_be(original)


- describe escape_pattern:
  - before:
      magic = {}
      meta = '^$()%.[]*+-?'
      for i = 1, string.len(meta) do
         magic[meta:sub(i, i)] = true
      end
      f = M.escape_pattern

  - context with bad arguments:
      badargs.diagnose(f, 'std.string.escape_pattern(string)')

  - context with each printable ASCII char:
    - before:
        subject, target = '', ''
        for c = 32, 126 do
           s = string.char(c)
           subject = subject .. s
           if magic[s] then
              target = target .. '%'
           end
           target = target .. s
        end
    - 'it inserts a % before any non-alphanumeric in a string':
        expect(f(subject)).to_be(target)
    - it is available as a string metamethod:
        expect(subject:escape_pattern()).to_be(target)
  - it does not perturb the original subject:
      original = subject
      newstring = f(subject)
      expect(subject).to_be(original)


- describe escape_shell:
  - before:
      f = M.escape_shell

  - context with bad arguments:
      badargs.diagnose(f, 'std.string.escape_shell(string)')

  - context with each printable ASCII char:
    - before:
        subject, target = '', ''
        for c = 32, 126 do
           s = string.char(c)
           subject = subject .. s
           if s:match('[][ ()\\\'"]') then
              target = target .. '\\'
           end
           target = target .. s
        end
    - 'it inserts a \\ before any shell metacharacters':
        expect(f(subject)).to_be(target)
    - it is available as a string metamethod:
        expect(subject:escape_shell()).to_be(target)
  - it does not perturb the original subject:
      original = subject
      newstring = f(subject)
      expect(subject).to_be(original)
  - 'it diagnoses non-string arguments':
      if typecheck then
         expect(f()).to_raise('string expected')
         expect(f {'a table'}).to_raise('string expected')
      end


- describe finds:
  - before:
      subject = 'abcd'
      f = M.finds

  - context with bad arguments:
      badargs.diagnose(f, 'std.string.finds(string, string, ?int, ?boolean|:plain)')

  - context given a complex nested list:
    - before:
        target = {{1, 2; capt={'a', 'b'}}, {3, 4; capt={'c', 'd'}}}
    - it creates a list of pattern captures:
        expect({f(subject, '(.)(.)')}).to_equal({target})
    - it is available as a string metamethod:
        expect({subject:finds('(.)(.)')}).to_equal({target})
  - it creates an empty list where no captures are matched:
      target = {}
      expect({f(subject, '(x)')}).to_equal({target})
  - it creates an empty list for a pattern without captures:
      target = {{1, 1; capt={}}}
      expect({f(subject, 'a')}).to_equal({target})
  - it starts the search at a specified index into the subject:
      target = {{8, 9; capt={'a', 'b'}}, {10, 11; capt={'c', 'd'}}}
      expect({f('garbage' .. subject, '(.)(.)', 8)}).to_equal({target})
  - it does not perturb the original subject:
      original = subject
      newstring = f(subject, '...')
      expect(subject).to_be(original)


- describe format:
  - before:
      subject = 'string=%s, number=%d'

      f = M.format

  - context with bad arguments:
      badargs.diagnose(f, 'std.string.format(string, ?any*)')

  - it returns a single argument without attempting formatting:
      expect(f(subject)).to_be(subject)
  - it is available as a string metamethod:
      expect(subject:format()).to_be(subject)
  - it does not perturb the original subject:
      original = subject
      newstring = f(subject)
      expect(subject).to_be(original)


- describe ltrim:
  - before:
      subject = ' \t\r\n  a  short  string  \t\r\n   '

      f = M.ltrim

  - context with bad arguments:
      badargs.diagnose(f, 'std.string.ltrim(string, ?string)')

  - it removes whitespace from the start of a string:
      target = 'a  short  string  \t\r\n   '
      expect(f(subject)).to_equal(target)
  - it supports custom removal patterns:
      target = '\r\n  a  short  string  \t\r\n   '
      expect(f(subject, '[ \t\n]+')).to_equal(target)
  - it is available as a string metamethod:
      target = '\r\n  a  short  string  \t\r\n   '
      expect(subject:ltrim('[ \t\n]+')).to_equal(target)
  - it does not perturb the original subject:
      original = subject
      newstring = f(subject, '%W')
      expect(subject).to_be(original)


- describe numbertosi:
  - before:
      f = M.numbertosi

  - context with bad arguments:
      badargs.diagnose(f, 'std.string.numbertosi(number|string)')

  - it returns a number using SI suffixes:
      target = {'1e-9', '1y', '1z', '1a', '1f', '1p', '1n', '1mu', '1m', '1',
                '1k', '1M', '1G', '1T', '1P', '1E', '1Z', '1Y', '1e9'}
      subject = {}
      for n = -28, 28, 3 do
         m = 10 *(10 ^ n)
         table.insert(subject, f(m))
      end
      expect(subject).to_equal(target)
  - it coerces string arguments to a number:
      expect(f '1000').to_be '1k'


- describe ordinal_suffix:
  - before:
      f = M.ordinal_suffix

  - context with bad arguments:
      badargs.diagnose(f, 'std.string.ordinal_suffix(int|string)')

  - it returns the English suffix for a number:
      subject, target = {}, {}
      for n = -120, 120 do
         suffix = 'th'
         m = math.abs(n) % 10
         if m == 1 and math.abs(n) % 100 ~= 11 then
            suffix = 'st'
         elseif m == 2 and math.abs(n) % 100 ~= 12 then
            suffix = 'nd'
         elseif m == 3 and math.abs(n) % 100 ~= 13 then
            suffix = 'rd'
         end
         table.insert(target, n .. suffix)
         table.insert(subject, n .. f(n))
      end
      expect(subject).to_equal(target)
  - it coerces string arguments to a number:
      expect(f '-91').to_be 'st'


- describe pad:
  - before:
      width = 20

      f = M.pad

  - context with bad arguments:
      badargs.diagnose(f, 'std.string.pad(string, int, ?string)')

  - context when string is shorter than given width:
    - before:
        subject = 'short string'
    - it right pads a string to the given width with spaces:
        target = 'short string        '
        expect(f(subject, width)).to_be(target)
    - it left pads a string to the given negative width with spaces:
        width = -width
        target = '        short string'
        expect(f(subject, width)).to_be(target)
    - it is available as a string metamethod:
        target = 'short string        '
        expect(subject:pad(width)).to_be(target)

  - context when string is longer than given width:
    - before:
        subject = "a string that's longer than twenty characters"
    - it truncates a string to the given width:
        target = "a string that's long"
        expect(f(subject, width)).to_be(target)
    - it left pads a string to given width with spaces:
        width = -width
        target = 'an twenty characters'
        expect(f(subject, width)).to_be(target)
    - it is available as a string metamethod:
        target = "a string that's long"
        expect(subject:pad(width)).to_be(target)

  - it does not perturb the original subject:
      original = subject
      newstring = f(subject, width)
      expect(subject).to_be(original)


- describe prettytostring:
  - before:
      f = M.prettytostring

  - context with bad arguments:
      badargs.diagnose(f, 'std.string.prettytostring(?any, ?string, ?string)')

  - it renders nil exactly like system tostring:
      expect(f(nil)).to_be(tostring(nil))
  - it renders booleans exactly like system tostring:
      expect(f(true)).to_be(tostring(true))
      expect(f(false)).to_be(tostring(false))
  - it renders numbers exactly like system tostring:
      n = 8723643
      expect(f(n)).to_be(tostring(n))
  - it renders functions exactly like system tostring:
      expect(f(f)).to_be(tostring(f))
  - it renders strings with format '%q' styling:
      s = 'a string'
      expect(f(s)).to_be(string.format('%q', s))
  - it renders empty tables as a pair of braces:
      expect(f {}).to_be('{\n}')
  - it renders an array prettily:
      a = {'one', 'two', 'three'}
      expect(f(a, '')).
         to_be '{\n[1] = "one",\n[2] = "two",\n[3] = "three",\n}'
  - it renders a table prettily:
      t = {one=true, two=2, three={3}}
      expect(f(t, '')).
         to_be '{\none = true,\nthree =\n{\n[1] = 3,\n},\ntwo = 2,\n}'
  - it renders table keys in table.sort order:
      t = {one=3, two=5, three=4, four=2, five=1}
      expect(f(t, '')).
         to_be '{\nfive = 1,\nfour = 2,\none = 3,\nthree = 4,\ntwo = 5,\n}'
  - it renders keys with invalid symbol names in long hand:
      t = {_=0, word=0, ['?']=1, ['a-key']=1, ['[]']=1}
      expect(f(t, '')).
         to_be '{\n["?"] = 1,\n["[]"] = 1,\n_ = 0,\n["a-key"] = 1,\nword = 0,\n}'


- describe rtrim:
  - before:
      subject = ' \t\r\n  a  short  string  \t\r\n   '

      f = M.rtrim

  - context with bad arguments:
      badargs.diagnose(f, 'std.string.rtrim(string, ?string)')

  - it removes whitespace from the end of a string:
      target = ' \t\r\n  a  short  string'
      expect(f(subject)).to_equal(target)
  - it supports custom removal patterns:
      target = ' \t\r\n  a  short  string  \t\r'
      expect(f(subject, '[ \t\n]+')).to_equal(target)
  - it is available as a string metamethod:
      target = ' \t\r\n  a  short  string  \t\r'
      expect(subject:rtrim('[ \t\n]+')).to_equal(target)
  - it does not perturb the original subject:
      original = subject
      newstring = f(subject, '%W')
      expect(subject).to_be(original)


- describe split:
  - before:
      target = {'first', 'the second one', 'final entry'}
      subject = table.concat(target, ', ')

      f = M.split

  - context with bad arguments:
      badargs.diagnose(f, 'std.string.split(string, ?string)')

  - it falls back to '%s+' when no pattern is given:
      expect(f(subject)).
         to_equal {'first,', 'the', 'second', 'one,', 'final', 'entry'}
  - it returns a one-element list for an empty string:
      expect(f('', ', ')).to_equal {''}
  - it makes a table of substrings delimited by a separator:
      expect(f(subject,  ', ')).to_equal(target)
  - it returns n+1 elements for n separators:
      expect(f(subject, 'zero')).to_have_size(1)
      expect(f(subject, 'c')).to_have_size(2)
      expect(f(subject, 's')).to_have_size(3)
      expect(f(subject, 't')).to_have_size(4)
      expect(f(subject, 'e')).to_have_size(5)
  - it returns an empty string element for consecutive separators:
      expect(f('xyzyzxy', 'yz')).to_equal {'x', '', 'xy'}
  - it returns an empty string element when starting with separator:
      expect(f('xyzyzxy', 'xyz')).to_equal {'', 'yzxy'}
  - it returns an empty string element when ending with separator:
      expect(f('xyzyzxy', 'zxy')).to_equal {'xyzy', ''}
  - it returns a table of 1-character strings for '' separator:
      expect(f('abcdef', '')).to_equal {'', 'a', 'b', 'c', 'd', 'e', 'f', ''}
  - it is available as a string metamethod:
      expect(subject:split ', ').to_equal(target)
      expect(('/foo/bar/baz.quux'):split '/').
         to_equal {'', 'foo', 'bar', 'baz.quux'}
  - it does not perturb the original subject:
      original = subject
      newstring = f(subject, 'e')
      expect(subject).to_be(original)
  - it takes a Lua pattern as a separator:
      expect(f(subject, '%s+')).
         to_equal {'first,', 'the', 'second', 'one,', 'final', 'entry'}


- describe tfind:
  - before:
      subject = 'abc'

      f = M.tfind

  - context with bad arguments:
      badargs.diagnose(f, 'std.string.tfind(string, string, ?int, ?boolean|:plain)')

  - it creates a list of pattern captures:
      target = {1, 3, {'a', 'b', 'c'}}
      expect({f(subject, '(.)(.)(.)')}).to_equal(target)
  - it creates an empty list where no captures are matched:
      target = {nil, nil, {}}
      expect({f(subject, '(x)(y)(z)')}).to_equal(target)
  - it creates an empty list for a pattern without captures:
      target = {1, 1, {}}
      expect({f(subject, 'a')}).to_equal(target)
  - it starts the search at a specified index into the subject:
      target = {8, 10, {'a', 'b', 'c'}}
      expect({f('garbage' .. subject, '(.)(.)(.)', 8)}).to_equal(target)
  - it is available as a string metamethod:
      target = {8, 10, {'a', 'b', 'c'}}
      expect({('garbage' .. subject):tfind('(.)(.)(.)', 8)}).to_equal(target)
  - it does not perturb the original subject:
      original = subject
      newstring = f(subject, '...')
      expect(subject).to_be(original)


- describe trim:
  - before:
      subject = ' \t\r\n  a  short  string  \t\r\n   '

      f = M.trim

  - context with bad arguments:
      badargs.diagnose(f, 'std.string.trim(string, ?string)')

  - it removes whitespace from each end of a string:
      target = 'a  short  string'
      expect(f(subject)).to_equal(target)
  - it supports custom removal patterns:
      target = '\r\n  a  short  string  \t\r'
      expect(f(subject, '[ \t\n]+')).to_equal(target)
  - it is available as a string metamethod:
      target = '\r\n  a  short  string  \t\r'
      expect(subject:trim('[ \t\n]+')).to_equal(target)
  - it does not perturb the original subject:
      original = subject
      newstring = f(subject, '%W')
      expect(subject).to_be(original)


- describe wrap:
  - before:
      subject = 'This is a collection of Lua libraries for Lua 5.1 ' ..
         'and 5.2. The libraries are copyright by their authors 2000' ..
         '-2015 (see the AUTHORS file for details), and released und' ..
         'er the MIT license (the same license as Lua itself). There' ..
         ' is no warranty.'

      f = M.wrap

  - context with bad arguments:
      badargs.diagnose(f, 'std.string.wrap(string, ?int, ?int, ?int)')

  - it inserts newlines to wrap a string:
      target = 'This is a collection of Lua libraries for Lua 5.1 a' ..
         'nd 5.2. The libraries are\ncopyright by their authors 2000' ..
         '-2015 (see the AUTHORS file for details), and\nreleased un' ..
         'der the MIT license (the same license as Lua itself). Ther' ..
         'e is no\nwarranty.'
      expect(f(subject)).to_be(target)
  - it honours a column width parameter:
      target = 'This is a collection of Lua libraries for Lua 5.1 a' ..
         'nd 5.2. The libraries\nare copyright by their authors 2000' ..
         '-2015 (see the AUTHORS file for\ndetails), and released un' ..
         'der the MIT license (the same license as Lua\nitself). The' ..
         're is no warranty.'
      expect(f(subject, 72)).to_be(target)
  - it supports indenting by a fixed number of columns:
      target = '        This is a collection of Lua libraries for L' ..
         'ua 5.1 and 5.2. The\n        libraries are copyright by th' ..
         'eir authors 2000-2015 (see the\n        AUTHORS file for d' ..
         'etails), and released under the MIT license\n        (the ' ..
         'same license as Lua itself). There is no warranty.'
      expect(f(subject, 72, 8)).to_be(target)
  - context given a long unwrapped string:
    - before:
        target = '    This is a collection of Lua libraries for Lua 5' ..
           '.1 and 5.2.\n  The libraries are copyright by their author' ..
           's 2000-2015 (see\n  the AUTHORS file for details), and rel' ..
           'eased under the MIT\n  license (the same license as Lua it' ..
           'self). There is no\n  warranty.'
    - it can indent the first line differently:
        expect(f(subject, 64, 2, 4)).to_be(target)
    - it is available as a string metamethod:
        expect(subject:wrap(64, 2, 4)).to_be(target)
  - it does not perturb the original subject:
      original = subject
      newstring = f(subject, 55, 5)
      expect(subject).to_be(original)
  - it diagnoses indent greater than line width:
      expect(f(subject, 10, 12)).to_raise('less than the line width')
      expect(f(subject, 99, 99)).to_raise('less than the line width')
  - it diagnoses non-string arguments:
      if have_typecheck then
         expect(f()).to_raise('string expected')
         expect(f {'a table'}).to_raise('string expected')
      end