Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/mock-injection/test/test_my_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ void test_connect(void)
When(OverloadedMethod(ArduinoFake(Client), println, size_t(const char *))).AlwaysReturn();
When(OverloadedMethod(ArduinoFake(Client), connect, int(const char*, uint16_t))).Return(1);

std::shared_ptr<Client> clientMock(ArduinoFakeMock(Client));
Client * clientMock(ArduinoFakeInstance0(Client));

MyService service(clientMock);

Expand Down
2 changes: 2 additions & 0 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@ build_flags =
-std=c++14
; Recommended for fakeit
-O0
-I src/arduino
-D USBCON
debug_build_flags = -O0 -g
65 changes: 35 additions & 30 deletions src/ArduinoFake.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,29 @@
#include <cstring>
#include <cstdint>
#include <stdexcept>
#if defined(abs)
#undef abs
#endif
#include <single_header/standalone/fakeit.hpp>

#include "arduino/Arduino.h"

#include "Function.h"
#include "Stream.h"
#include "Serial.h"
#include "Wire.h"
#include "Client.h"
#include "Print.h"
#include "SPI.h"
#include "EEPROM.h"
#include <Arduino.h>
#include <Stream.h>
#include <USBAPI.h>
#include <Wire.h>
#include <Client.h>
#include <Print.h>
#include <SPI.h>
#include <EEPROM.h>

#include "FunctionFake.h"

using PrintFake = Print;
using ClientFake = Client;
using StreamFake = Stream;
using SerialFake = Serial_;
using WireFake = TwoWire;
using SPIFake = SPIClass;
using EEPROMFake = EEPROMClass;

#define CONCAT2(x,y) x##y
#define CONCAT(x,y) CONCAT2(x,y)
Expand All @@ -45,9 +56,6 @@
#define ArduinoFakeInstance(clazz) \
getArduinoFakeContext()->getFake(clazz)

#define ArduinoFakeMock(mock) \
std::shared_ptr<mock##FakeProxy>(new mock##FakeProxy(ArduinoFakeInstance0(mock)))

#define _ArduinoFakeGetMock(mock) \
getArduinoFakeContext()->_##mock

Expand All @@ -73,17 +81,14 @@ struct IFake
virtual void* toFake(void) = 0;
};

template <class FakeT, class ProxyT, typename BaseT = fakeit::Mock<FakeT>>
struct ProxiedArduinoFake_t : public BaseT, IFake
{
template <class FakeT, typename BaseT = fakeit::Mock<FakeT>>
struct ArduinoFake_t : public BaseT, IFake
{
// Proxy to fake
template <class ArduinoT>
FakeT* getFake(ArduinoT *instance)
{
if (dynamic_cast<ProxyT*>(instance)) {
return dynamic_cast<ProxyT*>(instance)->getFake();
}
throw std::runtime_error("Unknown instance");
return static_cast<FakeT*>(toFake());
}

// Get the 'real' fake - the one that is actually mocked.
Expand Down Expand Up @@ -123,12 +128,12 @@ class FakeOverride_t
std::unordered_map<void*, IFake*> _mapping;
};

template <class FakeT, class ProxyT, typename BaseT = ProxiedArduinoFake_t<FakeT, ProxyT>>
struct OverrideableProxiedArduinoFake_t : public BaseT
template <class FakeT, typename BaseT = ArduinoFake_t<FakeT>>
struct OverrideableArduinoFake_t : public BaseT
{
FakeOverride_t &_overrides;

OverrideableProxiedArduinoFake_t(FakeOverride_t &overrides)
OverrideableArduinoFake_t(FakeOverride_t &overrides)
: BaseT()
, _overrides(overrides)
{
Expand All @@ -150,13 +155,13 @@ class ArduinoFakeContext
public:
FakeOverride_t _fakeOverrides;
fakeit::Mock<FunctionFake> _Function;
OverrideableProxiedArduinoFake_t<SerialFake, SerialFakeProxy> _Serial;
OverrideableProxiedArduinoFake_t<WireFake, WireFakeProxy> _Wire;
OverrideableProxiedArduinoFake_t<StreamFake, StreamFakeProxy> _Stream;
OverrideableProxiedArduinoFake_t<ClientFake, ClientFakeProxy> _Client;
OverrideableProxiedArduinoFake_t<PrintFake, PrintFakeProxy> _Print;
OverrideableProxiedArduinoFake_t<SPIFake, SPIFakeProxy> _SPI;
OverrideableProxiedArduinoFake_t<EEPROMFake, EEPROMFakeProxy> _EEPROM;
OverrideableArduinoFake_t<SerialFake> _Serial;
OverrideableArduinoFake_t<WireFake> _Wire;
OverrideableArduinoFake_t<StreamFake> _Stream;
OverrideableArduinoFake_t<ClientFake> _Client;
OverrideableArduinoFake_t<PrintFake> _Print;
OverrideableArduinoFake_t<SPIFake> _SPI;
OverrideableArduinoFake_t<EEPROMFake> _EEPROM;

#define _ArduinoFakeInstanceGetter1(mock) \
mock##Fake* mock() \
Expand Down
1 change: 0 additions & 1 deletion src/Client.h

This file was deleted.

10 changes: 2 additions & 8 deletions src/ClientFake.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "ArduinoFake.h"
#include "ClientFake.h"
#include <stdexcept>

int Client::connect(IPAddress ip, uint16_t port)
Expand Down Expand Up @@ -59,10 +58,5 @@ uint8_t Client::connected()

Client::operator bool()
{
return 1 == 1;
}

ClientFakeProxy::operator bool()
{
return 1 == 1;
}
return ArduinoFakeInstance(this)->operator bool();
}
103 changes: 0 additions & 103 deletions src/ClientFake.h

This file was deleted.

1 change: 0 additions & 1 deletion src/EEPROM.h

This file was deleted.

3 changes: 1 addition & 2 deletions src/EEPROMFake.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// clang-format off
#include "ArduinoFake.h"
#include "EEPROMFake.h"
// clang-format on

uint8_t EEPROMClass::read(int idx) {
Expand All @@ -14,4 +13,4 @@ void EEPROMClass::update(int idx, uint8_t val) {
};
uint16_t EEPROMClass::length() { return ArduinoFakeInstance(this)->length(); }

EEPROMClass EEPROM = EEPROMFakeProxy(ArduinoFakeInstance0(EEPROM));
EEPROMClass EEPROM;
20 changes: 0 additions & 20 deletions src/EEPROMFake.h

This file was deleted.

1 change: 0 additions & 1 deletion src/Function.h

This file was deleted.

1 change: 0 additions & 1 deletion src/Print.h

This file was deleted.

1 change: 0 additions & 1 deletion src/PrintFake.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "ArduinoFake.h"
#include "PrintFake.h"

int Print::availableForWrite(void)
{
Expand Down
63 changes: 0 additions & 63 deletions src/PrintFake.h

This file was deleted.

1 change: 0 additions & 1 deletion src/SPI.h

This file was deleted.

3 changes: 1 addition & 2 deletions src/SPIFake.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "ArduinoFake.h"
#include "SPIFake.h"

void SPIClass::begin() { ArduinoFakeInstance(this)->begin(); };

Expand All @@ -25,4 +24,4 @@ void SPIClass::transfer(void *buf, size_t count) {
return ArduinoFakeInstance(this)->transfer(buf, count);
};

SPIClass SPI = SPIFakeProxy(ArduinoFakeInstance0(SPI));
SPIClass SPI;
Loading
Loading