forked from FabioBatSilva/ArduinoFake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_wire.h
109 lines (90 loc) · 3.59 KB
/
test_wire.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#ifdef UNIT_TEST
namespace WireTest {
void test_extends_stream(void) {
TEST_ASSERT_NOT_EQUAL(ArduinoFakeInstance(Stream), ArduinoFakeInstance(Wire));
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(Wire), print, size_t(char))).AlwaysReturn();
When(OverloadedMethod(ArduinoFake(Wire), print, size_t(int, int)))
.AlwaysReturn();
Stream *stream = ArduinoFakeMock(Stream);
TwoWire *wire = ArduinoFakeMock(Wire);
stream->print(stream_char_var);
stream->print(stream_int_var, DEC);
wire->print(print_char_var);
wire->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(Wire), print, size_t(char))
.Using(print_char_var))
.Once();
Verify(OverloadedMethod(ArduinoFake(Wire), print, size_t(int, int))
.Using(print_int_var, DEC))
.Once();
}
void test_global_wire(void) {
When(Method(ArduinoFake(Wire), available)).Return(1);
When(OverloadedMethod(ArduinoFake(Wire), print, size_t(char))).Return(1);
TEST_ASSERT_EQUAL(1, Wire.available());
TEST_ASSERT_EQUAL(1, Wire.print('A'));
Verify(Method(ArduinoFake(Wire), available)).Once();
Verify(OverloadedMethod(ArduinoFake(Wire), print, size_t(char)).Using('A'))
.Once();
}
void test_basics(void) {
uint8_t device_addr = 0xab;
uint8_t register_addr = 0xcd;
int num_bytes_to_read = 1;
bool send_stop = false;
When(OverloadedMethod(ArduinoFake(Wire), begin, void(void))).AlwaysReturn();
When(OverloadedMethod(ArduinoFake(Wire), beginTransmission, void(uint8_t)))
.AlwaysReturn();
When(OverloadedMethod(ArduinoFake(Wire), write, size_t(uint8_t)))
.Return(true);
When(OverloadedMethod(ArduinoFake(Wire), endTransmission, uint8_t(bool)))
.Return(0);
When(OverloadedMethod(ArduinoFake(Wire), requestFrom, uint8_t(uint8_t, uint8_t)))
.Return(0);
When(OverloadedMethod(ArduinoFake(Wire), available, int(void))).Return(1);
When(OverloadedMethod(ArduinoFake(Wire), read, int(void))).Return(1);
Wire.begin();
Wire.beginTransmission(device_addr);
Wire.write(register_addr);
Wire.endTransmission(send_stop);
Wire.requestFrom(device_addr, num_bytes_to_read);
if (Wire.available()) {
Wire.read();
}
Verify(OverloadedMethod(ArduinoFake(Wire), begin, void(void))).Exactly(1);
Verify(OverloadedMethod(ArduinoFake(Wire), beginTransmission, void(uint8_t))
.Using(device_addr))
.Exactly(1);
Verify(OverloadedMethod(ArduinoFake(Wire), write, size_t(uint8_t))
.Using(register_addr))
.Exactly(1);
Verify(OverloadedMethod(ArduinoFake(Wire), endTransmission, uint8_t(bool))
.Using(send_stop))
.Exactly(1);
Verify(OverloadedMethod(ArduinoFake(Wire), requestFrom, uint8_t(uint8_t, uint8_t))
.Using(device_addr, num_bytes_to_read))
.Exactly(1);
Verify(OverloadedMethod(ArduinoFake(Wire), available, int(void))).Exactly(1);
Verify(OverloadedMethod(ArduinoFake(Wire), read, int(void))).Exactly(1);
}
void run_tests() {
RUN_TEST(WireTest::test_extends_stream);
RUN_TEST(WireTest::test_global_wire);
RUN_TEST(WireTest::test_basics);
}
} // namespace WireTest
#endif