samedi 3 octobre 2015

Is heavily relying on strings in unit tests an acceptable practice?

I'm writing tests for a class implementing signal concept, using Catch framerwork. I want to test if slots are called in the correct order, if parameters are passed correctly, etc. I used strings (std::stringstream, to be precise) in order to verify that.

#include "catch.hpp"

#include <nete/Signal.h>

#include <sstream>

std::stringstream ss;

void global_function1(int a, int b) {
    ss << "global_function1 " << a << " " << b << " ";
}

void global_function2(int a, int b) {
    ss << "global_function2 " << a << " " << b << " ";
}

TEST_CASE("Signal connect", "[signal]")
{
    SECTION("global")
    {
        ss.clear(), ss.str("");

        nete::Signal<void(int, int)> s;

        s.connect<global_function1>();
        s.connect<global_function2>();

        s(123, 456);
        s(234, 567);

        std::string expected =
        "global_function1 123 456 "
        "global_function2 123 456 "
        "global_function1 234 567 "
        "global_function2 234 567 ";

        REQUIRE(ss.str() == expected);
    }
}

On the one hand, the solution works as expected, on the other - it feels a bit hacky. Are there any caveats that I'm not aware of? Or maybe there are some other standard ways to veryfiy that the function/method actually got called with the correct arguments?

Aucun commentaire:

Enregistrer un commentaire