Skip to content

Instantly share code, notes, and snippets.

@ngerakines
Created January 16, 2013 18:07
Show Gist options
  • Save ngerakines/4549303 to your computer and use it in GitHub Desktop.
Save ngerakines/4549303 to your computer and use it in GitHub Desktop.
tdd workshop stuff
// This is the test executable: where the tests will be declared and run.
#include "test.h"
#include <iostream>
#include <string>
#include <map>
#include <time.h>
using namespace std;
typedef void (*ScriptFunction)(void);
typedef map<string, ScriptFunction> script_map;
#define HAS_MEM_FUNC(func, name) \
template<typename T, typename Sign> \
struct name { \
typedef char yes[1]; \
typedef char no [2]; \
template <typename U, U> struct type_check; \
template <typename _1> static yes &chk(type_check<Sign, &_1::func> *); \
template <typename > static no &chk(...); \
static bool const value = sizeof(chk<T>(0)) == sizeof(yes); \
}
void test_plan(int count, string name)
{
cout << "1.." << count << " # " << name << endl;
}
void is_true(bool value, int number, string description)
{
if (value == false)
{
cout << "not ";
}
cout << "ok " << number << " " << description << endl;
}
HAS_MEM_FUNC(setup, has_setup);
class SuiteOne
{
public:
void setup() {
cout << "WHAT?!?! " << endl;
}
};
class SuiteTwo
{
};
void suite1(void)
{
if (has_setup<SuiteOne, void(SuiteOne::*)()>::value) {
cout << "Call setup" << endl;
}
test_plan(1, "suite1");
is_true(dummyFunc() == 0, 1, "is dummyFunc 0");
}
void suite2(void)
{
if (has_setup<SuiteTwo, void(SuiteTwo::*)()>::value) {
cout << "Call setup" << endl;
}
test_plan(1, "suite2");
is_true(dummyFunc1() == 0, 1, "is dummyFunc1 0");
}
void suite3(void)
{
test_plan(1, "suite3");
is_true(dummyFunc2() == 0, 1, "is dummyFunc2 0");
}
int main(int argc, char* argv[])
{
script_map m;
m.insert(std::make_pair("suite1", &suite1));
m.insert(std::make_pair("suite2", &suite2));
m.insert(std::make_pair("suite3", &suite3));
clock_t init, final;
init = clock();
if (argc > 1) {
int executed_count = 0;
for (int i = 1; i < argc; i++)
{
script_map::const_iterator iter = m.find(argv[i]);
if (iter == m.end()) {
} else {
executed_count++;
iter->second();
}
}
if (executed_count == 0)
{
cout << "WARNING: suites were presented but nothing executed." << endl;
return 1;
}
return 0;
} else {
cout << "INFO: No suite defined, running all of them." << endl;
script_map::const_iterator iter;
for (iter = m.begin(); iter != m.end(); ++iter)
{
iter->second();
}
}
final = clock() - init;
cout << "# Tests completed in " << (double)final / ((double)CLOCKS_PER_SEC) << " seconds" << endl;
return 0;
}
// The implementation file where the test mechanisms will live. This file is
// compiled into a library which is linked with the test executable.
int dummyFunc()
{
return 0;
}
int dummyFunc1()
{
return 0;
}
int dummyFunc2()
{
return 0;
}
// The include file where we will expose our test interface.
int dummyFunc();
int dummyFunc1();
int dummyFunc2();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment