1
+
2
+
3
+ TestFramework = TestFramework or {}
4
+ TestFramework .RegisteredTests = {}
5
+
6
+ local TestMT = {}
7
+ TestMT .__index = TestMT
8
+
9
+ function TestFramework :RegisterTest (name , f )
10
+ local tbl = setmetatable ({}, {__index = TestMT })
11
+ tbl .TestFunction = f
12
+ tbl .Name = name
13
+ table.insert (TestFramework .RegisteredTests , tbl )
14
+ print (" Registered test " , name )
15
+ end
16
+
17
+ function TestFramework :RunNextTest ()
18
+ TestFramework .CurrentIndex = (TestFramework .CurrentIndex or 0 ) + 1
19
+ TestFramework .TestTimeout = CurTime () + 3
20
+ local test = TestFramework .RegisteredTests [TestFramework .CurrentIndex ]
21
+ TestFramework .CurrentTest = test
22
+ if (!test ) then
23
+ TestFramework :OnCompleted ()
24
+ else
25
+ test :Run ()
26
+ end
27
+ end
28
+
29
+ function TestFramework :CheckTimeout ()
30
+ if (!TestFramework .CurrentTest ) then return end
31
+ if (CurTime () > TestFramework .TestTimeout ) then
32
+ TestFramework .CurrentTest :Fail (" TIMEOUT" )
33
+ end
34
+ end
35
+
36
+ hook .Add (" Think" , " TestFrameworkTimeoutCheck" , function ()
37
+ TestFramework :CheckTimeout ()
38
+ end )
39
+
40
+ function TestFramework :ReportResult (success )
41
+ TestFramework .TestCount = (TestFramework .TestCount or 0 ) + 1
42
+ if (success ) then
43
+ TestFramework .SuccessCount = (TestFramework .SuccessCount or 0 ) + 1
44
+ else
45
+ TestFramework .FailureCount = (TestFramework .FailureCount or 0 ) + 1
46
+ end
47
+ end
48
+
49
+ function TestFramework :OnCompleted ()
50
+ print (" [MySQLOO] Tests completed" )
51
+ MsgC (Color (255 , 255 , 255 ), " Completed: " , Color (30 , 230 , 30 ), TestFramework .SuccessCount , Color (255 , 255 , 255 ), " Failures: " , Color (230 , 30 , 30 ), TestFramework .FailureCount , " \n " )
52
+
53
+ for j = 0 , 3 do
54
+ timer .Simple (j * 0.5 , function ()
55
+ for i = 1 , 100 do
56
+ collectgarbage (" collect" )
57
+ end
58
+ end )
59
+ end
60
+ timer .Simple (2 , function ()
61
+ for i = 1 , 100 do
62
+ collectgarbage (" collect" )
63
+ end
64
+ local diffBefore = TestFramework .AllocationCount - TestFramework .DeallocationCount
65
+ local diffAfter = mysqloo .allocationCount () - mysqloo .deallocationCount ()
66
+ if (diffAfter > diffBefore ) then
67
+ MsgC (Color (255 , 255 , 255 ), " Found potential memory leak with " , diffAfter - diffBefore , " new allocations that were not freed\n " )
68
+ else
69
+ MsgC (Color (255 , 255 , 255 ), " All allocated objects were freed\n " )
70
+ end
71
+ MsgC (Color (255 , 255 , 255 ), " Lua Heap Before: " , TestFramework .LuaMemory , " After: " , collectgarbage (" count" ), " \n " )
72
+ end )
73
+ end
74
+
75
+ function TestFramework :Start ()
76
+ for i = 1 , 5 do
77
+ collectgarbage (" collect" )
78
+ end
79
+ TestFramework .CurrentIndex = 0
80
+ TestFramework .SuccessCount = 0
81
+ TestFramework .FailureCount = 0
82
+ TestFramework .AllocationCount = mysqloo .allocationCount ()
83
+ TestFramework .DeallocationCount = mysqloo .deallocationCount ()
84
+ TestFramework .LuaMemory = collectgarbage (" count" )
85
+ TestFramework :RunNextTest ()
86
+ end
87
+
88
+ function TestMT :Fail (reason )
89
+ if (self .Completed ) then return end
90
+ self .Completed = true
91
+ MsgC (Color (230 , 30 , 30 ), " FAILED\n " )
92
+ MsgC (Color (230 , 30 , 30 ), " Error: " , reason , " \n " )
93
+ TestFramework :ReportResult (false )
94
+ TestFramework :RunNextTest ()
95
+ end
96
+
97
+ function TestMT :Complete ()
98
+ if (self .Completed ) then return end
99
+ self .Completed = true
100
+ MsgC (Color (30 , 230 , 30 ), " PASSED\n " )
101
+ TestFramework :ReportResult (true )
102
+ TestFramework :RunNextTest ()
103
+ end
104
+
105
+ function TestMT :Run ()
106
+ MsgC (" Test: " , self .Name , " " )
107
+ self .Completed = false
108
+ local status , err = pcall (function ()
109
+ self .TestFunction (self )
110
+ end )
111
+ if (!status ) then
112
+ self :Fail (err )
113
+ end
114
+ end
115
+
116
+ function TestMT :shouldBeNil (a )
117
+ if (a != nil ) then
118
+ self :Fail (tostring (a ) .. " was expected to be nil, but was not nil" )
119
+ error (" Assertion failed" )
120
+ end
121
+ end
122
+
123
+ function TestMT :shouldBeGreaterThan (a , num )
124
+ if (num >= a ) then
125
+ self :Fail (tostring (a ) .. " was expected to be greater than " .. tostring (num ))
126
+ error (" Assertion failed" )
127
+ end
128
+ end
129
+
130
+ function TestMT :shouldNotBeNil (a )
131
+ if (a == nil ) then
132
+ self :Fail (tostring (a ) .. " was expected to not be nil, but was nil" )
133
+ error (" Assertion failed" )
134
+ end
135
+ end
136
+
137
+ function TestMT :shouldNotBeEqual (a , b )
138
+ if (a == b ) then
139
+ self :Fail (tostring (a ) .. " was equal to " .. tostring (b ))
140
+ error (" Assertion failed" )
141
+ end
142
+ end
143
+
144
+ function TestMT :shouldBeEqual (a , b )
145
+ if (a != b ) then
146
+ self :Fail (tostring (a ) .. " was not equal to " .. tostring (b ))
147
+ error (" Assertion failed" )
148
+ end
149
+ end
150
+
151
+ function TestMT :shouldHaveLength (tbl , exactLength )
152
+ if (# tbl != exactLength ) then
153
+ self :Fail (" Length of " .. tostring (tbl ) .. " was not equal to " .. exactLength )
154
+ error (" Assertion failed" )
155
+ end
156
+ end
157
+
158
+ concommand .Add (" mysqloo_start_tests" , function (ply )
159
+ if (IsValid (ply )) then return end
160
+ print (" Starting MySQLOO Tests" )
161
+ if (# player .GetBots () == 0 ) then
162
+ RunConsoleCommand (" bot" )
163
+ end
164
+ timer .Simple (0.1 , function ()
165
+ TestFramework :Start ()
166
+ end )
167
+ end )
0 commit comments