patterncppMinor
CppUnit test suite for testing a routine
Viewed 0 times
suiteroutinetestingfortestcppunit
Problem
I wrote a CppUnit test suite to unit-test the code that I posted here which is a single routine that proposes how a Lua expression could be auto-completed.
What do you think? I appreciate all the comments.
```
#include
#include
class Tests : public CppUnit::TestFixture
{
private:
lua_State* L;
protected:
void execute (const std::string& script)
{
luaL_dostring(L, script.c_str());
}
void assertSize (const std::vector& candidates, size_t size)
{
CPPUNIT_ASSERT_EQUAL_MESSAGE(
boost::str(boost::format("The candidate list size is %d, but should be %d.") % candidates.size() % size),
size, candidates.size()
);
}
void assertContains (const std::vector& candidates, const std::string& value)
{
CPPUNIT_ASSERT_MESSAGE(
boost::str(boost::format("Candidate list doesn't contain '%s'.") % value),
std::find(candidates.begin(), candidates.end(), value) != candidates.end()
);
}
public:
void setUp()
{
L = luaL_newstate();
luaL_openlibs(L);
}
void tearDown()
{
CPPUNIT_ASSERT_EQUAL_MESSAGE("The stack is not empty.", 0, lua_gettop(L));
lua_close(L);
}
void globalNamesAreAutocompleted()
{
execute("resources = { textures = { 'texture1', 'texture2', 'texture3' } }");
execute("resolution = { sizeX = 2560, sizeY = 1440 }");
auto candidates = buildAutoCompleteCandidates(L, "reso");
assertSize(candidates, 2);
assertContains(candidates, "resources");
assertContains(candidates, "resolution");
}
void valuesWithinTablesAreAutocompleted()
{
execute("europe = { countries = { France = { cities = { Paris = { LeMarais = {}, LesHalls = {}, Montparnasse = {}, Montmartre = {} }}}}}");
auto candidates = buildAutoCompleteCandidates(L, "europe.countries['France'].cities['Paris'].Mont");
assertSize(candidate
What do you think? I appreciate all the comments.
```
#include
#include
class Tests : public CppUnit::TestFixture
{
private:
lua_State* L;
protected:
void execute (const std::string& script)
{
luaL_dostring(L, script.c_str());
}
void assertSize (const std::vector& candidates, size_t size)
{
CPPUNIT_ASSERT_EQUAL_MESSAGE(
boost::str(boost::format("The candidate list size is %d, but should be %d.") % candidates.size() % size),
size, candidates.size()
);
}
void assertContains (const std::vector& candidates, const std::string& value)
{
CPPUNIT_ASSERT_MESSAGE(
boost::str(boost::format("Candidate list doesn't contain '%s'.") % value),
std::find(candidates.begin(), candidates.end(), value) != candidates.end()
);
}
public:
void setUp()
{
L = luaL_newstate();
luaL_openlibs(L);
}
void tearDown()
{
CPPUNIT_ASSERT_EQUAL_MESSAGE("The stack is not empty.", 0, lua_gettop(L));
lua_close(L);
}
void globalNamesAreAutocompleted()
{
execute("resources = { textures = { 'texture1', 'texture2', 'texture3' } }");
execute("resolution = { sizeX = 2560, sizeY = 1440 }");
auto candidates = buildAutoCompleteCandidates(L, "reso");
assertSize(candidates, 2);
assertContains(candidates, "resources");
assertContains(candidates, "resolution");
}
void valuesWithinTablesAreAutocompleted()
{
execute("europe = { countries = { France = { cities = { Paris = { LeMarais = {}, LesHalls = {}, Montparnasse = {}, Montmartre = {} }}}}}");
auto candidates = buildAutoCompleteCandidates(L, "europe.countries['France'].cities['Paris'].Mont");
assertSize(candidate
Solution
Coverage
Your tests don't cover
-
the case where there is no completion because the last separator is called on an entry that is not a table
-
the case where
This hints at a general problem: It seems that you are doing sunny day tests only.
Non selfcontained source
It seems you did not post the whole code because this does not work as is. (Even after "including" the code from the other question.) This opens the door for problems that occur because the answerers did not create the same environment as you have there.
Missing includes
Your test code does not include
Naming
Your tests don't cover
-
the case where there is no completion because the last separator is called on an entry that is not a table
-
the case where
__index is not a tableThis hints at a general problem: It seems that you are doing sunny day tests only.
Non selfcontained source
It seems you did not post the whole code because this does not work as is. (Even after "including" the code from the other question.) This opens the door for problems that occur because the answerers did not create the same environment as you have there.
Missing includes
Your test code does not include
- the necessary lua libraries for functions (or macros) like
luaL_dostring.
- the test runner needed for actually running the test (I used
cppunit/ui/text/TestRunner.h)
Naming
L(looks like a macro, too generic (although probably idiomatic in Lua context))->luaState
script->expression(at least you did never pass a script but only expression toexecute)
blankExpressionAutocompletsWithAllGlobals(typo) ->blankExpressionAutocompletesWithAllGlobals
Context
StackExchange Code Review Q#40202, answer score: 7
Revisions (0)
No revisions yet.