I'm right now implementing a library for exporting data into various formats with a multitude of settings. I'm also using Boost.Test, but there is seemingly no function to test the file contents.
For my purposes it should be enough to check if the file at hand contains a given regular expression. I'm really looking for a very simple macro like shown below.
#define BOOST_TEST_MODULE ExportTest
#include <boost/test/included/unit_test.hpp>
BOOST_AUTO_TEST_SUITE(CsvExport)
BOOST_AUTO_TEST_CASE(SimpleTest) {
//
// ... Code writes Simple.csv to harddisk
//
//
//
std::string regExpr= ... // Arbitrary regular expression
BOOST_TEST_FILE("Simple.csv", regExpr)
}
BOOST_AUTO_TEST_SUITE_END();
Is there an extension around, that can be used like that? Or do I have to write a macro on my own?
How can I seamlessly endow Boost.Test with such a functionality, if there is no such macro around?
My final solution:
Finally, I included the following simple function in my tests.
#include <boost/regex.hpp>
bool fileContains(const std::string& filename, const std::string& regexp) {
std::ifstream file(filename);
if (file) {
try {
std::stringstream buffer;
buffer << file.rdbuf();
file.close();
return boost::regex_search(buffer.str(), boost::regex(regexp));
}
catch(const std::exception&) {
return false;
}
} else {
return false;
}
}
Usage would be as exemplified below:
BOOST_CHECK(fileContains("Export.csv","-48.434"));