2013-03-18 13:40:14 +01:00
|
|
|
/*
|
|
|
|
|
Copyright 2013 Statoil ASA.
|
|
|
|
|
|
|
|
|
|
This file is part of the Open Porous Media project (OPM).
|
|
|
|
|
|
|
|
|
|
OPM is free software: you can redistribute it and/or modify
|
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
|
|
OPM is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
|
along with OPM. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
2013-03-18 15:59:57 +01:00
|
|
|
#include <stdexcept>
|
2013-03-20 16:29:51 +01:00
|
|
|
#include <iostream>
|
2013-03-14 14:54:53 +01:00
|
|
|
#include <boost/filesystem.hpp>
|
2013-03-18 15:59:57 +01:00
|
|
|
#define BOOST_TEST_MODULE ParserTests
|
|
|
|
|
#include <boost/test/unit_test.hpp>
|
|
|
|
|
|
2013-07-30 14:10:07 +02:00
|
|
|
#include <opm/json/JsonObject.hpp>
|
2013-04-03 11:38:06 +02:00
|
|
|
|
2013-05-30 10:11:12 +02:00
|
|
|
#include <opm/parser/eclipse/Parser/Parser.hpp>
|
2013-06-20 15:30:37 +02:00
|
|
|
#include <opm/parser/eclipse/Parser/ParserKeyword.hpp>
|
2013-04-04 09:56:00 +02:00
|
|
|
|
2013-06-20 13:56:11 +02:00
|
|
|
#include <opm/parser/eclipse/Parser/ParserIntItem.hpp>
|
|
|
|
|
#include <opm/parser/eclipse/Parser/ParserStringItem.hpp>
|
|
|
|
|
|
2013-06-18 13:47:07 +02:00
|
|
|
|
2013-05-30 10:11:12 +02:00
|
|
|
using namespace Opm;
|
2013-04-04 09:56:00 +02:00
|
|
|
|
2013-06-20 15:40:45 +02:00
|
|
|
/************************Basic structural tests**********************'*/
|
|
|
|
|
|
2013-03-18 15:59:57 +01:00
|
|
|
BOOST_AUTO_TEST_CASE(Initializing) {
|
2013-06-21 10:23:24 +02:00
|
|
|
BOOST_CHECK_NO_THROW(Parser parser);
|
2013-05-06 12:13:49 +02:00
|
|
|
BOOST_CHECK_NO_THROW(Parser parser);
|
2013-05-30 10:11:12 +02:00
|
|
|
BOOST_CHECK_NO_THROW(ParserPtr parserPtr(new Parser()));
|
|
|
|
|
BOOST_CHECK_NO_THROW(ParserConstPtr parserConstPtr(new Parser()));
|
2013-03-14 12:07:33 +01:00
|
|
|
}
|
|
|
|
|
|
2013-06-20 15:40:45 +02:00
|
|
|
BOOST_AUTO_TEST_CASE(addKeyword_keyword_doesntfail) {
|
2013-05-06 12:13:49 +02:00
|
|
|
Parser parser;
|
|
|
|
|
{
|
2013-06-21 10:23:24 +02:00
|
|
|
ParserKeywordPtr equilKeyword(new ParserKeyword("EQUIL"));
|
2013-06-20 15:40:45 +02:00
|
|
|
parser.addKeyword(equilKeyword);
|
2013-05-06 12:13:49 +02:00
|
|
|
}
|
2013-03-20 17:35:03 +01:00
|
|
|
}
|
2013-04-03 11:38:06 +02:00
|
|
|
|
2013-07-31 11:30:21 +02:00
|
|
|
/************************ JSON config related tests **********************'*/
|
|
|
|
|
|
2013-06-04 14:32:30 +02:00
|
|
|
BOOST_AUTO_TEST_CASE(hasKeyword_hasKeyword_returnstrue) {
|
2013-06-18 14:12:00 +02:00
|
|
|
ParserPtr parser(new Parser());
|
2013-06-20 15:40:45 +02:00
|
|
|
parser->addKeyword(ParserKeywordConstPtr(new ParserKeyword("FJAS")));
|
2013-06-18 14:12:00 +02:00
|
|
|
BOOST_CHECK(parser->hasKeyword("FJAS"));
|
2013-06-04 14:32:30 +02:00
|
|
|
}
|
|
|
|
|
|
2013-06-20 15:40:45 +02:00
|
|
|
|
2013-07-30 14:10:07 +02:00
|
|
|
BOOST_AUTO_TEST_CASE(addKeywordJSON_hasKeyword_returnstrue) {
|
|
|
|
|
ParserPtr parser(new Parser());
|
2013-08-23 00:20:07 +02:00
|
|
|
Json::JsonObject jsonConfig("{\"name\": \"BPR\", \"size\" : 100 , \"items\" :[{\"name\":\"ItemX\" , \"size_type\":\"SINGLE\" , \"value_type\" : \"FLOAT\"}]}");
|
2013-07-30 14:10:07 +02:00
|
|
|
parser->addKeyword(ParserKeywordConstPtr(new ParserKeyword( jsonConfig )));
|
|
|
|
|
BOOST_CHECK(parser->hasKeyword("BPR"));
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-06 16:28:12 +02:00
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(addKeywordJSON_size_isObject_allGood) {
|
|
|
|
|
ParserPtr parser(new Parser());
|
2013-08-23 00:20:07 +02:00
|
|
|
Json::JsonObject jsonConfig("{\"name\": \"EQUIXL\", \"size\" : {\"keyword\":\"EQLDIMS\" , \"item\" : \"NTEQUL\"}, \"items\" :[{\"name\":\"ItemX\" , \"size_type\":\"SINGLE\" , \"value_type\" : \"FLOAT\"}]}");
|
2013-08-06 16:28:12 +02:00
|
|
|
parser->addKeyword(ParserKeywordConstPtr(new ParserKeyword( jsonConfig )));
|
2013-08-23 00:20:07 +02:00
|
|
|
BOOST_CHECK(parser->hasKeyword("EQUIXL"));
|
2013-08-06 16:28:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-07-30 14:10:07 +02:00
|
|
|
BOOST_AUTO_TEST_CASE(loadKeywordsJSON_notArray_throw) {
|
|
|
|
|
ParserPtr parser(new Parser());
|
2013-07-31 11:30:21 +02:00
|
|
|
Json::JsonObject jsonConfig( "{\"name\" : \"BPR\" , \"size\" : 100}");
|
2013-07-30 14:10:07 +02:00
|
|
|
|
|
|
|
|
BOOST_CHECK_THROW(parser->loadKeywords( jsonConfig ) , std::invalid_argument);
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-19 22:37:48 +02:00
|
|
|
BOOST_AUTO_TEST_CASE(empty_sizeReturns0) {
|
|
|
|
|
ParserPtr parser(new Parser());
|
|
|
|
|
BOOST_CHECK_EQUAL( 0U , parser->size());
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-30 14:10:07 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(loadKeywordsJSON_hasKeyword_returnstrue) {
|
|
|
|
|
ParserPtr parser(new Parser());
|
2013-08-23 00:20:07 +02:00
|
|
|
Json::JsonObject jsonConfig( "[{\"name\" : \"BPR\" , \"size\" : 100, \"items\" :[{\"name\":\"ItemX\" , \"size_type\":\"SINGLE\" , \"value_type\" : \"FLOAT\"}]}]");
|
2013-07-31 11:30:21 +02:00
|
|
|
|
|
|
|
|
parser->loadKeywords( jsonConfig );
|
|
|
|
|
BOOST_CHECK(parser->hasKeyword("BPR"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(loadKeywordsJSON_manyKeywords_returnstrue) {
|
|
|
|
|
ParserPtr parser(new Parser());
|
2013-08-23 00:20:07 +02:00
|
|
|
Json::JsonObject jsonConfig( "[{\"name\" : \"BPR\" , \"size\" : 100 , \"items\" :[{\"name\":\"ItemX\" , \"size_type\":\"SINGLE\" , \"value_type\" : \"FLOAT\"}]}, {\"name\" : \"WWCT\", \"size\" : 0} , {\"name\" : \"EQUIL\" , \"size\" : 0}]");
|
2013-07-30 14:10:07 +02:00
|
|
|
|
|
|
|
|
parser->loadKeywords( jsonConfig );
|
|
|
|
|
BOOST_CHECK(parser->hasKeyword("BPR"));
|
2013-07-31 11:30:21 +02:00
|
|
|
BOOST_CHECK(parser->hasKeyword("WWCT"));
|
|
|
|
|
BOOST_CHECK(parser->hasKeyword("EQUIL"));
|
2013-08-19 22:37:48 +02:00
|
|
|
BOOST_CHECK_EQUAL( 3U , parser->size() );
|
2013-07-31 11:30:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(inititalizeFromFile) {
|
|
|
|
|
ParserPtr parser(new Parser());
|
|
|
|
|
boost::filesystem::path jsonFile("testdata/json/example1.json");
|
|
|
|
|
BOOST_CHECK_NO_THROW(parser->initializeFromJsonFile( jsonFile ));
|
|
|
|
|
BOOST_CHECK(parser->hasKeyword("BPR"));
|
|
|
|
|
BOOST_CHECK(parser->hasKeyword("WWCT"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(constructFromJsonFile) {
|
|
|
|
|
boost::filesystem::path jsonFile("testdata/json/example1.json");
|
|
|
|
|
ParserPtr parser(new Parser(jsonFile));
|
|
|
|
|
BOOST_CHECK(parser->hasKeyword("BPR"));
|
|
|
|
|
BOOST_CHECK(parser->hasKeyword("WWCT"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(inititalizeFromFile_doesNotExist_throw) {
|
|
|
|
|
ParserPtr parser(new Parser());
|
|
|
|
|
boost::filesystem::path jsonFile("Does/not/exist");
|
|
|
|
|
BOOST_CHECK_THROW( parser->initializeFromJsonFile( jsonFile ) , std::invalid_argument );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(inititalizeFromFile_missing_keywords_throw) {
|
|
|
|
|
ParserPtr parser(new Parser());
|
|
|
|
|
boost::filesystem::path jsonFile("testdata/json/example_missing_keyword.json");
|
|
|
|
|
BOOST_CHECK_THROW( parser->initializeFromJsonFile( jsonFile ) , std::invalid_argument );
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-19 22:37:48 +02:00
|
|
|
/*****************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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") );
|
|
|
|
|
}
|
2013-06-20 15:40:45 +02:00
|
|
|
|
|
|
|
|
|
2013-06-21 10:23:24 +02:00
|
|
|
|
2013-08-21 12:50:21 +02:00
|
|
|
BOOST_AUTO_TEST_CASE(loadConfigFromDirectory_directoryDoesNotexist_throws) {
|
2013-08-23 00:20:07 +02:00
|
|
|
ParserPtr parser(new Parser());
|
|
|
|
|
boost::filesystem::path configPath("path/does/not/exist");
|
|
|
|
|
BOOST_CHECK_THROW(parser->loadKeywordsFromDirectory( configPath), std::invalid_argument);
|
2013-08-21 12:50:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(loadConfigFromDirectory_notRecursive_allNames) {
|
2013-08-23 00:20:07 +02:00
|
|
|
ParserPtr parser(new Parser());
|
|
|
|
|
BOOST_CHECK_EQUAL(false , parser->hasKeyword("BPR"));
|
|
|
|
|
boost::filesystem::path configPath("testdata/config/directory1");
|
|
|
|
|
BOOST_CHECK_NO_THROW(parser->loadKeywordsFromDirectory( configPath, false , false));
|
|
|
|
|
BOOST_CHECK(parser->hasKeyword("WWCT"));
|
|
|
|
|
BOOST_CHECK_EQUAL(true , parser->hasKeyword("BPR"));
|
|
|
|
|
BOOST_CHECK_EQUAL(false , parser->hasKeyword("DIMENS"));
|
2013-08-21 12:50:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(loadConfigFromDirectory_notRecursive_strictNames) {
|
2013-08-23 00:20:07 +02:00
|
|
|
ParserPtr parser(new Parser());
|
|
|
|
|
boost::filesystem::path configPath("testdata/config/directory1");
|
|
|
|
|
BOOST_CHECK_NO_THROW(parser->loadKeywordsFromDirectory( configPath, false , true ));
|
|
|
|
|
BOOST_CHECK(parser->hasKeyword("WWCT"));
|
|
|
|
|
BOOST_CHECK_EQUAL(false , parser->hasKeyword("BPR"));
|
|
|
|
|
BOOST_CHECK_EQUAL(false , parser->hasKeyword("DIMENS"));
|
2013-08-21 12:50:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(loadConfigFromDirectory_Recursive_allNames) {
|
2013-08-23 00:20:07 +02:00
|
|
|
ParserPtr parser(new Parser());
|
|
|
|
|
BOOST_CHECK_EQUAL(false , parser->hasKeyword("BPR"));
|
|
|
|
|
boost::filesystem::path configPath("testdata/config/directory1");
|
|
|
|
|
BOOST_CHECK_NO_THROW(parser->loadKeywordsFromDirectory( configPath, true, false));
|
|
|
|
|
BOOST_CHECK(parser->hasKeyword("WWCT"));
|
|
|
|
|
BOOST_CHECK_EQUAL(true , parser->hasKeyword("BPR"));
|
|
|
|
|
BOOST_CHECK_EQUAL(true , parser->hasKeyword("DIMENS"));
|
2013-08-21 12:50:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(loadConfigFromDirectory_default) {
|
2013-08-23 00:20:07 +02:00
|
|
|
ParserPtr parser(new Parser());
|
|
|
|
|
BOOST_CHECK_EQUAL(false , parser->hasKeyword("BPR"));
|
|
|
|
|
boost::filesystem::path configPath("testdata/config/directory1");
|
|
|
|
|
BOOST_CHECK_NO_THROW(parser->loadKeywordsFromDirectory( configPath ));
|
|
|
|
|
BOOST_CHECK(parser->hasKeyword("WWCT"));
|
|
|
|
|
BOOST_CHECK_EQUAL(false , parser->hasKeyword("BPR"));
|
|
|
|
|
BOOST_CHECK_EQUAL(true , parser->hasKeyword("DIMENS"));
|
2013-08-21 12:50:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2013-06-20 13:56:11 +02:00
|
|
|
/***************** Simple Int parsing ********************************/
|
|
|
|
|
|
2013-06-20 15:30:37 +02:00
|
|
|
ParserKeywordPtr setupParserKeywordInt(std::string name, int numberOfItems) {
|
|
|
|
|
ParserKeywordPtr parserKeyword(new ParserKeyword(name));
|
2013-07-31 15:02:00 +02:00
|
|
|
ParserRecordPtr parserRecord = parserKeyword->getRecord();
|
|
|
|
|
|
2013-06-18 14:12:00 +02:00
|
|
|
for (int i = 0; i < numberOfItems; i++) {
|
|
|
|
|
std::string name = "ITEM_" + boost::lexical_cast<std::string>(i);
|
|
|
|
|
ParserItemPtr intItem(new ParserIntItem(name, SINGLE));
|
|
|
|
|
parserRecord->addItem(intItem);
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-20 15:30:37 +02:00
|
|
|
return parserKeyword;
|
2013-06-18 14:12:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2013-06-20 13:56:11 +02:00
|
|
|
|
2013-06-18 14:12:00 +02:00
|
|
|
|
2013-06-18 13:47:07 +02:00
|
|
|
|
|
|
|
|
|
2013-06-04 14:32:30 +02:00
|
|
|
|
|
|
|
|
|
2013-04-03 11:38:06 +02:00
|
|
|
|
|
|
|
|
|