forked from FabioBatSilva/ArduinoFake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_serial.h
86 lines (64 loc) · 3.03 KB
/
test_serial.h
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
#ifdef UNIT_TEST
namespace SerialTest
{
void test_extends_stream(void)
{
TEST_ASSERT_NOT_EQUAL(
ArduinoFakeInstance(Stream),
ArduinoFakeInstance(Serial)
);
char print_char_var = 'A';
char stream_char_var = 'B';
int print_int_var = 123;
int stream_int_var = 321;
When(OverloadedMethod(ArduinoFake(Stream), print, size_t(char))).AlwaysReturn();
When(OverloadedMethod(ArduinoFake(Stream), print, size_t(int, int))).AlwaysReturn();
When(OverloadedMethod(ArduinoFake(Serial), print, size_t(char))).AlwaysReturn();
When(OverloadedMethod(ArduinoFake(Serial), print, size_t(int, int))).AlwaysReturn();
Stream* stream = ArduinoFakeMock(Stream);
Serial_* serial = ArduinoFakeMock(Serial);
stream->print(stream_char_var);
stream->print(stream_int_var, DEC);
serial->print(print_char_var);
serial->print(print_int_var, DEC);
Verify(OverloadedMethod(ArduinoFake(Stream), print, size_t(char)).Using(stream_char_var)).Once();
Verify(OverloadedMethod(ArduinoFake(Stream), print, size_t(int, int)).Using(stream_int_var, DEC)).Once();
Verify(OverloadedMethod(ArduinoFake(Serial), print, size_t(char)).Using(print_char_var)).Once();
Verify(OverloadedMethod(ArduinoFake(Serial), print, size_t(int, int)).Using(print_int_var, DEC)).Once();
}
void test_global_serial(void)
{
When(Method(ArduinoFake(Serial), available)).Return(1);
When(OverloadedMethod(ArduinoFake(Serial), print, size_t(char))).Return(1);
TEST_ASSERT_EQUAL(1, Serial.available());
TEST_ASSERT_EQUAL(1, Serial.print('A'));
Verify(Method(ArduinoFake(Serial), available)).Once();
Verify(OverloadedMethod(ArduinoFake(Serial), print, size_t(char)).Using('A')).Once();
}
void test_basics(void)
{
When(Method(ArduinoFake(Serial), end)).AlwaysReturn();
When(Method(ArduinoFake(Serial), flush)).AlwaysReturn();
When(Method(ArduinoFake(Serial), available)).Return(0, 1);
When(OverloadedMethod(ArduinoFake(Serial), write, size_t(uint8_t))).Return(1);
When(OverloadedMethod(ArduinoFake(Serial), begin, void(unsigned long))).AlwaysReturn();
Serial.begin(9600);
TEST_ASSERT_EQUAL(0, Serial.available());
TEST_ASSERT_EQUAL(1, Serial.available());
TEST_ASSERT_EQUAL(1, Serial.write(5));
Serial.flush();
Serial.end();
Verify(OverloadedMethod(ArduinoFake(Serial), begin, void(unsigned long)).Using(9600)).Once();
Verify(Method(ArduinoFake(Serial), available)).Exactly(2_Times);
Verify(OverloadedMethod(ArduinoFake(Serial), write, size_t(uint8_t)).Using(5)).Once();
Verify(Method(ArduinoFake(Serial), flush)).Once();
Verify(Method(ArduinoFake(Serial), end)).Once();
}
void run_tests()
{
RUN_TEST(SerialTest::test_extends_stream);
RUN_TEST(SerialTest::test_global_serial);
RUN_TEST(SerialTest::test_basics);
}
}
#endif