Skip to content

Commit 707c94d

Browse files
committed
Fix setup and teardown example
1 parent b9d71b7 commit 707c94d

File tree

2 files changed

+38
-21
lines changed

2 files changed

+38
-21
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1111
- `arduino_library_location.rb` script to print Arduino library location to stdout
1212
- `arduino_ci_remote.rb` now supports `--skip-unittests` and `--skip-compilation`. If you skip both, only the `autolocate!` of the Arduino binary will be performed.
1313
- `keepachangelog_manager` gem to begin streamlining the release process
14+
- `unittest_setup()` and `unittest_teardown()` macros, my thanks to @hlovdal for contributing this code
1415
- Added rspec sensitivity to the environment variable `$ARDUINO_CI_SKIP_SPLASH_SCREEN_RSPEC_TESTS` (for `arduino_ci` gem hackers)
1516

1617
### Changed

SampleProjects/TestSomething/test/setup_and_treardown.cpp renamed to SampleProjects/TestSomething/test/setup_and_teardown.cpp

+37-21
Original file line numberDiff line numberDiff line change
@@ -8,50 +8,66 @@ class LcdInterface {
88

99
class MockLcd : public LcdInterface {
1010
public:
11-
void print(const char *) {}
11+
String s;
12+
void print(const char* c)
13+
{
14+
s = String(c);
15+
}
1216
};
1317

14-
LcdInterface *Lcd_p;
15-
1618
class Calculator {
19+
private:
20+
LcdInterface *m_lcd;
21+
1722
public:
18-
int add(int a, int b) {
19-
int result = a + b;
20-
char buf[40];
21-
sprintf(buf, "%d + %d = %d", a, b, result);
22-
Lcd_p->print(buf);
23-
return result;
23+
Calculator(LcdInterface* lcd) {
24+
m_lcd = lcd;
25+
}
26+
27+
~Calculator() {
28+
m_lcd = 0;
29+
}
30+
31+
int add(int a, int b)
32+
{
33+
int result = a + b;
34+
char buf[40];
35+
sprintf(buf, "%d + %d = %d", a, b, result);
36+
m_lcd->print(buf);
37+
return result;
2438
}
2539
};
2640

41+
42+
// This is a typical test where using setup (and teardown) would be useful
43+
// to set up the "external" lcd dependency that the calculator uses indirectly
44+
// but it is not something that is related to the functionality that is tested.
45+
46+
MockLcd* lcd_p;
47+
Calculator* c;
48+
2749
unittest_setup()
2850
{
29-
Lcd_p = new MockLcd();
51+
lcd_p = new MockLcd();
52+
c = new Calculator(lcd_p);
3053
}
3154

3255
unittest_teardown()
3356
{
34-
delete Lcd_p;
57+
delete c;
58+
delete lcd_p;
3559
}
3660

37-
// This is a typical test where using setup (and teardown) would be useful
38-
// to set up the "external" lcd dependency that the calculator uses indirectly
39-
// but it is not something that is related to the functionality that is tested.
4061

4162
// When you want to test that the calculator actually prints the calculations,
4263
// then that should be done in the arrange part of the actual test (by setting
4364
// up an mock lcd class that keeps a list of all messages printed).
4465

4566
unittest(add)
4667
{
47-
// Arrange
48-
Calculator c;
49-
50-
// Act
51-
int result = c.add(11, 22);
52-
53-
// Assert
68+
int result = c->add(11, 22);
5469
assertEqual(33, result);
70+
assertEqual("11 + 22 = 33", lcd_p->s);
5571
}
5672

5773
unittest_main()

0 commit comments

Comments
 (0)