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

Can I inherit ConsoleReporter? #344

Closed
erelsgl opened this issue Feb 26, 2020 · 3 comments
Closed

Can I inherit ConsoleReporter? #344

erelsgl opened this issue Feb 26, 2020 · 3 comments

Comments

@erelsgl
Copy link

erelsgl commented Feb 26, 2020

Description

I tried to write a new reporter by inheriting ConsoleReporter and modifying some functions. I wrote this in a new file:

#include "doctest.h"
using namespace doctest;
struct ReporterGrader: public ConsoleReporter {
     ...
};

But the compiler gave me an error "expected class name", as if it does not recognize ConsoleReporter. What am I doing wrong?

Extra information

  • doctest version: v2.3.7
  • Operating System: Windows 10 / WSL
  • Compiler+version: clang++5.0
@onqtam
Copy link
Member

onqtam commented Feb 29, 2020

Well first the console reporter class is in the doctest::detail namespace, and also it's only visible in the translation unit where you have implemented the framework (where you have defined DOCTEST_CONFIG_IMPLEMENT before including the header) - there you could inherit from it, register it as a different reporter (perhaps my_new_console) and then use that through the --reporters=my_new_console command line option. Does this help?

@erelsgl
Copy link
Author

erelsgl commented Feb 29, 2020

Indeed it works in the main translation unit, but I did not find it in the "detail" namespace but in the "doctest" namespace. Here is a main program that works for me:


#define DOCTEST_CONFIG_IMPLEMENT
#include "doctest.h"
using namespace doctest;

struct ReporterGrader: public ConsoleReporter {
    ReporterGrader(const ContextOptions& opt): ConsoleReporter(opt) {}

    void test_run_end(const TestRunStats& run_stats) override {
        ConsoleReporter::test_run_end(run_stats);
        float grade = (run_stats.numAsserts - run_stats.numAssertsFailed) * 100 / run_stats.numAsserts;
        std::cout << "Grade: " << grade << std::endl;
    }
};

REGISTER_REPORTER("grader", /*priority=*/1, ReporterGrader);

int main(int argc, char** argv) {
    Context context;
    context.addFilter("reporters", "grader");
    context.run();
}

@onqtam
Copy link
Member

onqtam commented Feb 29, 2020

Glad that it worked! The class is actually in the detail namespace but that namespace gets used in an anonymous namespace inside of the doctest namespace and that's why you find it in doctest.

I'm closing this as the issue seems resolved :)

This issue was closed.
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

2 participants