Split parser function, to make more modular and testable. Moved integrationtests from ParserTests into Integration tests.

This commit is contained in:
Kristian Flikka
2013-06-18 10:28:30 +02:00
parent a312360c8e
commit 2cce97f115
8 changed files with 108 additions and 104 deletions

View File

@@ -1,7 +1,9 @@
add_executable(runIntegrationTests IntegrationTests.cpp)
target_link_libraries(runIntegrationTests Parser ${Boost_LIBRARIES})
add_test(NAME runIntegrationTests WORKING_DIRECTORY ${EXECUTABLE_OUTPUT_PATH} COMMAND runIntegrationTests)
set_property(SOURCE IntegrationTests.cpp PROPERTY COMPILE_FLAGS "-Wno-error")
add_executable(runIntegrationTestsInternalData IntegrationTestsInternalData.cpp)
target_link_libraries(runIntegrationTestsInternalData Parser ${Boost_LIBRARIES})
add_test(NAME runIntegrationTestsInternalData WORKING_DIRECTORY ${EXECUTABLE_OUTPUT_PATH} COMMAND ${EXECUTABLE_OUTPUT_PATH}/runIntegrationTestsInternalData )
set_property(TEST runIntegrationTestsInternalData PROPERTY LABELS Statoil)

View File

@@ -109,3 +109,87 @@ BOOST_AUTO_TEST_CASE(parse_fileWithBPRKeyword_dataiscorrect) {
BOOST_CHECK_EQUAL(3, K1->getInt(0));
}
BOOST_AUTO_TEST_CASE(PrintToOStream_noThrow) {
boost::filesystem::path singleKeywordFile("testdata/small.data");
ParserPtr parser(new Parser());
RawDeckPtr rawDeck = parser->readToRawDeck(singleKeywordFile.string());
std::cout << *rawDeck << "\n";
}
BOOST_AUTO_TEST_CASE(Parse_InvalidInputFile_Throws) {
ParserPtr parser(new Parser());
BOOST_CHECK_THROW(parser->readToRawDeck("nonexistingfile.asdf"), std::invalid_argument);
}
BOOST_AUTO_TEST_CASE(Parse_ValidInputFile_NoThrow) {
boost::filesystem::path singleKeywordFile("testdata/small.data");
ParserPtr parser(new Parser());
BOOST_CHECK_NO_THROW(parser->readToRawDeck(singleKeywordFile.string()));
}
BOOST_AUTO_TEST_CASE(ParseFileWithOneKeyword) {
boost::filesystem::path singleKeywordFile("testdata/mini.data");
ParserPtr parser(new Parser());
RawDeckPtr rawDeck = parser->readToRawDeck(singleKeywordFile.string());
BOOST_CHECK_EQUAL(1U, rawDeck->size());
RawKeywordConstPtr rawKeyword = rawDeck->getKeyword(0);
BOOST_CHECK_EQUAL(1U, rawKeyword->size());
RawRecordConstPtr record = rawKeyword->getRecord(rawKeyword->size() - 1);
const std::string& recordString = record->getRecordString();
BOOST_CHECK_EQUAL("'NODIR' 'REVERS' 1 20", recordString);
BOOST_CHECK_EQUAL(4U, record->size());
BOOST_CHECK_EQUAL("NODIR", record->getItem(0));
BOOST_CHECK_EQUAL("REVERS", record->getItem(1));
BOOST_CHECK_EQUAL("1", record->getItem(2));
BOOST_CHECK_EQUAL("20", record->getItem(3));
}
BOOST_AUTO_TEST_CASE(ParseFileWithFewKeywords) {
boost::filesystem::path singleKeywordFile("testdata/small.data");
ParserPtr parser(new Parser());
RawDeckPtr rawDeck = parser->readToRawDeck(singleKeywordFile.string());
BOOST_CHECK_EQUAL(7U, rawDeck->size());
RawKeywordConstPtr matchingKeyword = rawDeck->getKeyword(0);
BOOST_CHECK_EQUAL("OIL", matchingKeyword->getKeywordName());
BOOST_CHECK_EQUAL(0U, matchingKeyword->size());
// The two next come in via the include of the include path/readthis.sch file
matchingKeyword = rawDeck->getKeyword(1);
BOOST_CHECK_EQUAL("GRUPTREE", matchingKeyword->getKeywordName());
BOOST_CHECK_EQUAL(2U, matchingKeyword->size());
matchingKeyword = rawDeck->getKeyword(2);
BOOST_CHECK_EQUAL("WHISTCTL", matchingKeyword->getKeywordName());
BOOST_CHECK_EQUAL(1U, matchingKeyword->size());
matchingKeyword = rawDeck->getKeyword(3);
BOOST_CHECK_EQUAL("METRIC", matchingKeyword->getKeywordName());
BOOST_CHECK_EQUAL(0U, matchingKeyword->size());
matchingKeyword = rawDeck->getKeyword(4);
BOOST_CHECK_EQUAL("GRIDUNIT", matchingKeyword->getKeywordName());
BOOST_CHECK_EQUAL(1U, matchingKeyword->size());
matchingKeyword = rawDeck->getKeyword(5);
BOOST_CHECK_EQUAL("RADFIN4", matchingKeyword->getKeywordName());
BOOST_CHECK_EQUAL(1U, matchingKeyword->size());
matchingKeyword = rawDeck->getKeyword(6);
BOOST_CHECK_EQUAL("ABCDAD", matchingKeyword->getKeywordName());
BOOST_CHECK_EQUAL(2U, matchingKeyword->size());
}

