LMMS
Loading...
Searching...
No Matches
juce_UnitTest.cpp
Go to the documentation of this file.
1/*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
6
7 JUCE is an open source library subject to commercial or open-source
8 licensing.
9
10 The code included in this file is provided under the terms of the ISC license
11 http://www.isc.org/downloads/software-support-policy/isc-license. Permission
12 To use, copy, modify, and/or distribute this software for any purpose with or
13 without fee is hereby granted provided that the above copyright notice and
14 this permission notice appear in all copies.
15
16 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
17 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
18 DISCLAIMED.
19
20 ==============================================================================
21*/
22
23namespace juce
24{
25
26UnitTest::UnitTest (const String& nm, const String& ctg)
27 : name (nm), category (ctg)
28{
29 getAllTests().add (this);
30}
31
33{
34 getAllTests().removeFirstMatchingValue (this);
35}
36
42
44{
45 if (category.isEmpty())
46 return getAllTests();
47
48 Array<UnitTest*> unitTests;
49
50 for (auto* test : getAllTests())
51 if (test->getCategory() == category)
52 unitTests.add (test);
53
54 return unitTests;
55}
56
58{
59 StringArray categories;
60
61 for (auto* test : getAllTests())
62 if (test->getCategory().isNotEmpty())
63 categories.addIfNotAlreadyThere (test->getCategory());
64
65 return categories;
66}
67
70
72{
73 jassert (newRunner != nullptr);
74 runner = newRunner;
75
76 initialise();
77 runTest();
78 shutdown();
79}
80
82{
83 // This method's only valid while the test is being run!
84 jassert (runner != nullptr);
85
86 runner->logMessage (message);
87}
88
89void UnitTest::beginTest (const String& testName)
90{
91 // This method's only valid while the test is being run!
92 jassert (runner != nullptr);
93
94 runner->beginNewTest (this, testName);
95}
96
97void UnitTest::expect (const bool result, const String& failureMessage)
98{
99 // This method's only valid while the test is being run!
100 jassert (runner != nullptr);
101
102 if (result)
103 runner->addPass();
104 else
105 runner->addFail (failureMessage);
106}
107
109{
110 // This method's only valid while the test is being run!
111 jassert (runner != nullptr);
112
113 return runner->randomForTest;
114}
115
116//==============================================================================
119
120void UnitTestRunner::setAssertOnFailure (bool shouldAssert) noexcept
121{
122 assertOnFailure = shouldAssert;
123}
124
125void UnitTestRunner::setPassesAreLogged (bool shouldDisplayPasses) noexcept
126{
127 logPasses = shouldDisplayPasses;
128}
129
131{
132 return results.size();
133}
134
136{
137 return results [index];
138}
139
143
145{
146 results.clear();
148
149 if (randomSeed == 0)
150 randomSeed = Random().nextInt (0x7ffffff);
151
152 randomForTest = Random (randomSeed);
153 logMessage ("Random seed: 0x" + String::toHexString (randomSeed));
154
155 for (auto* t : tests)
156 {
157 if (shouldAbortTests())
158 break;
159
160 #if JUCE_EXCEPTIONS_DISABLED
161 t->performTest (this);
162 #else
163 try
164 {
165 t->performTest (this);
166 }
167 catch (...)
168 {
169 addFail ("An unhandled exception was thrown!");
170 }
171 #endif
172 }
173
174 endTest();
175}
176
178{
179 runTests (UnitTest::getAllTests(), randomSeed);
180}
181
182void UnitTestRunner::runTestsInCategory (const String& category, int64 randomSeed)
183{
184 runTests (UnitTest::getTestsInCategory (category), randomSeed);
185}
186
191
193{
194 return false;
195}
196
197void UnitTestRunner::beginNewTest (UnitTest* const test, const String& subCategory)
198{
199 endTest();
201
202 auto testName = test->getName();
203 results.add (new TestResult (testName, subCategory));
204
205 logMessage ("-----------------------------------------------------------------");
206 logMessage ("Starting test: " + testName + " / " + subCategory + "...");
207
209}
210
212{
213 if (auto* r = results.getLast())
214 {
215 r->endTime = Time::getCurrentTime();
216
217 if (r->failures > 0)
218 {
219 String m ("FAILED!! ");
220 m << r->failures << (r->failures == 1 ? " test" : " tests")
221 << " failed, out of a total of " << (r->passes + r->failures);
222
223 logMessage (String());
224 logMessage (m);
225 logMessage (String());
226 }
227 else
228 {
229 logMessage ("All tests completed successfully");
230 }
231 }
232}
233
235{
236 {
237 const ScopedLock sl (results.getLock());
238
239 auto* r = results.getLast();
240 jassert (r != nullptr); // You need to call UnitTest::beginTest() before performing any tests!
241
242 r->passes++;
243
244 if (logPasses)
245 {
246 String message ("Test ");
247 message << (r->failures + r->passes) << " passed";
249 }
250 }
251
253}
254
255void UnitTestRunner::addFail (const String& failureMessage)
256{
257 {
258 const ScopedLock sl (results.getLock());
259
260 auto* r = results.getLast();
261 jassert (r != nullptr); // You need to call UnitTest::beginTest() before performing any tests!
262
263 r->failures++;
264
265 String message ("!!! Test ");
266 message << (r->failures + r->passes) << " failed";
267
268 if (failureMessage.isNotEmpty())
269 message << ": " << failureMessage;
270
271 r->messages.add (message);
272
274 }
275
277
279}
280
281} // namespace juce
#define noexcept
Definition DistrhoDefines.h:72
static void shutdown(void)
Definition adplugdb.cpp:297
static void message(int level, const char *fmt,...)
Definition adplugdb.cpp:120
Definition juce_Array.h:56
void add(const ElementType &newElement)
Definition juce_Array.h:418
static void JUCE_CALLTYPE writeToLog(const String &message)
Definition juce_Logger.cpp:40
Definition juce_Random.h:35
int nextInt() noexcept
Definition juce_Random.cpp:74
Definition juce_StringArray.h:35
bool addIfNotAlreadyThere(const String &stringToAdd, bool ignoreCase=false)
Definition juce_StringArray.cpp:150
Definition juce_String.h:53
static String toHexString(IntegerType number)
Definition juce_String.h:1097
bool isNotEmpty() const noexcept
Definition juce_String.h:316
static Time JUCE_CALLTYPE getCurrentTime() noexcept
Definition juce_Time.cpp:233
void logMessage(const String &message)
Definition juce_UnitTest.cpp:81
static StringArray getAllCategories()
Definition juce_UnitTest.cpp:57
static Array< UnitTest * > getTestsInCategory(const String &category)
Definition juce_UnitTest.cpp:43
const String category
Definition juce_UnitTest.h:303
virtual void initialise()
Definition juce_UnitTest.cpp:68
UnitTest(const String &name, const String &category=String())
Definition juce_UnitTest.cpp:26
static Array< UnitTest * > & getAllTests()
Definition juce_UnitTest.cpp:37
void beginTest(const String &testName)
Definition juce_UnitTest.cpp:89
void expect(bool testResult, const String &failureMessage=String())
Definition juce_UnitTest.cpp:97
UnitTestRunner * runner
Definition juce_UnitTest.h:304
virtual void shutdown()
Definition juce_UnitTest.cpp:69
virtual void runTest()=0
const String name
Definition juce_UnitTest.h:303
Random getRandom() const
Definition juce_UnitTest.cpp:108
virtual ~UnitTest()
Definition juce_UnitTest.cpp:32
void performTest(UnitTestRunner *runner)
Definition juce_UnitTest.cpp:71
Definition juce_UnitTest.h:325
UnitTest * currentTest
Definition juce_UnitTest.h:437
void runAllTests(int64 randomSeed=0)
Definition juce_UnitTest.cpp:177
void runTestsInCategory(const String &category, int64 randomSeed=0)
Definition juce_UnitTest.cpp:182
virtual ~UnitTestRunner()
Definition juce_UnitTest.cpp:118
virtual bool shouldAbortTests()
Definition juce_UnitTest.cpp:192
const TestResult * getResult(int index) const noexcept
Definition juce_UnitTest.cpp:135
void beginNewTest(UnitTest *test, const String &subCategory)
Definition juce_UnitTest.cpp:197
int getNumResults() const noexcept
Definition juce_UnitTest.cpp:130
void setAssertOnFailure(bool shouldAssert) noexcept
Definition juce_UnitTest.cpp:120
friend class UnitTest
Definition juce_UnitTest.h:435
void runTests(const Array< UnitTest * > &tests, int64 randomSeed=0)
Definition juce_UnitTest.cpp:144
bool assertOnFailure
Definition juce_UnitTest.h:440
void endTest()
Definition juce_UnitTest.cpp:211
bool logPasses
Definition juce_UnitTest.h:440
void setPassesAreLogged(bool shouldDisplayPasses) noexcept
Definition juce_UnitTest.cpp:125
virtual void logMessage(const String &message)
Definition juce_UnitTest.cpp:187
void addPass()
Definition juce_UnitTest.cpp:234
void addFail(const String &failureMessage)
Definition juce_UnitTest.cpp:255
OwnedArray< TestResult, CriticalSection > results
Definition juce_UnitTest.h:439
Random randomForTest
Definition juce_UnitTest.h:441
UnitTestRunner()
Definition juce_UnitTest.cpp:117
virtual void resultsUpdated()
Definition juce_UnitTest.cpp:140
unsigned * m
Definition inflate.c:1559
struct huft * t
Definition inflate.c:943
#define jassert(expression)
#define jassertfalse
static struct TestCase tests[]
Definition lilv_test.c:2218
Definition carla_juce.cpp:31
CriticalSection::ScopedLockType ScopedLock
Definition juce_CriticalSection.h:186
long long int64
Definition juce_MathsFunctions.h:54
static int test(SerdEnv *env, bool top_level, bool pretty_numbers)
Definition sratom_test.c:79
Definition juce_UnitTest.h:378
int r
Definition crypt.c:458
int result
Definition process.c:1455
#define const
Definition zconf.h:137