Added parser->size() and parser->loadKeywordFromFile()

This commit is contained in:
Joakim Hove 2013-08-19 22:37:48 +02:00
parent 7c30145435
commit 47d29cc804
4 changed files with 60 additions and 2 deletions

View File

@ -29,7 +29,7 @@
namespace Opm {
Parser::Parser() {
populateDefaultKeywords();
//populateDefaultKeywords();
}
Parser::Parser(const boost::filesystem::path& jsonFile) {
@ -44,6 +44,10 @@ namespace Opm {
return deck;
}
size_t Parser::size() const {
return m_parserKeywords.size();
}
void Parser::parseFile(DeckPtr deck , const std::string &file) {
std::ifstream inputstream;
@ -173,6 +177,21 @@ namespace Opm {
return pathToInputFile.parent_path();
}
bool Parser::loadKeywordFromFile(const boost::filesystem::path& configFile) {
try {
Json::JsonObject jsonKeyword(configFile);
ParserKeywordConstPtr parserKeyword( new ParserKeyword( jsonKeyword ));
addKeyword( parserKeyword );
return true;
} catch (...) {
return false;
}
}
void Parser::populateDefaultKeywords() {
addKeyword(ParserKeywordConstPtr(new ParserKeyword("GRIDUNIT", 1)));
addKeyword(ParserKeywordConstPtr(new ParserKeyword("INCLUDE", 1)));

View File

@ -51,7 +51,8 @@ namespace Opm {
void initializeFromJsonFile( const boost::filesystem::path& jsonFile );
void loadKeywords(const Json::JsonObject& jsonKeywords);
bool loadKeywordFromFile(const boost::filesystem::path& configFile);
size_t size() const;
private:
std::map<std::string, ParserKeywordConstPtr> m_parserKeywords;
bool tryParseKeyword(const DeckConstPtr deck , std::ifstream& inputstream , RawKeywordPtr& rawKeyword);

View File

@ -204,3 +204,4 @@ BOOST_AUTO_TEST_CASE(hasFixedSize_sizeObjectDoesNotHaveFixedSize_returnsfalse) {
BOOST_CHECK(!parserKeyword->hasFixedSize());
}

View File

@ -84,6 +84,11 @@ BOOST_AUTO_TEST_CASE(loadKeywordsJSON_notArray_throw) {
BOOST_CHECK_THROW(parser->loadKeywords( jsonConfig ) , std::invalid_argument);
}
BOOST_AUTO_TEST_CASE(empty_sizeReturns0) {
ParserPtr parser(new Parser());
BOOST_CHECK_EQUAL( 0U , parser->size());
}
BOOST_AUTO_TEST_CASE(loadKeywordsJSON_hasKeyword_returnstrue) {
@ -103,6 +108,7 @@ BOOST_AUTO_TEST_CASE(loadKeywordsJSON_manyKeywords_returnstrue) {
BOOST_CHECK(parser->hasKeyword("BPR"));
BOOST_CHECK(parser->hasKeyword("WWCT"));
BOOST_CHECK(parser->hasKeyword("EQUIL"));
BOOST_CHECK_EQUAL( 3U , parser->size() );
}
@ -148,6 +154,37 @@ BOOST_AUTO_TEST_CASE(createWithValidJsonFileArgument) {
*/
/*****************************************************************/
BOOST_AUTO_TEST_CASE(loadKeywordFromFile_fileDoesNotExist_returnsFalse) {
ParserPtr parser(new Parser());
boost::filesystem::path configFile("File/does/not/exist");
BOOST_CHECK_EQUAL( false , parser->loadKeywordFromFile( configFile ));
}
BOOST_AUTO_TEST_CASE(loadKeywordFromFile_invalidJson_returnsFalse) {
ParserPtr parser(new Parser());
boost::filesystem::path configFile("testdata/json/example_invalid_json");
BOOST_CHECK_EQUAL( false , parser->loadKeywordFromFile( configFile ));
}
BOOST_AUTO_TEST_CASE(loadKeywordFromFile_invalidConfig_returnsFalse) {
ParserPtr parser(new Parser());
boost::filesystem::path configFile("testdata/json/example_missing_name.json");
BOOST_CHECK_EQUAL( false , parser->loadKeywordFromFile( configFile ));
}
BOOST_AUTO_TEST_CASE(loadKeywordFromFile_validKeyword_returnsTrueHasKeyword) {
ParserPtr parser(new Parser());
boost::filesystem::path configFile("testdata/json/BPR");
BOOST_CHECK_EQUAL( true , parser->loadKeywordFromFile( configFile ));
BOOST_CHECK_EQUAL( 1U , parser->size() );
BOOST_CHECK_EQUAL( true , parser->hasKeyword("BPR") );
}