diff --git a/Code/AutomatedTests/IsBetweenTest.cpp b/Code/AutomatedTests/IsBetweenTest.cpp index 11e4c19..e3a33f0 100644 --- a/Code/AutomatedTests/IsBetweenTest.cpp +++ b/Code/AutomatedTests/IsBetweenTest.cpp @@ -5,6 +5,8 @@ #include "IsBetweenTest.hpp" #include #include +#include +#include namespace maxAutomatedTests { @@ -13,24 +15,25 @@ namespace Algorithms void RunIsBetweenTestSuite() { - auto IsBetweenTestSuite = max::Testing::TestSuite{ "max::Algorithms::IsBetween test suite" }; + max::Testing::CoutResultPolicy ResultPolicy; + auto IsBetweenTestSuite = max::Testing::TestSuite< max::Testing::CoutResultPolicy >{ "max::Algorithms::IsBetween test suite", std::move( ResultPolicy ) }; - IsBetweenTestSuite.AddTest( max::Testing::Test{ "inside range", []( max::Testing::Test & CurrentTest ) { + IsBetweenTestSuite.AddTest( max::Testing::Test< max::Testing::CoutResultPolicy >{ "inside range", []( max::Testing::Test< max::Testing::CoutResultPolicy > & CurrentTest, max::Testing::CoutResultPolicy const & ResultPolicy ) { CurrentTest.MAX_TESTING_ASSERT( max::Algorithms::IsBetween( 10, { 1, 100 } ) ); } } ); - IsBetweenTestSuite.AddTest( max::Testing::Test{ "before range", []( max::Testing::Test & CurrentTest ) { + IsBetweenTestSuite.AddTest( max::Testing::Test< max::Testing::CoutResultPolicy >{ "before range", []( max::Testing::Test< max::Testing::CoutResultPolicy > & CurrentTest, max::Testing::CoutResultPolicy const & ResultPolicy ) { CurrentTest.MAX_TESTING_ASSERT( ! max::Algorithms::IsBetween( 0, { 1, 100 } ) ); } } ); - IsBetweenTestSuite.AddTest( max::Testing::Test{ "after range", []( max::Testing::Test & CurrentTest ) { + IsBetweenTestSuite.AddTest( max::Testing::Test< max::Testing::CoutResultPolicy >{ "after range", []( max::Testing::Test< max::Testing::CoutResultPolicy > & CurrentTest, max::Testing::CoutResultPolicy const & ResultPolicy ) { CurrentTest.MAX_TESTING_ASSERT( ! max::Algorithms::IsBetween( 200, { 1, 100 } ) ); } } ); - IsBetweenTestSuite.AddTest( max::Testing::Test{ "minimum and maximum switched", []( max::Testing::Test & CurrentTest ) { + IsBetweenTestSuite.AddTest( max::Testing::Test< max::Testing::CoutResultPolicy >{ "minimum and maximum switched", []( max::Testing::Test< max::Testing::CoutResultPolicy > & CurrentTest, max::Testing::CoutResultPolicy const & ResultPolicy ) { CurrentTest.MAX_TESTING_ASSERT( ! max::Algorithms::IsBetween( 10, { 100, 1 } ) ); } } ); diff --git a/Code/AutomatedTests/RangeTest.cpp b/Code/AutomatedTests/RangeTest.cpp index fcefdb2..71d6b9b 100644 --- a/Code/AutomatedTests/RangeTest.cpp +++ b/Code/AutomatedTests/RangeTest.cpp @@ -5,6 +5,8 @@ #include "RangeTest.hpp" #include #include +#include +#include namespace maxAutomatedTests { @@ -15,31 +17,31 @@ namespace Containers { static_assert( max::Containers::Range< int >{ 0, 1 }.Maximum == 1, "max::Containers::Range ctor should be constexpr" ); + max::Testing::CoutResultPolicy ResultPolicy; + auto RangeTestSuite = max::Testing::TestSuite< max::Testing::CoutResultPolicy >{ "max::Containers::Range test suite", std::move( ResultPolicy ) }; - auto RangeTestSuite = max::Testing::TestSuite{ "max::Containers::Range test suite" }; - - RangeTestSuite.AddTest( max::Testing::Test{ "constructor sets minimum and maximum correctly", []( max::Testing::Test & CurrentTest ) { + RangeTestSuite.AddTest( max::Testing::Test< max::Testing::CoutResultPolicy >{ "constructor sets minimum and maximum correctly", []( max::Testing::Test< max::Testing::CoutResultPolicy > & CurrentTest, max::Testing::CoutResultPolicy const & ResultPolicy ) { auto testRange = max::Containers::Range< int >{ 42, 100 }; CurrentTest.MAX_TESTING_ASSERT( testRange.Minimum == 42 ); CurrentTest.MAX_TESTING_ASSERT( testRange.Maximum == 100 ); } } ); - RangeTestSuite.AddTest( max::Testing::Test{ "constructor accepts same values", []( max::Testing::Test & CurrentTest ) { + RangeTestSuite.AddTest( max::Testing::Test< max::Testing::CoutResultPolicy >{ "constructor accepts same values", []( max::Testing::Test< max::Testing::CoutResultPolicy > & CurrentTest, max::Testing::CoutResultPolicy const & ResultPolicy ) { auto testRange = max::Containers::Range< int >{ 42, 42 }; CurrentTest.MAX_TESTING_ASSERT( testRange.Minimum == 42 ); CurrentTest.MAX_TESTING_ASSERT( testRange.Maximum == 42 ); } } ); - RangeTestSuite.AddTest( max::Testing::Test{ "MakeRange with minimum first", []( max::Testing::Test & CurrentTest ) { + RangeTestSuite.AddTest( max::Testing::Test< max::Testing::CoutResultPolicy >{ "MakeRange with minimum first", []( max::Testing::Test< max::Testing::CoutResultPolicy > & CurrentTest, max::Testing::CoutResultPolicy const & ResultPolicy ) { auto testRange = max::Containers::MakeRange( 1, 42 ); CurrentTest.MAX_TESTING_ASSERT( testRange.Minimum == 1 ); CurrentTest.MAX_TESTING_ASSERT( testRange.Maximum == 42 ); } } ); - RangeTestSuite.AddTest( max::Testing::Test{ "MakeRange with maximum first", []( max::Testing::Test & CurrentTest ) { + RangeTestSuite.AddTest( max::Testing::Test< max::Testing::CoutResultPolicy >{ "MakeRange with maximum first", []( max::Testing::Test< max::Testing::CoutResultPolicy > & CurrentTest, max::Testing::CoutResultPolicy const & ResultPolicy ) { auto testRange = max::Containers::MakeRange( 100, 42 ); CurrentTest.MAX_TESTING_ASSERT( testRange.Minimum == 42 ); CurrentTest.MAX_TESTING_ASSERT( testRange.Maximum == 100 ); diff --git a/Code/AutomatedTests/VectorTest.cpp b/Code/AutomatedTests/VectorTest.cpp index ff9978b..26f571d 100644 --- a/Code/AutomatedTests/VectorTest.cpp +++ b/Code/AutomatedTests/VectorTest.cpp @@ -5,6 +5,7 @@ #include "VectorTest.hpp" #include #include +#include #include namespace maxAutomatedTests @@ -14,9 +15,10 @@ namespace Containers void RunVectorTestSuite() { - auto VectorTestSuite = max::Testing::TestSuite{ "max::Containers::Vector test suite" }; + max::Testing::CoutResultPolicy ResultPolicy; + auto VectorTestSuite = max::Testing::TestSuite< max::Testing::CoutResultPolicy >{ "max::Containers::Vector test suite", std::move( ResultPolicy ) }; - VectorTestSuite.AddTest( max::Testing::Test{ "initializer_list constructor populates elements", []( max::Testing::Test & CurrentTest ) { + VectorTestSuite.AddTest( max::Testing::Test< max::Testing::CoutResultPolicy >{ "initializer_list constructor populates elements", []( max::Testing::Test< max::Testing::CoutResultPolicy > & CurrentTest, max::Testing::CoutResultPolicy const & ResultPolicy ) { max::Containers::Vector< int, 3 > Test = { 1, 2, 3 }; CurrentTest.MAX_TESTING_ASSERT( Test[ 0 ] == 1 ); @@ -25,7 +27,7 @@ namespace Containers } } ); - VectorTestSuite.AddTest( max::Testing::Test{ "copy constructor copies elements", []( max::Testing::Test & CurrentTest ) { + VectorTestSuite.AddTest( max::Testing::Test< max::Testing::CoutResultPolicy >{ "copy constructor copies elements", []( max::Testing::Test< max::Testing::CoutResultPolicy > & CurrentTest, max::Testing::CoutResultPolicy const & ResultPolicy ) { max::Containers::Vector< int, 3 > Original = { 1, 2, 3 }; max::Containers::Vector< int, 3 > Test = Original; @@ -36,7 +38,7 @@ namespace Containers } } ); - VectorTestSuite.AddTest( max::Testing::Test{ "move constructor moves elements", []( max::Testing::Test & CurrentTest ) { + VectorTestSuite.AddTest( max::Testing::Test< max::Testing::CoutResultPolicy >{ "move constructor moves elements", []( max::Testing::Test< max::Testing::CoutResultPolicy > & CurrentTest, max::Testing::CoutResultPolicy const & ResultPolicy ) { max::Containers::Vector< int, 3 > Original = { 1, 2, 3 }; max::Containers::Vector< int, 3 > Test = std::move( Original ); @@ -47,7 +49,7 @@ namespace Containers } } ); - VectorTestSuite.AddTest( max::Testing::Test{ "copy assignment operator copies elements", []( max::Testing::Test & CurrentTest ) { + VectorTestSuite.AddTest( max::Testing::Test< max::Testing::CoutResultPolicy >{ "copy assignment operator copies elements", []( max::Testing::Test< max::Testing::CoutResultPolicy > & CurrentTest, max::Testing::CoutResultPolicy const & ResultPolicy ) { max::Containers::Vector< int, 3 > Original = { 1, 2, 3 }; max::Containers::Vector< int, 3 > Test; @@ -59,7 +61,7 @@ namespace Containers } } ); - VectorTestSuite.AddTest( max::Testing::Test{ "move assignment operator moves elements", []( max::Testing::Test & CurrentTest ) { + VectorTestSuite.AddTest( max::Testing::Test< max::Testing::CoutResultPolicy >{ "move assignment operator moves elements", []( max::Testing::Test< max::Testing::CoutResultPolicy > & CurrentTest, max::Testing::CoutResultPolicy const & ResultPolicy ) { max::Containers::Vector< int, 3 > Original = { 1, 2, 3 }; max::Containers::Vector< int, 3 > Test; @@ -71,7 +73,7 @@ namespace Containers } } ); - VectorTestSuite.AddTest( max::Testing::Test{ "operator + adds two vectors", []( max::Testing::Test & CurrentTest ) { + VectorTestSuite.AddTest( max::Testing::Test< max::Testing::CoutResultPolicy >{ "operator + adds two vectors", []( max::Testing::Test< max::Testing::CoutResultPolicy > & CurrentTest, max::Testing::CoutResultPolicy const & ResultPolicy ) { max::Containers::Vector< int, 3 > lhs = { 1, 2, 3 }; max::Containers::Vector< int, 3 > rhs = { 4, 5, 6 }; @@ -83,7 +85,7 @@ namespace Containers } } ); - VectorTestSuite.AddTest( max::Testing::Test{ "operator - subtracts from the first vector", []( max::Testing::Test & CurrentTest ) { + VectorTestSuite.AddTest( max::Testing::Test< max::Testing::CoutResultPolicy >{ "operator - subtracts from the first vector", []( max::Testing::Test< max::Testing::CoutResultPolicy > & CurrentTest, max::Testing::CoutResultPolicy const & ResultPolicy ) { max::Containers::Vector< int, 3 > lhs = { 1, 2, 3 }; max::Containers::Vector< int, 3 > rhs = { 4, 6, 8 }; @@ -95,7 +97,7 @@ namespace Containers } } ); - VectorTestSuite.AddTest( max::Testing::Test{ "Cross function returns cross product", []( max::Testing::Test & CurrentTest ) { + VectorTestSuite.AddTest( max::Testing::Test< max::Testing::CoutResultPolicy >{ "Cross function returns cross product", []( max::Testing::Test< max::Testing::CoutResultPolicy > & CurrentTest, max::Testing::CoutResultPolicy const & ResultPolicy ) { max::Containers::Vector< int, 3 > lhs = { 1, 2, 3 }; max::Containers::Vector< int, 3 > rhs = { 4, 6, 8 }; @@ -107,7 +109,7 @@ namespace Containers } } ); - VectorTestSuite.AddTest( max::Testing::Test{ "Dot function returns dot product", []( max::Testing::Test & CurrentTest ) { + VectorTestSuite.AddTest( max::Testing::Test< max::Testing::CoutResultPolicy >{ "Dot function returns dot product", []( max::Testing::Test< max::Testing::CoutResultPolicy > & CurrentTest, max::Testing::CoutResultPolicy const & ResultPolicy ) { max::Containers::Vector< int, 3 > lhs = { 1, 2, 3 }; max::Containers::Vector< int, 3 > rhs = { 4, 6, 8 }; @@ -117,7 +119,7 @@ namespace Containers } } ); - VectorTestSuite.AddTest( max::Testing::Test{ "scalar operator * scales", []( max::Testing::Test & CurrentTest ) { + VectorTestSuite.AddTest( max::Testing::Test< max::Testing::CoutResultPolicy >{ "scalar operator * scales", []( max::Testing::Test< max::Testing::CoutResultPolicy > & CurrentTest, max::Testing::CoutResultPolicy const & ResultPolicy ) { max::Containers::Vector< int, 3 > Original = { 1, 2, 3 }; max::Containers::Vector< int, 3 > Result = Original * 2; @@ -128,7 +130,7 @@ namespace Containers } } ); - VectorTestSuite.AddTest( max::Testing::Test{ "scalar operator / scales", []( max::Testing::Test & CurrentTest ) { + VectorTestSuite.AddTest( max::Testing::Test< max::Testing::CoutResultPolicy >{ "scalar operator / scales", []( max::Testing::Test< max::Testing::CoutResultPolicy > & CurrentTest, max::Testing::CoutResultPolicy const & ResultPolicy ) { max::Containers::Vector< int, 3 > Original = { 1, 2, 3 }; max::Containers::Vector< int, 3 > Result = Original / 2; @@ -139,14 +141,14 @@ namespace Containers } } ); - VectorTestSuite.AddTest( max::Testing::Test{ "Length function returns length", []( max::Testing::Test & CurrentTest ) { + VectorTestSuite.AddTest( max::Testing::Test< max::Testing::CoutResultPolicy >{ "Length function returns length", []( max::Testing::Test< max::Testing::CoutResultPolicy > & CurrentTest, max::Testing::CoutResultPolicy const & ResultPolicy ) { max::Containers::Vector< double, 3 > Original = { 1.0, 2.0, 3.0 }; CurrentTest.MAX_TESTING_ASSERT( Original.Length() == 3.7416573867739413 ); } } ); - VectorTestSuite.AddTest( max::Testing::Test{ "Normalize function normalizes", []( max::Testing::Test & CurrentTest ) { + VectorTestSuite.AddTest( max::Testing::Test< max::Testing::CoutResultPolicy >{ "Normalize function normalizes", []( max::Testing::Test< max::Testing::CoutResultPolicy > & CurrentTest, max::Testing::CoutResultPolicy const & ResultPolicy ) { max::Containers::Vector< double, 3 > Original = { 1.0, 2.0, 3.0 }; max::Containers::Vector< double, 3 > Result = Original.Normalize(); @@ -157,7 +159,7 @@ namespace Containers } } ); - VectorTestSuite.AddTest( max::Testing::Test{ "non-const operator [] sets the value at that index", []( max::Testing::Test & CurrentTest ) { + VectorTestSuite.AddTest( max::Testing::Test< max::Testing::CoutResultPolicy >{ "non-const operator [] sets the value at that index", []( max::Testing::Test< max::Testing::CoutResultPolicy > & CurrentTest, max::Testing::CoutResultPolicy const & ResultPolicy ) { max::Containers::Vector< int, 3 > Original = { 1, 2, 3 }; Original[ 0 ] = 4; @@ -170,7 +172,7 @@ namespace Containers } } ); - VectorTestSuite.AddTest( max::Testing::Test{ "const operator [] reads the value", []( max::Testing::Test & CurrentTest ) { + VectorTestSuite.AddTest( max::Testing::Test< max::Testing::CoutResultPolicy >{ "const operator [] reads the value", []( max::Testing::Test< max::Testing::CoutResultPolicy > & CurrentTest, max::Testing::CoutResultPolicy const & ResultPolicy ) { max::Containers::Vector< int, 3 > Original = { 1, 2, 3 }; CurrentTest.MAX_TESTING_ASSERT( Original[ 0 ] == 1 ); @@ -179,7 +181,7 @@ namespace Containers } } ); - VectorTestSuite.AddTest( max::Testing::Test{ "data function returns a pointer to the contiguous array", []( max::Testing::Test & CurrentTest ) { + VectorTestSuite.AddTest( max::Testing::Test< max::Testing::CoutResultPolicy >{ "data function returns a pointer to the contiguous array", []( max::Testing::Test< max::Testing::CoutResultPolicy > & CurrentTest, max::Testing::CoutResultPolicy const & ResultPolicy ) { max::Containers::Vector< int, 3 > Original = { 1, 2, 3 }; const int * RawData = Original.data(); diff --git a/Code/Include/max/Testing/CoutResultPolicy.hpp b/Code/Include/max/Testing/CoutResultPolicy.hpp new file mode 100644 index 0000000..cae2e84 --- /dev/null +++ b/Code/Include/max/Testing/CoutResultPolicy.hpp @@ -0,0 +1,33 @@ +// Copyright 2019, The max Contributors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include + +namespace max +{ +namespace Testing +{ + + class CoutResultPolicy + { + public: + + static void OnTestSuiteRun( std::string const & TestSuiteName ); + static void OnTestSuiteSetup( std::string const & TestSuiteName ); + static void OnTestSuiteSetupComplete( std::string const & TestSuiteName ); + static void OnTestRun( std::string const & TestName ); + static void OnTestPassed( std::string const & TestName ); + static void OnTestFailed( std::string const & TestName ); + static void OnAssert( std::string const & FileName, + int LineNumber, + char const * const ExpressionString ); + static void OnTestSuiteTearDown( std::string const & TestSuiteName ); + static void OnTestSuiteDone( std::string const & TestSuiteName, + int TestsPassed, + int TestsFailed ); + + }; + +} // namespace Testing +} // namespace max diff --git a/Code/Include/max/Testing/Test.hpp b/Code/Include/max/Testing/Test.hpp index 607f32f..528120c 100644 --- a/Code/Include/max/Testing/Test.hpp +++ b/Code/Include/max/Testing/Test.hpp @@ -9,38 +9,39 @@ #include #define MAX_TESTING_ASSERT( CONDITION ) \ - Assert( __FILE__, __LINE__, #CONDITION, CONDITION ) + Assert( __FILE__, __LINE__, #CONDITION, CONDITION, ResultPolicy ) #define MAX_TESTING_ASSERT_THROWS( FUNCTOR ) \ - AssertThrows( __FILE__, __LINE__, #FUNCTOR, FUNCTOR ) + AssertThrows( __FILE__, __LINE__, #FUNCTOR, FUNCTOR, ResultPolicy ) #define MAX_TESTING_ASSERT_DOES_NOT_THROW( FUNCTOR ) \ - AssertDoesNotThrow( __FILE__, __LINE__, #FUNCTOR, FUNCTOR ) + AssertDoesNotThrow( __FILE__, __LINE__, #FUNCTOR, FUNCTOR, ResultPolicy ) namespace max { namespace Testing { + template< typename ResultPolicyType > class Test { public: - Test( char const * const Name, std::function< void( max::Testing::Test & CurrentTest ) > const & TestFunction ); + Test( char const * const Name, std::function< void( max::Testing::Test< ResultPolicyType > & CurrentTest, ResultPolicyType const & ResultPolicy ) > const & TestFunction ); bool DidTestPass() const; - void Assert( char const * const FileName, int LineNumber, char const * const ExpressionString, bool Expression ); + void Assert( char const * const FileName, int LineNumber, char const * const ExpressionString, bool Expression, ResultPolicyType const & ResultPolicy ); template< typename ExceptionType, typename FunctorType > - void AssertThrows( char const * const FileName, int LineNumber, char const * const ExpressionString, FunctorType const & Functor ); + void AssertThrows( char const * const FileName, int LineNumber, char const * const ExpressionString, FunctorType const & Functor, ResultPolicyType const & ResultPolicy ); template< typename ExceptionType, typename FunctorType > - void AssertDoesNotThrow( char const * const FileName, int LineNumber, char const * const ExpressionString, FunctorType const & Functor ); + void AssertDoesNotThrow( char const * const FileName, int LineNumber, char const * const ExpressionString, FunctorType const & Functor, ResultPolicyType const & ResultPolicy ); const std::string Name; - std::function< void( max::Testing::Test & CurrentTest ) > TestFunction; + std::function< void( max::Testing::Test< ResultPolicyType > & CurrentTest, ResultPolicyType const & ResultPolicy ) > TestFunction; private: @@ -51,4 +52,6 @@ namespace Testing } // namespace Testing } // namespace max +#include + #endif // #ifndef MAX_TESTING_TEST_HPP diff --git a/Code/Include/max/Testing/Test.inl b/Code/Include/max/Testing/Test.inl new file mode 100644 index 0000000..f4e70ab --- /dev/null +++ b/Code/Include/max/Testing/Test.inl @@ -0,0 +1,88 @@ +// Copyright 2015, The max Contributors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include + +namespace max +{ +namespace Testing +{ + + template< typename ResultPolicyType > + Test< ResultPolicyType >::Test( char const * const Name, std::function< void( max::Testing::Test< ResultPolicyType > & CurrentTest, ResultPolicyType const & ResultPolicy ) > const & TestFunction ) + : Name( Name ) + , TestFunction( TestFunction ) + , DidAllAssertionsPass( true ) + { + } + + template< typename ResultPolicyType > + bool Test< ResultPolicyType >::DidTestPass() const + { + return DidAllAssertionsPass; + } + + template< typename ResultPolicyType > + void Test< ResultPolicyType >::Assert( char const * const FileName, int LineNumber, char const * const ExpressionString, bool const Expression, ResultPolicyType const & ResultPolicy ) + { + bool const AssertionPassed = Expression; + DidAllAssertionsPass &= AssertionPassed; + if( ! AssertionPassed ) + { + ResultPolicy.OnAssert( FileName, LineNumber, ExpressionString ); + } + } + + template< typename ResultPolicyType > + template< typename ExceptionType, typename FunctorType > + void Test< ResultPolicyType >::AssertThrows( char const * const FileName, int LineNumber, char const * const ExpressionString, FunctorType const & Functor, ResultPolicyType const & ResultPolicy ) + { + bool ThrewExpectedType = false; + try + { + Functor(); + } + catch( ExceptionType & ) + { + ThrewExpectedType = true; + } + catch( ... ) + { + } + + bool const AssertionPassed = ThrewExpectedType; + DidAllAssertionsPass &= AssertionPassed; + if( ! AssertionPassed ) + { + ResultPolicy.OnAssert( FileName, LineNumber, ExpressionString ); + } + } + + template< typename ResultPolicyType > + template< typename ExceptionType, typename FunctorType > + void Test< ResultPolicyType >::AssertDoesNotThrow( char const * const FileName, int LineNumber, char const * const ExpressionString, FunctorType const & Functor, ResultPolicyType const & ResultPolicy ) + { + bool ThrewUnexpectedType = false; + try + { + Functor(); + } + catch( ExceptionType & ) + { + ThrewUnexpectedType = true; + } + catch( ... ) + { + } + + bool const AssertionPassed = ! ThrewUnexpectedType; + DidAllAssertionsPass &= AssertionPassed; + if( ! AssertionPassed ) + { + ResultPolicy.OnAssert( FileName, LineNumber, ExpressionString ); + } + } + +} // namespace Testing +} // namespace max diff --git a/Code/Include/max/Testing/TestSuite.hpp b/Code/Include/max/Testing/TestSuite.hpp index 02f22f9..4483dde 100644 --- a/Code/Include/max/Testing/TestSuite.hpp +++ b/Code/Include/max/Testing/TestSuite.hpp @@ -5,28 +5,6 @@ #ifndef MAX_TESTING_TESTSUITE_HPP #define MAX_TESTING_TESTSUITE_HPP -// Usage: -// -// #include -// #incldue -// -// int main() { -// max::Testing::TestSuite( "Example suite", -// max::Testing::AddTest( "Test equality", -// []( auto const & CurrentTestSuite ) { -// CurrentTestSuite.AssertEquals( 1, 1 ); -// } -// ), -// max::Testing::AddTest( "Test throws", -// []( auto const & CurrentTestSuite ) { -// CurrentTestSuite.AssertThrows< std::exception >( -// []() { throw std::exception{}; } -// ); -// } -// ) -// ); -// } - #include #include #include @@ -36,14 +14,16 @@ namespace max namespace Testing { + template< typename ResultPolicyType > class TestSuite { public: - explicit TestSuite( char const * const Name ); + TestSuite( char const * const Name, ResultPolicyType const & ResultPolicy ); + TestSuite( char const * const Name, ResultPolicyType && ResultPolicy ); - void AddTest( max::Testing::Test const & TestToAdd ); - void AddTest( max::Testing::Test && TestToAdd ); + void AddTest( max::Testing::Test< ResultPolicyType > const & TestToAdd ); + void AddTest( max::Testing::Test< ResultPolicyType > && TestToAdd ); void RunTests(); protected: @@ -54,11 +34,14 @@ namespace Testing private: std::string Name; - std::vector< max::Testing::Test > Tests; + ResultPolicyType ResultPolicy; + std::vector< max::Testing::Test< ResultPolicyType > > Tests; }; } // namespace Testing } // namespace max +#include + #endif // #ifndef MAX_TESTING_TESTSUITE_HPP diff --git a/Code/Include/max/Testing/TestSuite.inl b/Code/Include/max/Testing/TestSuite.inl new file mode 100644 index 0000000..8d81e2f --- /dev/null +++ b/Code/Include/max/Testing/TestSuite.inl @@ -0,0 +1,81 @@ +// Copyright 2015, The max Contributors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include + +namespace max +{ +namespace Testing +{ + + template< typename ResultPolicyType > + TestSuite< ResultPolicyType >::TestSuite( char const * const Name, + ResultPolicyType const & ResultPolicy ) + : Name( Name ) + , ResultPolicy( ResultPolicy ) + { + } + + template< typename ResultPolicyType > + TestSuite< ResultPolicyType >::TestSuite( char const * const Name, + ResultPolicyType && ResultPolicy ) + : Name( Name ) + , ResultPolicy( std::move( ResultPolicy ) ) + { + } + + template< typename ResultPolicyType > + void TestSuite< ResultPolicyType >::AddTest( max::Testing::Test< ResultPolicyType > const & TestToAdd ) + { + Tests.push_back( TestToAdd ); + } + + template< typename ResultPolicyType > + void TestSuite< ResultPolicyType >::AddTest( max::Testing::Test< ResultPolicyType > && TestToAdd ) + { + Tests.push_back( std::move( TestToAdd ) ); + } + + template< typename ResultPolicyType > + void TestSuite< ResultPolicyType >::RunTests() + { + ResultPolicy.OnTestSuiteRun( Name ); + ResultPolicy.OnTestSuiteSetup( Name ); + Setup(); + ResultPolicy.OnTestSuiteSetupComplete( Name ); + + int TestsPassed = 0; + int TestsFailed = 0; + for( auto & CurrentTest : Tests ) + { + ResultPolicy.OnTestRun( CurrentTest.Name ); + CurrentTest.TestFunction( CurrentTest, ResultPolicy ); + auto TestPassed = CurrentTest.DidTestPass(); + if( TestPassed ) + { + TestsPassed++; + ResultPolicy.OnTestPassed( CurrentTest.Name ); + } else { + TestsFailed++; + ResultPolicy.OnTestFailed( CurrentTest.Name ); + } + } + + ResultPolicy.OnTestSuiteTearDown( Name ); + TearDown(); + ResultPolicy.OnTestSuiteDone( Name, TestsPassed, TestsFailed ); + } + + template< typename ResultPolicyType > + void TestSuite< ResultPolicyType >::Setup() + { + } + + template< typename ResultPolicyType > + void TestSuite< ResultPolicyType >::TearDown() + { + } + +} // namespace Testing +} // namespace max diff --git a/Code/TranslationUnits/Testing/CoutResultPolicy.cpp b/Code/TranslationUnits/Testing/CoutResultPolicy.cpp new file mode 100644 index 0000000..d5da3f0 --- /dev/null +++ b/Code/TranslationUnits/Testing/CoutResultPolicy.cpp @@ -0,0 +1,67 @@ +// Copyright 2019, The max Contributors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "max/Testing/CoutResultPolicy.hpp" +#include + +namespace max +{ +namespace Testing +{ + + void CoutResultPolicy::OnTestSuiteRun( std::string const & TestSuiteName ) + { + std::cout << "Running test suite " << TestSuiteName << std::endl; + } + + void CoutResultPolicy::OnTestSuiteSetup( std::string const & TestSuiteName ) + { + std::cout << "Setting up test suite " << TestSuiteName << "." << std::endl; + } + + void CoutResultPolicy::OnTestSuiteSetupComplete( std::string const & TestSuiteName ) + { + std::cout << "Done setting up test suites " << TestSuiteName << "." << std::endl; + } + + void CoutResultPolicy::OnTestRun( std::string const & TestName ) + { + std::cout << "Running test " << TestName << "." << std::endl; + } + + void CoutResultPolicy::OnTestPassed( std::string const & TestName ) + { + std::cout << "Passed test " << TestName << "." << std::endl; + } + + void CoutResultPolicy::OnTestFailed( std::string const & TestName ) + { + std::cout << "Failed test " << TestName << "." << std::endl; + } + + void CoutResultPolicy::OnAssert( std::string const & FileName, + int LineNumber, + char const * const ExpressionString ) + { + std::cout << L"Assert failed: " << ExpressionString << "\n"; + std::cout << L"\tin file " << FileName << "\n"; + std::cout << L"\ton line " << LineNumber << std::endl; + } + + void CoutResultPolicy::OnTestSuiteTearDown( std::string const & TestSuiteName ) + { + std::cout << "Tearing down test suite " << TestSuiteName << "." << std::endl; + } + + void CoutResultPolicy::OnTestSuiteDone( char const * const TestSuiteName, + int TestsPassed, + int TestsFailed ) + { + std::cout << "Done tearing down test suite " << TestSuiteName << "." << "\n"; + std::cout << "Tests passed: " << TestsPassed << "\n"; + std::cout << "Tests failed: " << TestsFailed << std::endl; + } + +} // namespace Testing +} // namespace max diff --git a/Code/TranslationUnits/Testing/Test.cpp b/Code/TranslationUnits/Testing/Test.cpp deleted file mode 100644 index 7cd1903..0000000 --- a/Code/TranslationUnits/Testing/Test.cpp +++ /dev/null @@ -1,90 +0,0 @@ -// Copyright 2015, The max Contributors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "max/Testing/Test.hpp" -#include - -namespace max -{ -namespace Testing -{ - - Test::Test( char const * const Name, std::function< void( max::Testing::Test & CurrentTest ) > const & TestFunction ) - : Name( Name ) - , TestFunction( TestFunction ) - , DidAllAssertionsPass( true ) - { - } - - bool Test::DidTestPass() const - { - return DidAllAssertionsPass; - } - - void Test::Assert( char const * const FileName, int LineNumber, char const * const ExpressionString, bool const Expression ) - { - bool const AssertionPassed = Expression; - DidAllAssertionsPass &= AssertionPassed; - if( ! AssertionPassed ) - { - std::wcout << L"Assert failed: " << ExpressionString << "\n"; - std::wcout << L"\tin file " << FileName << "\n"; - std::wcout << L"\ton line " << LineNumber << std::endl; - } - } - - template< typename ExceptionType, typename FunctorType > - void Test::AssertThrows( char const * const FileName, int LineNumber, char const * const ExpressionString, FunctorType const & Functor ) - { - bool ThrewExpectedType = false; - try - { - Functor(); - } - catch( ExceptionType & ) - { - ThrewExpectedType = true; - } - catch( ... ) - { - } - - bool const AssertionPassed = ThrewExpectedType; - DidAllAssertionsPass &= AssertionPassed; - if( ! AssertionPassed ) - { - std::wcout << L"Assertion failed: " << ExpressionString << "\n"; - std::wcout << L"\tin file " << FileName << "\n"; - std::wcout << L"\ton line " << LineNumber << std::endl; - } - } - - template< typename ExceptionType, typename FunctorType > - void Test::AssertDoesNotThrow( char const * const FileName, int LineNumber, char const * const ExpressionString, FunctorType const & Functor ) - { - bool ThrewUnexpectedType = false; - try - { - Functor(); - } - catch( ExceptionType & ) - { - ThrewUnexpectedType = true; - } - catch( ... ) - { - } - - bool const AssertionPassed = ! ThrewUnexpectedType; - DidAllAssertionsPass &= AssertionPassed; - if( ! AssertionPassed ) - { - std::wcout << L"Assertion failed: " << ExpressionString << "\n"; - std::wcout << L"\tin file " << FileName << "\n"; - std::wcout << L"\ton line " << LineNumber << std::endl; - } - } - -} // namespace Testing -} // namespace max diff --git a/Code/TranslationUnits/Testing/TestSuite.cpp b/Code/TranslationUnits/Testing/TestSuite.cpp deleted file mode 100644 index 7f1b62d..0000000 --- a/Code/TranslationUnits/Testing/TestSuite.cpp +++ /dev/null @@ -1,69 +0,0 @@ -// Copyright 2015, The max Contributors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "max/Testing/TestSuite.hpp" -#include -#include "max/Compiling/UnreferencedValue.hpp" - -namespace max -{ -namespace Testing -{ - - TestSuite::TestSuite( char const * const Name ) - : Name( Name ) - { - } - - void TestSuite::AddTest( max::Testing::Test const & TestToAdd ) - { - Tests.push_back( TestToAdd ); - } - - void TestSuite::AddTest( max::Testing::Test && TestToAdd ) - { - Tests.push_back( std::move( TestToAdd ) ); - } - - void TestSuite::RunTests() - { - std::cout << "Running test suite " << Name << std::endl; - std::cout << "Setting up test suite " << Name << "." << std::endl; - Setup(); - std::cout << "Done setting up test suite " << Name << "." << std::endl; - - int TestsPassed = 0; - int TestsFailed = 0; - for( auto & CurrentTest : Tests ) - { - std::cout << "Running test " << CurrentTest.Name << "." << std::endl; - CurrentTest.TestFunction( CurrentTest ); - auto TestPassed = CurrentTest.DidTestPass(); - if( TestPassed ) - { - TestsPassed++; - std::cout << "Passed test " << CurrentTest.Name << "." << std::endl; - } else { - TestsFailed++; - std::cout << "Failed test " << CurrentTest.Name << "." << std::endl; - } - } - - std::cout << "Tearing down test suite " << Name << "." << std::endl; - TearDown(); - std::cout << "Done tearing down test suite " << Name << "." << std::endl; - std::cout << "Passed: " << TestsPassed << std::endl; - std::cout << "Failed: " << TestsFailed << std::endl; - } - - void TestSuite::Setup() - { - } - - void TestSuite::TearDown() - { - } - -} // namespace Testing -} // namespace max diff --git a/Projects/Clang X86 Make/Makefile b/Projects/Clang X86 Make/Makefile index 3e7eb9b..f4d26a4 100644 --- a/Projects/Clang X86 Make/Makefile +++ b/Projects/Clang X86 Make/Makefile @@ -12,8 +12,7 @@ CXX_SRCS = \ $(CODE_DIRECTORY)/Hardware/CPU/TLB.cpp \ $(CODE_DIRECTORY)/Hardware/CPU/TraceCache.cpp \ $(CODE_DIRECTORY)/Logging/DoNothingLogger.cpp \ - $(CODE_DIRECTORY)/Testing/Test.cpp \ - $(CODE_DIRECTORY)/Testing/TestSuite.cpp + $(CODE_DIRECTORY)/Testing/CoutResultPolicy.cpp CXX_OBJS = $(CXX_SRCS:.cpp=.o) INCLUDE_PATHS = \ diff --git a/Projects/Clang X86-64 Make/Makefile b/Projects/Clang X86-64 Make/Makefile index b81679d..5b33204 100644 --- a/Projects/Clang X86-64 Make/Makefile +++ b/Projects/Clang X86-64 Make/Makefile @@ -12,8 +12,7 @@ CXX_SRCS = \ $(CODE_DIRECTORY)/Hardware/CPU/TLB.cpp \ $(CODE_DIRECTORY)/Hardware/CPU/TraceCache.cpp \ $(CODE_DIRECTORY)/Logging/DoNothingLogger.cpp \ - $(CODE_DIRECTORY)/Testing/Test.cpp \ - $(CODE_DIRECTORY)/Testing/TestSuite.cpp + $(CODE_DIRECTORY)/Testing/CoutResultPolicy.cpp CXX_OBJS = $(CXX_SRCS:.cpp=.o) INCLUDE_PATHS = \ diff --git a/Projects/GCC X86 Make/Makefile b/Projects/GCC X86 Make/Makefile index 25247d3..7016124 100644 --- a/Projects/GCC X86 Make/Makefile +++ b/Projects/GCC X86 Make/Makefile @@ -12,8 +12,7 @@ CXX_SRCS = \ $(CODE_DIRECTORY)/Hardware/CPU/TLB.cpp \ $(CODE_DIRECTORY)/Hardware/CPU/TraceCache.cpp \ $(CODE_DIRECTORY)/Logging/DoNothingLogger.cpp \ - $(CODE_DIRECTORY)/Testing/Test.cpp \ - $(CODE_DIRECTORY)/Testing/TestSuite.cpp + $(CODE_DIRECTORY)/Testing/CoutResultPolicy.cpp CXX_OBJS = $(CXX_SRCS:.cpp=.o) INCLUDE_PATHS = \ diff --git a/Projects/GCC X86-64 Make/Makefile b/Projects/GCC X86-64 Make/Makefile index 54a937f..298bf70 100644 --- a/Projects/GCC X86-64 Make/Makefile +++ b/Projects/GCC X86-64 Make/Makefile @@ -12,8 +12,7 @@ CXX_SRCS = \ $(CODE_DIRECTORY)/Hardware/CPU/TLB.cpp \ $(CODE_DIRECTORY)/Hardware/CPU/TraceCache.cpp \ $(CODE_DIRECTORY)/Logging/DoNothingLogger.cpp \ - $(CODE_DIRECTORY)/Testing/Test.cpp \ - $(CODE_DIRECTORY)/Testing/TestSuite.cpp + $(CODE_DIRECTORY)/Testing/CoutResultPolicy.cpp CXX_OBJS = $(CXX_SRCS:.cpp=.o) INCLUDE_PATHS = \