forked from FabioBatSilva/ArduinoFake
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_client.cpp
More file actions
160 lines (120 loc) · 5.08 KB
/
test_client.cpp
File metadata and controls
160 lines (120 loc) · 5.08 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include <Arduino.h>
#include "SimpleArduinoFake.h"
#include <unity.h>
#include "unity_filename_helper.h"
static const char * localhost = "localhost";
using namespace fakeit;
class MyService
{
public:
MyService(Client* client)
{
_client = client;
}
size_t send(uint16_t value)
{
return _client->write(value);
}
private:
Client* _client;
};
static void test_basics(void)
{
auto &clientFake = SimpleArduinoFake::getContext()._Client;
When(Method(clientFake, stop)).Return();
When(Method(clientFake, peek)).Return(2);
When(Method(clientFake, flush)).Return();
When(Method(clientFake, connected)).Return(0, 1);
When(OverloadedMethod(clientFake, connect, int(const char*, uint16_t))).Return(1);
Client * client(clientFake.getFake());
TEST_ASSERT_EQUAL(0, client->connected());
TEST_ASSERT_EQUAL(1, client->connect(localhost, 8080));
TEST_ASSERT_EQUAL(1, client->connected());
TEST_ASSERT_EQUAL(2, client->peek());
client->flush();
client->stop();
Verify(Method(clientFake, stop)).Once();
Verify(Method(clientFake, peek)).Once();
Verify(Method(clientFake, flush)).Once();
Verify(Method(clientFake, connected)).Exactly(2_Times);
Verify(OverloadedMethod(clientFake, connect, int(const char*, uint16_t)).Using(localhost, 8080)).Once();
}
static void test_connect(void)
{
auto &clientFake = SimpleArduinoFake::getContext()._Client;
When(OverloadedMethod(clientFake, connect, int(const char*, uint16_t))).Return(1, 0);
When(OverloadedMethod(clientFake, connect, int(IPAddress, uint16_t))).Return(0, 1);
IPAddress ipAddress1(62, 145, 182, 225);
IPAddress ipAddress2(221, 155, 131, 19);
Client * client(clientFake.getFake());
TEST_ASSERT_EQUAL(1, client->connect(localhost, 8080));
TEST_ASSERT_EQUAL(0, client->connect(localhost, 80));
TEST_ASSERT_EQUAL(0, client->connect(ipAddress1, 8080));
TEST_ASSERT_EQUAL(1, client->connect(ipAddress2, 8080));
Verify(OverloadedMethod(clientFake, connect, int(const char*, uint16_t)).Using(localhost, 8080)).Once();
Verify(OverloadedMethod(clientFake, connect, int(const char*, uint16_t)).Using(localhost, 80)).Once();
Verify(OverloadedMethod(clientFake, connect, int(IPAddress, uint16_t)).Using(ipAddress1, 8080)).Once();
Verify(OverloadedMethod(clientFake, connect, int(IPAddress, uint16_t)).Using(ipAddress2, 8080)).Once();
}
static void test_write(void)
{
uint8_t val1 = 0x0;
uint8_t val2 = 0x1;
const uint8_t* ptr1 = &val1;
const uint8_t* ptr2 = &val2;
auto &clientFake = SimpleArduinoFake::getContext()._Client;
When(OverloadedMethod(clientFake, write, size_t(uint8_t))).Return(1, 0);
When(OverloadedMethod(clientFake, write, size_t(const uint8_t*, size_t))).Return(0, 1);
Client * client(clientFake.getFake());
TEST_ASSERT_EQUAL(1, client->write(val1));
TEST_ASSERT_EQUAL(0, client->write(val2));
TEST_ASSERT_EQUAL(0, client->write(ptr1, 2));
TEST_ASSERT_EQUAL(1, client->write(ptr2, 3));
Verify(OverloadedMethod(clientFake, write, size_t(uint8_t)).Using(val1)).Once();
Verify(OverloadedMethod(clientFake, write, size_t(uint8_t)).Using(val2)).Once();
Verify(OverloadedMethod(clientFake, write, size_t(const uint8_t*, size_t)).Using(ptr1, 2)).Once();
Verify(OverloadedMethod(clientFake, write, size_t(const uint8_t*, size_t)).Using(ptr2, 3)).Once();
}
static void test_read(void)
{
uint8_t val1 = 0x0;
uint8_t val2 = 0x1;
uint8_t* ptr1 = &val1;
uint8_t* ptr2 = &val2;
auto &clientFake = SimpleArduinoFake::getContext()._Client;
When(OverloadedMethod(clientFake, read, int())).Return(10, 20);
When(OverloadedMethod(clientFake, read, int(uint8_t*, size_t))).Return(30, 400);
Client * client(clientFake.getFake());
TEST_ASSERT_EQUAL(10, client->read());
TEST_ASSERT_EQUAL(20, client->read());
TEST_ASSERT_EQUAL(30, client->read(ptr1, 2));
TEST_ASSERT_EQUAL(400, client->read(ptr2, 3));
Verify(OverloadedMethod(clientFake, read, int())).Exactly(2_Times);
Verify(OverloadedMethod(clientFake, read, int(uint8_t*, size_t)).Using(ptr1, 2)).Once();
Verify(OverloadedMethod(clientFake, read, int(uint8_t*, size_t)).Using(ptr2, 3)).Once();
}
static void test_inject_instance(void)
{
uint8_t val1 = 0x0;
uint8_t val2 = 0x1;
auto &clientFake = SimpleArduinoFake::getContext()._Client;
When(OverloadedMethod(clientFake, write, size_t(uint8_t))).Return(11, 22);
Client * client(clientFake.getFake());
MyService service(client);
TEST_ASSERT_EQUAL(11, service.send(val1));
TEST_ASSERT_EQUAL(22, service.send(val2));
Verify(OverloadedMethod(clientFake, write, size_t(uint8_t)).Using(val1)).Once();
Verify(OverloadedMethod(clientFake, write, size_t(uint8_t)).Using(val2)).Once();
}
namespace ClientTest
{
void run_tests(void)
{
unity_filename_helper_t _ufname_helper(__FILE__);
RUN_TEST(test_basics);
RUN_TEST(test_connect);
RUN_TEST(test_write);
RUN_TEST(test_read);
RUN_TEST(test_inject_instance);
}
}