Skip to content

Commit e1bb7ea

Browse files
committed
Fake Proxy
1 parent 874822e commit e1bb7ea

21 files changed

Lines changed: 1309 additions & 942 deletions

src/ArduinoFake.h

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
1-
#ifndef ARDUINO_FAKE_H
2-
#define ARDUINO_FAKE_H
1+
#pragma once
32

43
#if !defined(UBRRH) && !defined(UBRR0H) && !defined(USBCON)
54
#define USBCON
65
#endif
76

8-
#include <HardwareSerialFake.h>
7+
#include <cstring>
8+
#include <fakeit/fakeit.hpp>
9+
910
#include <FunctionFake.h>
1011
#include <StreamFake.h>
1112
#include <SerialFake.h>
13+
#include <ClientFake.h>
1214
#include <PrintFake.h>
1315

14-
#define ArduinoFakeGetHardwareSerial() ArduinoFakeGetter(HardwareSerial)
16+
#include <arduino/Arduino.h>
17+
1518
#define ArduinoFakeGetFunction() ArduinoFakeGetter(Function)
1619
#define ArduinoFakeGetSerial() ArduinoFakeGetter(Serial)
1720
#define ArduinoFakeGetStream() ArduinoFakeGetter(Stream)
21+
#define ArduinoFakeGetClient() ArduinoFakeGetter(Client)
1822
#define ArduinoFakeGetPrint() ArduinoFakeGetter(Print)
1923
#define ArduinoFakeGet() ArduinoFakeGetter(Function)
2024

@@ -29,6 +33,12 @@
2933
#define ArduinoFakeInstance(mock, ...) \
3034
getArduinoFakeContext()->mock(__VA_ARGS__)
3135

