aboutsummaryrefslogtreecommitdiff
path: root/src/unitsuite.cpp
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/unitsuite.cpp95
1 files changed, 63 insertions, 32 deletions
diff --git a/src/unitsuite.cpp b/src/unitsuite.cpp
index 51cca10..711b9b5 100644
--- a/src/unitsuite.cpp
+++ b/src/unitsuite.cpp
@@ -7,16 +7,21 @@
7 7
8#include "bu/unitsuite.h" 8#include "bu/unitsuite.h"
9#include "bu/file.h" 9#include "bu/file.h"
10#include "bu/sio.h"
11
12using namespace Bu;
10 13
11#include <unistd.h> 14#include <unistd.h>
12 15
13Bu::UnitSuite::UnitSuite() : 16Bu::UnitSuite::UnitSuite() :
14 iOptions( 0 ) 17 iOptions( 0 ),
18 iNameWidth( 0 )
15{ 19{
16} 20}
17 21
18Bu::UnitSuite::UnitSuite( int iOptions ) : 22Bu::UnitSuite::UnitSuite( int iOptions ) :
19 iOptions( iOptions ) 23 iOptions( iOptions ),
24 iNameWidth( 0 )
20{ 25{
21} 26}
22 27
@@ -33,37 +38,46 @@ int Bu::UnitSuite::run( int /*argc*/, char * /*argv */ [] )
33 int iUFail = 0; 38 int iUFail = 0;
34 for( TestList::iterator i = lTests.begin(); i != lTests.end(); i++ ) 39 for( TestList::iterator i = lTests.begin(); i != lTests.end(); i++ )
35 { 40 {
36 printf("%s: ", i->sName.getStr() ); 41 sio << Fmt( iNameWidth+3, Fmt::Left ).fill('.') << i->sName
37 fflush( stdout ); 42 << sio.flush;
38 try 43 try
39 { 44 {
40 (this->*(i->fTest))(); 45 (this->*(i->fTest))();
41 switch( i->eExpect ) 46 switch( i->eExpect )
42 { 47 {
43 case expectPass: printf("expected pass.\n"); iEPass++; break; 48 case expectPass:
44 case expectFail: printf("unexpected pass.\n"); iUPass++; break; 49 sio << "pass." << sio.nl;
50 iEPass++;
51 break;
52
53 case expectFail:
54 sio << "unexpected pass." << sio.nl;
55 iUPass++;
56 break;
45 } 57 }
46 } 58 }
47 catch( Failed &e ) 59 catch( Failed &e )
48 { 60 {
49 switch( i->eExpect ) 61 switch( i->eExpect )
50 { 62 {
51 case expectPass: printf("unexpected "); iUFail++; break; 63 case expectPass:
52 case expectFail: printf("expected "); iEFail++; break; 64 sio << "unexpected ";
65 iUFail++;
66 break;
67
68 case expectFail:
69 sio << "expected ";
70 iEFail++;
71 break;
53 } 72 }
54 if( e.bFile ) 73 if( e.bFile )
55 { 74 {
56 printf("fail in unitTest(%s). (%s:%d)\n", 75 sio << "fail in unitTest(" << e.str << "). (" << e.sFile
57 e.str.getStr(), 76 << ":" << e.nLine << ")." << sio.nl;
58 e.sFile.getStr(),
59 e.nLine
60 );
61 } 77 }
62 else 78 else
63 { 79 {
64 printf("fail in unitTest(%s).\n", 80 sio << "fail in unitTest(" << e.str << ")." << sio.nl;
65 e.str.getStr()
66 );
67 } 81 }
68 82
69 if( (iOptions & optStopOnError) ) 83 if( (iOptions & optStopOnError) )
@@ -73,10 +87,17 @@ int Bu::UnitSuite::run( int /*argc*/, char * /*argv */ [] )
73 { 87 {
74 switch( i->eExpect ) 88 switch( i->eExpect )
75 { 89 {
76 case expectPass: printf("unexpected "); iUFail++; break; 90 case expectPass:
77 case expectFail: printf("expected "); iEFail++; break; 91 sio << "unexpected ";
92 iUFail++;
93 break;
94
95 case expectFail:
96 sio << "expected ";
97 iEFail++;
98 break;
78 } 99 }
79 printf("fail with unknown exception. what: %s\n", e.what() ); 100 sio << "fail with unknown exception. what: " << e.what() << sio.nl;
80 101
81 if( (iOptions & optStopOnError) ) 102 if( (iOptions & optStopOnError) )
82 return 0; 103 return 0;
@@ -85,25 +106,32 @@ int Bu::UnitSuite::run( int /*argc*/, char * /*argv */ [] )
85 { 106 {
86 switch( i->eExpect ) 107 switch( i->eExpect )
87 { 108 {
88 case expectPass: printf("unexpected "); iUFail++; break; 109 case expectPass:
89 case expectFail: printf("expected "); iEFail++; break; 110 sio << "unexpected ";
111 iUFail++;
112 break;
113
114 case expectFail:
115 sio << "expected ";
116 iEFail++;
117 break;
90 } 118 }
91 printf("fail with external exception.\n"); 119 sio << "fail with external exception." << sio.nl;
92 120
93 if( (iOptions & optStopOnError) ) 121 if( (iOptions & optStopOnError) )
94 return 0; 122 return 0;
95 } 123 }
96 } 124 }
97 125
98 printf("\nReport:\n" 126 sio << sio.nl
99 "\tTotal tests run: %ld\n" 127 << "Report:" << sio.nl
100 "\tExpected passes: %d\n" 128 << "\tTotal tests run: " << lTests.getSize() << sio.nl
101 "\tExpected failures: %d\n" 129 << "\tExpected passes: " << iEPass << sio.nl
102 "\tUnexpected passes: %d\n" 130 << "\tExpected failures: " << iEFail << sio.nl
103 "\tUnexpected failures: %d\n\n", 131 << "\tUnexpected passes: " << iUPass << sio.nl
104 lTests.getSize(), iEPass, iEFail, iUPass, iUFail ); 132 << "\tUnexpected failures: " << iUFail << sio.nl << sio.nl;
105 if( iUPass == 0 && iUFail == 0 ) 133 if( iUPass == 0 && iUFail == 0 )
106 printf("\tNothing unexpected.\n\n"); 134 sio << "\tNothing unexpected." << sio.nl << sio.nl;
107 135
108 for( StrList::iterator i = lFileCleanup.begin(); i; i++ ) 136 for( StrList::iterator i = lFileCleanup.begin(); i; i++ )
109 { 137 {
@@ -113,10 +141,11 @@ int Bu::UnitSuite::run( int /*argc*/, char * /*argv */ [] )
113 return 0; 141 return 0;
114} 142}
115 143
116void Bu::UnitSuite::tempFile( Bu::FString &sFileName ) 144Bu::File Bu::UnitSuite::tempFile( Bu::FString &sFileName )
117{ 145{
118 Bu::File::tempFile( sFileName ); 146 Bu::File f = Bu::File::tempFile( sFileName );
119 lFileCleanup.append( sFileName ); 147 lFileCleanup.append( sFileName );
148 return f;
120} 149}
121 150
122void Bu::UnitSuite::add( Test fTest, const Bu::FString &sName, Expect e ) 151void Bu::UnitSuite::add( Test fTest, const Bu::FString &sName, Expect e )
@@ -133,6 +162,8 @@ void Bu::UnitSuite::add( Test fTest, const Bu::FString &sName, Expect e )
133 } 162 }
134 ti.fTest = fTest; 163 ti.fTest = fTest;
135 lTests.append( ti ); 164 lTests.append( ti );
165 if( iNameWidth < ti.sName.getSize() )
166 iNameWidth = ti.sName.getSize();
136} 167}
137 168
138void Bu::UnitSuite::setName( const FString &sName ) 169void Bu::UnitSuite::setName( const FString &sName )