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

before: |
  this_module = 'std'
  global_table = '_G'

  exported_apis = {'assert', 'elems', 'eval', 'getmetamethod',
                   'ielems', 'ipairs', 'npairs', 'pairs',
                   'require', 'ripairs', 'rnpairs'}

  -- Tables with iterator metamethods used by various examples.
  __pairs = setmetatable({content='a string'}, {
     __pairs = function(t)
        return function(x, n)
           if n < #x.content then
              return n+1, string.sub(x.content, n+1, n+1)
           end
        end, t, 0
     end,
  })
  __index = setmetatable({content='a string'}, {
     __index = function(t, n)
        if n <= #t.content then
           return t.content:sub(n, n)
        end
     end,
     __len = function(t)
        return #t.content
     end,
  })

  M = require(this_module)
  M.version = nil               -- previous specs may have autoloaded it


specify std:
- context when required:
  - it does not touch the global table:
      expect(show_apis {added_to=global_table, by=this_module}).
         to_equal {}
  - it exports the documented apis:
      t = {}
      for k in pairs(M) do
         t[#t + 1] = k
      end
      expect(t).to_contain.a_permutation_of(exported_apis)

- context when lazy loading:
  - it has no submodules on initial load:
      for _, v in pairs(M) do
         expect(type(v)).not_to_be 'table'
      end
  - it loads submodules on demand:
      lazy = M.math
      expect(lazy).to_be(require 'std.math')
  - it loads submodule functions on demand:
      expect(M.math.round(3.141592)).to_be(3)

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

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

  - context when it does not trigger:
    - it has a truthy initial argument:
        expect(f(1)).not_to_raise 'any error'
        expect(f(true)).not_to_raise 'any error'
        expect(f 'yes').not_to_raise 'any error'
        expect(f(false == false)).not_to_raise 'any error'
    - it returns the initial argument:
        expect(f(1)).to_be(1)
        expect(f(true)).to_be(true)
        expect(f 'yes').to_be 'yes'
        expect(f(false == false)).to_be(true)
  - context when it triggers:
    - it has a falsey initial argument:
        expect(f()).to_raise()
        expect(f(false)).to_raise()
        expect(f(1 == 0)).to_raise()
    - it throws an optional error string:
        expect(f(false, 'ah boo')).to_raise 'ah boo'
    - it plugs specifiers with string.format: |
        expect(f(nil, '%s %d: %q', 'here', 42, 'a string')).
           to_raise(string.format('%s %d: %q', 'here', 42, 'a string'))


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

  - context with bad arguments:
      badargs.diagnose(f, 'std.elems(table)')

  - it is an iterator over table values:
      t = {}
      for e in f {'foo', bar='baz', 42} do
         t[#t + 1] = e
      end
      expect(t).to_contain.a_permutation_of {'foo', 'baz', 42}
  - it respects __pairs metamethod: |
      t = {}
      for v in f(__pairs) do
         t[#t + 1] = v
      end
      expect(t).
         to_contain.a_permutation_of {'a', ' ', 's', 't', 'r', 'i', 'n', 'g'}
  - it works for an empty list:
      t = {}
      for e in f {} do
         t[#t + 1] = e
      end
      expect(t).to_equal {}


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

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

  - it diagnoses invalid lua:
      # Some internal error when eval tries to call uncompilable '=' code.
      expect(f '=').to_raise()
  - it evaluates a string of lua code:
      expect(f 'math.min(2, 10)').to_be(math.min(2, 10))


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

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

  - context with a table:
    - before:
        method = function()
           return 'called'
        end
        functor = setmetatable({}, {__call=method})
        t = setmetatable({}, {
           _type='table', _method=method, _functor=functor,
        })
    - it returns nil for missing metamethods:
        expect(f(t, 'not a metamethod on t')).to_be(nil)
    - it returns nil for non-callable metatable entries:
        expect(f(t, '_type')).to_be(nil)
    - it returns a method from the metatable:
        expect(f(t, '_method')).to_be(method)
        expect(f(t, '_method')()).to_be 'called'
    - it returns a functor from the metatable:
        expect(f(t, '_functor')).to_be(functor)
        expect(f(t, '_functor')()).to_be 'called'


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

  - context with bad arguments:
      badargs.diagnose(f, 'std.ielems(table)')

  - it is an iterator over integer-keyed table values:
      t = {}
      for e in f {'foo', 42} do
         t[#t + 1] = e
      end
      expect(t).to_equal {'foo', 42}
  - it ignores the dictionary part of a table:
      t = {}
      for e in f {'foo', 42; bar='baz', qux='quux'} do
         t[#t + 1] = e
      end
      expect(t).to_equal {'foo', 42}
  - it respects __len metamethod:
      t = {}
      for v in f(__index) do
         t[#t + 1] = v
      end
      expect(t).to_equal {'a', ' ', 's', 't', 'r', 'i', 'n', 'g'}
  - it works for an empty list:
      t = {}
      for e in f {} do
         t[#t + 1] = e
      end
      expect(t).to_equal {}


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

  - context with bad arguments:
      badargs.diagnose(f, 'std.ipairs(table)')

  - it is an iterator over integer-keyed table values:
      t = {}
      for i, v in f {'foo', 42} do
         t[i] = v
      end
      expect(t).to_equal {'foo', 42}
  - it ignores the dictionary part of a table:
      t = {}
      for i, v in f {'foo', 42; bar='baz', qux='quux'} do
         t[i] = v
      end
      expect(t).to_equal {'foo', 42}
  - it respects __len metamethod:
      t = {}
      for k, v in f(__index) do
         t[k] = v
      end
      expect(t).to_equal {'a', ' ', 's', 't', 'r', 'i', 'n', 'g'}
  - it works for an empty list:
      t = {}
      for i, v in f {} do
         t[i] = v
      end
      expect(t).to_equal {}


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

  - context with bad arguments:
      badargs.diagnose(f, 'std.npairs(table)')

  - it is an iterator over integer-keyed table values:
      t = {}
      for i, v in f {'foo', 42, nil, nil, 'five'} do
         t[i] = v
      end
      expect(t).to_equal {'foo', 42, nil, nil, 'five'}
  - it ignores the dictionary part of a table:
      t = {}
      for i, v in f {'foo', 42, nil, nil, 'five'; bar='baz', qux='quux'} do
         t[i] = v
      end
      expect(t).to_equal {'foo', 42, nil, nil, 'five'}
  - it respects __len metamethod:
      t = {}
      for _, v in f(setmetatable({[2]=false}, {__len=function(self) return 4 end})) do
         t[#t + 1] = tostring(v)
      end
      expect(table.concat(t, ',')).to_be 'nil,false,nil,nil'
  - it works for an empty list:
      t = {}
      for i, v in f {} do
         t[i] = v
      end
      expect(t).to_equal {}


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

  - context with bad arguments:
      badargs.diagnose(f, 'std.pairs(table)')

  - it is an iterator over all table values:
      t = {}
      for k, v in f {'foo', bar='baz', 42} do
         t[k] = v
      end
      expect(t).to_equal {'foo', bar='baz', 42}
  - it respects __pairs metamethod: |
      t = {}
      for k, v in f(__pairs) do
         t[k] = v
      end
      expect(t).
         to_contain.a_permutation_of {'a', ' ', 's', 't', 'r', 'i', 'n', 'g'}
  - it works for an empty list:
      t = {}
      for k, v in f {} do
         t[k] = v
      end
      expect(t).to_equal {}


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

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

  - it diagnoses non-existent module:
      expect(f('module-not-exists', '', '')).to_raise 'module-not-exists'
  - it diagnoses module too old:
      expect(f('std', '9999', '9999')).
         to_raise "require 'std' with at least version 9999,"
  - it diagnoses module too new:
      expect(f('std', '0', '0')).
         to_raise "require 'std' with version less than 0,"
  - context when the module version is compatible:
    - it returns the module table:
        expect(f('std', '0', '9999')).to_be(require 'std')
    - it places no upper bound by default:
        expect(f('std', '0')).to_be(require 'std')
    - it places no lower bound by default:
        expect(f 'std').to_be(require 'std')
    - it uses _VERSION when version field is nil:
        expect(luaproc [[
          package.loaded['poop'] = {_VERSION='41.1'}
          f = require 'std'.require
          print(f('poop', '41', '9999')._VERSION)
        ]]).to_succeed_with '41.1\n'
  - context with semantic versioning:
    - before:
        std = require 'std'
        ver = std.version
        std.version = '1.2.3'
    - after:
        std.version = ver
    - it diagnoses module too old:
        expect(f('std', '1.2.4')).
           to_raise "require 'std' with at least version 1.2.4,"
        expect(f('std', '1.3')).
           to_raise "require 'std' with at least version 1.3,"
        expect(f('std', '2.1.2')).
           to_raise "require 'std' with at least version 2.1.2,"
        expect(f('std', '2')).
           to_raise "require 'std' with at least version 2,"
        expect(f('std', '1.2.10')).
           to_raise "require 'std' with at least version 1.2.10,"
    - it diagnoses module too new:
        expect(f('std', nil, '1.2.2')).
           to_raise "require 'std' with version less than 1.2.2,"
        expect(f('std', nil, '1.1')).
           to_raise "require 'std' with version less than 1.1,"
        expect(f('std', nil, '1.1.2')).
           to_raise "require 'std' with version less than 1.1.2,"
        expect(f('std', nil, '1')).
           to_raise "require 'std' with version less than 1,"
    - it returns modules with version in range:
        expect(f('std')).to_be(std)
        expect(f('std', '1')).to_be(std)
        expect(f('std', '1.2.3')).to_be(std)
        expect(f('std', nil, '2')).to_be(std)
        expect(f('std', nil, '1.3')).to_be(std)
        expect(f('std', nil, '1.2.10')).to_be(std)
        expect(f('std', '1.2.3', '1.2.4')).to_be(std)
  - context with several numbers in version string:
    - before:
        std = require 'std'
        ver = std.version
        std.version = 'standard library for Lua 5.3 / 41.0.0'
    - after:
        std.version = ver
    - it diagnoses module too old:
        expect(f('std', '42')).to_raise()
    - it diagnoses module too new:
        expect(f('std', nil, '40')).to_raise()
    - it returns modules with version in range:
        expect(f('std')).to_be(std)
        expect(f('std', '1')).to_be(std)
        expect(f('std', '41')).to_be(std)
        expect(f('std', nil, '42')).to_be(std)
        expect(f('std', '41', '42')).to_be(std)


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

  - context with bad arguments:
      badargs.diagnose(f, 'std.ripairs(table)')

  - it returns a function, the table and a number:
      fn, t, i = f {1, 2, 3}
      expect({type(fn), t, type(i)}).to_equal {'function', {1, 2, 3}, 'number'}
  - it iterates over the array part of a table:
      t, u = {1, 2, 3; a=4, b=5, c=6}, {}
      for i, v in f(t) do
         u[i] = v
      end
      expect(u).to_equal {1, 2, 3}
  - it returns elements in reverse order:
      t, u = {'one', 'two', 'five'}, {}
      for _, v in f(t) do
         u[#u + 1] = v
      end
      expect(u).to_equal {'five', 'two', 'one'}
  - it respects __len metamethod:
      t = {}
      for i, v in f(__index) do
         t[i] = v
      end
      expect(t).to_equal {'a', ' ', 's', 't', 'r', 'i', 'n', 'g'}
      t = {}
      for _, v in f(__index) do
         t[#t + 1] = v
      end
      expect(t).to_equal {'g', 'n', 'i', 'r', 't', 's', ' ', 'a'}
  - it works with the empty list:
      t = {}
      for k, v in f {} do
         t[k] = v
      end
      expect(t).to_equal {}


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

  - context with bad arguments:
      badargs.diagnose(f, 'std.rnpairs(table)')

  - it returns a function, the table and a number:
      fn, t, i = f {1, 2, nil, nil, 3}
      expect({type(fn), t, type(i)}).
         to_equal {'function', {1, 2, nil, nil, 3}, 'number'}
  - it iterates over the array part of a table:
      t, u = {1, 2, nil, nil, 3; a=4, b=5, c=6}, {}
      for i, v in f(t) do
         u[i] = v
      end
      expect(u).to_equal {1, 2, nil, nil, 3}
  - it returns elements in reverse order:
      t, u, i = {'one', 'two', nil, nil, 'five'}, {}, 1
      for _, v in f(t) do
         u[i], i = v, i + 1
      end
      expect(u).to_equal {'five', nil, nil, 'two', 'one'}
  - it respects __len metamethod:
      t = {}
      for _, v in f(setmetatable({[2]=false}, {__len=function(self) return 4 end})) do
         t[#t + 1] = tostring(v)
      end
      expect(table.concat(t, ',')).to_be 'nil,nil,false,nil'
  - it works with the empty list:
      t = {}
      for k, v in f {} do
         t[k] = v
      end
      expect(t).to_equal {}