1 /*
2  *  Created by Phil on 22/10/2010.
3  *  Copyright 2010 Two Blue Cubes Ltd
4  *
5  *  Distributed under the Boost Software License, Version 1.0. (See accompanying
6  *  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7  */
8 
9 #define CATCH_CONFIG_MAIN
10 #include "catch.hpp"
11 #include "../include/reporters/catch_reporter_teamcity.hpp"
12 
13 // Some example tag aliases
14 CATCH_REGISTER_TAG_ALIAS( "[@nhf]", "[failing]~[.]" )
15 CATCH_REGISTER_TAG_ALIAS( "[@tricky]", "[tricky]~[.]" )
16 
17 
18 #ifdef __clang__
19 #   pragma clang diagnostic ignored "-Wpadded"
20 #   pragma clang diagnostic ignored "-Wweak-vtables"
21 #   pragma clang diagnostic ignored "-Wc++98-compat"
22 #endif
23 
24 
25 template<size_t size>
parseIntoConfig(const char * (& argv)[size],Catch::ConfigData & config)26 void parseIntoConfig( const char * (&argv)[size], Catch::ConfigData& config ) {
27     Catch::Clara::CommandLine<Catch::ConfigData> parser = Catch::makeCommandLineParser();
28     parser.parseInto( Catch::Clara::argsToVector( size, argv ), config );
29 }
30 
31 template<size_t size>
parseIntoConfigAndReturnError(const char * (& argv)[size],Catch::ConfigData & config)32 std::string parseIntoConfigAndReturnError( const char * (&argv)[size], Catch::ConfigData& config ) {
33     try {
34         parseIntoConfig( argv, config );
35         FAIL( "expected exception" );
36     }
37     catch( std::exception& ex ) {
38         return ex.what();
39     }
40     return "";
41 }
42 
fakeTestCase(const char * name,const char * desc="")43 inline Catch::TestCase fakeTestCase( const char* name, const char* desc = "" ){ return Catch::makeTestCase( CATCH_NULL, "", name, desc, CATCH_INTERNAL_LINEINFO ); }
44 
45 TEST_CASE( "Process can be configured on command line", "[config][command-line]" ) {
46 
47     using namespace Catch::Matchers;
48 
49     Catch::ConfigData config;
50 
51     SECTION( "default - no arguments", "" ) {
52         const char* argv[] = { "test" };
53         CHECK_NOTHROW( parseIntoConfig( argv, config ) );
54 
55         CHECK( config.shouldDebugBreak == false );
56         CHECK( config.abortAfter == -1 );
57         CHECK( config.noThrow == false );
58         CHECK( config.reporterNames.empty() );
59     }
60 
61     SECTION( "test lists", "" ) {
62         SECTION( "1 test", "Specify one test case using" ) {
63             const char* argv[] = { "test", "test1" };
64             CHECK_NOTHROW( parseIntoConfig( argv, config ) );
65 
66             Catch::Config cfg( config );
67             REQUIRE( cfg.testSpec().matches( fakeTestCase( "notIncluded" ) ) == false );
68             REQUIRE( cfg.testSpec().matches( fakeTestCase( "test1" ) ) );
69         }
70         SECTION( "Specify one test case exclusion using exclude:", "" ) {
71             const char* argv[] = { "test", "exclude:test1" };
72             CHECK_NOTHROW( parseIntoConfig( argv, config ) );
73 
74             Catch::Config cfg( config );
75             REQUIRE( cfg.testSpec().matches( fakeTestCase( "test1" ) ) == false );
76             REQUIRE( cfg.testSpec().matches( fakeTestCase( "alwaysIncluded" ) ) );
77         }
78 
79         SECTION( "Specify one test case exclusion using ~", "" ) {
80             const char* argv[] = { "test", "~test1" };
81             CHECK_NOTHROW( parseIntoConfig( argv, config ) );
82 
83             Catch::Config cfg( config );
84             REQUIRE( cfg.testSpec().matches( fakeTestCase( "test1" ) ) == false );
85             REQUIRE( cfg.testSpec().matches( fakeTestCase( "alwaysIncluded" ) ) );
86         }
87 
88     }
89 
90     SECTION( "reporter", "" ) {
91         SECTION( "-r/console", "" ) {
92             const char* argv[] = { "test", "-r", "console" };
93             CHECK_NOTHROW( parseIntoConfig( argv, config ) );
94 
95             REQUIRE( config.reporterNames[0] == "console" );
96         }
97         SECTION( "-r/xml", "" ) {
98             const char* argv[] = { "test", "-r", "xml" };
99             CHECK_NOTHROW( parseIntoConfig( argv, config ) );
100 
101             REQUIRE( config.reporterNames[0] == "xml" );
102         }
103         SECTION( "-r xml and junit", "" ) {
104             const char* argv[] = { "test", "-r", "xml", "-r", "junit" };
105             CHECK_NOTHROW( parseIntoConfig( argv, config ) );
106 
107             REQUIRE( config.reporterNames.size() == 2 );
108             REQUIRE( config.reporterNames[0] == "xml" );
109             REQUIRE( config.reporterNames[1] == "junit" );
110         }
111         SECTION( "--reporter/junit", "" ) {
112             const char* argv[] = { "test", "--reporter", "junit" };
113             CHECK_NOTHROW( parseIntoConfig( argv, config ) );
114 
115             REQUIRE( config.reporterNames[0] == "junit" );
116         }
117     }
118 
119     SECTION( "debugger", "" ) {
120         SECTION( "-b", "" ) {
121             const char* argv[] = { "test", "-b" };
122             CHECK_NOTHROW( parseIntoConfig( argv, config ) );
123 
124             REQUIRE( config.shouldDebugBreak == true );
125         }
126         SECTION( "--break", "" ) {
127             const char* argv[] = { "test", "--break" };
128             CHECK_NOTHROW( parseIntoConfig( argv, config ) );
129 
130             REQUIRE( config.shouldDebugBreak );
131         }
132     }
133 
134     SECTION( "abort", "" ) {
135         SECTION( "-a aborts after first failure", "" ) {
136             const char* argv[] = { "test", "-a" };
137             CHECK_NOTHROW( parseIntoConfig( argv, config ) );
138 
139             REQUIRE( config.abortAfter == 1 );
140         }
141         SECTION( "-x 2 aborts after two failures", "" ) {
142             const char* argv[] = { "test", "-x", "2" };
143             CHECK_NOTHROW( parseIntoConfig( argv, config ) );
144 
145             REQUIRE( config.abortAfter == 2 );
146         }
147         SECTION( "-x must be greater than zero", "" ) {
148             const char* argv[] = { "test", "-x", "0" };
149             REQUIRE_THAT( parseIntoConfigAndReturnError( argv, config ), Contains( "greater than zero" ) );
150         }
151         SECTION( "-x must be numeric", "" ) {
152             const char* argv[] = { "test", "-x", "oops" };
153             REQUIRE_THAT( parseIntoConfigAndReturnError( argv, config ), Contains( "-x" ) );
154         }
155     }
156 
157     SECTION( "nothrow", "" ) {
158         SECTION( "-e", "" ) {
159             const char* argv[] = { "test", "-e" };
160             CHECK_NOTHROW( parseIntoConfig( argv, config ) );
161 
162             REQUIRE( config.noThrow == true );
163         }
164         SECTION( "--nothrow", "" ) {
165             const char* argv[] = { "test", "--nothrow" };
166             CHECK_NOTHROW( parseIntoConfig( argv, config ) );
167 
168             REQUIRE( config.noThrow == true );
169         }
170     }
171 
172     SECTION( "output filename", "" ) {
173         SECTION( "-o filename", "" ) {
174             const char* argv[] = { "test", "-o", "filename.ext" };
175             CHECK_NOTHROW( parseIntoConfig( argv, config ) );
176 
177             REQUIRE( config.outputFilename == "filename.ext" );
178         }
179         SECTION( "--out", "" ) {
180             const char* argv[] = { "test", "--out", "filename.ext" };
181             CHECK_NOTHROW( parseIntoConfig( argv, config ) );
182 
183             REQUIRE( config.outputFilename == "filename.ext" );
184         }
185     }
186 
187     SECTION( "combinations", "" ) {
188         SECTION( "Single character flags can be combined", "" ) {
189             const char* argv[] = { "test", "-abe" };
190             CHECK_NOTHROW( parseIntoConfig( argv, config ) );
191 
192             CHECK( config.abortAfter == 1 );
193             CHECK( config.shouldDebugBreak );
194             CHECK( config.noThrow == true );
195         }
196     }
197 
198     SECTION( "use-colour", "") {
199 
200         using Catch::UseColour;
201 
202         SECTION( "without option", "" ) {
203             const char* argv[] = { "test" };
204             CHECK_NOTHROW( parseIntoConfig( argv, config ) );
205 
206             REQUIRE( config.useColour == UseColour::Auto );
207         }
208 
209         SECTION( "auto", "" ) {
210             const char* argv[] = { "test", "--use-colour", "auto" };
211             CHECK_NOTHROW( parseIntoConfig( argv, config ) );
212 
213             REQUIRE( config.useColour == UseColour::Auto );
214         }
215 
216         SECTION( "yes", "" ) {
217             const char* argv[] = { "test", "--use-colour", "yes" };
218             CHECK_NOTHROW( parseIntoConfig( argv, config ) );
219 
220             REQUIRE( config.useColour == UseColour::Yes );
221         }
222 
223         SECTION( "no", "" ) {
224             const char* argv[] = { "test", "--use-colour", "no" };
225             CHECK_NOTHROW( parseIntoConfig( argv, config ) );
226 
227             REQUIRE( config.useColour == UseColour::No );
228         }
229 
230         SECTION( "error", "" ) {
231             const char* argv[] = { "test", "--use-colour", "wrong" };
232             REQUIRE_THROWS_WITH( parseIntoConfig( argv, config ), Contains( "colour mode must be one of" ) );
233         }
234     }
235 }
236 
237 
238 TEST_CASE( "Long strings can be wrapped", "[wrap]" ) {
239 
240     using namespace Catch;
241     SECTION( "plain string", "" ) {
242         // guide:                 123456789012345678
243         std::string testString = "one two three four";
244 
245         SECTION( "No wrapping", "" ) {
246             CHECK( Text( testString, TextAttributes().setWidth( 80 ) ).toString() == testString );
247             CHECK( Text( testString, TextAttributes().setWidth( 18 ) ).toString() == testString );
248         }
249         SECTION( "Wrapped once", "" ) {
250             CHECK( Text( testString, TextAttributes().setWidth( 17 ) ).toString() == "one two three\nfour" );
251             CHECK( Text( testString, TextAttributes().setWidth( 16 ) ).toString() == "one two three\nfour" );
252             CHECK( Text( testString, TextAttributes().setWidth( 14 ) ).toString() == "one two three\nfour" );
253             CHECK( Text( testString, TextAttributes().setWidth( 13 ) ).toString() == "one two three\nfour" );
254             CHECK( Text( testString, TextAttributes().setWidth( 12 ) ).toString() == "one two\nthree four" );
255         }
256         SECTION( "Wrapped twice", "" ) {
257             CHECK( Text( testString, TextAttributes().setWidth( 9 ) ).toString() == "one two\nthree\nfour" );
258             CHECK( Text( testString, TextAttributes().setWidth( 8 ) ).toString() == "one two\nthree\nfour" );
259             CHECK( Text( testString, TextAttributes().setWidth( 7 ) ).toString() == "one two\nthree\nfour" );
260         }
261         SECTION( "Wrapped three times", "" ) {
262             CHECK( Text( testString, TextAttributes().setWidth( 6 ) ).toString() == "one\ntwo\nthree\nfour" );
263             CHECK( Text( testString, TextAttributes().setWidth( 5 ) ).toString() == "one\ntwo\nthree\nfour" );
264         }
265         SECTION( "Short wrap", "" ) {
266             CHECK( Text( "abcdef", TextAttributes().setWidth( 4 ) ).toString() == "abc-\ndef" );
267             CHECK( Text( "abcdefg", TextAttributes().setWidth( 4 ) ).toString() == "abc-\ndefg" );
268             CHECK( Text( "abcdefgh", TextAttributes().setWidth( 4 ) ).toString() == "abc-\ndef-\ngh" );
269 
270             CHECK( Text( testString, TextAttributes().setWidth( 4 ) ).toString() == "one\ntwo\nthr-\nee\nfour" );
271             CHECK( Text( testString, TextAttributes().setWidth( 3 ) ).toString() == "one\ntwo\nth-\nree\nfo-\nur" );
272         }
273         SECTION( "As container", "" ) {
274             Text text( testString, TextAttributes().setWidth( 6 ) );
275             REQUIRE( text.size() == 4 );
276             CHECK( text[0] == "one" );
277             CHECK( text[1] == "two" );
278             CHECK( text[2] == "three" );
279             CHECK( text[3] == "four" );
280         }
281         SECTION( "Indent first line differently", "" ) {
282             Text text( testString, TextAttributes()
283                                         .setWidth( 10 )
284                                         .setIndent( 4 )
285                                         .setInitialIndent( 1 ) );
286             CHECK( text.toString() == " one two\n    three\n    four" );
287         }
288 
289     }
290 
291     SECTION( "With newlines", "" ) {
292 
293         // guide:                 1234567890123456789
294         std::string testString = "one two\nthree four";
295 
296         SECTION( "No wrapping" , "" ) {
297             CHECK( Text( testString, TextAttributes().setWidth( 80 ) ).toString() == testString );
298             CHECK( Text( testString, TextAttributes().setWidth( 18 ) ).toString() == testString );
299             CHECK( Text( testString, TextAttributes().setWidth( 10 ) ).toString() == testString );
300         }
301         SECTION( "Trailing newline" , "" ) {
302             CHECK( Text( "abcdef\n", TextAttributes().setWidth( 10 ) ).toString() == "abcdef" );
303             CHECK( Text( "abcdef", TextAttributes().setWidth( 6 ) ).toString() == "abcdef" );
304             CHECK( Text( "abcdef\n", TextAttributes().setWidth( 6 ) ).toString() == "abcdef" );
305             CHECK( Text( "abcdef\n", TextAttributes().setWidth( 5 ) ).toString() == "abcd-\nef" );
306         }
307         SECTION( "Wrapped once", "" ) {
308             CHECK( Text( testString, TextAttributes().setWidth( 9 ) ).toString() == "one two\nthree\nfour" );
309             CHECK( Text( testString, TextAttributes().setWidth( 8 ) ).toString() == "one two\nthree\nfour" );
310             CHECK( Text( testString, TextAttributes().setWidth( 7 ) ).toString() == "one two\nthree\nfour" );
311         }
312         SECTION( "Wrapped twice", "" ) {
313             CHECK( Text( testString, TextAttributes().setWidth( 6 ) ).toString() == "one\ntwo\nthree\nfour" );
314         }
315     }
316 
317     SECTION( "With wrap-before/ after characters", "" ) {
318         std::string testString = "one,two(three) <here>";
319 
320         SECTION( "No wrapping", "" ) {
321             CHECK( Text( testString, TextAttributes().setWidth( 80 ) ).toString() == testString );
322             CHECK( Text( testString, TextAttributes().setWidth( 24 ) ).toString() == testString );
323         }
324         SECTION( "Wrap before", "" ) {
325             CHECK( Text( testString, TextAttributes().setWidth( 11 ) ).toString() == "one,two\n(three)\n<here>" );
326         }
327         SECTION( "Wrap after", "" ) {
328             CHECK( Text( testString, TextAttributes().setWidth( 6 ) ).toString() == "one,\ntwo\n(thre-\ne)\n<here>" );
329             CHECK( Text( testString, TextAttributes().setWidth( 5 ) ).toString() == "one,\ntwo\n(thr-\nee)\n<her-\ne>" );
330             CHECK( Text( testString, TextAttributes().setWidth( 4 ) ).toString() == "one,\ntwo\n(th-\nree)\n<he-\nre>" );
331         }
332     }
333 
334 }
335 
336 using namespace Catch;
337 
338 class ColourString {
339 public:
340 
341     struct ColourIndex {
ColourIndexColourString::ColourIndex342         ColourIndex( Colour::Code _colour, std::size_t _fromIndex, std::size_t _toIndex )
343         :   colour( _colour ),
344             fromIndex( _fromIndex ),
345             toIndex( _toIndex )
346         {}
347 
348         Colour::Code colour;
349         std::size_t fromIndex;
350         std::size_t toIndex;
351     };
352 
ColourString(std::string const & _string)353     ColourString( std::string const& _string )
354     : string( _string )
355     {}
ColourString(std::string const & _string,std::vector<ColourIndex> const & _colours)356     ColourString( std::string const& _string, std::vector<ColourIndex> const& _colours )
357     : string( _string ), colours( _colours )
358     {}
359 
addColour(Colour::Code colour,int _index)360     ColourString& addColour( Colour::Code colour, int _index ) {
361         colours.push_back( ColourIndex( colour,
362                                         resolveRelativeIndex( _index ),
363                                         resolveRelativeIndex( _index )+1 ) );
364         return *this;
365     }
addColour(Colour::Code colour,int _fromIndex,int _toIndex)366     ColourString& addColour( Colour::Code colour, int _fromIndex, int _toIndex ) {
367         colours.push_back( ColourIndex( colour,
368                                         resolveRelativeIndex(_fromIndex),
369                                         resolveLastRelativeIndex( _toIndex ) ) );
370         return *this;
371     }
372 
writeToStream(std::ostream & _stream) const373     void writeToStream( std::ostream& _stream ) const {
374         std::size_t last = 0;
375         for( std::size_t i = 0; i < colours.size(); ++i ) {
376             ColourIndex const& index = colours[i];
377             if( index.fromIndex > last )
378                 _stream << string.substr( last, index.fromIndex-last );
379             {
380                 Colour colourGuard( index.colour );
381                 _stream << string.substr( index.fromIndex, index.toIndex-index.fromIndex );
382             }
383             last = index.toIndex;
384         }
385         if( last < string.size() )
386             _stream << string.substr( last );
387     }
operator <<(std::ostream & _stream,ColourString const & _colourString)388     friend std::ostream& operator << ( std::ostream& _stream, ColourString const& _colourString ) {
389         _colourString.writeToStream( _stream );
390         return _stream;
391     }
392 
393 private:
394 
resolveLastRelativeIndex(int _index)395     std::size_t resolveLastRelativeIndex( int _index ) {
396         std::size_t index = resolveRelativeIndex( _index );
397         return index == 0 ? string.size() : index;
398     }
resolveRelativeIndex(int _index)399     std::size_t resolveRelativeIndex( int _index ) {
400         return static_cast<std::size_t>( _index >= 0
401             ? _index
402             : static_cast<int>( string.size() )+_index );
403     }
404     std::string string;
405     std::vector<ColourIndex> colours;
406 };
407 
408 TEST_CASE( "replaceInPlace", "" ) {
409     std::string letters = "abcdefcg";
410     SECTION( "replace single char" ) {
411         CHECK( replaceInPlace( letters, "b", "z" ) );
412         CHECK( letters == "azcdefcg" );
413     }
414     SECTION( "replace two chars" ) {
415         CHECK( replaceInPlace( letters, "c", "z" ) );
416         CHECK( letters == "abzdefzg" );
417     }
418     SECTION( "replace first char" ) {
419         CHECK( replaceInPlace( letters, "a", "z" ) );
420         CHECK( letters == "zbcdefcg" );
421     }
422     SECTION( "replace last char" ) {
423         CHECK( replaceInPlace( letters, "g", "z" ) );
424         CHECK( letters == "abcdefcz" );
425     }
426     SECTION( "replace all chars" ) {
427         CHECK( replaceInPlace( letters, letters, "replaced" ) );
428         CHECK( letters == "replaced" );
429     }
430     SECTION( "replace no chars" ) {
431         CHECK_FALSE( replaceInPlace( letters, "x", "z" ) );
432         CHECK( letters == letters );
433     }
434     SECTION( "escape '" ) {
435         std::string s = "didn't";
436         CHECK( replaceInPlace( s, "'", "|'" ) );
437         CHECK( s == "didn|'t" );
438     }
439 }
440 
441 // !TBD: This will be folded into Text class
442 TEST_CASE( "Strings can be rendered with colour", "[.colour]" ) {
443 
444     {
445         ColourString cs( "hello" );
446         cs  .addColour( Colour::Red, 0 )
447             .addColour( Colour::Green, -1 );
448 
449         Catch::cout() << cs << std::endl;
450     }
451 
452     {
453         ColourString cs( "hello" );
454         cs  .addColour( Colour::Blue, 1, -2 );
455 
456         Catch::cout() << cs << std::endl;
457     }
458 
459 }
460 
461 TEST_CASE( "Text can be formatted using the Text class", "" ) {
462 
463     CHECK( Text( "hi there" ).toString() == "hi there" );
464 
465     TextAttributes narrow;
466     narrow.setWidth( 6 );
467 
468     CHECK( Text( "hi there", narrow ).toString() == "hi\nthere" );
469 }
470 
471 TEST_CASE( "Long text is truncted", "[Text][Truncated]" ) {
472 
473     std::string longLine( 90, '*' );
474 
475     std::ostringstream oss;
476     for(int i = 0; i < 600; ++i )
477         oss << longLine << longLine << "\n";
478     Text t( oss.str() );
479     CHECK_THAT( t.toString(), EndsWith( "... message truncated due to excessive size" ) );
480 
481 }
482 
manuallyRegisteredTestFunction()483 inline void manuallyRegisteredTestFunction() {
484     SUCCEED( "was called" );
485 }
486 struct AutoTestReg {
AutoTestRegAutoTestReg487     AutoTestReg() {
488         REGISTER_TEST_CASE( manuallyRegisteredTestFunction, "ManuallyRegistered", "" );
489     }
490 };
491 AutoTestReg autoTestReg;
492