File size: 6,160 Bytes
24b81cb |
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 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 |
enum TFR
{
FAIL = -1,
SUCCESS,
PENDING,
}
class TFResult // Pretty much just to be able to keep a reference...
{
TFR Result;
void TFResult(TFR result)
{
Result = result;
}
TFResult And(TFResult other)
{
if (Result == TFR.PENDING || other.Result == TFR.PENDING)
ErrorEx("Trying to And while one of the results are PENDING.");
if (Result == TFR.SUCCESS && other.Result == TFR.SUCCESS)
Result = TFR.SUCCESS;
else
Result = TFR.FAIL;
return this;
}
TFResult Or(TFResult other)
{
if (Result == TFR.PENDING || other.Result == TFR.PENDING)
ErrorEx("Trying to Or while one of the results are PENDING.");
if (Result == TFR.SUCCESS || other.Result == TFR.SUCCESS)
Result = TFR.SUCCESS;
else
Result = TFR.FAIL;
return this;
}
}
typedef array<ref TFResult> TFResultArr;
class TFCaller
{
private Class m_Instance;
private string m_Test;
private ref TFResult m_Result;
void TFCaller(Class instance, string test, TFResult result)
{
m_Instance = instance;
m_Test = test;
m_Result = result;
}
TFResult Run(float dt)
{
bool callResult = GetGame().GameScript.CallFunction(m_Instance, m_Test, m_Result, dt);
if (!callResult)
{
ErrorEx(string.Format("Failed to call function \'%1\' on \'%2\'", m_Test, m_Instance.GetDebugName()));
m_Result.Result = TFR.FAIL;
}
return m_Result;
}
string GetTest()
{
return m_Test;
}
string GetTestEx()
{
return string.Format("%1::%2", m_Instance.ClassName(), m_Test);
}
}
typedef array<ref TFCaller> TFCallerArr;
class TFModule
{
private int m_Count;
private int m_Failed;
private int m_Success;
private ref TFCallerArr m_Tests;
private ref TFResultArr m_Results;
private ref array<string> m_SucceededTests;
private ref array<string> m_FailedTests;
void TFModule()
{
m_Tests = {};
m_Results = {};
m_SucceededTests = {};
m_FailedTests = {};
}
int Count()
{
return m_Count;
}
int Failed()
{
return m_Failed;
}
int Success()
{
return m_Success;
}
int Pending()
{
return m_Count - (m_Failed + m_Success);
}
void AddTest(Class instance, string test, bool repeat)
{
++m_Count;
TFResult result = new TFResult(TFR.PENDING);
m_Results.Insert(result);
m_Tests.Insert(new TFCaller(instance, test, result));
}
bool Run(bool fatal, float dt)
{
array<TFCaller> done = new array<TFCaller>();
// Run the tests
int runningTests = m_Tests.Count();
for (int i = 0; i < runningTests; ++i)
{
TFCaller t = m_Tests[i];
if (RunTest(t, dt))
done.Insert(t);
}
// Remove finished tests
foreach (TFCaller doneT : done)
m_Tests.RemoveItem(doneT);
// Validate fatal
if (fatal && m_Tests.Count() > 0)
{
Print("- Active tests -------------------------");
foreach (TFCaller rTest : m_Tests)
Print(rTest.GetTest());
Print("----------------------------------------");
ErrorEx("Not all tests are done while run was fatal.");
m_Tests.Clear();
}
return m_Tests.Count() == 0;
}
private bool RunTest(TFCaller caller, float dt)
{
TFR res = caller.Run(dt).Result;
switch (res)
{
case TFR.FAIL:
++m_Failed;
m_FailedTests.Insert(caller.GetTestEx());
break;
case TFR.SUCCESS:
++m_Success;
m_SucceededTests.Insert(caller.GetTestEx());
break;
}
return res != TFR.PENDING;
}
string Result()
{
return string.Format("{ [TFModule] :: Tests: %1 | Success: %2 | Failed: %3 | Pending: %4 }", Count(), Success(), Failed(), Pending());
}
void PrintResult(string prefix = "", TestFramework caller = null, string function = "")
{
Debug.TFLog(string.Format("%1%2", prefix, Result()), caller, function);
if (m_SucceededTests.Count())
{
Debug.TFLog(" |-[SUCCESS]", caller, function);
foreach (string success : m_SucceededTests)
{
Debug.TFLog(string.Format(" |- %1", success), caller, function);
}
}
if (m_FailedTests.Count())
{
Debug.TFLog(" |-[FAILED]", caller, function);
foreach (string fail : m_FailedTests)
{
Debug.TFLog(string.Format(" |- %1", fail), caller, function);
}
}
}
}
//! Test Framework
class TestFramework : ScriptedEntity
{
private ref TFModule m_OnInitModule;
private ref TFModule m_OnFrameModule;
void TestFramework()
{
SetEventMask(EntityEvent.INIT);
SetEventMask(EntityEvent.FRAME);
m_OnInitModule = new TFModule();
m_OnFrameModule = new TFModule();
}
void ~TestFramework()
{
m_OnInitModule.PrintResult("IM: ", this, "~TestFrameWork");
m_OnFrameModule.PrintResult("FM: ", this, "~TestFrameWork");
}
//---------------------------------------------------------------------------
// Perform tests
//---------------------------------------------------------------------------
protected override void EOnInit(IEntity other, int extra)
{
m_OnInitModule.Run(true, 0);
}
protected override void EOnFrame(IEntity other, float timeSlice)
{
if (m_OnFrameModule.Run(false, timeSlice))
GetGame().GetCallQueue(CALL_CATEGORY_SYSTEM).Call(Delete);
}
//---------------------------------------------------------------------------
// Add a test
//---------------------------------------------------------------------------
protected void AddInitTest(string test)
{
m_OnInitModule.AddTest(this, test, false);
}
protected void AddFrameTest(string test)
{
m_OnFrameModule.AddTest(this, test, true);
}
//---------------------------------------------------------------------------
// Asserts
//---------------------------------------------------------------------------
protected bool Assert(bool condition)
{
if (!condition)
ErrorEx("ASSERTION FAILED.");
return condition;
}
//---------------------------------------------------------------------------
// Helpers
//---------------------------------------------------------------------------
TFResult NTFR(TFR result)
{
return new TFResult(result);
}
TFResult BTFR(bool result)
{
if (result)
return new TFResult(TFR.SUCCESS);
else
return new TFResult(TFR.FAIL);
}
TFResult CTFR()
{
return new TFResult(TFR.SUCCESS);
}
} |