I am writing a short unit-test-like program as part of a larger project. I have written a few simple functions in a "Utilities.hpp" file which I am writing tests for. Here is the code... some function definitions have been excluded for lengths' sake.
//Main.cpp
#ifdef RUN_TESTS
#include "Logger.hpp"
#include "UnitTester.hpp"
#define TEST_ALL
int main(int, char*[])
{
logger::init();
logger::setSeverityLevel(INFO);
auto slg = logger::getSLogger();
BOOST_LOG_SEV(slg, INFO) << "Starting Unit Tests...";
testing::UnitTester testObject;
testObject.runTests();
}
#endif
Utilities.hpp
#pragma once
#include <string>
#include <stack>
#include <vector>
namespace util
{
void swapChars(char& a, char& b); //flips two chars
std::string reverseString(const std::string& str); //stack based implementation of string inversion
std::vector<std::string> splitStrAtSubstr(const std::string& str, const std::string& split); //splits string into parts seperated by "split"
}
Utilities.cpp
#include "Utilities.hpp"
void util::swapChars(char& a, char& b)
{
...
}
std::string util::reverseString(const std::string& str)
{
...
}
std::vector<std::string> util::splitStrAtSubstr(const std::string& str, const std::string& split)
{
...
}
Lastly the UnitTester class... UnitTester.hpp
#pragma once
#define RUN_TESTS
#define RUN_ALL
#ifdef RUN_TESTS
#include "Logger.hpp"
#include <string>
#include <vector>
namespace testing
{
class UnitTester
{
public:
UnitTester();
~UnitTester();
void runTests();
private:
src::severity_logger<severity_level> testLogger;
void utilities();
void logging();
void resourceGroup();
void resourceManager();
void INIParser();
};
}
#endif
UnitTester.cpp
#include "UnitTester.hpp"
#ifdef RUN_TESTS
void testing::UnitTester::runTests()
{
#ifdef RUN_ALL
utilities();
logging();
resourceGroup();
resourceManager();
INIParser();
#elif
#ifdef TEST_UTILITIES
utilities();
#endif
#ifdef TEST_LOGGING
logging();
#endif
#ifdef TEST_RESOURCE_GROUP
resourceGroup();
#endif
#ifdef TEST_RESOURCE_MANAGER
resourceMananager();
#endif
#ifdef TEST_INI_PARSER
INIParser();
#endif
#endif
}
//PRIVATE FUNCTIONS
void testing::UnitTester::utilities()
{
#include "Utilities.hpp"
BOOST_LOG_SEV(testLogger, INFO) << "Now testing: void util::swapChars(char& a, char& b);";
char a = 'a', b = 'b';
util::swapChars(a, b);
if (a != 'b' || b != 'a') { BOOST_LOG_SEV(testLogger, ERROR) << "Input to util::swapChars failed."; }
BOOST_LOG_SEV(testLogger, INFO) << "Now testing: std::string util::reverseString(const std::string& str);";
if (util::reverseString("myString") != "gnirtSym") { BOOST_LOG_SEV(testLogger, ERROR) << "Input to util::reverseString failed. IN: \"myString\""; }
if (util::reverseString("MYSTRING ") != " GNIRTSYM") { BOOST_LOG_SEV(testLogger, ERROR) << "Input to util::reverseString failed. IN: \"MYSTRING \""; }
if (util::reverseString("aaaaaa") != "aaaaaa") { BOOST_LOG_SEV(testLogger, ERROR) << "Input to util::reverseString failed. IN: \"aaaaaa\""; }
if (util::reverseString("This Is An Extended TEST") != "TSET dednetxE nA sI sihT") { BOOST_LOG_SEV(testLogger, ERROR) << "Input to util::reverseString failed. IN: \"This Is An Extended Test\""; }
if (util::reverseString("\"\\\"Escape") != "epacsE\"\\\"") { BOOST_LOG_SEV(testLogger, ERROR) << "Input to util::reverseString failed. IN: \"\"\\\"Escape\""; }
BOOST_LOG_SEV(testLogger, INFO) << "Now testing: std::vector<std::string> splitStrAtSubstr(const std::string& str, const std::string& split)";
std::vector<std::string> expected("One", "Two", "Three");
std::vector<std::string> expected2("One", "Three");
if (util::splitStrAtSubstr("One.Two.Three", ".") != expected) { BOOST_LOG_SEV(testLogger, ERROR) << "Input to util::splitStrAtSubstr failed. IN: \"One.Two.Three\", \".\""; }
if (util::splitStrAtSubstr("One\"Two\"Three", "\"") != expected) { BOOST_LOG_SEV(testLogger, ERROR) << "Input to util::splitStrAtSubstr failed. IN: \"One\"Two\"Three\", \"\"\""; }
if (util::splitStrAtSubstr("OneTwoThree", ".") != expected) { BOOST_LOG_SEV(testLogger, ERROR) << "Input to util::splitStrAtSubstr failed. IN: \"One.Two.Three\", \".\""; }
if (util::splitStrAtSubstr("OneTwoThree", "Two") != expected) { BOOST_LOG_SEV(testLogger, ERROR) << "Input to util::splitStrAtSubstr failed. IN: \"OneTwoThree\", \"Two\""; }
}
void testing::UnitTester::logging()
{
#include "Logger.hpp"
}
void testing::UnitTester::resourceGroup()
{
#include "ResourceManager\ResourceGroup.hpp"
}
void testing::UnitTester::resourceManager()
{
#include "ResourceManager\ResourceGroup.hpp"
}
void testing::UnitTester::INIParser()
{
#include "INIParser.hpp"
}
#endif
Here is what I think to be a representative sample of the different errors I am getting. I'll put the ones I think most suspect at the top. As they often do, many of the errors are just caused due to other ones...
On function declarations in Utilities.hpp:
Error 14 error C2039: 'vector' : is not a member of 'std'
Error 13 error C2143: syntax error : missing ',' before '&'
Error 15 error C2143: syntax error : missing ';' before '<'
Error 10 error C2039: 'string' : is not a member of 'std'
On BOOST_LOG_SEV calls in UnitTester.hpp:
Error 69 error C2597: illegal reference to non-static member 'testing::UnitTester::testLogger'
Error 49 error C2228: left of '.stream' must have class/struct/union Error 63 error C2228: left of '.open_record' must have class/struct/union
On function calls in UnitTester.cpp:
Error 51 error C2039: 'splitStrAtSubstr' : is not a member of 'util'
Error 31 error C2664: 'int util::reverseString(const int)' : cannot convert argument 1 from 'const char [10]' to 'const int' ^^happens because compiler cannot find std::string as type and defaults to int
These are the most important of the errors it seems. To me the problem is almost certainly occuring in Utilities.hpp where the compiler seems to not recognize std::string and std::vector and comes up with some wierd interpretation of the function declarations.
Thanks in advance.
Aucun commentaire:
Enregistrer un commentaire