Read-only mirrors. Development, issues and pull requests are on GitHub.

summaryrefslogtreecommitdiffhomepage
path: root/projects/SelfTest/TestMain.cpp
diff options
context:
space:
mode:
authorPhil Nash2012-05-31 19:40:26 +0100
committerPhil Nash2012-05-31 19:40:26 +0100
commit163088a11f5b20c561e624c8ed5cdc8e8c0464ef (patch)
tree836283cb7d2f8b3ac811520e451dcdede6f79c41 /projects/SelfTest/TestMain.cpp
parent06e959be3ed974932d2ade722a0e6442cfd45fea (diff)
downloadcatch3-163088a11f5b20c561e624c8ed5cdc8e8c0464ef.tar.gz
Refactored command line parser to be more cohesive
Also added a set of unit tests for them (incomplete)
Diffstat (limited to 'projects/SelfTest/TestMain.cpp')
-rw-r--r--projects/SelfTest/TestMain.cpp148
1 files changed, 148 insertions, 0 deletions
diff --git a/projects/SelfTest/TestMain.cpp b/projects/SelfTest/TestMain.cpp
index 97a35886..eba029be 100644
--- a/projects/SelfTest/TestMain.cpp
+++ b/projects/SelfTest/TestMain.cpp
@@ -53,3 +53,151 @@ TEST_CASE( "meta/Misc/Sections", "looped tests" ) {
CHECK( runner.getTotals().assertions.passed == 2 );
CHECK( runner.getTotals().assertions.failed == 1 );
}
+
+#include "catch_commandline.hpp"
+#include "catch_reporter_basic.hpp"
+#include "catch_reporter_xml.hpp"
+#include "catch_reporter_junit.hpp"
+
+template<size_t size>
+bool parseIntoConfig( const char * (&argv)[size], Catch::Config& config ) {
+ return Catch::parseIntoConfig( Catch::CommandParser( size, argv ), config );
+}
+
+TEST_CASE( "selftest/parser", "" ) {
+
+ SECTION( "default", "" ) {
+ const char* argv[] = { "test" };
+ Catch::Config config;
+ CHECK( parseIntoConfig( argv, config ) );
+
+ CHECK( config.getTestSpecs().empty() );
+ CHECK( config.shouldDebugBreak() == false );
+ CHECK( config.showHelp() == false );
+ CHECK( dynamic_cast<Catch::BasicReporter*>( config.getReporter().get() ) );
+ }
+
+ SECTION( "test lists", "" ) {
+ SECTION( "-t/1", "Specify one test case using -t" ) {
+ const char* argv[] = { "test", "-t", "test1" };
+ Catch::Config config;
+ CHECK( parseIntoConfig( argv, config ) );
+
+ REQUIRE( config.getTestSpecs().size() == 1 );
+ REQUIRE( config.getTestSpecs()[0] == "test1" );
+ }
+
+ SECTION( "--test/1", "Specify one test case using --test" ) {
+ const char* argv[] = { "test", "--test", "test1" };
+ Catch::Config config;
+ CHECK( parseIntoConfig( argv, config ) );
+
+ REQUIRE( config.getTestSpecs().size() == 1 );
+ REQUIRE( config.getTestSpecs()[0] == "test1" );
+ }
+
+ SECTION( "-t/2", "Specify two test cases using -t" ) {
+ const char* argv[] = { "test", "-t", "test1", "test2" };
+ Catch::Config config;
+ CHECK( parseIntoConfig( argv, config ) );
+
+ REQUIRE( config.getTestSpecs().size() == 2 );
+ REQUIRE( config.getTestSpecs()[0] == "test1" );
+ REQUIRE( config.getTestSpecs()[1] == "test2" );
+ }
+
+ SECTION( "-t/0", "When no test names are supplied it is an error" ) {
+ const char* argv[] = { "test", "-t" };
+ Catch::Config config;
+ CHECK( parseIntoConfig( argv, config ) == false );
+
+ REQUIRE_THAT( config.getMessage(), Contains( "none" ) );
+ }
+ }
+
+ SECTION( "reporter", "" ) {
+ SECTION( "-r/basic", "" ) {
+ const char* argv[] = { "test", "-reporter", "basic" };
+ Catch::Config config;
+ CHECK( parseIntoConfig( argv, config ) );
+
+ REQUIRE( dynamic_cast<Catch::BasicReporter*>( config.getReporter().get() ) );
+ }
+ SECTION( "-r/xml", "" ) {
+ const char* argv[] = { "test", "-r", "xml" };
+ Catch::Config config;
+ CHECK( parseIntoConfig( argv, config ) );
+
+ REQUIRE( dynamic_cast<Catch::XmlReporter*>( config.getReporter().get() ) );
+ }
+ SECTION( "-r/junit", "" ) {
+ const char* argv[] = { "test", "-r", "junit" };
+ Catch::Config config;
+ CHECK( parseIntoConfig( argv, config ) );
+
+ REQUIRE( dynamic_cast<Catch::JunitReporter*>( config.getReporter().get() ) );
+ }
+ SECTION( "-r/error", "reporter config only accepts one argument" ) {
+ const char* argv[] = { "test", "-r", "one", "two" };
+ Catch::Config config;
+ CHECK( parseIntoConfig( argv, config ) == false );
+
+ REQUIRE_THAT( config.getMessage(), Contains( "one argument" ) );
+ }
+ }
+
+ SECTION( "debugger", "" ) {
+ SECTION( "-b", "" ) {
+ const char* argv[] = { "test", "-b" };
+ Catch::Config config;
+ CHECK( parseIntoConfig( argv, config ) );
+
+ REQUIRE( config.shouldDebugBreak() == true );
+ }
+ SECTION( "--break", "" ) {
+ const char* argv[] = { "test", "--break" };
+ Catch::Config config;
+ CHECK( parseIntoConfig( argv, config ) );
+
+ REQUIRE( config.shouldDebugBreak() );
+ }
+ SECTION( "-b", "break option has no arguments" ) {
+ const char* argv[] = { "test", "-b", "unexpected" };
+ Catch::Config config;
+ CHECK( parseIntoConfig( argv, config ) == false );
+
+ REQUIRE_THAT( config.getMessage(), Contains( "not accept" ) );
+ }
+ }
+
+ SECTION( "help", "" ) {
+ SECTION( "-h", "" ) {
+ const char* argv[] = { "test", "-h" };
+ Catch::Config config;
+ CHECK( parseIntoConfig( argv, config ) );
+
+ REQUIRE( config.showHelp() );
+ }
+ SECTION( "-?", "" ) {
+ const char* argv[] = { "test", "-?" };
+ Catch::Config config;
+ CHECK( parseIntoConfig( argv, config ) );
+
+ REQUIRE( config.showHelp() );
+ }
+ SECTION( "--help", "" ) {
+ const char* argv[] = { "test", "--help" };
+ Catch::Config config;
+ CHECK( parseIntoConfig( argv, config ) );
+
+ REQUIRE( config.showHelp() );
+ }
+ SECTION( "-h", "help option has no arguments" ) {
+ const char* argv[] = { "test", "-h", "unexpected" };
+ Catch::Config config;
+ CHECK( parseIntoConfig( argv, config ) == false );
+
+ REQUIRE_THAT( config.getMessage(), Contains( "not accept" ) );
+ }
+ }
+}