Arma Reforger Explorer 1.7.0.54
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
Loading...
Searching...
No Matches
SCR_AutotestHarness.c
Go to the documentation of this file.
1
24{
25 protected static ref SCR_AutotestReport s_Report;
26 protected static ref SCR_AutotestPrinter s_Logger;
27
28 protected static TestBase s_ActiveTestCase;
29
30 static bool s_bIsRunning = false;
31 static bool s_bOpenLogAfterRun = false;
32 static bool s_bOpenDialogAfterRun = false;
33 static bool s_bCloseGameAfterRun = true;
34
35 //------------------------------------------------------------------------------------------------
37 static void DebugPrintSuites()
38 {
39 Print("(SCR_AutotestHarness) Tests to run:", level: LogLevel.DEBUG);
40 int suitesCount = SCR_AutotestHarness.GetNSuites();
41 for (int i = 0; i < suitesCount; i++)
42 {
43 TestSuite suite = SCR_AutotestHarness.GetSuite(i);
44 PrintFormat("\t%1: %2", suite.GetName(), suite.IsEnabled(), level: LogLevel.DEBUG);
45
46 int testsCount = suite.GetNTests();
47 for (int j = 0; j < testsCount; j++)
48 {
49 TestBase test = suite.GetTest(j);
50 PrintFormat("\t\t%1: %2", test.GetName(), test.IsEnabled(), level: LogLevel.DEBUG);
51 }
52 }
53 }
54
55 //------------------------------------------------------------------------------------------------
57 static void Begin(SCR_AutotestGroup testGroup, bool autorun = false, bool verboseLog = false)
58 {
59 if (s_bIsRunning)
60 Print("SCR_AutotestHarness::Begin was called while the test is already running", LogLevel.ERROR);
61
62 if (!testGroup)
63 testGroup = GetDefaultTestGroupConfig();
64
65 ConfigureTestGroup(testGroup);
66 BeginInternal(autorun, verboseLog);
67 }
68
69 //------------------------------------------------------------------------------------------------
71 static void Begin(notnull SCR_AutotestSuiteBase testSuite, bool autorun = false, bool verboseLog = false)
72 {
73 if (s_bIsRunning)
74 Print("SCR_AutotestHarness::Begin was called while the test is already running", LogLevel.ERROR);
75
76 array<ref SCR_AutotestSuiteBase> wantedSuites = {testSuite};
77 ConfigureTestSuites(wantedSuites);
78
79 BeginInternal(autorun, verboseLog);
80 }
81
82 //------------------------------------------------------------------------------------------------
84 static void Begin(notnull SCR_AutotestCaseBase testCase, bool autorun = false, bool verboseLog = false)
85 {
86 if (s_bIsRunning)
87 Print("SCR_AutotestHarness::Begin was called while the test is already running", LogLevel.ERROR);
88
89 ConfigureTestCases({testCase});
90
91 BeginInternal(autorun, verboseLog);
92 }
93
94 //------------------------------------------------------------------------------------------------
96 private static void BeginInternal(bool autorun, bool verboseLog)
97 {
99
100 super.Begin();
101
102 // signal to SCR_AutotestRunnerCore that it should execute the TestHarness.Run() on every tick
103 s_bIsRunning = autorun;
105 s_Logger = new SCR_AutotestPrinter(verbose: verboseLog);
106
107#ifdef WORKBENCH
108 if (!GetGame().InPlayMode())
109 {
110 WorldEditor we = Workbench.GetModule(WorldEditor);
111 if (!we.GetApi())
112 {
113 Print("WorldEditor is not open!", LogLevel.ERROR);
114 return;
115 }
116
117 we.SwitchToGameMode();
118 }
119#endif
120 }
121
122 //------------------------------------------------------------------------------------------------
124 static SCR_AutotestReport Finish(bool abort = false)
125 {
126 if (abort)
127 s_Logger.Log("Test run was aborted by going back to editor mode...", forceFileWrite: true);
128
129 super.End();
130
131 // signal to SCR_AutotestRunnerCore to stop
132 s_bIsRunning = false;
133 delete s_Logger;
134 _SetActiveTestCase(null);
135
136 s_Report.CollectResults();
137
138 // abort happens mid transition back to editor mode
139 // doing things like opening the report dialog will deadlock the UI
140 if (abort)
141 return s_Report;
142
143#ifdef WORKBENCH
144 if (SCR_AutotestHarness.s_bOpenLogAfterRun)
145 {
146 ScriptEditor scriptEditor = Workbench.GetModule(ScriptEditor);
147 scriptEditor.SetOpenedResource(SCR_AutotestPrinter.LOG_PATH);
148 SCR_AutotestHarness.s_bOpenLogAfterRun = false;
149 }
150
151 if (SCR_AutotestHarness.s_bOpenDialogAfterRun)
152 {
153 s_Report.OpenDialog();
154 SCR_AutotestHarness.s_bOpenDialogAfterRun = false;
155 }
156#endif
157
158 return s_Report;
159 }
160
161 //------------------------------------------------------------------------------------------------
163 static void _SetActiveTestCase(TestBase testCase)
164 {
165 s_ActiveTestCase = testCase;
166 }
167
168 //------------------------------------------------------------------------------------------------
170 static TestBase ActiveTestCase()
171 {
172 return s_ActiveTestCase;
173 }
174
175 //------------------------------------------------------------------------------------------------
177 private static void ConfigureTestGroup(notnull SCR_AutotestGroup testGroup)
178 {
179 ConfigureTestSuites(testGroup.GetSuites());
180 }
181
182 //------------------------------------------------------------------------------------------------
184 private static void ConfigureTestSuites(notnull array<ref SCR_AutotestSuiteBase> wantedSuites)
185 {
186 int allSuiteCount = GetNSuites();
187 for (int i = 0; i < allSuiteCount; i++)
188 {
189 TestSuite suite = GetSuite(i);
190 suite.SetEnabled(false);
191 foreach (SCR_AutotestSuiteBase wantedSuite : wantedSuites)
192 {
193 // test suites from the test harness and config are instantiated separately, do the checks via ClassName
194 if (suite.ClassName() == wantedSuite.ClassName())
195 {
196 int suiteTestsCount = suite.GetNTests();
197 for (int j = 0; j < suiteTestsCount; j++)
198 {
199 TestBase test = suite.GetTest(j);
200 test.SetEnabled(true);
201 OnTestEnabled(test);
202 }
203 suite.SetEnabled(true);
204 break;
205 }
206 }
207 }
208 }
209
210 //------------------------------------------------------------------------------------------------
212 private static void ConfigureTestCases(notnull array<ref SCR_AutotestCaseBase> wantedTests)
213 {
214 int allSuiteCount = GetNSuites();
215 array<TestSuite> allTestSuites = {};
216 for (int i = 0; i < allSuiteCount; i++)
217 {
218 TestSuite suite = GetSuite(i);
219 suite.SetEnabled(false);
220 allTestSuites.Insert(suite);
221 }
222
223 /*
224 disables all test suites, then for each wanted testcase scans:
225 test suite:
226 all test cases:
227 if wanted testcase is found, enable it and its parent test suite
228 */
229 foreach (SCR_AutotestCaseBase wantedTest : wantedTests)
230 {
231 foreach (TestSuite suite : allTestSuites)
232 {
233 int suiteTestsCount = suite.GetNTests();
234 bool testFoundInThisSuite = false;
235 for (int i = 0; i < suiteTestsCount; i++)
236 {
237 TestBase test = suite.GetTest(i);
238 // prevent tests from being run in case the test suite would be re-enabled
239 test.SetEnabled(false);
240
241 if (test.ClassName() == wantedTest.ClassName())
242 {
243 testFoundInThisSuite = true;
244 test.SetEnabled(true);
245 suite.SetEnabled(true);
246 OnTestEnabled(test);
247 }
248 }
249
250 // no point in scanning further if we found our TestCase
251 if (testFoundInThisSuite)
252 break;
253 }
254 }
255 }
256
257 //------------------------------------------------------------------------------------------------
258 private static void OnTestEnabled(TestBase test)
259 {
260#ifdef ENABLE_DIAG
261 SCR_AutotestDebugMenu.GetInstance().s_aAllTests.Insert(test.Type());
262#endif
263 }
264
265 //------------------------------------------------------------------------------------------------
266 static SCR_AutotestPrinter GetLogger()
267 {
268 return s_Logger;
269 }
270
271 //------------------------------------------------------------------------------------------------
274 static SCR_AutotestGroup GetDefaultTestGroupConfig()
275 {
276 /*
277 Resource configHolder = BaseContainerTools.LoadContainer(CORE_CONFIG);
278 return SCR_AutotestGroup.Cast(BaseContainerTools.CreateInstanceFromContainer(configHolder.GetResource().ToBaseContainer()));
279 */
280 return new SCR_AutotestGroup();
281 }
282}
ArmaReforgerScripted GetGame()
Definition game.c:1398
Collection of test suites.
static void Begin(SCR_AutotestGroup testGroup, bool autorun=false, bool verboseLog=false)
Configures the test runner to execute only the test suites present in the specified test group and in...
static bool s_bOpenDialogAfterRun
static void DebugPrintSuites()
Prints test suites and their test case state.
static void Begin(notnull SCR_AutotestCaseBase testCase, bool autorun=false, bool verboseLog=false)
Configures the test runner to execute single test case and initializes required structures.
static ref SCR_AutotestPrinter s_Logger
static ref SCR_AutotestReport s_Report
static void Begin(notnull SCR_AutotestSuiteBase testSuite, bool autorun=false, bool verboseLog=false)
Configures the test runner to execute single test suite and initializes required structures.
static TestBase s_ActiveTestCase
Test base class.
Definition TestBase.c:14
Collection of suites and main interface of the Testing framework.
Definition TestHarness.c:14
Collection of tests. Provides API for environment preparation.
Definition TestSuite.c:14
proto void Print(void var, LogLevel level=LogLevel.NORMAL)
Prints content of variable to console/log.
LogLevel
Enum with severity of the logging message.
Definition LogLevel.c:14
proto void PrintFormat(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, LogLevel level=LogLevel.NORMAL)