Skip to content

Commit 1596592

Browse files
Sturla22stla
andauthored
Add unit tests for beeper (ikeyasu#25)
* clean up test setup * clang-format tests * fix include paths * add beeper tests Co-authored-by: stla <[email protected]>
1 parent 21f4251 commit 1596592

File tree

6 files changed

+176
-152
lines changed

6 files changed

+176
-152
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
build/
2-
Testing/
32
.dep/
43
*.o
54
*.hex

solenoids.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ This file is part of AYAB.
2929
#ifndef HARD_I2C
3030
#define HARD_I2C
3131
#endif
32-
#include "./libraries/Alt_MCP23008/Alt_MCP23008.h"
32+
#include <Alt_MCP23008.h>
3333
#include <Wire.h>
3434

3535
Alt_MCP23008 mcp_0;

test/CMakeLists.txt

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
cmake_minimum_required(VERSION 2.8.8)
1+
cmake_minimum_required(VERSION 3.1)
22
project(ayab_test)
33

44
set(CMAKE_CXX_STANDARD 11 CACHE STRING "Set the C++ standard to be used for compiling")
55

6-
message("building all tests")
7-
86
find_package(Threads REQUIRED)
97

108
add_subdirectory(arduino_mock)
@@ -21,6 +19,9 @@ add_executable(${PROJECT_NAME}
2119

2220
${PROJECT_SOURCE_DIR}/../encoders.cpp
2321
${PROJECT_SOURCE_DIR}/./test_encoders.cpp
22+
23+
${PROJECT_SOURCE_DIR}/../beeper.cpp
24+
${PROJECT_SOURCE_DIR}/./test_beeper.cpp
2425
)
2526

2627
target_compile_definitions(${PROJECT_NAME}
@@ -45,11 +46,5 @@ target_link_libraries(${PROJECT_NAME}
4546

4647
add_dependencies(${PROJECT_NAME} arduino_mock)
4748

48-
# add_custom_command(TARGET ${PROJECT_NAME}
49-
# POST_BUILD
50-
# COMMAND gcovr -r . -e test_* -e arduino_mock*
51-
# COMMAND gcovr -r . --branches -e test_* -e arduino_mock*
52-
# )
53-
5449
enable_testing()
5550
add_test(test_all ${PROJECT_NAME})

test/test_all.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44

55
#include "gtest/gtest.h"
66

7-
int main(int argc, char *argv[])
8-
{
9-
::testing::InitGoogleTest(&argc, argv);
10-
return RUN_ALL_TESTS();
7+
int main(int argc, char *argv[]) {
8+
::testing::InitGoogleTest(&argc, argv);
9+
return RUN_ALL_TESTS();
1110
}

test/test_beeper.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include "gtest/gtest.h"
2+
3+
#include "../beeper.h"
4+
5+
using ::testing::Return;
6+
7+
class BeeperTest : public ::testing::Test {
8+
protected:
9+
void SetUp() override {
10+
arduinoMock = arduinoMockInstance();
11+
b = Beeper();
12+
}
13+
14+
void TearDown() override {
15+
releaseArduinoMock();
16+
}
17+
18+
void checkBeepTime(byte length) {
19+
EXPECT_CALL(*arduinoMock, analogWrite(PIEZO_PIN, 0)).Times(length);
20+
EXPECT_CALL(*arduinoMock, analogWrite(PIEZO_PIN, 20)).Times(length);
21+
EXPECT_CALL(*arduinoMock, delay(BEEPDELAY)).Times(length * 2);
22+
EXPECT_CALL(*arduinoMock, analogWrite(PIEZO_PIN, 255)).Times(1);
23+
}
24+
25+
ArduinoMock *arduinoMock;
26+
Beeper b;
27+
};
28+
29+
TEST_F(BeeperTest, test_ready) {
30+
checkBeepTime(5);
31+
b.ready();
32+
}
33+
34+
TEST_F(BeeperTest, test_finishedLine) {
35+
checkBeepTime(3);
36+
b.finishedLine();
37+
}
38+
39+
TEST_F(BeeperTest, test_endWork) {
40+
checkBeepTime(10);
41+
b.endWork();
42+
}

0 commit comments

Comments
 (0)