Jon Test
C++ Unit Test Tool. Not for production -- sample code only
Loading...
Searching...
No Matches
SimpleTest.cpp

Example Test Case with simple int main().

Example Test Case with simple int main().

#include "JonTest/Assert.h"
#include <cstring>
#include <iostream>
#include <string>
int
main(
int argc,
char**argv
)
{
JonTest::StreamLogger logger(std::cout, true);
const JonTest::Count counts = JonTest::TestRunner::get().run(logger);
return counts.fails;
}
TEST_SUITE(SimpleTest)
const std::string initialString = "This is a test string!";
std::string testString;
void setup()
{
testString = initialString;
// NOTE: testing assert in setup()
assertEqual(initialString, testString, "No change has been made");
}
void teardown()
{
testString.clear();
// NOTICE: testing assert in teardown()
assertEqual(0, testString.length(), "Length should be zero");
}
TEST_CASE(stringEqual)
{
// Duplicates test in setup()
assertEqual(initialString, testString, "No change has been made");
}
TEST_CASE(stringClear)
{
testString.clear();
assertEqual(std::string(), testString, "Should be empty string");
// Duplicates test in teardown()
assertEqual(0, testString.length(), "Length should be zero");
// NOTICE: This compares a char string with a std::string
assertEqual("", testString, "Should be empty string");
}
TEST_CASE(stringAssignCharEmpty)
{
testString = "";
assertEqual(std::string(), testString, "Should be empty string");
assertEqual(0, testString.length(), "Length should be zero");
// NOTICE: This compares a char string with a std::string
assertEqual("", testString, "Should be empty string");
}
TEST_CASE(stringAssignInitial)
{
testString = "";
assertEqual(std::string(), testString, "Should be empty string");
testString = initialString;
assertEqual(initialString, testString, "Should match after assignment");
assertEqual(strlen(initialString.c_str()), testString.length(), "Compare length against c string");
assertEqual(strlen(initialString.c_str()), strlen(testString.c_str()), "Compare length of both as c strings");
assertEqual(0, strncmp(initialString.c_str(), testString.c_str(), initialString.length()), "Compare both as c strings");
// NOTICE: This compares a char string with a std::string
assertEqual(initialString, testString.c_str(), "Should match as c string after assignment");
}
#define assertEqual(first, second, message)
Fail this test if (first) is not equal to (second) with the given message as explanation.
Definition Assert.h:45
#define TEST_SUITE_END()
Finish definition of a Test Suite started with TEST_SUITE().
Definition TestSuite.h:77
#define TEST_CASE(testCaseName)
Defines a Test Case method named testCaseName within a Test Suite defined by TEST_SUITE().
Definition TestSuite.h:91
#define TEST_SUITE(testSuiteName)
Defines a Test Suite named testSuiteName.
Definition TestSuite.h:63
Count of tests run, and fails.
Definition Count.h:10
int fails
Number of tests that have failed.
Definition Count.h:18
Stream-based Logger for events while running tests.
Definition Logger.h:89
Count run(Logger &logger) const
Run all tests that have been add()ed.
static TestRunner & get()
Get the singleton.