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

Dynamically instantiated test suite + cxxtestgen --no-static-init: broken run-time behaviour #144

Open
mp-chet opened this issue Apr 24, 2019 · 0 comments

Comments

@mp-chet
Copy link

mp-chet commented Apr 24, 2019

Overview

I have a dynamically instantiated test suite that contains a createSuite() method.
I call cxxtestgen with --no-static-init.

The resulting code misbehaves during execution as follows:

  1. An instance A of my test-suite object gets created.
  2. Another instance B of my test-suite object gets created.
  3. setUp() is called on B.
  4. The test-case methods are called on A, which is obviously uninitialised.
  5. tearDown() is called on B.
  6. B is destroyed.

Minimal example

Save this as bug.h:

#include <cxxtest/TestSuite.h>
  
class BuggyTestSuite : public CxxTest::TestSuite {

public:

    void setUp() override {
        std::cerr << "Calling setUp()        on " << this << std::endl;
    }

    void tearDown() override {
        std::cerr << "Calling tearDown()     on " << this << std::endl;
    }

    void testBreakage() {
        std::cerr << "Calling testBreakage() on " << this << " <- !!"
                  << std::endl;
    }

    static BuggyTestSuite *createSuite() {
        BuggyTestSuite *suite = new BuggyTestSuite();
        std::cerr << "Created " << suite << std::endl;
        return suite;
    }

    static void destroySuite(BuggyTestSuite *suite) {
        std::cerr << "Destroying " << suite << std::endl;
        delete suite;
    }
};

Steps to reproduce

cxxtestgen --error-printer --no-static-init -o bug.cpp bug.h
c++ -o bug bug.cpp
./bug

Example output

Created 0x557bfb48be70
Running cxxtest tests (1 test)Created 0x557bfb48c2c0
Calling setUp()        on 0x557bfb48c2c0
Calling testBreakage() on 0x557bfb48be70 <- !!
Calling tearDown()     on 0x557bfb48c2c0
.Destroying 0x557bfb48c2c0
OK!
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