@@ -14,13 +14,20 @@ function assertions:__call(...)
1414 return self .call (... )
1515end
1616
17+ function assertions :cleanseString (s )
18+ s = string.gsub (s , ' \n ' , ' \\ n' )
19+ s = string.gsub (s , ' \t ' , ' \\ t' )
20+ s = string.gsub (s , ' \r ' , ' \\ r' )
21+ return s
22+ end
23+
1724function assertions :Fail (t , ...)
1825 t :LogHelper (2 , ... )
1926 if self .fail_now then
20- print ( " Failing now " )
27+ assert ( t . FailNow , " First parameter must be t (the testing.T object) " )
2128 t :FailNow ()
2229 else
23- print ( " Failing " )
30+ assert ( t . Fail , " First parameter must be t (the testing.T object) " )
2431 t :Fail ()
2532 end
2633 return false
2936function assertions :Failf (t , fmt , ...)
3037 t :LogHelperf (2 , fmt , ... )
3138 if self .fail_now then
39+ assert (t .FailNow , " First parameter must be t (the testing.T object)" )
3240 t :FailNow ()
3341 else
42+ assert (t .Fail , " First parameter must be t (the testing.T object)" )
3443 t :Fail ()
3544 end
3645 return false
@@ -40,56 +49,89 @@ function assertions:Equal(t, expected, actual, ...)
4049 if expected == actual then
4150 return true
4251 end
43- return self :Fail (t , string.format ([[ expected "%s"; got "%s"]] .. ' \n ' , expected , actual ), ... )
52+ return self :Fail (t , string.format ([[
53+
54+ Error: Not equal:
55+ expected: "%s"
56+ actual : "%s"
57+ Messages: ]] , self :cleanseString (expected ), self :cleanseString (actual )), ... )
4458end
4559
4660function assertions :Equalf (t , expected , actual , fmt , ...)
4761 if expected == actual then
4862 return true
4963 end
50- return self :Failf (t , string.format ([[ expected "%s"; got "%s"%s%s]] , expected , actual , ' \n ' , fmt ), ... )
64+ return self :Failf (t , string.format ([[
65+
66+ Error: Not equal:
67+ expected: "%s"
68+ actual : "%s"
69+ Messages: %s
70+ ]] , self :cleanseString (expected ), self :cleanseString (actual ), fmt ), ... )
5171end
5272
5373function assertions :NotEqual (t , expected , actual , ...)
5474 if expected ~= actual then
5575 return true
5676 end
57- return self :Fail (t , string.format ([[ expected ~= "%s";]] .. ' \n ' , expected ), ... )
77+ return self :Fail (t , string.format ([[
78+
79+ Error: Should not be %s
80+ Messages: ]] , expected ), ... )
5881end
5982
6083function assertions :NotEqualf (t , expected , actual , fmt , ...)
6184 if expected ~= actual then
6285 return true
6386 end
64- return self :Failf (t , string.format ([[ expected ~= "%s"; got "%s"%s%s]] , expected , ' \n ' , fmt ), ... )
87+ return self :Failf (t , string.format ([[
88+
89+ Error: Should not be %s
90+ Messages: %s
91+ ]] , expected , fmt ), ... )
6592end
6693
6794function assertions :True (t , actual , ...)
6895 if actual then
6996 return true
7097 end
71- return self :Fail (t , string.format ([[ expected true; got %s]] .. ' \n ' , actual ), ... )
98+ return self :Fail (t , string.format ([[
99+
100+ Error: Should be true
101+ Messages: ]] ), ... )
72102end
73103
74104function assertions :Truef (t , actual , fmt , ...)
75105 if actual then
76106 return true
77107 end
78- return self :Failf (t , string.format ([[ expected true; got %s%s%s]] , actual , ' \n ' , fmt ), ... )
108+ return self :Failf (t , string.format ([[
109+
110+ Error: Should be true
111+ Messages: %s
112+ ]] , fmt ), ... )
113+
79114end
80115
81116function assertions :False (t , actual , ...)
82117 if not actual then
83118 return true
84119 end
85- return self :Fail (t , string.format ([[ expected false; got %s]] .. ' \n ' , actual ), ... )
120+ return self :Fail (t , string.format ([[
121+
122+ Error Should be false
123+ Messages: ]] ), ... )
86124end
87125
88126function assertions :Falsef (t , actual , fmt , ...)
89127 if not actual then
90128 return true
91129 end
92- return self :Failf (t , string.format ([[ expected false; got %s%s%s]] , actual , ' \n ' , fmt ), ... )
130+ return self :Failf (t , string.format ([[
131+
132+ Error: Should be false
133+ Messages: %s
134+ ]] , fmt ), ... )
93135end
94136
95137return assertions
0 commit comments