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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
|
#include "UnityPrefix.h"
#include "Configuration/UnityConfigure.h"
// Disclaimer: What we do here isn't real unit testing. It is a reasonable compromise where we accept the
// codebase mostly as is without major refactors and run reasonably small tests that sorta look like unit
// tests but are really integration tests. The key compromise here is that we create a global execution
// environment that all the tests share and run within. This environment dictates what tests are allowed
// to do and what shared functionality they have access to.
#if ENABLE_UNIT_TESTS
#include "External/UnitTest++/src/UnitTest++.h"
#include "External/UnitTest++/src/NunitTestReporter.h"
#include "External/UnitTest++/src/TestReporterStdout.h"
#include "External/UnitTest++/src/TestList.h"
#include "Runtime/Threads/Thread.h"
#include "Runtime/Allocator/LinearAllocator.h"
#include "Runtime/Utilities/Argv.h"
#if !UNITY_EXTERNAL_TOOL
#include "Runtime/Serialize/PathNamePersistentManager.h"
#include "Runtime/BaseClasses/ManagerContext.h"
#include "Runtime/BaseClasses/GameManager.h"
#include "Runtime/Misc/SaveAndLoadHelper.h"
#include "Runtime/Misc/ResourceManager.h"
#include "Runtime/Shaders/Shader.h"
#include "Runtime/Shaders/ShaderNameRegistry.h"
#include "Runtime/Camera/GraphicsSettings.h"
#include "Runtime/Dynamics/PhysicsManager.h"
#include "Runtime/Input/GetInput.h"
#include "Runtime/BaseClasses/Tags.h"
#endif
#include "Testing.h"
#include "ConsoleTestReporter.h"
using namespace UnitTest;
using namespace std;
#endif
#include <iostream>
#include <fstream>
#include <algorithm>
#if ENABLE_UNIT_TESTS
static void GetLengthsOfLongestSuiteAndTestNames (int& longestSuiteNameLength, int& longestTestNameLength)
{
longestSuiteNameLength = 0;
longestTestNameLength = 0;
TestList& allTests = Test::GetTestList ();
for (Test* test = allTests.GetHead (); test != NULL; test = test->next)
{
longestSuiteNameLength = std::max<int> ((int) strlen (test->m_details.suiteName), longestSuiteNameLength);
longestTestNameLength = std::max<int> ((int) strlen (test->m_details.testName), longestTestNameLength);
}
}
static bool SwallowLogMessages (LogType type, const char* log, va_list args)
{
// Ignore everything.
return false;
}
#endif
template<typename Predicate>
static int RunUnitTests (const std::string& resultLog, bool summaryOnly, const Predicate& predicate)
{
#if !ENABLE_UNIT_TESTS
return 0;
#else
int failures;
if (!resultLog.empty ())
{
std::ostringstream stringStream;
UnitTest::NunitTestReporter reporter (stringStream);
UnitTest::TestRunner runner (reporter);
failures = runner.RunTestsIf (Test::GetTestList (), NULL, predicate, 0);
std::ofstream fileStream (resultLog.c_str (), std::ios_base::out | std::ios_base::trunc);
fileStream << stringStream.str();
}
else
{
int longestSuiteNameLength;
int longestTestNameLength;
GetLengthsOfLongestSuiteAndTestNames (longestSuiteNameLength, longestTestNameLength);
ConsoleTestReporter reporter;
reporter.SetShowOnlySummary (summaryOnly);
reporter.SetTestNameColumnIndex (longestSuiteNameLength + 4);
reporter.SetResultColumnIndex (longestSuiteNameLength + 4 + longestTestNameLength + 4);
UnitTest::TestRunner runner (reporter);
failures = runner.RunTestsIf (Test::GetTestList (), NULL, predicate, 0);
}
return failures;
#endif
}
template<typename Predicate>
static void PrintUnitTestList (const Predicate& filter)
{
#if ENABLE_UNIT_TESTS
// Group tests by their files.
int matchingTestCount = 0;
vector<const char*> listedFileNames;
for (Test* temp = Test::GetTestList ().GetHead(); temp != NULL; temp = temp->next)
{
if (!filter (temp))
continue;
const char* filename = temp->m_details.filename;
// Find out whether we've already listed this file.
bool alreadyListedThisFile = false;
for (int i = 0; i < listedFileNames.size (); ++i)
if (strcmp (listedFileNames[i], filename) == 0)
{
alreadyListedThisFile = true;
break;
}
if (alreadyListedThisFile)
continue;
// Print filename.
printf_console ("%s:\n", filename);
listedFileNames.push_back (filename);
// List matching tests in file.
for (Test* test = Test::GetTestList ().GetHead(); test != NULL; test = test->next)
{
if (!filter (test))
continue;
if (strcmp (test->m_details.filename, filename) != 0)
continue;
printf_console ("\t[%s] %s\n", test->m_details.suiteName, test->m_details.testName);
++ matchingTestCount;
}
}
printf_console ("%i test%s\n", matchingTestCount, matchingTestCount == 1 ? "" : "s");
#endif
}
#if !UNITY_EXTERNAL_TOOL
#if ENABLE_UNIT_TESTS
struct TestFilter
{
std::vector<std::string> m_MatchNames;
TestFilter(const std::vector<std::string>& matchNames)
: m_MatchNames (matchNames)
{
// Lowercase everything.
for (int i = 0; i < m_MatchNames.size(); ++i)
m_MatchNames[i] = ToLower (m_MatchNames[i]);
}
bool operator () (const Test* test) const
{
if (m_MatchNames.empty ())
return true;
for (int i = 0; i < m_MatchNames.size(); ++i)
{
if (ToLower (std::string (test->m_details.suiteName)).find (m_MatchNames[i]) != std::string::npos ||
ToLower (std::string (test->m_details.testName)).find (m_MatchNames[i]) != std::string::npos)
return true;
}
return false;
}
};
inline void CreateAndInstallGameManager (ManagerContext::Managers id)
{
GameManager* instance = CreateGameManager (GetManagerContext ().m_ManagerClassIDs[id]);
SetManagerPtrInContext (id, instance);
}
static void InitializeScriptMapper ()
{
// Create manager.
CreateAndInstallGameManager (ManagerContext::kScriptMapper);
#if UNITY_EDITOR
// Populate with builtin shaders. Only available in editor.
GetBuiltinExtraResourceManager ().RegisterShadersWithRegistry (GetScriptMapperPtr ());
#endif
}
static void InitializeGraphicsSettings ()
{
CreateAndInstallGameManager (ManagerContext::kGraphicsSettings);
GetGraphicsSettings ().SetDefaultAlwaysIncludedShaders ();
}
static void InitializePhysicsManager ()
{
#if ENABLE_PHYSICS
CreateAndInstallGameManager (ManagerContext::kPhysicsManager);
#endif
}
static void InitializeTagManager ()
{
CreateAndInstallGameManager (ManagerContext::kTagManager);
RegisterDefaultTagsAndLayerMasks ();
}
static void InitializeBuildSettings ()
{
CreateAndInstallGameManager (ManagerContext::kBuildSettings);
}
#endif
void RunUnitTestsIfRequiredAndExit ()
{
#if !ENABLE_UNIT_TESTS
return;
#else
const bool listUnitTests = HasARGV ("listUnitTests");
const bool runUnitTests = HasARGV ("runUnitTests");
// Check if we are supposed to run or list unit tests.
if (!listUnitTests && !runUnitTests)
return;
// Yes, so initialize the systems we need.
// We log to stdout so get that in place on Windows.
#if UNITY_WIN
OpenConsole ();
#endif
// For unit testing, we're not interested in log output from engine initialization.
// Temporarily disable logging.
SetLogEntryHandler (SwallowLogMessages);
// We want the test runner to behave like batchmode and not pop up
// any dialogs, so force running in batchmode.
SetIsBatchmode (true);
// Start with persistent manager. Required by InitializeEngineNoGraphics().
UNITY_NEW_AS_ROOT (PathNamePersistentManager (0), kMemManager, "PersistentManager", "");
if (!InitializeEngineNoGraphics ())
{
fprintf (stderr, "Failed to initialize engine (no graphics)!\n");
exit (-1);
}
if (!InitializeEngineGraphics ())
{
fprintf (stderr, "Failed to initialize engine!\n");
exit (-1);
}
// Initialize the global game managers we want to have available.
InitializeScriptMapper ();
InitializeGraphicsSettings ();
InitializePhysicsManager ();
InitializeTagManager ();
InitializeBuildSettings ();
////TODO: this path is broken; the EXPECT stuff doesn't work for it
std::string log;
#if 0
if (HasARGV ("unitTestsLog"))
log = GetFirstValueForARGV ("unitTestsLog");
#endif
const bool showOnlySummary = HasARGV ("unitTestsSummaryOnly");
std::vector<std::string> matchNames;
if (runUnitTests)
matchNames = GetValuesForARGV ("runUnitTests");
else if (listUnitTests)
matchNames = GetValuesForARGV ("listUnitTests");
TestFilter filter (matchNames);
// Run or list tests.
int numFailures = 0;
if (runUnitTests)
{
numFailures = RunUnitTests (log, showOnlySummary, filter);
}
else if (listUnitTests)
{
SetLogEntryHandler (NULL);
PrintUnitTestList (filter);
}
// Shut down.
CleanupEngine ();
InputShutdown ();
// Done.
exit (numFailures != 0 ? 1 : 0);
#endif // ENABLE_UNIT_TESTS
}
#endif // UNITY_EXTERNAL_TOOL
int RunUnitTests (const std::string& resultLog)
{
#if !ENABLE_UNIT_TESTS
return 0;
#else
return RunUnitTests (resultLog, false, UnitTest::True ());
#endif
}
void ExpectLogMessageTriggeredByTest (LogType type, const char* logFragment)
{
#if ENABLE_UNIT_TESTS
ConsoleTestReporter::GetInstance ()->ExpectLogMessage (type, logFragment);
#endif
}
#if ENABLE_PERFORMANCE_TESTS
void TestMultiplyMatrices();
void RUN_PERFORMANCE_TESTS ()
{
TestMultiplyMatrices();
}
#endif
|