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
|
// ---
//
// $Id: mytest.cpp,v 1.5 2008/07/11 16:49:43 hartwork Exp $
//
// CppTest - A C++ Unit Testing Framework
// Copyright (c) 2003 Niklas Lundell
//
// ---
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the
// Free Software Foundation, Inc., 59 Temple Place - Suite 330,
// Boston, MA 02111-1307, USA.
//
// ---
//
// Test program demonstrating all assert types and output handlers.
//
// ---
#include <cstdlib>
#include <cstring>
#include <iostream>
#ifdef _MSC_VER
#pragma warning (disable: 4290)
#endif
#include "../src/cpptest.h"
using namespace std;
// Tests unconditional fail asserts
//
class FailTestSuite : public Test::Suite
{
public:
FailTestSuite()
{
TEST_ADD(FailTestSuite::success)
TEST_ADD(FailTestSuite::always_fail)
}
private:
void success() {}
void always_fail()
{
// This will always fail
//
TEST_FAIL("unconditional fail");
}
};
// Tests compare asserts
//
class CompareTestSuite : public Test::Suite
{
public:
CompareTestSuite()
{
TEST_ADD(CompareTestSuite::success)
TEST_ADD(CompareTestSuite::compare)
TEST_ADD(CompareTestSuite::delta_compare)
}
private:
void success() {}
void compare()
{
// Will succeed since the expression evaluates to true
//
TEST_ASSERT(1 + 1 == 2)
// Will fail since the expression evaluates to false
//
TEST_ASSERT(0 == 1);
}
void delta_compare()
{
// Will succeed since the expression evaluates to true
//
TEST_ASSERT_DELTA(0.5, 0.7, 0.3);
// Will fail since the expression evaluates to false
//
TEST_ASSERT_DELTA(0.5, 0.7, 0.1);
}
};
// Tests throw asserts
//
class ThrowTestSuite : public Test::Suite
{
public:
ThrowTestSuite()
{
TEST_ADD(ThrowTestSuite::success)
TEST_ADD(ThrowTestSuite::test_throw)
}
private:
void success() {}
void test_throw()
{
// Will fail since the none of the functions throws anything
//
TEST_THROWS_MSG(func(), int, "func() does not throw, expected int exception")
TEST_THROWS_MSG(func_no_throw(), int, "func_no_throw() does not throw, expected int exception")
TEST_THROWS_ANYTHING_MSG(func(), "func() does not throw, expected any exception")
TEST_THROWS_ANYTHING_MSG(func_no_throw(), "func_no_throw() does not throw, expected any exception")
// Will succeed since none of the functions throws anything
//
TEST_THROWS_NOTHING(func())
TEST_THROWS_NOTHING(func_no_throw())
// Will succeed since func_throw_int() throws an int
//
TEST_THROWS(func_throw_int(), int)
TEST_THROWS_ANYTHING(func_throw_int())
// Will fail since func_throw_int() throws an int (not a float)
//
TEST_THROWS_MSG(func_throw_int(), float, "func_throw_int() throws an int, expected a float exception")
TEST_THROWS_NOTHING_MSG(func_throw_int(), "func_throw_int() throws an int, expected no exception at all")
}
void func() {}
void func_no_throw() {}
void func_throw_int() { throw 13; }
};
enum OutputType
{
Compiler,
Html,
TextTerse,
TextVerbose
};
static void
usage()
{
cout << "usage: mytest [MODE]\n"
<< "where MODE may be one of:\n"
<< " --compiler\n"
<< " --html\n"
<< " --text-terse (default)\n"
<< " --text-verbose\n";
exit(0);
}
static unique_ptr<Test::Output>
cmdline(int argc, char* argv[])
{
if (argc > 2)
usage(); // will not return
Test::Output* output = 0;
if (argc == 1)
output = new Test::TextOutput(Test::TextOutput::Verbose);
else
{
const char* arg = argv[1];
if (strcmp(arg, "--compiler") == 0)
output = new Test::CompilerOutput;
else if (strcmp(arg, "--html") == 0)
output = new Test::HtmlOutput;
else if (strcmp(arg, "--text-terse") == 0)
output = new Test::TextOutput(Test::TextOutput::Terse);
else if (strcmp(arg, "--text-verbose") == 0)
output = new Test::TextOutput(Test::TextOutput::Verbose);
else
{
cout << "invalid commandline argument: " << arg << endl;
usage(); // will not return
}
}
return unique_ptr<Test::Output>(output);
}
// Main test program
//
int
main(int argc, char* argv[])
{
try
{
// Demonstrates the ability to use multiple test suites
//
Test::Suite ts;
ts.add(unique_ptr<Test::Suite>(new FailTestSuite));
ts.add(unique_ptr<Test::Suite>(new CompareTestSuite));
ts.add(unique_ptr<Test::Suite>(new ThrowTestSuite));
// Run the tests
//
unique_ptr<Test::Output> output(cmdline(argc, argv));
ts.run(*output, true);
Test::HtmlOutput* const html = dynamic_cast<Test::HtmlOutput*>(output.get());
if (html)
html->generate(cout, true, "MyTest");
}
catch (...)
{
cout << "unexpected exception encountered\n";
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
|