Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
ondras12345 opened this issue Jul 29, 2023 · 0 comments
Open

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

ondras12345 opened this issue Jul 29, 2023 · 0 comments

Comments

@ondras12345
Copy link

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/[email protected]

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:

#define _ArduinoFakeInstanceGetter2(name, clazz) \
name##Fake* name(class clazz* instance) \
{ \
if (Mapping[instance]) { \
return (name##Fake*) Mapping[instance]; \
} \
if (dynamic_cast<name##FakeProxy*>(instance)) { \
return dynamic_cast<name##FakeProxy*>(instance)->get##name##Fake(); \
} \
throw std::runtime_error("Unknown instance"); \
}

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant