forked from FAForever/fa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
luft.lua
1170 lines (1085 loc) · 35 KB
/
luft.lua
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
--**************************************************************************************************
--** lust v0.1.0 - Lua test framework
--** https://github.com/bjornbytes/lust - shared under the MIT license
--**
--** Modified by Askaholic for lua 5.0
--**
--** Extended by Hdt80bro from the "lust" version to the "luft" version and renamed for
--** clarification
--**************************************************************************************************
-- Useful for better test output
require "../lua/system/repr.lua"
---@class Luft
---@field print fun(...) printer used by the `out` function family
---@field tab string tab string used by the indentation system
---@field environment string name of the loaded environment
---@field strings table<string, string> set of strings used for printing or formatting; change these for localization or environment needs
---@field total_module_passes number
---@field total_module_errors number
---@field total_module_assertions number
---
---@field befores? fun(name: string)[] list of functions to run before each test
---@field afters? fun(name: string)[] list of functions to run after each test
---@field indentation string current indentation prepended to output
---@field indentation_level number current indentation level
---@field level number current test description level
---@field sublevel any current subtest name
---@field expectation_number number current assertion number
---@field passes number current number of passed tests
---@field errors number current number of failed tests
---@field assertions number current number of assertions
local Luft = {}
---@class LuftAssertion
---@field values any[] starting values to form expectation under
---@field head LuftPathNode
---@field negate boolean
---@field support? any[] support values for the expectation
local Assertion = {}
---@class LuftPathNode
---@field Name string
---@field Alias? string
---@field NotName? string
---@field NotAlias? string
---@field Chain? fun(a: LuftAssertion) called after simple chaining e.g. a`.b`.c
---@field ChainCall? fun(a: LuftAssertion, ...) called after chained calling e.g. a`.b()`.c
---@field Test? fun(...): boolean function to test with all arguments (from the starting values, calling arguments, and support values); expects both `FailString` formatters to be defined
---@field Parameters number number of parameters the test function has
---@field FailString? string formatter for unnegated tests, where `$x` indicates the `x`th argument to replace. auto genertes `NotFailString` if that one is absent by replacing "to " with "to not "
---@field NotFailString? string formatter for negated tests, where `$x` indicates the `x`th argument to replace
---@field RequiresSupport? boolean if the node action test requires support values to be previously set
local PathNode = {}
---@class LuftSpy
---@field capture function
local Spy = {}
----------------------------------------
-- Environment setup
----------------------------------------
Luft.environment = ""
Luft.tab = '\t'
Luft.print = print
Luft.strings = {
pass = "PASS",
fail = "FAIL",
bad_argument_count = "expected %s arguments, but got %s", -- expected args, actual args
subtest_fail = "%s %s, subtest %s, assertion %s:", -- result indicator, test name, subtest name, expect number
test_fail = "%s %s, assertion %s:", -- result indicator, test name, expect number
test_success = "%s %s", -- result indicator, test name
unknown_error = "unknown error",
support_required = "expected support values",
condition_unnegated = "to",
condition_negated = "to not",
type_nil = "nil",
type_nonnil = "non-nil",
type_truthy = "truthy",
type_falsy = "falsy",
type_boolean = "a boolean",
type_number = "a number",
type_integer = "an integer",
type_float = "a float",
type_string = "a string",
type_function = "a function",
type_userdata = "userdata",
type_table = "a table",
type_positive = "positive",
type_nonpositive = "non-positive",
type_negative = "negative",
type_nonnegative = "non-negative",
cond_exist = "exist",
cond_an = "an %s", -- type
cond_equal = "equal",
cond_unequal = "unequal",
cond_succeed = "succeed",
cond_fail = "fail",
cond_strict_eq = "exactly equal",
cond_contains = "contain %s", -- contained
cond_within = "within %s of %s", -- error, target
expectation1 = "expected %s %s %s", -- value, state condition, expected
expectation1be = "expected %s %s be %s", -- value, state condition, expected
expectation2 = "expected %s and %s %s %s", -- value1, value2, state condition, expected
expectation2be = "expected %s and %s %s be %s", -- value1, value2, state condition, expected
}
Luft.modules_ran = 0
Luft.total_module_errors = 0
Luft.total_module_passes = 0
Luft.total_module_assertions = 0
local environments = {
------------------------------
--- FA init file environment
------------------------------
FA = {
tab = (' '):rep(4),
print = LOG, -- pipe everything to the LOG instead of the invisible commandline
finish = function()
if Luft.errors ~= 0 then
-- each test is run from the same process in the init file, so we can't exit yet
Luft.out("Test module finished with errors!")
end
return Luft
end;
OnLoad = function()
-- Moho still differentiates function from cfunctions, which throws some tests
local oldType = type
function type(x)
local tt = oldType(x)
if tt == "cfunction" then
return "function"
end
return tt
end
end;
},
------------------------------
--- Github workflow environment
------------------------------
GH = {
-- ANSI CSI SGR supported; use that for colors
string_color = function(col, str)
local colors = Luft.colors
col = colors[col]
if col then
-- make sure to replace color resets already in the string to be the color we're
-- setting it to (so that nesting works)
return col .. str:gsub(colors.reset, col) .. colors.reset
end
return str
end;
OnLoad = function()
local esc = string.char(27)
Luft.colors = {
red = esc .. "[31m",
green = esc .. "[32m",
reset = esc .. "[0m",
}
end;
},
}
-- primitive check for FA environment
if LOG ~= nil then
Luft.environment = "FA"
else
Luft.environment = "GH"
end
-- load environment
local env = environments[Luft.environment]
if env.OnLoad then
env:OnLoad()
end
local function addTo(augend, addand, merge)
for key, val in addand do
-- ignore meta-type fields (like "_merge")
if tostring(key):sub(1, 1) == "_" then continue end
if type(val) == "table" then
local existing = augend[key]
if type(existing) == "table" and (merge or val._merge) then
addTo(existing, val, true)
continue
end
end
augend[key] = val
end
end
addTo(Luft, env)
--- Formats a string to be printed a certain color, if the environment supports it
---@param col string
---@param str string
---@return string
Luft.string_color = Luft.string_color or function(col, str)
return str
end
-- if a global called `test_path` is provided, update relative paths to use it
if test_path then
local _dofile = dofile
-- modify `dofile` to be relative to the test directory
function dofile(file)
_dofile(test_path .. file)
end
end
----------------------------------------
-- Unit Test IO Setup
----------------------------------------
--- Resets the testing state. Automatically called when a 0-level test description encounters
--- garbage.
---@return Luft
Luft.start = Luft.start or function()
if Luft.passes then
Luft.total_module_passes = Luft.total_module_passes + Luft.passes
end
if Luft.errors then
Luft.total_module_errors = Luft.total_module_errors + Luft.errors
end
if Luft.assertions then
Luft.total_module_assertions = Luft.total_module_assertions + Luft.assertions
end
Luft.margin_of_error = 0.01
Luft.indentation = ""
Luft.indentation_level = 0
Luft.level = 0
Luft.passes = 0
Luft.errors = 0
Luft.assertions = 0
Luft.modules_ran = Luft.modules_ran + 1
return Luft
end
--- Exits if there are errors
---@return Luft
Luft.finish = Luft.finish or function()
if Luft.errors ~= 0 then
os.exit(-1)
end
return Luft
end
--------------------
-- indentation system
--------------------
---@overload fun(fn: function, ...)
--- Indents a level for the function
---@param amt number defaults to `1`
---@param fn function
---@param ... any
function Luft.indent(amt, fn, ...)
local indentlvl = Luft.indentation_level
local indent = Luft.indentation
if type(amt) == "number" then
local newindent = indent
local tab = Luft.tab
if amt > 0 then
for _ = 1, amt do
newindent = newindent .. tab
end
else
local len = tab:len()
for _ = -1, amt, -1 do
-- check for tabs to remove on the ends of the indentation
if newindent:sub(1, len) == tab then
newindent = newindent:sub(len + 1)
elseif newindent:sub(-len) == tab then
newindent = newindent:sub(-len)
else -- couldn't remove a tab; construct the new indentation level
newindent = tab:rep(indentlvl + amt)
break
end
end
end
Luft.indentation_level = indentlvl + amt
Luft.indentation = newindent
fn(unpack(arg))
else
Luft.indentation_level = indentlvl + 1
Luft.indentation = indent .. Luft.tab
amt(fn, unpack(arg))
end
Luft.indentation = indent
Luft.indentation_level = indentlvl
end
--------------------
-- output system & convenience functions
--------------------
--- Prints to the testing unit's printer using its indentation system
---@param ... any
function Luft.out(...)
return Luft.print(Luft.indentation .. table.concat(arg, '\t'))
end
--- Formats arguments and prints them to the testing unit's printer using its indentation system.
--- Convenience for `Luft.out(formatter:format(...))`
---@param formatter string
---@param ... any
function Luft.outf(formatter, ...)
return Luft.out(formatter:format(unpack(arg)))
end
--- Prints to the testing unit's printer using its indentation system in a specified color.
--- Convenience for `Luft.out(Luft.string_color(col, str))`.
---@param col string
---@param str string
function Luft.outc(col, str)
return Luft.out(Luft.string_color(col, str))
end
--- Formats arguments and prints them to the testing unit's printer using its indentation system in
--- a specified color.
--- Convenience for `Luft.out(Luft.string_color(col, formatter:format(...)))`.
---@param col string
---@param formatter string
---@param ... any
function Luft.outcf(col, formatter, ...)
return Luft.out(Luft.string_color(col, formatter:format(unpack(arg))))
end
---@overload fun(...)
--- Prints to the testing unit's printer using its indentation system with a specified indentation
--- change (defaulting to `1`).
--- Convenience for `Luft.indent(amt, function() Luft.out(...) end)`
---@param amt number
---@param ... any
function Luft.indent_out(amt, ...)
if type(amt) == "number" then
return Luft.indent(amt, Luft.out, unpack(arg))
else
return Luft.indent(1, Luft.out, amt, unpack(arg))
end
end
---@overload fun(formatter: string, ...)
--- Formats arguments and prints them to the testing unit's printer using its indentation system
--- with a specified indentation change (defaulting to `1`).
--- Convenience for `Luft.indent_out(amt, formatter:formatter(...))`
---@param amt number
---@param formatter string
---@param ... any
function Luft.indent_outf(amt, formatter, ...)
if type(amt) == "number" then
return Luft.indent_out(amt, formatter:format(unpack(arg)))
else
return Luft.indent_out(1, amt:format(formatter, unpack(arg)))
end
end
---@overload fun(col: string, str: string)
--- Prints to the testing unit's printer using its indentation system with a specified indentation
--- change (defaulting to `1`) and col.
--- Convenience function for `Luft.indent_out(amt, Luft.string_color(col, str))`
---@param amt number
---@param col string
---@param str string
function Luft.indent_outc(amt, col, str)
if type(amt) == "number" then
return Luft.indent_out(amt, Luft.string_color(col, str))
else
return Luft.indent_out(1, Luft.string_color(amt, col))
end
end
---@overload fun(col: string, formatter: string, ...)
--- Formats arguments and prints them to the testing unit's printer using its indentation system
--- with a specified indentation change (defaulting to `1`).
--- Convenience for `Luft.indent_outc(amt, col, formatter:formatter(...))`
---@param amt number
---@param formatter string
---@param ... any
function Luft.indent_outcf(amt, col, formatter, ...)
if type(amt) == "number" then
return Luft.indent_outc(amt, col, formatter:format(unpack(arg)))
else
return Luft.indent_outc(1, amt, amt:format(formatter, unpack(arg)))
end
end
---@param name string
---@param expect number
---@param success boolean
---@param err? string
Luft.PrintTestResult = Luft.PrintTestResult or function(name, expect, success, err)
local strings = Luft.strings
if success then
Luft.outf(strings.test_success, Luft.string_color("green", strings.pass), name)
else
local sublvl = Luft.sublevel
if not sublvl then
Luft.outf(strings.test_fail, Luft.string_color("red", strings.fail), name, expect)
else
Luft.outf(strings.subtest_fail, Luft.string_color("red", strings.fail), name, sublvl, expect)
end
end
if err then
Luft.indent_outc("red", err)
end
end
----------------------------------------
-- Lua Unit Tester
----------------------------------------
--- Starts a new test description, scoping new `before` and `after` functions added inside
--- the function
---@param name string
---@param fn fun(...)
---@param ... any
---@return Luft
function Luft.describe(name, fn, ...)
local level = Luft.level
if level == 0 then
-- check for previous state
if Luft.assertions ~= 0 then
Luft.start()
end
end
local befores, afters = Luft.befores, Luft.afters
local restore_befores_n, restore_afters_n
if befores then
restore_befores_n = table.getn(befores)
end
if afters then
restore_afters_n = table.getn(afters)
end
Luft.out(name)
Luft.level = level + 1
Luft.indent(fn, unpack(arg))
Luft.level = level
if befores then
table.setn(befores, restore_befores_n)
end
if afters then
table.setn(afters, restore_afters_n)
end
return Luft
end
--- Starts a new test description for each item in a list
---@param pattern string
---@param list table
---@param fn fun(item: any)
---@return Luft
function Luft.describe_each(pattern, list, fn)
for name, item in pairs(list) do
Luft.describe(pattern:format(name), fn, name, item)
end
return Luft
end
--- Runs a unit test, running all `before` and `after` functions of all described test levels,
--- and then prints the test results.
---@param name string
---@param fn any
---@param ... any
---@return Luft
function Luft.test(name, fn, ...)
Luft.sublevel = nil
Luft.expectation_number = 0
local befores = Luft.befores
if befores then
for i = 1, table.getn(befores) do
befores[i](name)
end
end
local success, err = pcall(fn, unpack(arg))
if success then
Luft.passes = Luft.passes + 1
else
Luft.errors = Luft.errors + 1
end
Luft.PrintTestResult(name, Luft.expectation_number, success, err)
local afters = Luft.afters
if afters then
for i = 1, table.getn(afters) do
afters[i](name)
end
end
return Luft
end
--- Runs a new test over each item in a list
---@param pattern string
---@param list table
---@param fn fun(item: any)
---@return Luft
function Luft.test_each(pattern, list, fn)
for key, item in pairs(list) do
Luft.test(pattern:format(key), fn, item)
end
return Luft
end
--- Runs a test function over each item in a list as a single test, each item as a subtest
---@param name string
---@param list table
---@param fn fun(item: any)
---@return Luft
function Luft.test_all(name, list, fn)
Luft.test(name, function()
for key, item in pairs(list) do
Luft.subtest(key)
fn(item)
end
end)
return Luft
end
--- Indicates that the following assertions are under a subtest group. This will be printed out
--- with the failed test if it failed due to one of these.
---@param name any
---@return Luft
function Luft.subtest(name)
Luft.sublevel = name
Luft.expectation_number = 0
return Luft
end
--- Registers a function to run before each test in the current test scope
---@param fn fun(name: string)
---@return Luft
function Luft.before(fn)
local befores = Luft.befores
if not befores then
befores = {}
Luft.befores = befores
end
table.insert(befores, fn)
return Luft
end
--- Registers a function to run after each test in the current test scope
---@param fn fun(name: string)
---@return Luft
function Luft.after(fn)
local afters = Luft.afters
if not afters then
afters = {}
Luft.afters = afters
end
table.insert(afters, fn)
return Luft
end
------------------------------
-- Assertions
------------------------------
--------------------
-- Assertion class
--------------------
--- Starts an assertion statement
---@param ... any
---@return LuftAssertion
function Luft.expect(...)
Luft.expectation_number = Luft.expectation_number + 1
local assertion = {
values = arg,
head = false,
negate = false,
}
setmetatable(assertion, Assertion)
Luft.assertions = Luft.assertions + 1
return assertion
end
Assertion.__index = function(self, word)
if word:lower() ~= word then
return Assertion[word]
end
local node = Assertion.GetCurrentNode(self)
local next_node, process_next = node:GetNext(word)
if next_node then
while process_next do
local chain = next_node.Chain
if chain then
chain(self)
end
self.head = next_node
next_node, process_next = node:GetNext(process_next)
end
local chain = next_node.Chain
if chain then
chain(self)
end
self.head = next_node
return self
end
return Assertion[word]
end
Assertion.__call = function(self, ...)
local node = self:GetCurrentNode()
local Test = node.Test
if Test then
local argCount = self:CallEach(Test, arg)
if argCount > node.Parameters then
error(Luft.strings.bad_argument_count:format(node.Parameters, argCount), 2)
end
else
local ChainCall = node.ChainCall
if ChainCall then
ChainCall(self, unpack(arg))
return self
end
end
end
---@return LuftPathNode
function Assertion:GetCurrentNode()
return self.head or PathNode.Nodes.root
end
---@param test fun(...): boolean, string
---@param args? any[]
---@return number numArgs
function Assertion:CallEach(test, args)
local node = self:GetCurrentNode()
local support = self.support
if node.RequiresSupport and not support then
error(Luft.strings.support_required, 2)
end
local values = self.values
local arguments = {}
local argCount = 0
local lists, iterators = {}, {}
if values then table.insert(lists, values) end
if args then table.insert(lists, args) end
if support then table.insert(lists, support) end
local eachn
-- find the number of times we will be iterating
for _, list in ipairs(lists) do
local n = list.n
if n > 0 then
eachn = n
break
end
end
if not eachn then
-- there must not be *any* arguments
local err = self:GetError(node, pcall(test))
if err then
error(err, 2)
end
return argCount
end
-- reserve one argument for each iterating argument
for k, list in ipairs(lists) do
if list.n >= eachn then
argCount = argCount + 1
iterators[k] = argCount
end
end
-- the rest of the non-iterating values in each list will be added as extra arguments
for k, list in ipairs(lists) do
local start = 1
if iterators[k] then
start = eachn + 1
end
for i = start, list.n do
argCount = argCount + 1
arguments[argCount] = list[i]
end
end
-- now do the actual iteration
for i = 1, eachn do
-- set the iterating arguments
for k, list in ipairs(lists) do
local iter = iterators[k]
if iter then
arguments[iter] = list[i]
end
end
-- then run the test with the argument list
local err = self:GetError(node, pcall(test, unpack(arguments)))
if err then
if eachn > 1 then
-- in order to produce more meaningful error messages, all of the iterating lists
-- are displayed in the error instead of only the current value of each iterator
for k, list in ipairs(lists) do
if not iterators[k] then continue end
local rep = repr(list[1])
for j = 2, list.n do
rep = rep .. ", " .. repr(list[j])
end
arguments[iterators[k]] = '{' .. rep .. '}'
end
end
for j = 1, argCount do
err = err:gsub("$" .. j, repr(arguments[j]))
end
error(err, 3)
end
end
return argCount
end
---@param node LuftPathNode
---@param okay boolean
---@param err boolean | string
---@return string?
function Assertion:GetError(node, okay, err)
if okay then
-- function was called with no errors; `err` is the return value to compare
if (not err) ~= self.negate then
if not self.negate then
return node.FailString or Luft.strings.unknown_error
else
return node.NotFailString or Luft.strings.unknown_error
end
else
return nil
end
else
-- function had errors; `err` is the actual Lua error
if self.negate then
return nil
else
return err
end
end
end
--------------------
-- Path Node class
--------------------
PathNode.__index = PathNode
PathNode.Nodes = {}
---@param word string
---@return LuftPathNode
---@return string? next_word
function PathNode:GetNext(word)
local pos = word:find('_', 1, true)
local next_word
if pos then
next_word = word:sub(pos + 1)
word = word:sub(1, pos - 1)
end
local node = self[word]
if node then
return node, next_word
end
node = self['!' .. word]
if node then
local not_node = self.Nodes["not"]
if next_word then
return not_node, self.Name .. '_' .. next_word
end
return not_node, self.Name
end
return self.Nodes[word]
end
do
local nodes = PathNode.Nodes
local strings = Luft.strings
---@param self LuftAssertion
local function negate(self)
self.negate = not self.negate
end
---@param t1 any
---@param t2 any
---@return boolean
local function strict_eq(t1, t2)
if type(t1) ~= type(t2) then
return false
end
if type(t1) ~= "table" then
return t1 == t2
end
for k, _ in pairs(t1) do
if not strict_eq(t1[k], t2[k]) then return false end
end
for k, _ in pairs(t2) do
if not strict_eq(t2[k], t1[k]) then return false end
end
return true
end
nodes["root"] = {
To = {"not", "to"},
}
nodes["not"] = {
To = "to",
Chain = negate,
}
nodes["to"] = {
NotName = "not_to",
To = {"to.not", "succeed", "to.equal", "be", "have"},
}
nodes["to.not"] = {
To = {"succeed", "to.equal", "be", "have"},
Chain = negate,
}
nodes["succeed"] = {
Alias = "pass",
NotName = "fail",
NotAlias = "error",
Test = function(v)
return pcall(v)
end;
Parameters = 1,
FailString = strings.expectation1:format("$1", strings.condition_unnegated, strings.cond_succeed),
NotFailString = strings.expectation1:format("$1", strings.condition_unnegated, strings.cond_fail),
}
nodes["to.equal"] = {
Test = strict_eq,
Parameters = 2,
FailFormat = strings.expectation2be:format("$1", "$2", "%s", strings.cond_strict_eq),
}
nodes["have"] = {
---@param t any
---@param x any
---@return boolean
Test = function(t, x)
if type(t) ~= "table" then
error(strings.expectation1be:format(repr(t), strings.condition_unnegated, "a table"))
end
for _, v in pairs(t) do
if v == x then return true end
end
return false
end;
Parameters = 2,
FailFormat = strings.expectation1:format("$1", "%s", strings.cond_contains:format("$1")),
}
nodes["exist"] = {
---@param v any
---@return boolean
Test = function(v)
return v ~= nil
end;
Parameters = 1,
FailFormat = strings.expectation1:format("$1", "%s", strings.cond_exist),
}
nodes["be"] = {
To = {
"equal", "unequal",
"greater", "less",
"positive", "negative",
"falsy",
"nil", "userdata",
"within", "close",
"an",
},
Test = function(v, x)
return v == x
end;
Parameters = 2,
FailString = strings.expectation2be:format("$1", "$2", strings.condition_unnegated, strings.cond_equal),
NotFailString = strings.expectation2be:format("$1", "$2", strings.condition_unnegated, strings.cond_unequal),
}
nodes["equal"] = {
To = "equal.to",
}
nodes["equal.to"] = {
test = strict_eq,
Parameters = 2,
FailFormat = nodes["to.equal"].FailFormat,
}
nodes["greater"] = {
To = "than",
Chain = function(sert)
sert.support = {true}
end;
}
nodes["less"] = {
To = "than",
Chain = function(sert)
sert.support = {false}
end;
}
nodes["than"] = {
Test = function(v, x, t)
if t then
return v > x
else
return v < x
end
end;
Parameters = 3,
RequiresSupport = true,
}
nodes["falsy"] = {
NotName = "truthy",
---@param v any
---@return boolean
Test = function(v)
return not v
end;
Parameters = 1,
FailString = strings.expectation1be:format("$1", strings.condition_unnegated, strings.type_falsy),
NotFailString = strings.expectation1be:format("$1", strings.condition_unnegated, strings.type_truthy),
}
nodes["within"] = {
To = "of",
ChainCall = function(sert, ...)
sert.support = arg
end;
}
nodes["of"] = {
---@param v1 number
---@param v2 number
---@param scale number
---@return boolean
Test = function(v1, v2, scale)
return math.abs(v1 - v2) < math.abs(scale)
end;
Parameters = 3,
FailFormat = strings.expectation1be:format("$1", "%s", strings.cond_within:format("$3", "$2")),
RequiresSupport = true,
}
nodes["close"] = {
To = "close.to",
Chain = function(sert)
sert.support = {Luft.margin_of_error, n = 1}
end;
}
nodes["close.to"] = {
Test = nodes["of"].Test,
Parameters = nodes["of"].Parameters,
FailFormat = nodes["of"].FailFormat,
RequiresSupport = nodes["of"].RequiresSupport
}
nodes["negative"] = {
NotName = "nonnegative",
Test = function(v)
return type(v) == "number" and v < 0
end;
Parameters = 1,
FailString = strings.expectation1be:format("$1", strings.condition_unnegated, strings.type_negative),
NotFailString = strings.expectation1be:format("$1", strings.condition_unnegated, strings.type_nonnegative),
}
nodes["positive"] = {
NotName = "nonpositive",
Test = function(v)
return type(v) == "number" and v > 0
end;
Parameters = 1,
FailString = strings.expectation1be:format("$1", strings.condition_unnegated, strings.type_positive),
NotFailString = strings.expectation1be:format("$1", strings.condition_unnegated, strings.type_nonpositive),
}
nodes["nil"] = {
Test = function(v)
return v == nil
end;
Parameters = 1,
FailString = strings.expectation1be:format("$1", strings.condition_unnegated, strings.type_nil),
NotFailString = strings.expectation1be:format("$1", strings.condition_unnegated, strings.type_nonnil),
}
nodes["userdata"] = {
Test = function(v)
return type(v) == "userdata"
end;
Parameters = 1,
FailFormat = strings.expectation1be:format("$1", "%s", strings.type_userdata)
}
nodes["an"] = {
Alias = "a",
---@param v any
---@param x any
---@return boolean
Test = function(v, x)
if type(x) == "string" then
return type(v) == x
elseif type(x) == "table" then