Loading...
Searching...
No Matches
juce_UnitTest.h
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2022 - Raw Material Software Limited
JUCE is an open source library subject to commercial or open-source
licensing.
The code included in this file is provided under the terms of the ISC license
http://www.isc.org/downloads/software-support-policy/isc-license. Permission
To use, copy, modify, and/or distribute this software for any purpose with or
without fee is hereby granted provided that the above copyright notice and
this permission notice appear in all copies.
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
DISCLAIMED.
==============================================================================
*/
namespace juce
{
class UnitTestRunner;
//==============================================================================
class JUCE_API UnitTest
{
public:
//==============================================================================
explicit UnitTest (const String& name, const String& category = String());
virtual ~UnitTest();
const String& getName() const noexcept { return name; }
const String& getCategory() const noexcept { return category; }
void performTest (UnitTestRunner* runner);
static Array<UnitTest*>& getAllTests();
static Array<UnitTest*> getTestsInCategory (const String& category);
static StringArray getAllCategories();
//==============================================================================
virtual void initialise();
virtual void shutdown();
virtual void runTest() = 0;
//==============================================================================
void beginTest (const String& testName);
//==============================================================================
void expect (bool testResult, const String& failureMessage = String());
//==============================================================================
template <class ValueType>
void expectEquals (ValueType actual, ValueType expected, String failureMessage = String())
{
bool result = exactlyEqual (actual, expected);
expectResultAndPrint (actual, expected, result, "", failureMessage);
}
template <class ValueType>
void expectNotEquals (ValueType value, ValueType valueToCompareTo, String failureMessage = String())
{
bool result = ! exactlyEqual (value, valueToCompareTo);
expectResultAndPrint (value, valueToCompareTo, result, "unequal to", failureMessage);
}
template <class ValueType>
void expectGreaterThan (ValueType value, ValueType valueToCompareTo, String failureMessage = String())
{
bool result = value > valueToCompareTo;
expectResultAndPrint (value, valueToCompareTo, result, "greater than", failureMessage);
}
template <class ValueType>
void expectLessThan (ValueType value, ValueType valueToCompareTo, String failureMessage = String())
{
bool result = value < valueToCompareTo;
expectResultAndPrint (value, valueToCompareTo, result, "less than", failureMessage);
}
template <class ValueType>
void expectGreaterOrEqual (ValueType value, ValueType valueToCompareTo, String failureMessage = String())
{
bool result = value >= valueToCompareTo;
expectResultAndPrint (value, valueToCompareTo, result, "greater or equal to", failureMessage);
}
template <class ValueType>
void expectLessOrEqual (ValueType value, ValueType valueToCompareTo, String failureMessage = String())
{
bool result = value <= valueToCompareTo;
expectResultAndPrint (value, valueToCompareTo, result, "less or equal to", failureMessage);
}
template <class ValueType>
void expectWithinAbsoluteError (ValueType actual, ValueType expected, ValueType maxAbsoluteError, String failureMessage = String())
{
const ValueType diff = std::abs (actual - expected);
const bool result = diff <= maxAbsoluteError;
expectResultAndPrint (actual, expected, result, " within " + String (maxAbsoluteError) + " of" , failureMessage);
}
//==============================================================================
#define expectDoesNotThrow(expr) \
try \
{ \
(expr); \
expect (true); \
} \
catch (...) \
{ \
expect (false, "Expected: does not throw an exception, Actual: throws."); \
}
#define expectThrows(expr) \
try \
{ \
(expr); \
expect (false, "Expected: throws an exception, Actual: does not throw."); \
} \
catch (...) \
{ \
expect (true); \
}
#define expectThrowsType(expr, type) \
try \
{ \
(expr); \
expect (false, "Expected: throws an exception of type " #type ", Actual: does not throw."); \
} \
catch (type&) \
{ \
expect (true); \
} \
catch (...) \
{ \
expect (false, "Expected: throws an exception of type " #type ", Actual: throws another type."); \
}
//==============================================================================
void logMessage (const String& message);
Random getRandom() const;
private:
//==============================================================================
template <class ValueType>
void expectResultAndPrint (ValueType value, ValueType valueToCompareTo, bool result,
String compDescription, String failureMessage)
{
if (! result)
{
if (failureMessage.isNotEmpty())
failureMessage << " -- ";
failureMessage << "Expected value" << (compDescription.isEmpty() ? "" : " ")
<< compDescription << ": " << valueToCompareTo
<< ", Actual value: " << value;
}
expect (result, failureMessage);
}
//==============================================================================
const String name, category;
UnitTestRunner* runner = nullptr;
JUCE_DECLARE_NON_COPYABLE (UnitTest)
};
//==============================================================================
class JUCE_API UnitTestRunner
{
public:
//==============================================================================
UnitTestRunner();
virtual ~UnitTestRunner();
void runTests (const Array<UnitTest*>& tests, int64 randomSeed = 0);
void runAllTests (int64 randomSeed = 0);
void runTestsInCategory (const String& category, int64 randomSeed = 0);
void setAssertOnFailure (bool shouldAssert) noexcept;
void setPassesAreLogged (bool shouldDisplayPasses) noexcept;
//==============================================================================
struct TestResult
{
TestResult() = default;
explicit TestResult (const String& name, const String& subCategory)
: unitTestName (name),
subcategoryName (subCategory)
{
}
String unitTestName;
String subcategoryName;
int passes = 0;
int failures = 0;
StringArray messages;
Time startTime = Time::getCurrentTime();
Time endTime;
};
int getNumResults() const noexcept;
const TestResult* getResult (int index) const noexcept;
protected:
virtual void resultsUpdated();
virtual void logMessage (const String& message);
virtual bool shouldAbortTests();
private:
//==============================================================================
friend class UnitTest;
UnitTest* currentTest = nullptr;
String currentSubCategory;
OwnedArray<TestResult, CriticalSection> results;
bool assertOnFailure = true, logPasses = false;
Random randomForTest;
void beginNewTest (UnitTest* test, const String& subCategory);
void endTest();
void addPass();
void addFail (const String& failureMessage);
JUCE_DECLARE_NON_COPYABLE (UnitTestRunner)
};
} // namespace juce
juce::Array
Definition juce_Array.h:56
juce::CriticalSection
Definition juce_CriticalSection.h:43
juce::OwnedArray
Definition juce_OwnedArray.h:50
juce::Random
Definition juce_Random.h:35
juce::StringArray
Definition juce_StringArray.h:35
juce::String
Definition juce_String.h:53
juce::String::isEmpty
bool isEmpty() const noexcept
Definition juce_String.h:310
juce::String::isNotEmpty
bool isNotEmpty() const noexcept
Definition juce_String.h:316
juce::Time
Definition juce_Time.h:37
juce::Time::getCurrentTime
static Time JUCE_CALLTYPE getCurrentTime() noexcept
Definition juce_Time.cpp:233
juce::UnitTestRunner
Definition juce_UnitTest.h:325
juce::UnitTestRunner::runAllTests
void runAllTests(int64 randomSeed=0)
Definition juce_UnitTest.cpp:177
juce::UnitTestRunner::runTestsInCategory
void runTestsInCategory(const String &category, int64 randomSeed=0)
Definition juce_UnitTest.cpp:182
juce::UnitTestRunner::shouldAbortTests
virtual bool shouldAbortTests()
Definition juce_UnitTest.cpp:192
juce::UnitTestRunner::getResult
const TestResult * getResult(int index) const noexcept
Definition juce_UnitTest.cpp:135
juce::UnitTestRunner::getNumResults
int getNumResults() const noexcept
Definition juce_UnitTest.cpp:130
juce::UnitTestRunner::setAssertOnFailure
void setAssertOnFailure(bool shouldAssert) noexcept
Definition juce_UnitTest.cpp:120
juce::UnitTestRunner::runTests
void runTests(const Array< UnitTest * > &tests, int64 randomSeed=0)
Definition juce_UnitTest.cpp:144
juce::UnitTestRunner::setPassesAreLogged
void setPassesAreLogged(bool shouldDisplayPasses) noexcept
Definition juce_UnitTest.cpp:125
juce::UnitTestRunner::logMessage
virtual void logMessage(const String &message)
Definition juce_UnitTest.cpp:187
juce::UnitTestRunner::resultsUpdated
virtual void resultsUpdated()
Definition juce_UnitTest.cpp:140
juce::UnitTest::getName
const String & getName() const noexcept
Definition juce_UnitTest.h:80
juce::UnitTest::getCategory
const String & getCategory() const noexcept
Definition juce_UnitTest.h:83
juce::UnitTest::expectEquals
void expectEquals(ValueType actual, ValueType expected, String failureMessage=String())
Definition juce_UnitTest.h:151
juce::UnitTest::expectGreaterThan
void expectGreaterThan(ValueType value, ValueType valueToCompareTo, String failureMessage=String())
Definition juce_UnitTest.h:171
juce::UnitTest::UnitTest
UnitTest(const String &name, const String &category=String())
Definition juce_UnitTest.cpp:26
juce::UnitTest::beginTest
void beginTest(const String &testName)
Definition juce_UnitTest.cpp:89
juce::UnitTest::expect
void expect(bool testResult, const String &failureMessage=String())
Definition juce_UnitTest.cpp:97
juce::UnitTest::expectWithinAbsoluteError
void expectWithinAbsoluteError(ValueType actual, ValueType expected, ValueType maxAbsoluteError, String failureMessage=String())
Definition juce_UnitTest.h:212
juce::UnitTest::runTest
virtual void runTest()=0
juce::UnitTest::expectLessThan
void expectLessThan(ValueType value, ValueType valueToCompareTo, String failureMessage=String())
Definition juce_UnitTest.h:181
juce::UnitTest::expectLessOrEqual
void expectLessOrEqual(ValueType value, ValueType valueToCompareTo, String failureMessage=String())
Definition juce_UnitTest.h:201
juce::UnitTest::expectNotEquals
void expectNotEquals(ValueType value, ValueType valueToCompareTo, String failureMessage=String())
Definition juce_UnitTest.h:161
juce::UnitTest::expectGreaterOrEqual
void expectGreaterOrEqual(ValueType value, ValueType valueToCompareTo, String failureMessage=String())
Definition juce_UnitTest.h:191
juce::UnitTestRunner::TestResult
Definition juce_UnitTest.h:378
juce::UnitTestRunner::TestResult::failures
int failures
Definition juce_UnitTest.h:395
juce::UnitTestRunner::TestResult::endTime
Time endTime
Definition juce_UnitTest.h:403
juce::UnitTestRunner::TestResult::messages
StringArray messages
Definition juce_UnitTest.h:398
juce::UnitTestRunner::TestResult::startTime
Time startTime
Definition juce_UnitTest.h:401
juce::UnitTestRunner::TestResult::unitTestName
String unitTestName
Definition juce_UnitTest.h:388
juce::UnitTestRunner::TestResult::subcategoryName
String subcategoryName
Definition juce_UnitTest.h:390
juce::UnitTestRunner::TestResult::passes
int passes
Definition juce_UnitTest.h:393