DayZ 1.24
Loading...
Searching...
No Matches
TestFramework.c
Go to the documentation of this file.
1enum TFR
2{
3 FAIL = -1,
6}
7
8class TFResult // Pretty much just to be able to keep a reference...
9{
11
13 {
14 Result = result;
15 }
16
18 {
19 if (Result == TFR.PENDING || other.Result == TFR.PENDING)
20 ErrorEx("Trying to And while one of the results are PENDING.");
21
22 if (Result == TFR.SUCCESS && other.Result == TFR.SUCCESS)
23 Result = TFR.SUCCESS;
24 else
25 Result = TFR.FAIL;
26
27 return this;
28 }
29
31 {
32 if (Result == TFR.PENDING || other.Result == TFR.PENDING)
33 ErrorEx("Trying to Or while one of the results are PENDING.");
34
35 if (Result == TFR.SUCCESS || other.Result == TFR.SUCCESS)
36 Result = TFR.SUCCESS;
37 else
38 Result = TFR.FAIL;
39
40 return this;
41 }
42}
44
45class TFCaller
46{
48 private string m_Test;
50
51 void TFCaller(Class instance, string test, TFResult result)
52 {
53 m_Instance = instance;
54 m_Test = test;
55 m_Result = result;
56 }
57
59 {
60 bool callResult = GetGame().GameScript.CallFunction(m_Instance, m_Test, m_Result, dt);
61 if (!callResult)
62 {
63 ErrorEx(string.Format("Failed to call function \'%1\' on \'%2\'", m_Test, m_Instance.GetDebugName()));
64 m_Result.Result = TFR.FAIL;
65 }
66
67 return m_Result;
68 }
69
70 string GetTest()
71 {
72 return m_Test;
73 }
74
75 string GetTestEx()
76 {
77 return string.Format("%1::%2", m_Instance.ClassName(), m_Test);
78 }
79}
81
83{
84 private int m_Count;
85 private int m_Failed;
86 private int m_Success;
87
90
93
94 void TFModule()
95 {
96 m_Tests = {};
97 m_Results = {};
98
100 m_FailedTests = {};
101 }
102
103 int Count()
104 {
105 return m_Count;
106 }
107
108 int Failed()
109 {
110 return m_Failed;
111 }
112
114 {
115 return m_Success;
116 }
117
119 {
120 return m_Count - (m_Failed + m_Success);
121 }
122
123 void AddTest(Class instance, string test, bool repeat)
124 {
125 ++m_Count;
126
127 TFResult result = new TFResult(TFR.PENDING);
128 m_Results.Insert(result);
129
130 m_Tests.Insert(new TFCaller(instance, test, result));
131 }
132
133 bool Run(bool fatal, float dt)
134 {
136
137 // Run the tests
138 int runningTests = m_Tests.Count();
139 for (int i = 0; i < runningTests; ++i)
140 {
141 TFCaller t = m_Tests[i];
142 if (RunTest(t, dt))
143 done.Insert(t);
144 }
145
146 // Remove finished tests
147 foreach (TFCaller doneT : done)
148 m_Tests.RemoveItem(doneT);
149
150 // Validate fatal
151 if (fatal && m_Tests.Count() > 0)
152 {
153 Print("- Active tests -------------------------");
154 foreach (TFCaller rTest : m_Tests)
155 Print(rTest.GetTest());
156 Print("----------------------------------------");
157
158 ErrorEx("Not all tests are done while run was fatal.");
159 m_Tests.Clear();
160 }
161
162 return m_Tests.Count() == 0;
163 }
164
165 private bool RunTest(TFCaller caller, float dt)
166 {
167 TFR res = caller.Run(dt).Result;
168
169 switch (res)
170 {
171 case TFR.FAIL:
172 ++m_Failed;
173 m_FailedTests.Insert(caller.GetTestEx());
174 break;
175 case TFR.SUCCESS:
176 ++m_Success;
177 m_SucceededTests.Insert(caller.GetTestEx());
178 break;
179 }
180
181 return res != TFR.PENDING;
182 }
183
184 string Result()
185 {
186 return string.Format("{ [TFModule] :: Tests: %1 | Success: %2 | Failed: %3 | Pending: %4 }", Count(), Success(), Failed(), Pending());
187 }
188
189 void PrintResult(string prefix = "", TestFramework caller = null, string function = "")
190 {
191 Debug.TFLog(string.Format("%1%2", prefix, Result()), caller, function);
192 if (m_SucceededTests.Count())
193 {
194 Debug.TFLog(" |-[SUCCESS]", caller, function);
195 foreach (string success : m_SucceededTests)
196 Debug.TFLog(string.Format(" |- %1", success), caller, function);
197 }
198 if (m_FailedTests.Count())
199 {
200 Debug.TFLog(" |-[FAILED]", caller, function);
201 foreach (string fail : m_FailedTests)
202 Debug.TFLog(string.Format(" |- %1", fail), caller, function);
203 }
204 }
205}
206
208class TestFramework : ScriptedEntity
209{
212
214 {
215 SetEventMask(EntityEvent.INIT);
216 SetEventMask(EntityEvent.FRAME);
217
218 m_OnInitModule = new TFModule();
220 }
221
223 {
224 m_OnInitModule.PrintResult("IM: ", this, "~TestFrameWork");
225 m_OnFrameModule.PrintResult("FM: ", this, "~TestFrameWork");
226 }
227
228 //---------------------------------------------------------------------------
229 // Perform tests
230 //---------------------------------------------------------------------------
231 protected override void EOnInit(IEntity other, int extra)
232 {
233 m_OnInitModule.Run(true, 0);
234 }
235
236 protected override void EOnFrame(IEntity other, float timeSlice)
237 {
238 if (m_OnFrameModule.Run(false, timeSlice))
239 GetGame().GetCallQueue(CALL_CATEGORY_SYSTEM).Call(Delete);
240 }
241
242 //---------------------------------------------------------------------------
243 // Add a test
244 //---------------------------------------------------------------------------
245 protected void AddInitTest(string test)
246 {
247 m_OnInitModule.AddTest(this, test, false);
248 }
249
250 protected void AddFrameTest(string test)
251 {
252 m_OnFrameModule.AddTest(this, test, true);
253 }
254
255 //---------------------------------------------------------------------------
256 // Asserts
257 //---------------------------------------------------------------------------
258 protected bool Assert(bool condition)
259 {
260 if (!condition)
261 ErrorEx("ASSERTION FAILED.");
262
263 return condition;
264 }
265
266 //---------------------------------------------------------------------------
267 // Helpers
268 //---------------------------------------------------------------------------
270 {
271 return new TFResult(result);
272 }
273
275 {
276 if (result)
277 return new TFResult(TFR.SUCCESS);
278 else
279 return new TFResult(TFR.FAIL);
280 }
281
283 {
284 return new TFResult(TFR.SUCCESS);
285 }
286}
static ref RadialMenu m_Instance
Definition RadialMenu.c:55
void ~TestFramework()
void TestFramework()
TFResult NTFR(TFR result)
class TFModule m_OnInitModule
Test Framework.
TFResult CTFR()
TFResult Or(TFResult other)
bool Assert(bool condition)
void TFResult(TFR result)
TFR
@ PENDING
@ FAIL
@ SUCCESS
TFResult And(TFResult other)
ref TFModule m_OnFrameModule
void AddFrameTest(string test)
void AddInitTest(string test)
override void EOnInit(IEntity other, int extra)
TFResult BTFR(bool result)
enum TFR Result
override void EOnFrame(IEntity other, float timeSlice)
array< ref TFResult > TFResultArr
Super root of all classes in Enforce script.
Definition EnScript.c:11
Definition Debug.c:14
static void TFLog(string message=LOG_DEFAULT, TestFramework caller=null, string function="")
Definition Debug.c:243
ref array< string > m_FailedTests
ref array< string > m_SucceededTests
void TFModule()
ref TFResultArr m_Results
void PrintResult(string prefix="", TestFramework caller=null, string function="")
string Result()
ref TFCallerArr m_Tests
bool RunTest(TFCaller caller, float dt)
int Success()
void AddTest(Class instance, string test, bool repeat)
bool Run(bool fatal, float dt)
int Pending()
Result for an object found in CGame.IsBoxCollidingGeometryProxy.
ref TFResult m_Result
TFResult Run(float dt)
Class m_Instance
string GetTestEx()
void TFCaller(Class instance, string test, TFResult result)
string m_Test
string GetTest()
proto native CGame GetGame()
proto void Print(void var)
Prints content of variable to console/log.
enum ShapeType ErrorEx
EntityEvent
Entity events for event-mask, or throwing event from code.
Definition EnEntity.c:44
static proto string Format(string fmt, void param1=NULL, void param2=NULL, void param3=NULL, void param4=NULL, void param5=NULL, void param6=NULL, void param7=NULL, void param8=NULL, void param9=NULL)
Gets n-th character from string.
const int CALL_CATEGORY_SYSTEM
Definition tools.c:8