27 : name (nm), category (ctg)
51 if (test->getCategory() == category)
62 if (test->getCategory().isNotEmpty())
73 jassert (newRunner !=
nullptr);
84 jassert (runner !=
nullptr);
92 jassert (runner !=
nullptr);
94 runner->beginNewTest (
this, testName);
100 jassert (runner !=
nullptr);
105 runner->addFail (failureMessage);
111 jassert (runner !=
nullptr);
113 return runner->randomForTest;
117 UnitTestRunner::UnitTestRunner() {}
122 assertOnFailure = shouldAssert;
127 logPasses = shouldDisplayPasses;
132 return results.size();
137 return results [index];
152 randomForTest =
Random (randomSeed);
155 for (
auto* t : tests)
157 if (shouldAbortTests())
160 #if JUCE_EXCEPTIONS_DISABLED 161 t->performTest (
this);
165 t->performTest (
this);
169 addFail (
"An unhandled exception was thrown!");
197 void UnitTestRunner::beginNewTest (
UnitTest*
const test,
const String& subCategory)
204 r->unitTestName = test->
getName();
205 r->subcategoryName = subCategory;
209 logMessage (
"-----------------------------------------------------------------");
210 logMessage (
"Starting test: " + r->unitTestName +
" / " + subCategory +
"...");
215 void UnitTestRunner::endTest()
217 if (
auto* r = results.getLast())
222 m << r->failures << (r->failures == 1 ?
" test" :
" tests")
223 <<
" failed, out of a total of " << (r->passes + r->failures);
231 logMessage (
"All tests completed successfully");
236 void UnitTestRunner::addPass()
241 auto* r = results.getLast();
242 jassert (r !=
nullptr);
249 message << (r->failures + r->passes) <<
" passed";
257 void UnitTestRunner::addFail (
const String& failureMessage)
262 auto* r = results.getLast();
263 jassert (r !=
nullptr);
267 String message (
"!!! Test ");
268 message << (r->failures + r->passes) <<
" failed";
271 message <<
": " << failureMessage;
273 r->messages.add (message);
280 if (assertOnFailure) { jassertfalse; }
void runAllTests(int64 randomSeed=0)
Runs all the UnitTest objects that currently exist.
virtual void resultsUpdated()
Called when the list of results changes.
bool isNotEmpty() const noexcept
Returns true if the string contains at least one character.
static Array< UnitTest * > getTestsInCategory(const String &category)
Returns the set of UnitTests in a specified category.
virtual void logMessage(const String &message)
Logs a message about the current test progress.
int nextInt() noexcept
Returns the next random 32 bit integer.
void add(const ElementType &newElement)
Appends a new element at the end of the array.
UnitTest(const String &name, const String &category=String())
Creates a test with the given name and optionally places it in a category.
const String & getName() const noexcept
Returns the name of the test.
void performTest(UnitTestRunner *runner)
Runs the test, using the specified UnitTestRunner.
void logMessage(const String &message)
Writes a message to the test log.
A special array for holding a list of strings.
virtual void runTest()=0
Implement this method in your subclass to actually run your tests.
void runTestsInCategory(const String &category, int64 randomSeed=0)
Runs all the UnitTest objects within a specified category.
virtual void shutdown()
You can optionally implement this method to clear up after your test has been run.
This is a base class for classes that perform a unit test.
virtual bool shouldAbortTests()
This can be overridden to let the runner know that it should abort the tests as soon as possible...
virtual ~UnitTest()
Destructor.
const TestResult * getResult(int index) const noexcept
Returns one of the TestResult objects that describes a test that has been run.
void beginTest(const String &testName)
Tells the system that a new subsection of tests is beginning.
Runs a set of unit tests.
void runTests(const Array< UnitTest *> &tests, int64 randomSeed=0)
Runs a set of tests.
static StringArray getAllCategories()
Returns a StringArray containing all of the categories of UnitTests that have been registered...
static String toHexString(IntegerType number)
Returns a string representing this numeric value in hexadecimal.
virtual void initialise()
You can optionally implement this method to set up your test.
Random getRandom() const
Returns a shared RNG that all unit tests should use.
bool addIfNotAlreadyThere(const String &stringToAdd, bool ignoreCase=false)
Adds a string to the array as long as it's not already in there.
static Array< UnitTest * > & getAllTests()
Returns the set of all UnitTest objects that currently exist.
Holds a resizable array of primitive or copy-by-value objects.
int getNumResults() const noexcept
Returns the number of TestResult objects that have been performed.
bool isEmpty() const noexcept
Returns true if the string contains no characters.
void setAssertOnFailure(bool shouldAssert) noexcept
Sets a flag to indicate whether an assertion should be triggered if a test fails. ...
void expect(bool testResult, const String &failureMessage=String())
Checks that the result of a test is true, and logs this result.
static void JUCE_CALLTYPE writeToLog(const String &message)
Writes a string to the current logger.
A random number generator.
Contains the results of a test.
virtual ~UnitTestRunner()
Destructor.
void setPassesAreLogged(bool shouldDisplayPasses) noexcept
Sets a flag to indicate whether successful tests should be logged.
Automatically locks and unlocks a mutex object.