36+
#define ArduinoFakeInstanceFake(mock, ...) \
37+
getArduinoFakeContext()->mock(__VA_ARGS__)
38+
39+
#define ArduinoFakeMock(mock, ...) \
40+
new mock##FakeProxy(ArduinoFakeInstance(mock, __VA_ARGS__))
41+
3242
#define ArduinoFakeReturnInstaceOf(var, mock) \
3343
if (std::strstr(typeid(*var).name(), #mock)) { \
3444
return this->mock(); \
@@ -45,19 +55,19 @@
4555

4656
struct ArduinoFakeMocks
4757
{
48-
fakeit::Mock<HardwareSerialFake> HardwareSerial;
4958
fakeit::Mock<FunctionFake> Function;
5059
fakeit::Mock<SerialFake> Serial;
5160
fakeit::Mock<StreamFake> Stream;
61+
fakeit::Mock<ClientFake> Client;
5262
fakeit::Mock<PrintFake> Print;
5363
};
5464

5565
struct ArduinoFakeInstances
5666
{
57-
HardwareSerialFake* HardwareSerial;
5867
FunctionFake* Function;
5968
SerialFake* Serial;
6069
StreamFake* Stream;
70+
ClientFake* Client;
6171
PrintFake* Print;
6272
};
6373

@@ -70,12 +80,11 @@ class ArduinoFakeContext
7080
ArduinoFakeSingleInstanceGetter(Print)
7181
ArduinoFakeSingleInstanceGetter(Stream)
7282
ArduinoFakeSingleInstanceGetter(Serial)
83+
ArduinoFakeSingleInstanceGetter(Client)
7384
ArduinoFakeSingleInstanceGetter(Function)
74-
ArduinoFakeSingleInstanceGetter(HardwareSerial)
7585

7686
PrintFake* Print(class Print* p)
7787
{
78-
ArduinoFakeReturnInstaceOf(p, HardwareSerial)
7988
ArduinoFakeReturnInstaceOf(p, Serial)
8089
ArduinoFakeReturnInstaceOf(p, Stream)
8190

@@ -84,24 +93,26 @@ class ArduinoFakeContext
8493

8594
StreamFake* Stream(class Stream* s)
8695
{
87-
ArduinoFakeReturnInstaceOf(s, HardwareSerial)
8896
ArduinoFakeReturnInstaceOf(s, Serial)
8997

9098
return this->Stream();
9199
}
92100

101+
ClientFake* Client(class Client* c)
102+
{
103+
return this->Client();
104+
}
105+
93106
void reset(void)
94107
{
95108
this->Instances = new ArduinoFakeInstances();
96109

97-
this->Mocks->HardwareSerial.Reset();
98110
this->Mocks->Function.Reset();
99111
this->Mocks->Stream.Reset();
100112
this->Mocks->Serial.Reset();
113+
this->Mocks->Client.Reset();
101114
this->Mocks->Print.Reset();
102115
}
103116
};
104117

105118
ArduinoFakeContext* getArduinoFakeContext();
106-
107-
#endif // ARDUINO_FAKE_H

src/ClientFake.cpp

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#include "ArduinoFake.h"
2+
#include "ClientFake.h"
3+
4+
ClientFake* getClientFakeProxy(Client* client)
5+
{
6+
if (ClientFakeProxy* p = dynamic_cast<ClientFakeProxy*>(client)) {
7+
return p->clientFake;
8+
}
9+
10+
return ArduinoFakeInstance(Client, client);
11+
}
12+
13+
int Client::connect(IPAddress ip, uint16_t port)
14+
{
15+
return getClientFakeProxy(this)->connect(ip, port);
16+
}
17+
18+
int Client::connect(const char *host, uint16_t port)
19+
{
20+
return getClientFakeProxy(this)->connect(host, port);
21+
}
22+
23+
size_t Client::write(uint8_t value)
24+
{
25+
return getClientFakeProxy(this)->write(value);
26+
}
27+
28+
size_t Client::write(const uint8_t *buf, size_t size)
29+
{
30+
return getClientFakeProxy(this)->write(buf, size);
31+
}
32+
33+
int Client::available()
34+
{
35+
return getClientFakeProxy(this)->available();
36+
}
37+
38+
int Client::read()
39+
{
40+
return getClientFakeProxy(this)->read();
41+
}
42+
43+
int Client::read(uint8_t *buf, size_t size)
44+
{
45+
return getClientFakeProxy(this)->read(buf, size);
46+
}
47+
48+
int Client::peek()
49+
{
50+
return getClientFakeProxy(this)->peek();
51+
}
52+
53+
void Client::flush()
54+
{
55+
return getClientFakeProxy(this)->flush();
56+
}
57+
58+
void Client::stop()
59+
{
60+
return getClientFakeProxy(this)->stop();
61+
}
62+
63+
uint8_t Client::connected()
64+
{
65+
return getClientFakeProxy(this)->connected();
66+
}
67+
68+
Client::operator bool()
69+
{
70+
return 1 == 1;
71+
}
72+
73+
ClientFakeProxy::operator bool()
74+
{
75+
return 1 == 1;
76+
}

src/ClientFake.h

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
#pragma once
2+
3+
#include "ArduinoFake.h"
4+
#include "StreamFake.h"
5+
#include "arduino/Client.h"
6+
7+
struct ClientFake : public StreamFake
8+
{
9+
virtual int connect(IPAddress ip, uint16_t port) = 0;
10+
11+
virtual int connect(const char *host, uint16_t port) = 0;
12+
13+
virtual size_t write(uint8_t) = 0;
14+
15+
virtual size_t write(const uint8_t *buf, size_t size) = 0;
16+
17+
virtual int available() = 0;
18+
19+
virtual int read() = 0;
20+
21+
virtual int read(uint8_t *buf, size_t size) = 0;
22+
23+
virtual int peek() = 0;
24+
25+
virtual void flush() = 0;
26+
27+
virtual void stop() = 0;
28+
29+
virtual uint8_t connected() = 0;
30+
};
31+
32+
class ClientFakeProxy : public Client
33+
{
34+
public:
35+
ClientFake* clientFake;
36+
37+
ClientFakeProxy(ClientFake* clientFake)
38+
{
39+
clientFake = clientFake;
40+
}
41+
42+
int connect(IPAddress ip, uint16_t port)
43+
{
44+
return clientFake->connect(ip, port);
45+
}
46+
47+
int connect(const char *host, uint16_t port)
48+
{
49+
return clientFake->connect(host, port);
50+
}
51+
52+
size_t write(uint8_t value)
53+
{
54+
return clientFake->write(value);
55+
}
56+
57+
size_t write(const uint8_t *buf, size_t size)
58+
{
59+
return clientFake->write(buf, size);
60+
}
61+
62+
int available()
63+
{
64+
return clientFake->available();
65+
}
66+
67+
int read()
68+
{
69+
return clientFake->read();
70+
}
71+
72+
int read(uint8_t *buf, size_t size)
73+
{
74+
return clientFake->read(buf, size);
75+
}
76+
77+
int peek()
78+
{
79+
return clientFake->peek();
80+
}
81+
82+
void flush()
83+
{
84+
clientFake->flush();
85+
}
86+
87+
void stop()
88+
{
89+
clientFake->stop();
90+
}
91+
92+
uint8_t connected()
93+
{
94+
return clientFake->connected();
95+
}
96+
97+
virtual operator bool();
98+
};

src/FunctionFake.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
#define FUNCTION_FAKE_H
33

44
#include <fakeit/fakeit.hpp>
5-
#include <arduino/Arduino.h>
65

76
struct FunctionFake
87
{

src/HardwareSerialFake.cpp

Lines changed: 0 additions & 42 deletions
This file was deleted.

src/HardwareSerialFake.h

Lines changed: 0 additions & 20 deletions
This file was deleted.

0 commit comments

Comments
 (0)