-
Notifications
You must be signed in to change notification settings - Fork 0
/
EnlightenTest.cpp
200 lines (157 loc) · 5.39 KB
/
EnlightenTest.cpp
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
#ifndef CPPUNIT_EXTENSIONS_HELPERMACROS_H
#include <cppunit/extensions/HelperMacros.h>
#endif
#include <sstream>
#include <iostream>
#include "enlighten.h"
#include <string>
////////////////////////////////////////////////////////////////////////////////
using namespace enlighten;
////////////////////////////////////////////////////////////////////////////////
class EnlightenTest : public CPPUNIT_NS::TestFixture
{
CPPUNIT_TEST_SUITE(EnlightenTest);
CPPUNIT_TEST(testGivenWhenThen);
CPPUNIT_TEST(testStringsPassedAreMappedToAStep);
CPPUNIT_TEST(testStringsPassedThatNotMapToAStepShouldThrow);
CPPUNIT_TEST(testPassingNotPrimitiveParametersToSteps);
CPPUNIT_TEST(testSpecifyingStepsThatThrow);
CPPUNIT_TEST(testIfAStepFailInAssertionItShouldPassTheMessageToTheUser);
CPPUNIT_TEST_SUITE_END();
public:
void setUp() {}
void tearDown() {}
bool wasCalled;
int apples;
public:
void testGivenWhenThen()
{
GIVEN << "i have " << 1 << "apple" << OK;
WHEN << "i buy " << 2 << "apple(s)" << OK;
THEN << "i should have " << 3 << "apples" << OK;
}
void testStringsPassedAreMappedToAStep()
{
wasCalled= false;
WHEN << "a string " << lit("any descriptive requisit") << "mapped to a step was formed" << OK;
THEN << "the step should be called" << OK;
}
void testStringsPassedThatNotMapToAStepShouldThrow()
{
try {
WHEN << "a string passed didnt have a method mapped" << OK;
THEN << "that line should not be executed" << OK;
} catch (std::runtime_error &) {
THEN << "that line should be executed" << OK;
}
}
struct DummyClass {
int attr;
DummyClass() : attr(-1) {}
} _dummy;
void testPassingNotPrimitiveParametersToSteps()
{
//static setup
//given << "i have a step with a not primitive parameter" << ok;
DummyClass notPrimitiveParameter;
notPrimitiveParameter.attr= 12;
WHEN << "i describe a string passing a" << notPrimitiveParameter << OK;
THEN << "the step called should have received the parameter" << notPrimitiveParameter << OK;
}
void testSpecifyingStepsThatThrow()
{
GIVEN << "i specify that a step will throw" << OK;
WHEN << "i call a step that throws" << OK;
THEN << "it should not throw to the user" << OK;
}
void testIfAStepFailInAssertionItShouldPassTheMessageToTheUser()
{
try {
WHEN << "a step fail some assertion" << OK;
THEN << "that line should not be executed" << OK;
} catch (...)
{
THEN << "that line should be executed" << OK;
}
}
//////////////////////////////////////////////////////////////////////////
//step definition
//////////////////////////////////////////////////////////////////////////
STEP1("i describe a string passing a", EnlightenTest, iDescribeAStringPassingA, EnlightenTest::DummyClass)
void iDescribeAStringPassingA( EnlightenTest::DummyClass p1)
{
_dummy= p1;
}
STEP1("the step called should have received the parameter", EnlightenTest
, theStepCalledShouldHaveReceivedTheParameter, EnlightenTest::DummyClass)
void theStepCalledShouldHaveReceivedTheParameter( EnlightenTest::DummyClass p1)
{
CPPUNIT_ASSERT(_dummy.attr == p1.attr);
}
STEP("the step should be called", EnlightenTest, theStepShouldBeCalled)
void theStepShouldBeCalled()
{
CPPUNIT_ASSERT(wasCalled);
}
STEP("any descriptive requisit", EnlightenTest, anyDescriptiveRequisit)
void anyDescriptiveRequisit()
{
wasCalled= true;
}
STEP1("a string mapped to a step was formed", EnlightenTest, aStringMappedToAStepWasFormed, std::string)
void aStringMappedToAStepWasFormed(std::string p1)
{
GIVEN << p1.c_str() << OK;
}
STEP("a string passed didnt have a method mapped", EnlightenTest, aStringPassedDidntHaveAMethodMapped)
void aStringPassedDidntHaveAMethodMapped()
{
WHEN << "a unknow string" << OK;
}
STEP("that line should be executed", EnlightenTest, thatLineShouldBeExecuted)
void thatLineShouldBeExecuted()
{
CPPUNIT_ASSERT(true);
}
STEP("that line should not be executed", EnlightenTest, thatLineShouldNotBeExecuted)
void thatLineShouldNotBeExecuted()
{
CPPUNIT_ASSERT(false);
}
STEP1("i have apple", EnlightenTest, iHaveApple, int)
void iHaveApple(int p1)
{
apples = p1;
}
STEP1("i buy apple(s)", EnlightenTest, iBuyApple, int)
void iBuyApple(int p1)
{
apples+= p1;
}
STEP1("i should have apples", EnlightenTest, iShouldHaveapples, int)
void iShouldHaveapples(int p1)
{
CPPUNIT_ASSERT_EQUAL((int)p1, apples);
}
STEP("i specify that a step will throw", EnlightenTest, iSpecifyThatAStepWillThrow)
void iSpecifyThatAStepWillThrow()
{
GIVEN << "it should throw" << OK;
}
STEP("i call a step that throws", EnlightenTest, iCallAStepThatThrows)
void iCallAStepThatThrows()
{
throw "throwing a exception";
}
STEP("it should not throw to the user", EnlightenTest, itShouldNotThrowToTheUser)
void itShouldNotThrowToTheUser()
{
}
STEP("a step fail some assertion", EnlightenTest, aStepFailSomeAssertion)
void aStepFailSomeAssertion()
{
CPPUNIT_ASSERT(false);
}
};
////////////////////////////////////////////////////////////////////////////////
CPPUNIT_TEST_SUITE_REGISTRATION(EnlightenTest);