diff options
| author | Phil Nash | 2011-04-26 08:32:40 +0100 |
|---|---|---|
| committer | Phil Nash | 2011-04-26 08:32:40 +0100 |
| commit | 823ea3efd44b988e4e50a05069b19062587e72c9 (patch) | |
| tree | 65c5c6a1549c0e0d299c0fa40352f813dba7ae65 /include | |
| parent | 02c877f91644e498ce0c9c257823e63a1154c652 (diff) | |
| download | catch3-823ea3efd44b988e4e50a05069b19062587e72c9.tar.gz | |
Re-org
Diffstat (limited to 'include')
37 files changed, 6288 insertions, 0 deletions
diff --git a/include/catch.hpp b/include/catch.hpp new file mode 100644 index 00000000..f9ff324c --- /dev/null +++ b/include/catch.hpp @@ -0,0 +1,73 @@ +/* + * catch.hpp + * Catch + * + * Created by Phil on 22/10/2010. + * Copyright 2010 Two Blue Cubes Ltd + * + * Distributed under the Boost Software License, Version 1.0. (See accompanying + * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + * + */ + +/* TBD: + + Next: + + Later: + Finish command line parser (list as xml, specify FP tolerance) + Revisit Approx() + Tags? + Finish macros, listed here, later (just CHECK_NOFAIL now) + */ +#ifndef TWOBLUECUBES_CATCH_HPP_INCLUDED +#define TWOBLUECUBES_CATCH_HPP_INCLUDED + +#include "internal/catch_hub.h" +#include "internal/catch_test_registry.hpp" +#include "internal/catch_capture.hpp" +#include "internal/catch_section.hpp" +#include "internal/catch_generators.hpp" +#include "internal/catch_interfaces_exception.h" + +////// + +#define REQUIRE( expr ) INTERNAL_CATCH_TEST( expr, false, true, "REQUIRE" ) +#define REQUIRE_FALSE( expr ) INTERNAL_CATCH_TEST( expr, true, true, "REQUIRE_FALSE" ) + +#define REQUIRE_THROWS( expr ) INTERNAL_CATCH_THROWS( expr, ..., true, "REQUIRE_THROWS" ) +#define REQUIRE_THROWS_AS( expr, exceptionType ) INTERNAL_CATCH_THROWS_AS( expr, exceptionType, true, "REQUIRE_THROWS_AS" ) +#define REQUIRE_NOTHROW( expr ) INTERNAL_CATCH_NO_THROW( expr, true, "REQUIRE_NOTHROW" ) + +#define CHECK( expr ) INTERNAL_CATCH_TEST( expr, false, false, "CHECK" ) +#define CHECK_FALSE( expr ) INTERNAL_CATCH_TEST( expr, true, false, "CHECK_FALSE" ) + +#define CHECK_THROWS( expr ) INTERNAL_CATCH_THROWS( expr, ..., false, "CHECK_THROWS" ) +#define CHECK_THROWS_AS( expr, exceptionType ) INTERNAL_CATCH_THROWS_AS( expr, exceptionType, false, "CHECK_THROWS_AS" ) +#define CHECK_NOTHROW( expr ) INTERNAL_CATCH_NO_THROW( expr, false, "CHECK_NOTHROW" ) + +#define INFO( msg ) INTERNAL_CATCH_MSG( msg, Catch::ResultWas::Info, false, "INFO" ) +#define WARN( msg ) INTERNAL_CATCH_MSG( msg, Catch::ResultWas::Warning, false, "WARN" ) +#define FAIL( msg ) INTERNAL_CATCH_MSG( msg, Catch::ResultWas::ExplicitFailure, true, "FAIL" ) +#define SCOPED_INFO( msg ) INTERNAL_CATCH_SCOPED_INFO( msg ) +#define CAPTURE( msg ) INTERNAL_CATCH_MSG( #msg " := " << msg, Catch::ResultWas::Info, false, "CAPTURE" ) + +#define SECTION( name, description ) INTERNAL_CATCH_SECTION( name, description ) + +#define TEST_CASE( name, description ) INTERNAL_CATCH_TESTCASE( name, description ) +#define TEST_CASE_NORETURN( name, description ) INTERNAL_CATCH_TESTCASE_NORETURN( name, description ) +#define ANON_TEST_CASE() INTERNAL_CATCH_TESTCASE( "", "Anonymous test case" ) +#define METHOD_AS_TEST_CASE( method, name, description ) CATCH_METHOD_AS_TEST_CASE( method, name, description ) + +#define REGISTER_REPORTER( name, reporterType ) INTERNAL_CATCH_REGISTER_REPORTER( name, reporterType ) +#define CATCH_TRANSLATE_EXCEPTION( signature ) INTERNAL_CATCH_TRANSLATE_EXCEPTION( signature ) + +#define GENERATE( expr) INTERNAL_CATCH_GENERATE( expr ) + +/////////////// +// Still to be implemented +#define CHECK_NOFAIL( expr ) // !TBD - reports violation, but doesn't fail Test + +using Catch::Detail::Approx; + +#endif // TWOBLUECUBES_CATCH_HPP_INCLUDED diff --git a/include/catch_objc.hpp b/include/catch_objc.hpp new file mode 100644 index 00000000..690ce5d4 --- /dev/null +++ b/include/catch_objc.hpp @@ -0,0 +1,185 @@ +/* + * catch_objc.hpp + * Catch + * + * Created by Phil on 14/11/2010. + * Copyright 2010 Two Blue Cubes Ltd. All rights reserved. + * + * Distributed under the Boost Software License, Version 1.0. (See accompanying + * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + * + */ + +#ifndef TWOBLUECUBES_CATCH_OBJC_HPP_INCLUDED +#define TWOBLUECUBES_CATCH_OBJC_HPP_INCLUDED + +#import <objc/runtime.h> +#include <string> + +#include "catch.hpp" +#include "internal/catch_test_case_info.hpp" + +/////////////////////////////////////////////////////////////////////////////// +// This protocol is really only here for (self) documenting purposes, since +// all its methods are optional. +@protocol OcFixture + +@optional + +-(void) setUp; +-(void) tearDown; + +@end + +namespace Catch +{ + class OcMethod : public ITestCase + { + public: + /////////////////////////////////////////////////////////////////////// + OcMethod + ( + Class cls, + SEL sel + ) + : m_cls( cls ), + m_sel( sel ) + { + } + + /////////////////////////////////////////////////////////////////////// + virtual void invoke + () + const + { + id obj = class_createInstance( m_cls, 0 ); + obj = [obj init]; + + if( [obj respondsToSelector: @selector(setUp) ] ) + [obj performSelector: @selector(setUp)]; + + if( [obj respondsToSelector: m_sel] ) + [obj performSelector: m_sel]; + + if( [obj respondsToSelector: @selector(tearDown) ] ) + [obj performSelector: @selector(tearDown)]; + + [obj release]; + } + + /////////////////////////////////////////////////////////////////////// + virtual ITestCase* clone + () + const + { + return new OcMethod( m_cls, m_sel ); + } + + /////////////////////////////////////////////////////////////////////// + virtual bool operator == + ( + const ITestCase& other + ) + const + { + const OcMethod* ocmOther = dynamic_cast<const OcMethod*> ( &other ); + return ocmOther && ocmOther->m_sel == m_sel; + } + + /////////////////////////////////////////////////////////////////////// + virtual bool operator < + ( + const ITestCase& other + ) + const + { + const OcMethod* ocmOther = dynamic_cast<const OcMethod*> ( &other ); + return ocmOther && ocmOther->m_sel < m_sel; + } + + private: + Class m_cls; + SEL m_sel; + }; + + namespace Detail + { + + /////////////////////////////////////////////////////////////////////// + inline bool startsWith + ( + const std::string& str, + const std::string& sub + ) + { + return str.length() > sub.length() && str.substr( 0, sub.length() ) == sub; + } + + /////////////////////////////////////////////////////////////////////// + inline const char* getAnnotation + ( + Class cls, + const std::string& annotationName, + const std::string& testCaseName + ) + { + NSString* selStr = [[NSString alloc] initWithFormat:@"Catch_%s_%s", annotationName.c_str(), testCaseName.c_str()]; + SEL sel = NSSelectorFromString( selStr ); + [selStr release]; + if( [cls respondsToSelector: sel] ) + return (const char*)[cls performSelector: sel]; + return ""; + } + } + + /////////////////////////////////////////////////////////////////////////// + inline size_t registerTestMethods + () + { + size_t noTestMethods = 0; + int noClasses = objc_getClassList( NULL, 0 ); + + std::vector<Class> classes( noClasses ); + objc_getClassList( &classes[0], noClasses ); + + for( int c = 0; c < noClasses; c++ ) + { + Class cls = classes[c]; + { + u_int count; + Method* methods = class_copyMethodList( cls, &count ); + for( int m = 0; m < count ; m++ ) + { + SEL selector = method_getName(methods[m]); + std::string methodName = sel_getName(selector); + if( Detail::startsWith( methodName, "Catch_TestCase_" ) ) + { + std::string testCaseName = methodName.substr( 15 ); + const char* name = Detail::getAnnotation( cls, "Name", testCaseName ); + const char* desc = Detail::getAnnotation( cls, "Description", testCaseName ); + + Hub::getTestCaseRegistry().registerTest( TestCaseInfo( new OcMethod( cls, selector ), name, desc, "", 0 ) ); + noTestMethods++; + + } + } + free(methods); + } + } + return noTestMethods; + } +} + +/////////////////////////////////////////////////////////////////////////////// +#define OC_TEST_CASE( name, desc )\ ++(const char*) INTERNAL_CATCH_UNIQUE_NAME( Catch_Name_test ) \ +{\ +return name; \ +}\ ++(const char*) INTERNAL_CATCH_UNIQUE_NAME( Catch_Description_test ) \ +{ \ +return desc; \ +} \ +-(void) INTERNAL_CATCH_UNIQUE_NAME( Catch_TestCase_test ) + +#endif // TWOBLUECUBES_CATCH_OBJC_HPP_INCLUDED
\ No newline at end of file diff --git a/include/catch_objc_main.hpp b/include/catch_objc_main.hpp new file mode 100644 index 00000000..7933a636 --- /dev/null +++ b/include/catch_objc_main.hpp @@ -0,0 +1,31 @@ +/* + * catch_objc_main.hpp + * Catch + * + * Created by Phil on 28/12/2010. + * Copyright 2010 Two Blue Cubes Ltd. All rights reserved. + * + * Distributed under the Boost Software License, Version 1.0. (See accompanying + * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + * + */ + +#ifndef TWOBLUECUBES_CATCH_OBJC_MAIN_HPP_INCLUDED +#define TWOBLUECUBES_CATCH_OBJC_MAIN_HPP_INCLUDED + +#include "catch_runner.hpp" +#include "catch_objc.hpp" + +int main (int argc, const char * argv[]) +{ + NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; + + Catch::registerTestMethods(); + + int result = Catch::Main( argc, (char* const*)argv ); + + [pool drain]; + return result; +} + +#endif
\ No newline at end of file diff --git a/include/catch_runner.hpp b/include/catch_runner.hpp new file mode 100644 index 00000000..3b859443 --- /dev/null +++ b/include/catch_runner.hpp @@ -0,0 +1,148 @@ +/* + * catch_runner.hpp + * Catch + * + * Created by Phil on 31/10/2010. + * Copyright 2010 Two Blue Cubes Ltd. All rights reserved. + * + * Distributed under the Boost Software License, Version 1.0. (See accompanying + * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + * + */ + +#ifndef TWOBLUECUBES_CATCH_RUNNER_HPP_INCLUDED +#define TWOBLUECUBES_CATCH_RUNNER_HPP_INCLUDED + +#include "internal/catch_hub_impl.hpp" + +#include "internal/catch_commandline.hpp" +#include "internal/catch_list.hpp" +#include "reporters/catch_reporter_basic.hpp" +#include "reporters/catch_reporter_xml.hpp" +#include "reporters/catch_reporter_junit.hpp" + +#include <fstream> +#include <stdlib.h> +#include <limits> + +namespace Catch +{ + ////////////////////////////////////////////////////////////////////////// + inline int Main + ( + Config& config + ) + { + // Handle list request + if( config.listWhat() != Config::List::None ) + return List( config ); + + // Open output file, if specified + std::ofstream ofs; + if( !config.getFilename().empty() ) + { + ofs.open( config.getFilename().c_str() ); + if( ofs.fail() ) + { + std::cerr << "Unable to open file: '" << config.getFilename() << "'" << std::endl; + return (std::numeric_limits<int>::max)(); + } + config.setStreamBuf( ofs.rdbuf() ); + } + + Runner runner( config ); + + // Run test specs specified on the command line - or default to all + if( !config.testsSpecified() ) + { + config.getReporter()->StartGroup( "" ); + runner.runAll(); + config.getReporter()->EndGroup( "", runner.getSuccessCount(), runner.getFailureCount() ); + } + else + { + // !TBD We should get all the testcases upfront, report any missing, + // then just run them + std::vector<std::string>::const_iterator it = config.getTestSpecs().begin(); + std::vector<std::string>::const_iterator itEnd = config.getTestSpecs().end(); + for(; it != itEnd; ++it ) + { + size_t prevSuccess = runner.getSuccessCount(); + size_t prevFail = runner.getFailureCount(); + config.getReporter()->StartGroup( *it ); + if( runner.runMatching( *it ) == 0 ) + { + // Use reporter? +// std::cerr << "\n[Unable to match any test cases with: " << *it << "]" << std::endl; + } + config.getReporter()->EndGroup( *it, runner.getSuccessCount()-prevSuccess, runner.getFailureCount()-prevFail ); + } + } + return static_cast<int>( runner.getFailureCount() ); + } + + ////////////////////////////////////////////////////////////////////////// + void showHelp + ( + std::string exeName + ) + { + std::string::size_type pos = exeName.find_last_of( "/\\" ); + if( pos != std::string::npos ) + { + exeName = exeName.substr( pos+1 ); + } + + std::cout << exeName << " is a CATCH host application. Options are as follows:\n\n" + << "\t-l, --list <tests | reporters> [xml]\n" + << "\t-t, --test <testspec> [<testspec>...]\n" + << "\t-r, --reporter <reporter name>\n" + << "\t-o, --out <file name>|<%stream name>\n" + << "\t-s, --success\n" + << "\t-b, --break\n" + << "\t-n, --name <name>\n\n" + << "For more detail usage please see: https://github.com/philsquared/Catch/wiki/Command-line" << std::endl; + } + + ////////////////////////////////////////////////////////////////////////// + inline int Main + ( + int argc, + char* const argv[], + Config& config + ) + { + ArgParser( argc, argv, config ); + + if( !config.getMessage().empty() ) + { + std::cerr << config.getMessage() << std::endl; + return (std::numeric_limits<int>::max)(); + } + + // Handle help + if( config.showHelp() ) + { + showHelp( argv[0] ); + return 0; + } + + return Main( config ); + } + + ////////////////////////////////////////////////////////////////////////// + inline int Main + ( + int argc, + char* const argv[] + ) + { + Config config; +// if( isDebuggerActive() ) +// config.useStream( "debug" ); + return Main( argc, argv, config ); + } + +} // end namespace Catch + +#endif // TWOBLUECUBES_CATCH_RUNNER_HPP_INCLUDED diff --git a/include/catch_with_main.hpp b/include/catch_with_main.hpp new file mode 100644 index 00000000..ae68c7cc --- /dev/null +++ b/include/catch_with_main.hpp @@ -0,0 +1,23 @@ +/* + * catch_with_main.hpp + * Catch + * + * Created by Phil on 01/11/2010. + * Copyright 2010 Two Blue Cubes Ltd. All rights reserved. + * + * Distributed under the Boost Software License, Version 1.0. (See accompanying + * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + * + */ +#ifndef TWOBLUECUBES_CATCH_WITH_MAIN_HPP_INCLUDED +#define TWOBLUECUBES_CATCH_WITH_MAIN_HPP_INCLUDED + +#include "catch_runner.hpp" +#include "catch.hpp" + +int main (int argc, char * const argv[]) +{ + return Catch::Main( argc, argv ); +} + +#endif // TWOBLUECUBES_CATCH_WITH_MAIN_HPP_INCLUDED diff --git a/include/internal/catch_capture.hpp b/include/internal/catch_capture.hpp new file mode 100644 index 00000000..34f86f22 --- /dev/null +++ b/include/internal/catch_capture.hpp @@ -0,0 +1,785 @@ +/* + * catch_capture.hpp + * Catch + * + * Created by Phil on 18/10/2010. + * Copyright 2010 Two Blue Cubes Ltd. All rights reserved. + * + * Distributed under the Boost Software License, Version 1.0. (See accompanying + * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + * + */ +#ifndef TWOBLUECUBES_CATCH_CAPTURE_HPP_INCLUDED +#define TWOBLUECUBES_CATCH_CAPTURE_HPP_INCLUDED + +#include "catch_resultinfo.hpp" +#include "catch_result_type.h" +#include "catch_interfaces_capture.h" +#include "catch_debugger.hpp" +#include "catch_evaluate.hpp" +#include <sstream> +#include <cmath> + +namespace Catch +{ +namespace Detail +{ + // The following code, contributed by Sam Partington, allows us to choose an implementation + // of toString() depending on whether a << overload is available + + struct NonStreamable + { + // allow construction from anything... + template<typename Anything> + NonStreamable(Anything) + {} + }; + + // a local operator<< which may be called if there isn't a better one elsewhere... + inline NonStreamable operator << ( std::ostream&, const NonStreamable& ns ) + { + return ns; + } + + template<typename T> + struct IsStreamable + { + static NoType Deduce( const NonStreamable& ); + static YesType Deduce( std::ostream& ); + + enum + { + value = sizeof( Deduce( Synth<std::ostream&>() << Synth<const T&>() ) ) + == sizeof( YesType ) + }; + }; + + // << is available, so use it with ostringstream to make the string + template<typename T, bool streamable> + struct StringMaker + { + /////////////////////////////////////////////////////////////////////// + static std::string apply + ( + const T& value + ) + { + std::ostringstream oss; + oss << value; + return oss.str(); + } + }; + + // << not available - use a default string + template<typename T> + struct StringMaker<T, false> + { + /////////////////////////////////////////////////////////////////////// + static std::string apply + ( + const T& + ) + { + return "{?}"; + } + }; + +}// end namespace Detail + +/////////////////////////////////////////////////////////////////////////////// +template<typename T> +std::string toString +( + const T& value +) +{ + return Detail::StringMaker<T, Detail::IsStreamable<T>::value>::apply( value ); +} + +// Shortcut overloads + +/////////////////////////////////////////////////////////////////////////////// +inline std::string toString +( + const std::string& value +) +{ + return "\"" + value + "\""; +} + +/////////////////////////////////////////////////////////////////////////////// +inline std::string toString +( + const std::wstring& value +) +{ + std::ostringstream oss; + oss << "\""; + for(size_t i = 0; i < value.size(); ++i ) + oss << static_cast<char>( value[i] <= 0xff ? value[i] : '?'); + oss << "\""; + return oss.str(); +} + +/////////////////////////////////////////////////////////////////////////////// +inline std::string toString +( + const char* const value +) +{ + return value ? Catch::toString( std::string( value ) ) : std::string( "{null string}" ); +} + +/////////////////////////////////////////////////////////////////////////////// +inline std::string toString +( + char* const value +) +{ + return Catch::toString( static_cast<const char* const>( value ) ); +} + +/////////////////////////////////////////////////////////////////////////////// +inline std::string toString +( + int value +) +{ + std::ostringstream oss; + oss << value; + return oss.str(); +} + +/////////////////////////////////////////////////////////////////////////////// +inline std::string toString +( + unsigned int value +) +{ + std::ostringstream oss; + if( value > 8192 ) + oss << "0x" << std::hex << value; + else + oss << value; + return oss.str(); +} + +/////////////////////////////////////////////////////////////////////////////// +inline std::string toString +( + unsigned long value +) +{ + std::ostringstream oss; + if( value > 8192 ) + oss << "0x" << std::hex << value; + else + oss << value; + return oss.str(); +} + +/////////////////////////////////////////////////////////////////////////////// +inline std::string toString +( + const double value +) +{ + std::ostringstream oss; + oss << value; + return oss.str(); +} + +/////////////////////////////////////////////////////////////////////////////// +inline std::string toString +( + bool value +) +{ + return value ? "true" : "false"; +} + +/////////////////////////////////////////////////////////////////////////////// +inline std::string toString +( + void* p +) +{ + if( !p ) + return INTERNAL_CATCH_STRINGIFY( NULL ); + std::ostringstream oss; + oss << p; + return oss.str(); +} + +/////////////////////////////////////////////////////////////////////////////// +template<typename T> +inline std::string toString +( + T* p +) +{ + return Catch::toString( static_cast<void*>( p ) ); +} + +/////////////////////////////////////////////////////////////////////////////// +template<typename T> +inline std::string toString +( |