forked from Arduino-CI/arduino_ci
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup_and_teardown.cpp
80 lines (64 loc) · 1.46 KB
/
setup_and_teardown.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
#include <ArduinoUnitTests.h>
#include <Arduino.h>
class LcdInterface {
public:
virtual void print(const char *) = 0;
};
class MockLcd : public LcdInterface {
public:
String s;
void print(const char* c)
{
s = String(c);
}
};
class Calculator {
private:
LcdInterface *m_lcd;
public:
Calculator(LcdInterface* lcd) {
m_lcd = lcd;
}
~Calculator() {
m_lcd = 0;
}
int add(int a, int b)
{
int result = a + b;
char buf[40];
sprintf(buf, "%d + %d = %d", a, b, result);
m_lcd->print(buf);
return result;
}
};
// This is a typical test where using setup (and teardown) would be useful
// to set up the "external" lcd dependency that the calculator uses indirectly
// but it is not something that is related to the functionality that is tested.
MockLcd* lcd_p;
Calculator* c;
unittest_setup()
{
lcd_p = new MockLcd();
c = new Calculator(lcd_p);
}
unittest_teardown()
{
delete c;
delete lcd_p;
}
// When you want to test that the calculator actually prints the calculations,
// then that should be done in the arrange part of the actual test (by setting
// up an mock lcd class that keeps a list of all messages printed).
unittest(add)
{
int result = c->add(11, 22);
assertEqual(33, result);
assertEqual("11 + 22 = 33", lcd_p->s);
}
unittest(add_again)
{
int result = c->add(33, 44);
assertEqual(77, result);
assertEqual("33 + 44 = 77", lcd_p->s);
}
unittest_main()