Skip to content

Commit 16f286a

Browse files
committed
Add support for the Arduino EEPROM class.
Note: For now, this only supports the simple methods. `get/put` aren't implemented because virtual template functions are not a thing.
1 parent 8b400de commit 16f286a

File tree

7 files changed

+107
-0
lines changed

7 files changed

+107
-0
lines changed

src/ArduinoFake.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#pragma once
2+
// clang-format off
23

34
#if !defined(UBRRH) && !defined(UBRR0H) && !defined(USBCON)
45
#define USBCON
@@ -19,6 +20,7 @@
1920
#include "Client.h"
2021
#include "Print.h"
2122
#include "SPI.h"
23+
#include "EEPROM.h"
2224

2325
#define ArduinoFake(mock) _ArduinoFakeGet##mock()
2426

@@ -38,6 +40,7 @@
3840
#define _ArduinoFakeGetSerial() _ArduinoFakeGetMock(Serial)
3941
#define _ArduinoFakeGetWire() _ArduinoFakeGetMock(Wire)
4042
#define _ArduinoFakeGetSPI() _ArduinoFakeGetMock(SPI)
43+
#define _ArduinoFakeGetEEPROM() _ArduinoFakeGetMock(EEPROM)
4144
#define _ArduinoFakeGetStream() _ArduinoFakeGetMock(Stream)
4245
#define _ArduinoFakeGetClient() _ArduinoFakeGetMock(Client)
4346
#define _ArduinoFakeGetPrint() _ArduinoFakeGetMock(Print)
@@ -73,6 +76,7 @@ struct ArduinoFakeMocks
7376
fakeit::Mock<ClientFake> Client;
7477
fakeit::Mock<PrintFake> Print;
7578
fakeit::Mock<SPIFake> SPI;
79+
fakeit::Mock<EEPROMFake> EEPROM;
7680
};
7781

7882
struct ArduinoFakeInstances
@@ -84,6 +88,7 @@ struct ArduinoFakeInstances
8488
ClientFake* Client;
8589
PrintFake* Print;
8690
SPIFake* SPI;
91+
EEPROMFake* EEPROM;
8792
};
8893

8994
class ArduinoFakeContext
@@ -100,13 +105,15 @@ class ArduinoFakeContext
100105
_ArduinoFakeInstanceGetter1(Client)
101106
_ArduinoFakeInstanceGetter1(Function)
102107
_ArduinoFakeInstanceGetter1(SPI)
108+
_ArduinoFakeInstanceGetter1(EEPROM)
103109

104110
_ArduinoFakeInstanceGetter2(Print, Print)
105111
_ArduinoFakeInstanceGetter2(Client, Client)
106112
_ArduinoFakeInstanceGetter2(Stream, Stream)
107113
_ArduinoFakeInstanceGetter2(Serial, Serial_)
108114
_ArduinoFakeInstanceGetter2(Wire, TwoWire)
109115
_ArduinoFakeInstanceGetter2(SPI, SPIClass)
116+
_ArduinoFakeInstanceGetter2(EEPROM, EEPROMClass)
110117

111118
ArduinoFakeContext()
112119
{
@@ -124,11 +131,15 @@ class ArduinoFakeContext
124131
this->Mocks->Client.Reset();
125132
this->Mocks->Print.Reset();
126133
this->Mocks->SPI.Reset();
134+
this->Mocks->EEPROM.Reset();
127135

128136
Mapping[&::Serial] = this->Serial();
129137
Mapping[&::Wire] = this->Wire();
130138
Mapping[&::SPI] = this->SPI();
139+
Mapping[&::EEPROM] = this->EEPROM();
131140
}
132141
};
133142

134143
ArduinoFakeContext* getArduinoFakeContext();
144+
145+
// clang-format on

src/EEPROM.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#include "EEPROMFake.h"

src/EEPROMFake.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// clang-format off
2+
#include "ArduinoFake.h"
3+
#include "EEPROMFake.h"
4+
// clang-format on
5+
6+
uint8_t EEPROMClass::read(int idx) {
7+
return ArduinoFakeInstance(EEPROM)->read(idx);
8+
};
9+
void EEPROMClass::write(int idx, uint8_t val) {
10+
ArduinoFakeInstance(EEPROM)->write(idx, val);
11+
};
12+
void EEPROMClass::update(int idx, uint8_t val) {
13+
ArduinoFakeInstance(EEPROM)->update(idx, val);
14+
};
15+
uint16_t EEPROMClass::length() { return ArduinoFakeInstance(EEPROM)->length(); }
16+
17+
EEPROMClass EEPROM = EEPROMFakeProxy(ArduinoFakeInstance(EEPROM));

