Dayz Explorer  1.24.157551 (v105080)
Dayz Code Explorer by Zeroy
testframework.c
Go to the documentation of this file.
1 enum TFR
2 {
3  FAIL = -1,
6 }
7 
8 class TFResult // Pretty much just to be able to keep a reference...
9 {
10  TFR Result;
11 
12  void TFResult(TFR result)
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 
45 class TFCaller
46 {
47  private Class m_Instance;
48  private string m_Test;
49  private ref TFResult m_Result;
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 
58  TFResult Run(float dt)
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 }
80 typedef array<ref TFCaller> TFCallerArr;
81 
82 class TFModule
83 {
84  private int m_Count;
85  private int m_Failed;
86  private int m_Success;
87 
88  private ref TFCallerArr m_Tests;
89  private ref TFResultArr m_Results;
90 
91  private ref array<string> m_SucceededTests;
92  private ref array<string> m_FailedTests;
93 
94  void TFModule()
95  {
96  m_Tests = {};
97  m_Results = {};
98 
99  m_SucceededTests = {};
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 
113  int Success()
114  {
115  return m_Success;
116  }
117 
118  int Pending()
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  {
135  array<TFCaller> done = new array<TFCaller>();
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  {
197  Debug.TFLog(string.Format(" |- %1", success), caller, function);
198  }
199  }
200  if (m_FailedTests.Count())
201  {
202  Debug.TFLog(" |-[FAILED]", caller, function);
203  foreach (string fail : m_FailedTests)
204  {
205  Debug.TFLog(string.Format(" |- %1", fail), caller, function);
206  }
207  }
208  }
209 }
210 
213 {
214  private ref TFModule m_OnInitModule;
216 
218  {
219  SetEventMask(EntityEvent.INIT);
220  SetEventMask(EntityEvent.FRAME);
221 
222  m_OnInitModule = new TFModule();
223  m_OnFrameModule = new TFModule();
224  }
225 
227  {
228  m_OnInitModule.PrintResult("IM: ", this, "~TestFrameWork");
229  m_OnFrameModule.PrintResult("FM: ", this, "~TestFrameWork");
230  }
231 
232  //---------------------------------------------------------------------------
233  // Perform tests
234  //---------------------------------------------------------------------------
235  protected override void EOnInit(IEntity other, int extra)
236  {
237  m_OnInitModule.Run(true, 0);
238  }
239 
240  protected override void EOnFrame(IEntity other, float timeSlice)
241  {
242  if (m_OnFrameModule.Run(false, timeSlice))
243  GetGame().GetCallQueue(CALL_CATEGORY_SYSTEM).Call(Delete);
244  }
245 
246  //---------------------------------------------------------------------------
247  // Add a test
248  //---------------------------------------------------------------------------
249  protected void AddInitTest(string test)
250  {
251  m_OnInitModule.AddTest(this, test, false);
252  }
253 
254  protected void AddFrameTest(string test)
255  {
256  m_OnFrameModule.AddTest(this, test, true);
257  }
258 
259  //---------------------------------------------------------------------------
260  // Asserts
261  //---------------------------------------------------------------------------
262  protected bool Assert(bool condition)
263  {
264  if (!condition)
265  ErrorEx("ASSERTION FAILED.");
266 
267  return condition;
268  }
269 
270  //---------------------------------------------------------------------------
271  // Helpers
272  //---------------------------------------------------------------------------
273  TFResult NTFR(TFR result)
274  {
275  return new TFResult(result);
276  }
277 
278  TFResult BTFR(bool result)
279  {
280  if (result)
281  return new TFResult(TFR.SUCCESS);
282  else
283  return new TFResult(TFR.FAIL);
284  }
285 
287  {
288  return new TFResult(TFR.SUCCESS);
289  }
290 }
GetGame
proto native CGame GetGame()
TFResultArr
array< ref TFResult > TFResultArr
Definition: testframework.c:43
CALL_CATEGORY_SYSTEM
const int CALL_CATEGORY_SYSTEM
Definition: tools.c:8
NTFR
TFResult NTFR(TFR result)
Definition: testframework.c:273
m_OnInitModule
class TFModule m_OnInitModule
Test Framework.
m_OnFrameModule
private ref TFModule m_OnFrameModule
Definition: testframework.c:215
TFResult
void TFResult(TFR result)
Definition: testframework.c:12
And
TFResult And(TFResult other)
Definition: testframework.c:17
TestFramework
void TestFramework()
Definition: testframework.c:217
Print
proto void Print(void var)
Prints content of variable to console/log.
EOnInit
protected override void EOnInit(IEntity other, int extra)
Definition: testframework.c:235
~TestFramework
void ~TestFramework()
Definition: testframework.c:226
FAIL
@ FAIL
Definition: testframework.c:3
PENDING
@ PENDING
Definition: testframework.c:5
ErrorEx
enum ShapeType ErrorEx
IEntity
Definition: enentity.c:164
AddFrameTest
protected void AddFrameTest(string test)
Definition: testframework.c:254
ScriptedEntity
Definition: triggercarrierbase.c:1
TFModule
Definition: testframework.c:82
m_Count
int m_Count
Definition: itembase.c:21
SUCCESS
@ SUCCESS
Definition: testframework.c:4
Result
enum TFR Result
Assert
protected bool Assert(bool condition)
Definition: testframework.c:262
CTFR
TFResult CTFR()
Definition: testframework.c:286
Run
override void Run()
Definition: dayztools.c:8
array
Result for an object found in CGame.IsBoxCollidingGeometryProxy.
Definition: isboxcollidinggeometryproxyclasses.c:27
EOnFrame
protected override void EOnFrame(IEntity other, float timeSlice)
Definition: testframework.c:240
AddInitTest
protected void AddInitTest(string test)
Definition: testframework.c:249
Or
TFResult Or(TFResult other)
Definition: testframework.c:30
Debug
Definition: debug.c:13
TFR
TFR
Definition: testframework.c:1
BTFR
TFResult BTFR(bool result)
Definition: testframework.c:278
EntityEvent
EntityEvent
Entity events for event-mask, or throwing event from code.
Definition: enentity.c:44
Class
Super root of all classes in Enforce script.
Definition: enscript.c:10