jeudi 16 février 2017

Writing test that saves temporary values passed between classes

I try to write an internal class that inherits from the real class for testing. The internal class copies temporary variables pass between classes.

Example:

#include <iostream>
#include <random>

class Internal_a;

class A {
public:
    A() {}
    const int test() const { return std::rand() % 10; }
private:
    friend class Internal_a;
};

class Internal_a : public A {
public:
    int test() { 
        _val = A::test();
        return _val;
    }
    int _val;
};

class C {
public:
    C(const A& a) { _x = a.test() * 2; }
private:
    int _x;
};

int main() {
    Internal_a i_a;
    C c(i_a);
    std::cout << i_a._val << "\n";
}

Problem:

The i_a._val value is not a copy of the result of A::test() during the construction of the c object. This is because inheritance doesn't work that way (ok, I just learned that.)

How to write a test that checks that temporary value (A::test()) ?

Aucun commentaire:

Enregistrer un commentaire