FabioBatSilva / ArduinoFake

Arduino mocking made easy
https://platformio.org/lib/show/1689/ArduinoFake
MIT License
102 stars 47 forks source link

How to inherit from Stream (`what(): Unknown instance`) #50

Open ondras12345 opened 1 year ago

ondras12345 commented 1 year ago

Hi, I have been struggling with this for some time. I am trying to write tests for a library that creates its own Stream child classes and I am encountering the following error:

what():  Unknown instance

MWE:

$ tree
.
├── include
│   └── README
├── lib
│   ├── README
│   └── StreamTest
│       └── StreamTest.h
├── platformio.ini
├── src
└── test
    ├── README
    └── test_Stream
        └── test_Stream.cpp

6 directories, 6 files

platformio.ini:

; PlatformIO Project Configuration File
;
;   Build options: build flags, source filter
;   Upload options: custom upload port, speed and extra flags
;   Library options: dependencies, extra library storages
;   Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html

[env:native]
platform = native
lib_deps =
        fabiobatsilva/ArduinoFake@0.4.0

lib/StreamTest/StreamTest.h:

#pragma once
#include <Arduino.h>

class StreamTest : public Stream {
public:
    int available() { return 0; }

    int read() { return 0; }

    int peek() { return 0; }

    void flush() { };

    size_t write( uint8_t b ) { return 0; }
};

test/test_Stream/test_Stream.cpp:

#include <unity.h>
#include <ArduinoFake.h>
using namespace fakeit;

#include "StreamTest.h"

void setUp()
{
    ArduinoFakeReset();
}

void test_print()
{
    StreamTest s;
    s.println("test");
}

int runUnityTests()
{
    UNITY_BEGIN();
    RUN_TEST(test_print);
    return UNITY_END();
}

int main()
{
    return runUnityTests();
}
$ pio test -vvv
Collected 1 tests (test_Stream)

Processing test_Stream in native environment
----------------------------------------------------------------------------------------------------------------
Building...
LDF: Library Dependency Finder -> https://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 3 compatible libraries
Scanning dependencies...
Dependency Graph
|-- ArduinoFake @ 0.4.0 (License: Unknown, Path: /home/ondra/temp/ArduinoFake-stream/.pio/libdeps/native/ArduinoFake)
|-- StreamTest (License: Unknown, Path: /home/ondra/temp/ArduinoFake-stream/lib/StreamTest)
|   |-- ArduinoFake @ 0.4.0 (License: Unknown, Path: /home/ondra/temp/ArduinoFake-stream/.pio/libdeps/native/ArduinoFake)
|-- Unity @ 2.5.2 (License: MIT, Path: /home/ondra/temp/ArduinoFake-stream/.pio/libdeps/native/Unity)
Building in test mode

Testing...
terminate called after throwing an instance of 'std::runtime_error'
  what():  Unknown instance
Program received signal SIGABRT (Aborted)
---------------------------------------- native:test_Stream [ERRORED] Took 0.81 seconds ------------------------

================================================== SUMMARY =====================================================
Environment    Test         Status    Duration
-------------  -----------  --------  ------------
native         test_Stream  ERRORED   00:00:00.808
=================================== 1 test cases: 0 succeeded in 00:00:00.808 ==================================

The error seems to be coming from here: https://github.com/FabioBatSilva/ArduinoFake/blob/cf9c596b73ffe7e7cf39faeb550bbef8598edb98/src/ArduinoFake.h#L58-L68 I have changed it to

        throw std::runtime_error("Unknown instance: " # name); \

and the resulting error message is

  what():  Unknown instance: Print

Unfortunately, I don't know how to proceed with debugging.