Correct spelling of unit tests and the name of the testing function
The task is to test the structure with a single method toStr
that returns the type as a string.
This structure looks like this:
struct Individ
{
enum : qint8
{
TypeX01 = 1,
TypeX02 = 2,
TypeX03 = 3,
// ...
TypeUnk = -127,
} Type;
Individ(decltype(Type) type = TypeX01) : Type(type) {}
QString toStr() const
{
switch (Type)
{
case Individ::TypeX01 : return "Type X01";
case Individ::TypeX02 : return "Type X02";
case Individ::TypeX03 : return "Type X03";
// ...
case Individ::TypeUnk : return "Unknown type";
default : return QString();
}
}
explicit operator bool() const { return Type != Individ::TypeX00N; }
};
inline bool operator ==(const Individ& lIndivid, const Individ& rIndivid) noexcept {
return lIndivid.Type == rIndivid.Type;
}
inline bool operator !=(const Individ& lIndivid, const Individ& rIndivid) noexcept {
return lIndivid.Type != rIndivid.Type;
}
We can notice that in addition to testing the function itself, we also have to check the equality (operator==
) and inequality (operator!=
) check operators. And here comes the question. What is the correct name for such a method that checks such operators ? In my case, the method of checking the operator equality is called equalityOperatorTests
, and inequality is called inequalityOperatorTests
.
Also, is it worth checking, for example, the equality operator to return true
and false
. In my case, I only check for a true return.
Here is the code for an example of how I implemented the tests. For testing, I use Google Test Framework:
Individ_tests.h
#include <gtest/gtest.h>
#include "individ.h"
static const quint8 NUM_OF_INDIVID_TYPES = 4;
static const struct {
Individ i;
QString s;
} listOfIndividTypes[NUM_OF_INDIVID_TYPES] {
{ Individ::TypeX01, "Type X01" },
{ Individ::TypeX02, "Type X02" },
{ Individ::TypeX03, "Type X03" },
{ Individ::TypeUnk, "Unknown type" }
};
TEST(IndividTests, toStrTests)
{
for (qint8 i = 0; i < NUM_OF_INDIVID_TYPES; i++) {
ASSERT_STREQ(listOfIndividTypes[i].i.toStr().toUtf8(),
listOfIndividTypes[i].s.toUtf8());
}
// Симулируем подачу несуществующего индивида.
const Individ individ = static_cast<decltype(Individ::Type)>(127);
ASSERT_STREQ(individ.toStr().toUtf8(), QString().toUtf8());
}
TEST(IndividTests, equalityOperatorTests)
{
for (qint8 i = 0; i < NUM_OF_INDIVID_TYPES; i++) {
ASSERT_TRUE (listOfIndividTypes[i].i == listOfIndividTypes[i].i);
}
}
TEST(IndividTests, inequalityOperatorTests)
{
for (qint8 i = 0; i < NUM_OF_INDIVID_TYPES; i++) {
ASSERT_FALSE(listOfIndividTypes[i].i != listOfIndividTypes[i].i);
}
}
Main.cpp
#include "individ_tests.h"
int main(int argc, char *argv[])
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
P.S Don't judge this structure too harshly. It is given as an example.