-
Notifications
You must be signed in to change notification settings - Fork 0
/
unit-test.tf
125 lines (106 loc) · 3.97 KB
/
unit-test.tf
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
;
; tf-util/unit-test.tf
;
; Some basic unit test facilities to help make sure code does what we think it does.
;
/loaded __tf_util__unit_test__
/require tf-util/list-macros.tf
/require lisp.tf
/def tfunit_declareGroup = \
/test setVar("tfunit.runGroup", {1})
/def tfunit_endGroup = \
/test setVar("tfunit.runGroup", "")
/def assert = \
/test tfunit_assertTrue({1}, {2})
/def tfunit_assertTrue = \
/let _success=%{1}%;\
/let _message=%{2}%;\
/let _testGroup=$[getVar("tfunit.currentGroup.name")]%;\
/let _testName=$[getVar("tfunit.currentTest.name")]%;\
/if (_success) \
/tfunit_assertSuccess%;\
/else \
/test tfunit_assertFailed(1, 0, _message)%;\
/endif
/def tfunit_assertStrEqual = \
/let _expected=%{1}%;\
/let _observed=%{2}%;\
/let _message=%{-2}%;\
/let _testGroup=$[getVar("tfunit.currentGroup.name")]%;\
/let _testName=$[getVar("tfunit.currentTest.name")]%;\
/if (_expected =~ _observed) \
/tfunit_assertSuccess%;\
/else \
/test tfunit_assertFailed(_expected, _observed, _message)%;\
/endif
/def tfunit_assertEqual = \
/let _expected=%{1}%;\
/let _observed=%{2}%;\
/let _message=%{-2}%;\
/let _testGroup=$[getVar("tfunit.currentGroup.name")]%;\
/let _testName=$[getVar("tfunit.currentTest.name")]%;\
/if (_expected == _observed) \
/tfunit_assertSuccess%;\
/else \
/test tfunit_assertFailed(_expected, _observed, _message)%;\
/endif
/def -i tfunit_assertSuccess = \
/let _count=tfunit.currentTest.assertCount%;\
/test setVar(_count, getVar(_count)+1)
/def -i tfunit_assertFailed = \
/let _aCount=tfunit.currentTest.assertCount%;\
/let _fCount=tfunit.currentTest.failCount%;\
/let _failMsgs=tfunit.currentTest.failMessages%;\
/test setVar(_aCount, getVar(_aCount)+1)%;\
/test setVar(_fCount, getVar(_fCount)+1)%;\
/let _msg=Expected: '%{1}', Observed: '%{2}', : %{3}%;\
/test setVar(_failMsgs, strcat(getVar(_failMsgs), " ", textencode(_msg)))
/util_watchVar tfunit.runGroup tfunit_groupChanged
/def -i tfunit_groupChanged = \
; /echo group changed: '%{1}' -> '%{2}'%;\
/let _group=%{1}%;\
/let _newGroup=%{2}%;\
/if (_newGroup =~ "") \
/tfunit_testGroup %{_group}%;\
/endif%;\
/test echo("==========")%;\
/test 1
/def tfunit_testGroup = \
/let _group=%{1}%;\
/test setVar("tfunit.currentGroup.name", _group)%;\
/echo Running Test Group: %{_group}%;\
/let _tests=$(/list_macros test_%{_group}_)%;\
/mapcar /tfunit_runTest %{_tests}
/def -i tfunit_runTest = \
/let _test=%{1}%;\
/tfunit_resetCurrentTest %{_test}%;\
/%{_test}%;\
/tfunit_analyzeTestResults
/def -i tfunit_analyzeTestResults = \
/let _shortName=$[tfunit_testShortName(getVar("tfunit.currentTest.name"))]%;\
/let _total=$[getVar("tfunit.currentTest.assertCount")]%;\
/let _failed=$[getVar("tfunit.currentTest.failCount")]%;\
/let _success=$[_total - _failed]%;\
/if (_failed) \
/test echo(strcat(" [ @{Cred}FAILED@{n} ] ", _shortName, " - ( ", _success, "/", _total, " passed )"), "", 1)%;\
/tfunit_dumpReasons $[getVar("tfunit.currentTest.failMessages")]%;\
/else \
/test echo(strcat(" [ @{Cgreen}Ok@{n} ] ", _shortName, " - ( ", _success, "/", _total, " passed )"), "", 1)%;\
/endif
/def -i tfunit_testShortName = \
/let _test=%{1}%;\
/let _regex=$[strcat("^test_", getVar("tfunit.currentGroup.name"), "_(.*)$")]%;\
/test regmatch(_regex, _test)%;\
/result replace("_", " ", {P1})%;\
/def -i tfunit_resetCurrentTest = \
/let _test=%{1}%;\
/test setVar("tfunit.currentTest.assertCount", 0)%;\
/test setVar("tfunit.currentTest.failCount", 0)%;\
/test setVar("tfunit.currentTest.name", _test)%;\
/test setVar("tfunit.currentTest.failMessages", "")
/def -i tfunit_dumpReasons = \
/while ({#}) \
/let _reason=$[textdecode({1})]%;\
/test echo(strcat(" - ", _reason))%;\
/shift%;\
/done