View File

@@ -17,14 +17,14 @@
along with OPM. If not, see <http://www.gnu.org/licenses/>.
*/
#define BOOST_TEST_MODULE ParserIntegrationTestsInternalData
#include <stdexcept>
#include <iostream>
#include <boost/filesystem.hpp>
#define BOOST_TEST_MODULE ParserTestsInternalData
#include <boost/test/unit_test.hpp>
#include <opm/parser/eclipse/Parser/Parser.hpp>
#include <opm/parser/eclipse/Parser/ParserKW.hpp>
#include <opm/parser/eclipse/Parser/ParserRecordSize.hpp>

View File

@@ -31,7 +31,10 @@ namespace Opm {
DeckPtr Parser::parse(const std::string &path) {
RawDeckPtr rawDeck = readToRawDeck(path);
return parseFromRawDeck(rawDeck);
}
DeckPtr Parser::parseFromRawDeck(RawDeckConstPtr rawDeck) {
DeckPtr deck(new Deck());
for (size_t i = 0; i < rawDeck->size(); i++) {
RawKeywordConstPtr rawKeyword = rawDeck->getKeyword(i);
@@ -40,8 +43,7 @@ namespace Opm {
ParserKWConstPtr parserKW = m_parserKeywords[rawKeyword->getKeywordName()];
DeckKWConstPtr deckKW = parserKW->parse(rawKeyword);
deck->addKeyword(deckKW);
}
else
} else
std::cerr << "Keyword: " << rawKeyword->getKeywordName() << " is not recognized, skipping this." << std::endl;
}
return deck;
@@ -50,12 +52,12 @@ namespace Opm {
void Parser::addKW(ParserKWConstPtr parserKW) {
m_parserKeywords.insert(std::make_pair(parserKW->getName(), parserKW));
}
bool Parser::hasKeyword(const std::string& keyword) const {
return m_parserKeywords.find(keyword) != m_parserKeywords.end();
}
RawDeckPtr Parser::readToRawDeck(const std::string& path) {
RawDeckPtr Parser::readToRawDeck(const std::string& path) const {
RawDeckPtr rawDeck(new RawDeck(RawParserKWsConstPtr(new RawParserKWs())));
readToRawDeck(rawDeck, path);
return rawDeck;
@@ -66,7 +68,7 @@ namespace Opm {
/// The data is read into a keyword, record by record, until the fixed number of records specified
/// in the RawParserKW is met, or till a slash on a separate line is found.
void Parser::readToRawDeck(RawDeckPtr rawDeck, const std::string& path) {
void Parser::readToRawDeck(RawDeckPtr rawDeck, const std::string& path) const {
boost::filesystem::path dataFolderPath = verifyValidInputPath(path);
{
std::ifstream inputstream;
@@ -109,7 +111,7 @@ namespace Opm {
}
}
void Parser::processIncludeKeyword(RawDeckPtr rawDeck, RawKeywordConstPtr keyword, const boost::filesystem::path& dataFolderPath) {
void Parser::processIncludeKeyword(RawDeckPtr rawDeck, RawKeywordConstPtr keyword, const boost::filesystem::path& dataFolderPath) const {
RawRecordConstPtr firstRecord = keyword->getRecord(0);
std::string includeFileString = firstRecord->getItem(0);
boost::filesystem::path pathToIncludedFile(dataFolderPath);
@@ -118,7 +120,7 @@ namespace Opm {
readToRawDeck(rawDeck, pathToIncludedFile.string());
}
boost::filesystem::path Parser::verifyValidInputPath(const std::string& inputPath) {
boost::filesystem::path Parser::verifyValidInputPath(const std::string& inputPath) const {
Logger::info("Verifying path: " + inputPath);
boost::filesystem::path pathToInputFile(inputPath);
if (!boost::filesystem::is_regular_file(pathToInputFile)) {

View File

@@ -42,8 +42,12 @@ namespace Opm {
/// The starting point of the parsing process. The supplied file is parsed, and the resulting Deck is returned.
DeckPtr parse(const std::string &path);
/// Function to parse directly from a raw deck
DeckPtr parseFromRawDeck(RawDeckConstPtr rawDeck);
RawDeckPtr readToRawDeck(const std::string& path);
/// Reads an eclipse file and returns a tokenized RawDeck
RawDeckPtr readToRawDeck(const std::string& path) const;
/// Method to add ParserKW instances, these holding type and size information about the keywords and their data.
void addKW(ParserKWConstPtr parserKW);
@@ -51,9 +55,9 @@ namespace Opm {
private:
std::map<std::string, ParserKWConstPtr> m_parserKeywords;
void readToRawDeck(RawDeckPtr rawDeck, const std::string& path);
void processIncludeKeyword(RawDeckPtr rawDeck, RawKeywordConstPtr keyword, const boost::filesystem::path& dataFolderPath);
boost::filesystem::path verifyValidInputPath(const std::string& inputPath);
void readToRawDeck(RawDeckPtr rawDeck, const std::string& path) const;
void processIncludeKeyword(RawDeckPtr rawDeck, RawKeywordConstPtr keyword, const boost::filesystem::path& dataFolderPath) const;
boost::filesystem::path verifyValidInputPath(const std::string& inputPath) const;
};

View File

@@ -39,7 +39,6 @@ namespace Opm {
/// Scans the rawRecords data according to the ParserItems definition.
/// returns a DeckItem object.
/// NOTE: data are popped from the rawRecords deque!
DeckItemConstPtr ParserIntItem::scan__(size_t expectedItems , bool scanAll , RawRecordPtr rawRecord) const {
if (sizeType() == SINGLE && expectedItems > 1)
throw std::invalid_argument("Can only ask for one item when sizeType == SINGLE");

View File

@@ -3,21 +3,17 @@ add_executable(runParserKWTests ParserKWTests.cpp)
add_executable(runParserRecordTests ParserRecordTests.cpp)
add_executable(runParserRecordSizeTests ParserRecordSizeTests.cpp)
add_executable(runParserItemTests ParserItemTests.cpp)
add_executable(runParserTestsInternalDataTests ParserTestsInternalData.cpp)
target_link_libraries(runParserTests Parser ${Boost_LIBRARIES})
target_link_libraries(runParserKWTests Parser ${Boost_LIBRARIES})
target_link_libraries(runParserRecordSizeTests Parser ${Boost_LIBRARIES})
target_link_libraries(runParserRecordTests Parser ${Boost_LIBRARIES})
target_link_libraries(runParserItemTests Parser ${Boost_LIBRARIES})
target_link_libraries(runParserTestsInternalDataTests Parser ${Boost_LIBRARIES})
add_test(NAME runParserTests WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} COMMAND ${EXECUTABLE_OUTPUT_PATH}/runParserTests )
add_test(NAME runParserKWTests COMMAND ${EXECUTABLE_OUTPUT_PATH}/runParserKWTests )
add_test(NAME runParserRecordSizeTests COMMAND ${EXECUTABLE_OUTPUT_PATH}/runParserRecordSizeTests )
add_test(NAME runParserRecordTests COMMAND ${EXECUTABLE_OUTPUT_PATH}/runParserRecordTests )
add_test(NAME runParserItemTests COMMAND ${EXECUTABLE_OUTPUT_PATH}/runParserItemTests )
add_test(NAME runParserTestsInternalDataTests WORKING_DIRECTORY ${EXECUTABLE_OUTPUT_PATH} COMMAND ${EXECUTABLE_OUTPUT_PATH}/runParserTestsInternalDataTests )
set_property(TEST runParserTestsInternalDataTests PROPERTY LABELS Statoil)
set_property(SOURCE ParserRecordTests.cpp PROPERTY COMPILE_FLAGS "-Wno-error")

View File

@@ -54,89 +54,6 @@ BOOST_AUTO_TEST_CASE(hasKeyword_hasKeyword_returnstrue) {
}
BOOST_AUTO_TEST_CASE(PrintToOStream_noThrow) {
boost::filesystem::path singleKeywordFile("testdata/small.data");
ParserPtr parser(new Parser());
RawDeckPtr rawDeck = parser->readToRawDeck(singleKeywordFile.string());
std::cout << *rawDeck << "\n";
}
BOOST_AUTO_TEST_CASE(Parse_InvalidInputFile_Throws) {
ParserPtr parser(new Parser());
BOOST_CHECK_THROW(parser->readToRawDeck("nonexistingfile.asdf"), std::invalid_argument);
}
BOOST_AUTO_TEST_CASE(Parse_ValidInputFile_NoThrow) {
boost::filesystem::path singleKeywordFile("testdata/small.data");
ParserPtr parser(new Parser());
BOOST_CHECK_NO_THROW(parser->readToRawDeck(singleKeywordFile.string()));
}
BOOST_AUTO_TEST_CASE(ParseFileWithOneKeyword) {
boost::filesystem::path singleKeywordFile("testdata/mini.data");
ParserPtr parser(new Parser());
RawDeckPtr rawDeck = parser->readToRawDeck(singleKeywordFile.string());
BOOST_CHECK_EQUAL(1U, rawDeck->size());
RawKeywordConstPtr rawKeyword = rawDeck->getKeyword(0);
BOOST_CHECK_EQUAL(1U, rawKeyword->size());
RawRecordConstPtr record = rawKeyword->getRecord(rawKeyword->size() - 1);
const std::string& recordString = record->getRecordString();
BOOST_CHECK_EQUAL("'NODIR' 'REVERS' 1 20", recordString);
BOOST_CHECK_EQUAL(4U, record->size());
BOOST_CHECK_EQUAL("NODIR", record->getItem(0));
BOOST_CHECK_EQUAL("REVERS", record->getItem(1));
BOOST_CHECK_EQUAL("1", record->getItem(2));
BOOST_CHECK_EQUAL("20", record->getItem(3));
}
BOOST_AUTO_TEST_CASE(ParseFileWithFewKeywords) {
boost::filesystem::path singleKeywordFile("testdata/small.data");
ParserPtr parser(new Parser());
RawDeckPtr rawDeck = parser->readToRawDeck(singleKeywordFile.string());
BOOST_CHECK_EQUAL(7U, rawDeck->size());
RawKeywordConstPtr matchingKeyword = rawDeck->getKeyword(0);
BOOST_CHECK_EQUAL("OIL", matchingKeyword->getKeywordName());
BOOST_CHECK_EQUAL(0U, matchingKeyword->size());
// The two next come in via the include of the include path/readthis.sch file
matchingKeyword = rawDeck->getKeyword(1);
BOOST_CHECK_EQUAL("GRUPTREE", matchingKeyword->getKeywordName());
BOOST_CHECK_EQUAL(2U, matchingKeyword->size());
matchingKeyword = rawDeck->getKeyword(2);
BOOST_CHECK_EQUAL("WHISTCTL", matchingKeyword->getKeywordName());
BOOST_CHECK_EQUAL(1U, matchingKeyword->size());
matchingKeyword = rawDeck->getKeyword(3);
BOOST_CHECK_EQUAL("METRIC", matchingKeyword->getKeywordName());
BOOST_CHECK_EQUAL(0U, matchingKeyword->size());
matchingKeyword = rawDeck->getKeyword(4);
BOOST_CHECK_EQUAL("GRIDUNIT", matchingKeyword->getKeywordName());
BOOST_CHECK_EQUAL(1U, matchingKeyword->size());
matchingKeyword = rawDeck->getKeyword(5);
BOOST_CHECK_EQUAL("RADFIN4", matchingKeyword->getKeywordName());
BOOST_CHECK_EQUAL(1U, matchingKeyword->size());
matchingKeyword = rawDeck->getKeyword(6);
BOOST_CHECK_EQUAL("ABCDAD", matchingKeyword->getKeywordName());
BOOST_CHECK_EQUAL(2U, matchingKeyword->size());
}