Skip to content
Open
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
37 changes: 28 additions & 9 deletions .github/workflows/check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,38 @@ name: Check
on: [push, pull_request]

jobs:
build:
unit-tests:
runs-on: ${{ matrix.os }}

strategy:
fail-fast: false
matrix:
os: [ubuntu-22.04]
os:
- ubuntu-22.04
- windows-latest
pio_env:
- native
- Win32
exclude:
- os: ubuntu-22.04
pio_env: Win32
- os: windows-latest
pio_env: native

steps:
-
name: Checkout
uses: actions/checkout@v3
-
name: Tests
run: make
- uses: actions/checkout@v4

- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.11'

- name: Install PlatformIO
run: |
python -m pip install --upgrade pip
pip install --upgrade platformio

- name: Run unit tests
run: platformio test -v -e ${{ matrix.pio_env }}

examples:
runs-on: ${{ matrix.os }}
Expand Down
5 changes: 5 additions & 0 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,8 @@
platform = native
build_flags = -std=gnu++17
test_build_src = yes

[env:Win32]
platform = windows_x86
test_build_src = yes
build_flags = -std=c++11 -Wno-attributes
5 changes: 5 additions & 0 deletions test/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ void setUp(void)
ArduinoFakeReset();
}

void tearDown(void)
{
// Required by Unity v2.6
}

int main(int argc, char **argv)
{
UNITY_BEGIN();
Expand Down
40 changes: 40 additions & 0 deletions test/test_arduino_string.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include <ArduinoFake.h>
#include <unity.h>
#include "unity_filename_helper.h"

using namespace fakeit;

namespace ArduinoStringTest
{
void test_constructors(void)
{
String string01 = "Hello String";
String string02 = String('a');
String string03 = String("This is a string");
String string04 = String(string03 + " with more");
String string05 = String(13);
String string06 = String(1000, DEC);
String string07 = String(45, HEX);
String string08 = String(255, BIN);
String string09 = String(20000L, DEC);
String string10 = String(5.698, 3);

TEST_ASSERT_EQUAL_STRING("Hello String", string01.c_str());
TEST_ASSERT_EQUAL_STRING("a", string02.c_str());
TEST_ASSERT_EQUAL_STRING("This is a string", string03.c_str());
TEST_ASSERT_EQUAL_STRING("This is a string with more", string04.c_str());

TEST_ASSERT_EQUAL_STRING("13", string05.c_str());
TEST_ASSERT_EQUAL_STRING("1000", string06.c_str());
TEST_ASSERT_EQUAL_STRING("2d", string07.c_str());
TEST_ASSERT_EQUAL_STRING("11111111", string08.c_str());
TEST_ASSERT_EQUAL_STRING("20000", string09.c_str());
TEST_ASSERT_EQUAL_STRING("5.698", string10.c_str());
}

void run_tests()
{
unity_filename_helper_t _ufname_helper(__FILE__);
RUN_TEST(ArduinoStringTest::test_constructors);
}
}
32 changes: 1 addition & 31 deletions test/test_arduino_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,6 @@

namespace ArduinoStringTest
{
void test_constructors(void)
{
String string01 = "Hello String";
String string02 = String('a');
String string03 = String("This is a string");
String string04 = String(string03 + " with more");
String string05 = String(13);
String string06 = String(1000, DEC);
String string07 = String(45, HEX);
String string08 = String(255, BIN);
String string09 = String(20000L, DEC);
String string10 = String(5.698, 3);

TEST_ASSERT_EQUAL_STRING("Hello String", string01.c_str());
TEST_ASSERT_EQUAL_STRING("a", string02.c_str());
TEST_ASSERT_EQUAL_STRING("This is a string", string03.c_str());
TEST_ASSERT_EQUAL_STRING("This is a string with more", string04.c_str());

TEST_ASSERT_EQUAL_STRING("13", string05.c_str());
TEST_ASSERT_EQUAL_STRING("1000", string06.c_str());
TEST_ASSERT_EQUAL_STRING("2d", string07.c_str());
TEST_ASSERT_EQUAL_STRING("11111111", string08.c_str());
TEST_ASSERT_EQUAL_STRING("20000", string09.c_str());
TEST_ASSERT_EQUAL_STRING("5.698", string10.c_str());
}

void run_tests()
{
RUN_TEST(ArduinoStringTest::test_constructors);
}
}
void run_tests();}

#endif
155 changes: 155 additions & 0 deletions test/test_client.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
#include <ArduinoFake.h>
#include <unity.h>
#include "unity_filename_helper.h"

using namespace fakeit;