src/EEPROMFake.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#pragma once
2+
3+
#include "ArduinoFake.h"
4+
#include "arduino/EEPROM.h"
5+
6+
struct EEPROMFake {
7+
virtual uint8_t read(int idx) = 0;
8+
virtual void write(int idx, uint8_t val) = 0;
9+
virtual void update(int idx, uint8_t val) = 0;
10+
virtual uint16_t length() = 0;
11+
};
12+
13+
class EEPROMFakeProxy : public EEPROMClass {
14+
private:
15+
EEPROMFake *eepromFake;
16+
17+
public:
18+
EEPROMFakeProxy(EEPROMFake *fake) { eepromFake = fake; }
19+
20+
EEPROMFake *getEEPROMFake() { return eepromFake; }
21+
};

src/arduino/EEPROM.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#pragma once
2+
#include <inttypes.h>
3+
4+
struct EEPROMClass {
5+
virtual uint8_t read(int idx);
6+
virtual void write(int idx, uint8_t val);
7+
virtual void update(int idx, uint8_t val);
8+
virtual uint16_t length();
9+
/*
10+
EERef operator[](const int idx);
11+
12+
// TODO: How to implement this functionality??
13+
// https://docs.arduino.cc/learn/built-in-libraries/eeprom
14+
template <typename T> T &get(int idx, T &t);
15+
16+
template <typename T> const T &put(int idx, const T &t);
17+
*/
18+
};
19+
20+
extern EEPROMClass EEPROM;

test/main.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// clang-format off
12
#include <Arduino.h>
23
#include <cstdlib>
34
#include <unity.h>
@@ -11,6 +12,7 @@ using namespace fakeit;
1112
#include "test_serial.h"
1213
#include "test_wire.h"
1314
#include "test_spi.h"
15+
#include "test_eeprom.h"
1416
#include "test_client.h"
1517
#include "test_arduino_string.h"
1618
#include "test_include.h"
@@ -40,6 +42,7 @@ int main(int argc, char **argv)
4042
RUN_TEST_GROUP(SerialTest);
4143
RUN_TEST_GROUP(WireTest);
4244
RUN_TEST_GROUP(SpiTest);
45+
RUN_TEST_GROUP(EEPROMTest);
4346
RUN_TEST_GROUP(ClientTest);
4447
RUN_TEST_GROUP(IncludeTest);
4548

@@ -49,3 +52,4 @@ int main(int argc, char **argv)
4952
}
5053

5154
#endif
55+
// clang-format on

test/test_eeprom.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#ifdef UNIT_TEST
2+
3+
namespace EEPROMTest {
4+
5+
#include "arduino/EEPROM.h"
6+
7+
void test_basics(void) {
8+
When(OverloadedMethod(ArduinoFake(EEPROM), read, uint8_t(int)))
9+
.AlwaysReturn();
10+
When(OverloadedMethod(ArduinoFake(EEPROM), write, void(int, uint8_t)))
11+
.AlwaysReturn();
12+
When(OverloadedMethod(ArduinoFake(EEPROM), update, void(int, uint8_t)))
13+
.AlwaysReturn();
14+
When(OverloadedMethod(ArduinoFake(EEPROM), length, uint16_t(void)))
15+
.AlwaysReturn();
16+
17+
EEPROM.read(1);
18+
EEPROM.write(1, 1);
19+
EEPROM.update(1, 2);
20+
EEPROM.length();
21+
22+
Verify(OverloadedMethod(ArduinoFake(EEPROM), read, uint8_t(int))).Once();
23+
Verify(OverloadedMethod(ArduinoFake(EEPROM), write, void(int, uint8_t)))
24+
.Once();
25+
Verify(OverloadedMethod(ArduinoFake(EEPROM), update, void(int, uint8_t)))
26+
.Once();
27+
Verify(OverloadedMethod(ArduinoFake(EEPROM), length, uint16_t(void))).Once();
28+
}
29+
30+
void run_tests() { RUN_TEST(EEPROMTest::test_basics); }
31+
} // namespace EEPROMTest
32+
33+
#endif

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy