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

summaryrefslogtreecommitdiffhomepage
path: root/projects/SelfTest/TestMain.cpp
blob: bc7825493ee885bc6872f0c1a933b23b8a3bfae2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*
 *  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)
 */
#include "catch_self_test.hpp"

TEST_CASE( "selftest/main", "Runs all Catch self tests and checks their results" ) {
    using namespace Catch;
    
    ///////////////////////////////////////////////////////////////////////////
    SECTION(    "selftest/expected result",
                "Tests do what they claim" ) {
                
        SECTION(    "selftest/expected result/failing tests", 
                    "Tests in the 'failing' branch fail" ) {
            MetaTestRunner::runMatching( "./failing/*",  MetaTestRunner::Expected::ToFail );
        }
        
        SECTION(    "selftest/expected result/succeeding tests", 
                    "Tests in the 'succeeding' branch succeed" ) {
            MetaTestRunner::runMatching( "./succeeding/*",  MetaTestRunner::Expected::ToSucceed );
        }
    }

    ///////////////////////////////////////////////////////////////////////////
    SECTION(    "selftest/test counts", 
                "Number of test cases that run is fixed" ) {
        EmbeddedRunner runner;
        
        SECTION(    "selftest/test counts/succeeding tests", 
                    "Number of 'succeeding' tests is fixed" ) {
            runner.runMatching( "./succeeding/*" );
            CHECK( runner.getTotals().assertions.passed == 277 );
            CHECK( runner.getTotals().assertions.failed == 0 );
        }

        SECTION(    "selftest/test counts/failing tests", 
                    "Number of 'failing' tests is fixed" ) {
            runner.runMatching( "./failing/*" );        
            CHECK( runner.getTotals().assertions.passed == 0 );
            CHECK( runner.getTotals().assertions.failed == 72 );
        }
    }
}

TEST_CASE( "meta/Misc/Sections", "looped tests" ) {
    Catch::EmbeddedRunner runner;
    
    runner.runMatching( "./mixed/Misc/Sections/nested2" );
    CHECK( runner.getTotals().assertions.passed == 2 );
    CHECK( runner.getTotals().assertions.failed == 1 );
}

#include "../../include/internal/catch_commandline.hpp"
#include "../../include/reporters/catch_reporter_basic.hpp"
#include "../../include/reporters/catch_reporter_xml.hpp"
#include "../../include/reporters/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( config.getCutoff() == -1 );
        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( "at least one" ) );
        }
    }
    
    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" ) );
        }
    }
    
    SECTION( "abort", "" ) {
        SECTION( "-a", "" ) {
            const char* argv[] = { "test", "-a" };
            Catch::Config config;
            CHECK( parseIntoConfig( argv, config ) );

            REQUIRE( config.getCutoff() == 1 );
        }
        SECTION( "-a/2", "" ) {
            const char* argv[] = { "test", "-a", "2" };
            Catch::Config config;
            CHECK( parseIntoConfig( argv, config ) );

            REQUIRE( config.getCutoff() == 2 );
        }
        SECTION( "-a/error", "cutoff only takes one argument" ) {
            const char* argv[] = { "test", "-a", "1", "2" };
            Catch::Config config;
            CHECK( parseIntoConfig( argv, config ) == false );

            REQUIRE_THAT( config.getMessage(), Contains( "accepts" ) );
        }
    }
    SECTION( "combinations", "" ) {
        SECTION( "-a -b", "" ) {
            const char* argv[] = { "test", "-a", "-b" };
            Catch::Config config;
            CHECK( parseIntoConfig( argv, config ) );

            REQUIRE( config.getCutoff() == 1 );
            REQUIRE( config.shouldDebugBreak() );
        }
    }
    
}