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
|
/*
* Robot Testing Framework
*
* Copyright (C) 2015-2019 Istituto Italiano di Tecnologia (IIT)
*
* 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.1 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <robottestingframework/Asserter.h>
#include <robottestingframework/ConsoleListener.h>
#include <robottestingframework/TestAssert.h>
#include <robottestingframework/TestCase.h>
#include <robottestingframework/TestResult.h>
#include <robottestingframework/TestResultCollector.h>
#include <iostream>
using namespace robottestingframework;
class TestCheck : public TestCase
{
public:
size_t exCount;
TestCheck() :
TestCase("TestCheck")
{
exCount = 0;
}
void run() override
{
try {
Asserter::testCheck(true, TestMessage("true"), this);
} catch (std::exception& e) {
exCount++;
}
try {
Asserter::testCheck(false, TestMessage("false"), this);
} catch (std::exception& e) {
exCount++;
}
}
};
class TestFail : public TestCase
{
public:
size_t exCount;
TestFail() :
TestCase("TestFail")
{
exCount = 0;
}
void run() override
{
try {
Asserter::testFail(true, TestMessage("true"), this);
} catch (std::exception& e) {
exCount++;
}
try {
Asserter::testFail(false, TestMessage("false"), this);
} catch (std::exception& e) {
exCount++;
}
}
};
class MyTest : public TestCase
{
public:
MyTest() :
TestCase("AsserterTest")
{
}
void run() override
{
ROBOTTESTINGFRAMEWORK_TEST_REPORT("Cheking Asserter::fail");
try {
Asserter::fail(true, TestMessage("TRUE"));
Asserter::fail(false, TestMessage("FALSE"));
} catch (TestFailureException& e) {
ROBOTTESTINGFRAMEWORK_TEST_FAIL_IF_FALSE(std::string(e.what()) == std::string("FALSE"), "exception message");
} catch (std::exception& e) {
ROBOTTESTINGFRAMEWORK_ASSERT_FAIL("Got wrong exception std::exception");
}
ROBOTTESTINGFRAMEWORK_TEST_REPORT("Cheking Asserter::error");
try {
Asserter::error(true, TestMessage("TRUE"));
Asserter::error(false, TestMessage("FALSE"));
} catch (TestErrorException& e) {
ROBOTTESTINGFRAMEWORK_TEST_FAIL_IF_FALSE(std::string(e.what()) == std::string("FALSE"), "exception message");
} catch (std::exception& e) {
ROBOTTESTINGFRAMEWORK_ASSERT_FAIL("Got wrong exception std::exception");
}
ROBOTTESTINGFRAMEWORK_TEST_REPORT("Cheking Asserter::testCheck");
TestResultCollector collector;
TestResult result;
result.addListener(&collector);
TestCheck check;
check.TestCase::run(result);
ROBOTTESTINGFRAMEWORK_TEST_CHECK(collector.failedCount() == 1, "Cheking Asserter::testCheck for failures count");
ROBOTTESTINGFRAMEWORK_TEST_CHECK(check.exCount == 0, "Cheking Asserter::testCheck for exceptions count");
ROBOTTESTINGFRAMEWORK_TEST_REPORT("Cheking Asserter::testFail");
collector.reset();
ROBOTTESTINGFRAMEWORK_TEST_CHECK(collector.failedCount() == 0, "Cheking collector.reset()");
TestFail fail;
fail.TestCase::run(result);
ROBOTTESTINGFRAMEWORK_TEST_CHECK(collector.failedCount() == 1, "Cheking Asserter::testFail for failures count");
ROBOTTESTINGFRAMEWORK_TEST_CHECK(fail.exCount == 0, "Cheking Asserter::testFail for exceptions count");
}
};
int main(int argc, char** argv)
{
ConsoleListener listener;
TestResultCollector collector;
TestResult result;
result.addListener(&listener);
result.addListener(&collector);
MyTest test;
test.TestCase::run(result);
return collector.failedCount();
}
|