TestAssertion.cpp

Tests that validate the testing framework

00001 
00002 #include "Assertion.hpp"
00003 
00004 using namespace unittest;
00005 
00006 //-----------------------------------------------------------------------------------------
00007 
00008 class A {
00009 public:
00010     A(int value) { this->value = value; }
00011     
00012     bool operator==(const A& other) const { return value == other.value; }
00013     bool operator!=(const A& other) const { return !(value == other.value); }
00014     
00015     int value;
00016     
00017 };
00018 
00019 ostream& operator<<(ostream& out, const A& a) { out << a.value; return out; }
00020     
00021 //-----------------------------------------------------------------------------------------
00022 
00023 class TestAssertionFixture {
00024 };
00025 
00026 //-----------------------------------------------------------------------------------------
00027 
00028 TESTCASE(TestAssertion)
00029     
00030 //-----------------------------------------------------------------------------------------
00031 
00032 TEST(WrongAssertionThrowsException, 
00033     
00034     bool exceptionCatched = false;
00035     
00036     try {
00037         assertTrue(false);
00038     } catch(AssertionException& e) {
00039         exceptionCatched = true;
00040     }
00041     
00042     assertTrue(exceptionCatched);
00043     
00044 )
00045 
00046 //-----------------------------------------------------------------------------------------
00047 
00048 TEST(IntegerEqualsAssertion,
00049     
00050     bool exceptionCatched = false;
00051     
00052     try {
00053         assertEquals(12, 22);
00054     } catch(AssertionException& e) {
00055         exceptionCatched = true;
00056     }
00057     
00058     assertTrue(exceptionCatched);
00059     
00060     assertEquals(12, 12);
00061     assertNotEquals(12, 22);
00062     
00063 )
00064 
00065 //-----------------------------------------------------------------------------------------
00066 
00067 TEST(CStringEqualsAssertion,
00068     
00069     bool exceptionCatched = false;
00070     
00071     try {
00072         assertEquals("hello", "no");
00073     } catch(AssertionException& e) {
00074         exceptionCatched = true;
00075     }
00076     
00077     assertTrue(exceptionCatched);
00078     
00079     assertEquals("hello", "hello");
00080     assertNotEquals("hello", "no");
00081         
00082 )
00083 
00084 //-----------------------------------------------------------------------------------------
00085 
00086 TEST(DoubleEqualsAssertion,
00087     
00088     bool exceptionCatched = false;
00089     
00090     try {
00091         assertEquals(12.98, 22.65);
00092     } catch(AssertionException& e) {
00093         exceptionCatched = true;
00094     }
00095     
00096     assertTrue(exceptionCatched);
00097     
00098     assertEquals(12.98, 12.98);
00099     assertNotEquals(12.98, 22.65);
00100     
00101 )
00102 
00103 //-----------------------------------------------------------------------------------------
00104 
00105 TEST(StringEqualsAssertion,
00106     
00107     bool exceptionCatched = false;
00108     
00109     try {
00110         assertEquals(string("hello"), string("no"));
00111     } catch(AssertionException& e) {
00112         exceptionCatched = true;
00113     }
00114     
00115     assertTrue(exceptionCatched);
00116     
00117     assertEquals(string("hello"), string("hello"));
00118     assertNotEquals(string("hello"), string("no"));
00119     
00120 )
00121 
00122 //-----------------------------------------------------------------------------------------
00123 
00124 TEST(ObjectEqualsAssertion,
00125     
00126     bool exceptionCatched = false;
00127     
00128     try {
00129         assertEquals(A(16), A(3));
00130     } catch(AssertionException& e) {
00131         exceptionCatched = true;
00132     }
00133     
00134     assertTrue(exceptionCatched);
00135     
00136     assertEquals(A(16), A(16));
00137     assertNotEquals(A(16), A(3));
00138     
00139 )
00140 
00141 //-----------------------------------------------------------------------------------------
00142 
00143 TEST(StopTestOnFailure,
00144 
00145     static bool reached = false;
00146     
00147     class AutoTest : public Test {
00148     public:
00149     
00150         virtual void test() {
00151             
00152             assertTrue(false);
00153             
00154             reached = true;
00155             
00156         }
00157         
00158         static Test* factory() {
00159             return new AutoTest();
00160         }
00161         
00162     };
00163     
00164     TestReport report(true);
00165     TestSet set("AutoTest");
00166     
00167     set.names.push_back("Test");
00168     set.tests.push_back(AutoTest::factory);
00169     
00170     set.run(report);
00171     
00172     assertTrue(!reached);
00173 
00174 )
00175 
00176 //-----------------------------------------------------------------------------------------
00177 
00178 END_TESTCASE

Generated on Mon Jun 5 22:05:27 2006 for ut4cpp by  doxygen 1.4.6-NO