namespace ClientTest
{
const char * localhost = "localhost";

class MyService
{
public:
MyService(Client* client)
{
_client = client;
}

size_t send(uint16_t value)
{
return _client->write(value);
}

private:
Client* _client;
};

void test_basics(void)
{
When(Method(ArduinoFake(Client), stop)).Return();
When(Method(ArduinoFake(Client), peek)).Return(2);
When(Method(ArduinoFake(Client), flush)).Return();
When(Method(ArduinoFake(Client), connected)).Return(0, 1);
When(OverloadedMethod(ArduinoFake(Client), connect, int(const char*, uint16_t))).Return(1);

Client* client = ArduinoFakeMock(Client);

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(ArduinoFake(Client), stop)).Once();
Verify(Method(ArduinoFake(Client), peek)).Once();
Verify(Method(ArduinoFake(Client), flush)).Once();
Verify(Method(ArduinoFake(Client), connected)).Exactly(2_Times);
Verify(OverloadedMethod(ArduinoFake(Client), connect, int(const char*, uint16_t)).Using(localhost, 8080)).Once();
}

void test_connect(void)
{
When(OverloadedMethod(ArduinoFake(Client), connect, int(const char*, uint16_t))).Return(1, 0);
When(OverloadedMethod(ArduinoFake(Client), connect, int(IPAddress, uint16_t))).Return(0, 1);

IPAddress ipAddress1(62, 145, 182, 225);
IPAddress ipAddress2(221, 155, 131, 19);

Client* client = ArduinoFakeMock(Client);

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(ArduinoFake(Client), connect, int(const char*, uint16_t)).Using(localhost, 8080)).Once();
Verify(OverloadedMethod(ArduinoFake(Client), connect, int(const char*, uint16_t)).Using(localhost, 80)).Once();

Verify(OverloadedMethod(ArduinoFake(Client), connect, int(IPAddress, uint16_t)).Using(ipAddress1, 8080)).Once();
Verify(OverloadedMethod(ArduinoFake(Client), connect, int(IPAddress, uint16_t)).Using(ipAddress2, 8080)).Once();
}

void test_write(void)
{
uint8_t val1 = 0x0;
uint8_t val2 = 0x1;

const uint8_t* ptr1 = &val1;
const uint8_t* ptr2 = &val2;

When(OverloadedMethod(ArduinoFake(Client), write, size_t(uint8_t))).Return(1, 0);
When(OverloadedMethod(ArduinoFake(Client), write, size_t(const uint8_t*, size_t))).Return(0, 1);

Client* client = ArduinoFakeMock(Client);

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(ArduinoFake(Client), write, size_t(uint8_t)).Using(val1)).Once();
Verify(OverloadedMethod(ArduinoFake(Client), write, size_t(uint8_t)).Using(val2)).Once();

Verify(OverloadedMethod(ArduinoFake(Client), write, size_t(const uint8_t*, size_t)).Using(ptr1, 2)).Once();
Verify(OverloadedMethod(ArduinoFake(Client), write, size_t(const uint8_t*, size_t)).Using(ptr2, 3)).Once();
}

void test_read(void)
{
uint8_t val1 = 0x0;
uint8_t val2 = 0x1;

uint8_t* ptr1 = &val1;
uint8_t* ptr2 = &val2;

When(OverloadedMethod(ArduinoFake(Client), read, int())).Return(10, 20);
When(OverloadedMethod(ArduinoFake(Client), read, int(uint8_t*, size_t))).Return(30, 400);

Client* client = ArduinoFakeMock(Client);

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(ArduinoFake(Client), read, int())).Exactly(2_Times);

Verify(OverloadedMethod(ArduinoFake(Client), read, int(uint8_t*, size_t)).Using(ptr1, 2)).Once();
Verify(OverloadedMethod(ArduinoFake(Client), read, int(uint8_t*, size_t)).Using(ptr2, 3)).Once();
}

void test_inject_instance(void)
{
uint8_t val1 = 0x0;
uint8_t val2 = 0x1;

When(OverloadedMethod(ArduinoFake(Client), write, size_t(uint8_t))).Return(11, 22);

Client* client = ArduinoFakeMock(Client);

MyService service(client);

TEST_ASSERT_EQUAL(11, service.send(val1));
TEST_ASSERT_EQUAL(22, service.send(val2));

Verify(OverloadedMethod(ArduinoFake(Client), write, size_t(uint8_t)).Using(val1)).Once();
Verify(OverloadedMethod(ArduinoFake(Client), write, size_t(uint8_t)).Using(val2)).Once();
}

void run_tests()
{
unity_filename_helper_t _ufname_helper(__FILE__);

RUN_TEST(ClientTest::test_basics);
RUN_TEST(ClientTest::test_connect);
RUN_TEST(ClientTest::test_write);
RUN_TEST(ClientTest::test_read);
RUN_TEST(ClientTest::test_inject_instance);
}
}
Loading
Loading