From 817f24fdaa06f987589fb755cb6449106af7e9fa Mon Sep 17 00:00:00 2001 From: Oliver Hamlet Date: Thu, 24 Mar 2016 13:59:38 +0000 Subject: [PATCH] Improve ConditionGrammar tests --- CMakeLists.txt | 2 +- .../backend/metadata/condition_grammar_test.h | 585 +++++++++++++++ .../backend/metadata/test_condition_grammar.h | 664 ------------------ src/tests/main.cpp | 2 +- 4 files changed, 587 insertions(+), 666 deletions(-) create mode 100644 src/tests/backend/metadata/condition_grammar_test.h delete mode 100644 src/tests/backend/metadata/test_condition_grammar.h diff --git a/CMakeLists.txt b/CMakeLists.txt index c730d9b5..c6d60e2b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -244,7 +244,7 @@ set (LOOT_TESTS_HEADERS "${CMAKE_SOURCE_DIR}/src/tests/base_game_test.h" "${CMAKE_SOURCE_DIR}/src/tests/backend/helpers/language_test.h" "${CMAKE_SOURCE_DIR}/src/tests/backend/helpers/version_test.h" "${CMAKE_SOURCE_DIR}/src/tests/backend/helpers/yaml_set_helpers_test.h" - "${CMAKE_SOURCE_DIR}/src/tests/backend/metadata/test_condition_grammar.h" + "${CMAKE_SOURCE_DIR}/src/tests/backend/metadata/condition_grammar_test.h" "${CMAKE_SOURCE_DIR}/src/tests/backend/metadata/test_conditional_metadata.h" "${CMAKE_SOURCE_DIR}/src/tests/backend/metadata/test_file.h" "${CMAKE_SOURCE_DIR}/src/tests/backend/metadata/test_location.h" diff --git a/src/tests/backend/metadata/condition_grammar_test.h b/src/tests/backend/metadata/condition_grammar_test.h new file mode 100644 index 00000000..bb0def0b --- /dev/null +++ b/src/tests/backend/metadata/condition_grammar_test.h @@ -0,0 +1,585 @@ +/* LOOT + +A load order optimisation tool for Oblivion, Skyrim, Fallout 3 and +Fallout: New Vegas. + +Copyright (C) 2014-2016 WrinklyNinja + +This file is part of LOOT. + +LOOT 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. + +LOOT 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 LOOT. If not, see +. +*/ + +#ifndef LOOT_TEST_BACKEND_METADATA_CONDITION_GRAMMAR +#define LOOT_TEST_BACKEND_METADATA_CONDITION_GRAMMAR + +#include "backend/error.h" +#include "backend/metadata/condition_grammar.h" +#include "tests/base_game_test.h" + +namespace loot { + namespace test { + class ConditionGrammarTest : public BaseGameTest { + protected: + ConditionGrammarTest() : + resourcePath(dataPath / "resource" / "detail" / "resource.txt"), + game(Game(GetParam()).SetGamePath(dataPath.parent_path())), + result(false), + success(false) {} + + inline void SetUp() { + BaseGameTest::SetUp(); + + // Write out an empty resource file. + ASSERT_NO_THROW(boost::filesystem::create_directories(resourcePath.parent_path())); + boost::filesystem::ofstream out(resourcePath); + out.close(); + ASSERT_TRUE(boost::filesystem::exists(resourcePath)); + } + + inline void TearDown() { + BaseGameTest::TearDown(); + + ASSERT_NO_THROW(boost::filesystem::remove(resourcePath)); + } + + typedef ConditionGrammar Grammar; + + const boost::filesystem::path resourcePath; + + Game game; + boost::spirit::qi::space_type skipper; + bool result; + bool success; + }; + + // Pass an empty first argument, as it's a prefix for the test instantation, + // but we only have the one so no prefix is necessary. + INSTANTIATE_TEST_CASE_P(, + ConditionGrammarTest, + ::testing::Values( + GameSettings::tes4, + GameSettings::tes5, + GameSettings::fo3, + GameSettings::fonv, + GameSettings::fo4)); + + TEST_P(ConditionGrammarTest, parsingInvalidSyntaxShouldThrow) { + Grammar grammar(nullptr); + std::string condition("file(foo)"); + + EXPECT_THROW(boost::spirit::qi::phrase_parse(std::begin(condition), + std::end(condition), + grammar, + skipper, + result), error); + } + + TEST_P(ConditionGrammarTest, evaluatingInvalidSyntaxShouldThrow) { + Grammar grammar(&game); + std::string condition("file(foo)"); + + EXPECT_THROW(boost::spirit::qi::phrase_parse(std::begin(condition), + std::end(condition), + grammar, + skipper, + result), error); + } + + TEST_P(ConditionGrammarTest, parsingAnEmptyConditionShouldThrow) { + Grammar grammar(nullptr); + std::string condition(""); + + EXPECT_THROW(boost::spirit::qi::phrase_parse(std::begin(condition), + std::end(condition), + grammar, + skipper, + result), error); + } + + TEST_P(ConditionGrammarTest, evaluatingAnEmptyConditionShouldThrow) { + Grammar grammar(&game); + std::string condition(""); + + EXPECT_THROW(boost::spirit::qi::phrase_parse(std::begin(condition), + std::end(condition), + grammar, + skipper, + result), error); + } + + TEST_P(ConditionGrammarTest, aFileConditionWithAPluginThatExistsShouldEvaluateToTrue) { + Grammar grammar(&game); + std::string condition("file(\"" + blankEsm + "\")"); + + success = boost::spirit::qi::phrase_parse(std::begin(condition), + std::end(condition), + grammar, + skipper, + result); + EXPECT_TRUE(success); + EXPECT_TRUE(result); + } + + TEST_P(ConditionGrammarTest, aFileConditionWithAPluginThatDoesNotExistShouldEvaluateToFalse) { + Grammar grammar(&game); + std::string condition("file(\"" + missingEsp + "\")"); + + success = boost::spirit::qi::phrase_parse(std::begin(condition), + std::end(condition), + grammar, + skipper, + result); + EXPECT_TRUE(success); + EXPECT_FALSE(result); + } + + TEST_P(ConditionGrammarTest, evaluatingAFileConditionForAnUnsafePathShouldThrow) { + Grammar grammar(&game); + std::string condition("file(\"../../" + blankEsm + "\")"); + + EXPECT_THROW(boost::spirit::qi::phrase_parse(std::begin(condition), + std::end(condition), + grammar, + skipper, + result), error); + } + + TEST_P(ConditionGrammarTest, aRegexConditionWithAnInvalidRegexShouldThrow) { + Grammar grammar(&game); + std::string condition("regex(\"RagnvaldBook(Farengar(+Ragnvald)?)?\\.esp\")"); + + EXPECT_THROW(boost::spirit::qi::phrase_parse(std::begin(condition), + std::end(condition), + grammar, + skipper, + result), error); + } + + TEST_P(ConditionGrammarTest, aRegexConditionWithARegexMatchingAPluginThatExistsShouldEvaluateToTrue) { + Grammar grammar(&game); + std::string condition("regex(\"Blank.+\\.esm\")"); + + success = boost::spirit::qi::phrase_parse(std::begin(condition), + std::end(condition), + grammar, + skipper, + result); + EXPECT_TRUE(success); + EXPECT_TRUE(result); + } + + TEST_P(ConditionGrammarTest, aRegexConditionWithARegexMatchingAPluginThatDoesNotExistShouldEvaluateToFalse) { + Grammar grammar(&game); + std::string condition("regex(\"Blank\\.m.+\\.esm\")"); + + success = boost::spirit::qi::phrase_parse(std::begin(condition), + std::end(condition), + grammar, + skipper, + result); + EXPECT_TRUE(success); + EXPECT_FALSE(result); + } + + TEST_P(ConditionGrammarTest, aRegexConditionWithARegexMatchingAFileInASubfolderThatExistsShouldEvaluateToTrue) { + Grammar grammar(&game); + std::string condition("regex(\"resource\\\\detail\\\\resource\\.txt\")"); + + success = boost::spirit::qi::phrase_parse(std::begin(condition), + std::end(condition), + grammar, + skipper, + result); + EXPECT_TRUE(success); + EXPECT_TRUE(result); + } + + TEST_P(ConditionGrammarTest, aManyConditionWithARegexMatchingMoreThanOnePluginShouldEvaluateToTrue) { + Grammar grammar(&game); + std::string condition("many(\"Blank.+\\.esm\")"); + + success = boost::spirit::qi::phrase_parse(std::begin(condition), + std::end(condition), + grammar, + skipper, + result); + EXPECT_TRUE(success); + EXPECT_TRUE(result); + } + + TEST_P(ConditionGrammarTest, aManyConditionWithARegexMatchingOnlyOnePluginShouldEvaluateToFalse) { + Grammar grammar(&game); + std::string condition("many(\"Blank\\.esm\")"); + + success = boost::spirit::qi::phrase_parse(std::begin(condition), + std::end(condition), + grammar, + skipper, + result); + EXPECT_TRUE(success); + EXPECT_FALSE(result); + } + + TEST_P(ConditionGrammarTest, aChecksumConditionWithACrcThatMatchesTheActualPluginCrcShouldEvaluateToTrue) { + Grammar grammar(&game); + std::string condition("checksum(\"" + blankEsm + "\", " + IntToHexString(blankEsmCrc) + ")"); + + success = boost::spirit::qi::phrase_parse(std::begin(condition), + std::end(condition), + grammar, + skipper, + result); + EXPECT_TRUE(success); + EXPECT_TRUE(result); + } + + TEST_P(ConditionGrammarTest, aChecksumConditionWithACrcThatDoesNotMatchTheActualPluginCrcShouldEvaluateToFalse) { + Grammar grammar(&game); + std::string condition("checksum(\"" + blankEsm + "\", DEADBEEF)"); + + success = boost::spirit::qi::phrase_parse(std::begin(condition), + std::end(condition), + grammar, + skipper, + result); + EXPECT_TRUE(success); + EXPECT_FALSE(result); + } + + TEST_P(ConditionGrammarTest, aVersionEqualityConditionWithAVersionThatEqualsTheActualPluginVersionShouldEvaluateToTrue) { + ASSERT_NO_THROW(game.LoadPlugins(true)); + + Grammar grammar(&game); + std::string condition("version(\"" + blankEsm + "\", \"5.0\", ==)"); + + success = boost::spirit::qi::phrase_parse(std::begin(condition), + std::end(condition), + grammar, + skipper, + result); + EXPECT_TRUE(success); + EXPECT_TRUE(result); + } + + TEST_P(ConditionGrammarTest, aVersionEqualityConditionWithAVersionThatDoesNotEqualTheActualPluginVersionShouldEvaluateToFalse) { + ASSERT_NO_THROW(game.LoadPlugins(true)); + + Grammar grammar(&game); + std::string condition("version(\"" + blankEsm + "\", \"6.0\", ==)"); + + success = boost::spirit::qi::phrase_parse(std::begin(condition), + std::end(condition), + grammar, + skipper, + result); + EXPECT_TRUE(success); + EXPECT_FALSE(result); + } + + TEST_P(ConditionGrammarTest, aVersionInequalityConditionWithAVersionThatDoesNotEqualTheActualPluginVersionShouldEvaluateToTrue) { + ASSERT_NO_THROW(game.LoadPlugins(true)); + + Grammar grammar(&game); + std::string condition("version(\"" + blankEsm + "\", \"6.0\", !=)"); + + success = boost::spirit::qi::phrase_parse(std::begin(condition), + std::end(condition), + grammar, + skipper, + result); + EXPECT_TRUE(success); + EXPECT_TRUE(result); + } + + TEST_P(ConditionGrammarTest, aVersionInequalityConditionWithAVersionThatEqualsTheActualPluginVersionShouldEvaluateToFalse) { + ASSERT_NO_THROW(game.LoadPlugins(true)); + + Grammar grammar(&game); + std::string condition("version(\"" + blankEsm + "\", \"5.0\", !=)"); + + success = boost::spirit::qi::phrase_parse(std::begin(condition), + std::end(condition), + grammar, + skipper, + result); + EXPECT_TRUE(success); + EXPECT_FALSE(result); + } + + TEST_P(ConditionGrammarTest, aVersionLessThanConditionWithAnActualPluginVersionLessThanTheGivenVersionShouldEvaluateToTrue) { + ASSERT_NO_THROW(game.LoadPlugins(true)); + + Grammar grammar(&game); + std::string condition("version(\"" + blankEsm + "\", \"6.0\", <)"); + + success = boost::spirit::qi::phrase_parse(std::begin(condition), + std::end(condition), + grammar, + skipper, + result); + EXPECT_TRUE(success); + EXPECT_TRUE(result); + } + + TEST_P(ConditionGrammarTest, aVersionLessThanConditionWithAnActualPluginVersionEqualToTheGivenVersionShouldEvaluateToFalse) { + ASSERT_NO_THROW(game.LoadPlugins(true)); + + Grammar grammar(&game); + std::string condition("version(\"" + blankEsm + "\", \"5.0\", <)"); + + success = boost::spirit::qi::phrase_parse(std::begin(condition), + std::end(condition), + grammar, + skipper, + result); + EXPECT_TRUE(success); + EXPECT_FALSE(result); + } + + TEST_P(ConditionGrammarTest, aVersionGreaterThanConditionWithAnActualPluginVersionGreaterThanTheGivenVersionShouldEvaluateToTrue) { + ASSERT_NO_THROW(game.LoadPlugins(true)); + + Grammar grammar(&game); + std::string condition("version(\"" + blankEsm + "\", \"4.0\", >)"); + + success = boost::spirit::qi::phrase_parse(std::begin(condition), + std::end(condition), + grammar, + skipper, + result); + EXPECT_TRUE(success); + EXPECT_TRUE(result); + } + + TEST_P(ConditionGrammarTest, aVersionGreaterThanConditionWithAnActualPluginVersionEqualToTheGivenVersionShouldEvaluateToFalse) { + ASSERT_NO_THROW(game.LoadPlugins(true)); + + Grammar grammar(&game); + std::string condition("version(\"" + blankEsm + "\", \"5.0\", >)"); + + success = boost::spirit::qi::phrase_parse(std::begin(condition), + std::end(condition), + grammar, + skipper, + result); + EXPECT_TRUE(success); + EXPECT_FALSE(result); + } + + TEST_P(ConditionGrammarTest, aVersionLessThanOrEqualToConditionWithAnActualPluginVersionEqualToTheGivenVersionShouldEvaluateToTrue) { + ASSERT_NO_THROW(game.LoadPlugins(true)); + + Grammar grammar(&game); + std::string condition("version(\"" + blankEsm + "\", \"5.0\", <=)"); + + success = boost::spirit::qi::phrase_parse(std::begin(condition), + std::end(condition), + grammar, + skipper, + result); + EXPECT_TRUE(success); + EXPECT_TRUE(result); + } + + TEST_P(ConditionGrammarTest, aVersionLessThanOrEqualToConditionWithAnActualPluginVersionGreaterThanTheGivenVersionShouldEvaluateToFalse) { + ASSERT_NO_THROW(game.LoadPlugins(true)); + + Grammar grammar(&game); + std::string condition("version(\"" + blankEsm + "\", \"4.0\", <=)"); + + success = boost::spirit::qi::phrase_parse(std::begin(condition), + std::end(condition), + grammar, + skipper, + result); + EXPECT_TRUE(success); + EXPECT_FALSE(result); + } + + TEST_P(ConditionGrammarTest, aVersionGreaterThanOrEqualToConditionWithAnActualPluginVersionEqualToTheGivenVersionShouldEvaluateToTrue) { + ASSERT_NO_THROW(game.Init(false, localPath)); + ASSERT_NO_THROW(game.LoadPlugins(true)); + + Grammar grammar(&game); + std::string condition("version(\"" + blankEsm + "\", \"5.0\", >=)"); + + success = boost::spirit::qi::phrase_parse(std::begin(condition), + std::end(condition), + grammar, + skipper, + result); + EXPECT_TRUE(success); + EXPECT_TRUE(result); + } + + TEST_P(ConditionGrammarTest, aVersionGreaterThanOrEqualToConditionWithAnActualPluginVersionLessThanTheGivenVersionShouldEvaluateToFalse) { + ASSERT_NO_THROW(game.LoadPlugins(true)); + + Grammar grammar(&game); + std::string condition("version(\"" + blankEsm + "\", \"6.0\", >=)"); + + success = boost::spirit::qi::phrase_parse(std::begin(condition), + std::end(condition), + grammar, + skipper, + result); + EXPECT_TRUE(success); + EXPECT_FALSE(result); + } + + TEST_P(ConditionGrammarTest, anActiveConditionWithAPluginThatIsActiveShouldEvaluateToTrue) { + ASSERT_NO_THROW(game.Init(false, localPath)); + + Grammar grammar(&game); + std::string condition("active(\"" + blankEsm + "\")"); + + success = boost::spirit::qi::phrase_parse(std::begin(condition), + std::end(condition), + grammar, + skipper, + result); + EXPECT_TRUE(success); + EXPECT_TRUE(result); + } + + TEST_P(ConditionGrammarTest, anActiveConditionWithAPluginThatIsNotActiveShouldEvaluateToFalse) { + ASSERT_NO_THROW(game.Init(false, localPath)); + + Grammar grammar(&game); + std::string condition("active(\"" + blankEsp + "\")"); + + success = boost::spirit::qi::phrase_parse(std::begin(condition), + std::end(condition), + grammar, + skipper, + result); + EXPECT_TRUE(success); + EXPECT_FALSE(result); + } + + TEST_P(ConditionGrammarTest, aFalseConditionPrecededByANegatorShouldEvaluateToTrue) { + Grammar grammar(&game); + std::string condition("not file(\"" + missingEsp + "\")"); + + success = boost::spirit::qi::phrase_parse(std::begin(condition), + std::end(condition), + grammar, + skipper, + result); + EXPECT_TRUE(success); + EXPECT_TRUE(result); + } + + TEST_P(ConditionGrammarTest, aTrueConditionPrecededByANegatorShouldEvaluateToFalse) { + Grammar grammar(&game); + std::string condition("not file(\"" + blankEsm + "\")"); + + success = boost::spirit::qi::phrase_parse(std::begin(condition), + std::end(condition), + grammar, + skipper, + result); + EXPECT_TRUE(success); + EXPECT_FALSE(result); + } + + TEST_P(ConditionGrammarTest, twoTrueConditionsJoinedByAnAndShouldEvaluateToTrue) { + Grammar grammar(&game); + std::string condition("file(\"" + blankEsm + "\")"); + std::string compound(condition + " and " + condition); + + success = boost::spirit::qi::phrase_parse(std::begin(compound), + std::end(compound), + grammar, + skipper, + result); + EXPECT_TRUE(success); + EXPECT_TRUE(result); + } + + TEST_P(ConditionGrammarTest, aTrueAndAFalseConditionJoinedByAnAndShouldEvaluateToFalse) { + Grammar grammar(&game); + std::string condition("file(\"" + blankEsm + "\")"); + std::string compound(condition + " and not " + condition); + + success = boost::spirit::qi::phrase_parse(std::begin(compound), + std::end(compound), + grammar, + skipper, + result); + EXPECT_TRUE(success); + EXPECT_FALSE(result); + } + + TEST_P(ConditionGrammarTest, aFalseAndATrueConditionJoinedByAnOrShouldEvaluateToTrue) { + Grammar grammar(&game); + std::string condition("file(\"" + blankEsm + "\")"); + std::string compound("not " + condition + " or " + condition); + + success = boost::spirit::qi::phrase_parse(std::begin(compound), + std::end(compound), + grammar, + skipper, + result); + EXPECT_TRUE(success); + EXPECT_TRUE(result); + } + + TEST_P(ConditionGrammarTest, twoFalseConditionsJoinedByAnOrShouldEvaluateToFalse) { + Grammar grammar(&game); + std::string condition("file(\"" + blankEsm + "\")"); + std::string compound("not " + condition + " or not " + condition); + + success = boost::spirit::qi::phrase_parse(std::begin(compound), + std::end(compound), + grammar, + skipper, + result); + EXPECT_TRUE(success); + EXPECT_FALSE(result); + } + + TEST_P(ConditionGrammarTest, andOperatorsShouldTakePrecedenceOverOrOperators) { + Grammar grammar(&game); + std::string condition("file(\"" + blankEsm + "\")"); + std::string compound("not " + condition + " and " + condition + " or " + condition); + + success = boost::spirit::qi::phrase_parse(std::begin(compound), + std::end(compound), + grammar, + skipper, + result); + EXPECT_TRUE(success); + EXPECT_TRUE(result); + } + + TEST_P(ConditionGrammarTest, parenthesesShouldTakePrecedenceOverAndOperators) { + Grammar grammar(&game); + std::string condition("file(\"" + blankEsm + "\")"); + std::string compound("not " + condition + " and ( " + condition + " or " + condition + " )"); + + success = boost::spirit::qi::phrase_parse(std::begin(compound), + std::end(compound), + grammar, + skipper, + result); + EXPECT_TRUE(success); + EXPECT_FALSE(result); + } + } +} + +#endif diff --git a/src/tests/backend/metadata/test_condition_grammar.h b/src/tests/backend/metadata/test_condition_grammar.h deleted file mode 100644 index 61031f61..00000000 --- a/src/tests/backend/metadata/test_condition_grammar.h +++ /dev/null @@ -1,664 +0,0 @@ -/* LOOT - -A load order optimisation tool for Oblivion, Skyrim, Fallout 3 and -Fallout: New Vegas. - -Copyright (C) 2014-2016 WrinklyNinja - -This file is part of LOOT. - -LOOT 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. - -LOOT 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 LOOT. If not, see -. -*/ - -#ifndef LOOT_TEST_BACKEND_METADATA_CONDITION_GRAMMAR -#define LOOT_TEST_BACKEND_METADATA_CONDITION_GRAMMAR - -#include "backend/error.h" -#include "backend/metadata/condition_grammar.h" -#include "tests/fixtures.h" - -namespace loot { - namespace test { - class ConditionGrammarTest : public SkyrimTest {}; - - typedef ConditionGrammar Grammar; - - TEST_F(ConditionGrammarTest, Constructor) { - EXPECT_NO_THROW(Grammar cg(nullptr)); - - Game game(Game::tes5); - game.SetGamePath(dataPath.parent_path()); - - EXPECT_NO_THROW(Grammar cg(&game)); - } - - TEST_F(ConditionGrammarTest, InvalidSyntax) { - Game game(Game::tes5); - game.SetGamePath(dataPath.parent_path()); - - boost::spirit::qi::space_type skipper; - bool eval = false; - bool r = false; - Grammar cg(&game); - - std::string condition("file(foo)"); - std::string::const_iterator begin = condition.begin(); - std::string::const_iterator end = condition.end(); - - EXPECT_THROW(r = boost::spirit::qi::phrase_parse(begin, end, cg, skipper, eval), error); - } - - TEST_F(ConditionGrammarTest, EmptyCondition) { - Game game(Game::tes5); - game.SetGamePath(dataPath.parent_path()); - - boost::spirit::qi::space_type skipper; - bool eval = false; - bool r = false; - Grammar cg(&game); - - std::string condition(""); - std::string::const_iterator begin = condition.begin(); - std::string::const_iterator end = condition.end(); - - EXPECT_THROW(r = boost::spirit::qi::phrase_parse(begin, end, cg, skipper, eval), error); - } - - TEST_F(ConditionGrammarTest, FileConditionTrue) { - Game game(Game::tes5); - game.SetGamePath(dataPath.parent_path()); - - boost::spirit::qi::space_type skipper; - bool eval = false; - bool r = false; - Grammar cg(&game); - - std::string condition("file(\"Blank.esm\")"); - std::string::const_iterator begin = condition.begin(); - std::string::const_iterator end = condition.end(); - - r = boost::spirit::qi::phrase_parse(begin, end, cg, skipper, eval); - EXPECT_TRUE(r); - EXPECT_TRUE(eval); - } - - TEST_F(ConditionGrammarTest, FileConditionFalse) { - Game game(Game::tes5); - game.SetGamePath(dataPath.parent_path()); - - boost::spirit::qi::space_type skipper; - bool eval = true; - bool r = false; - Grammar cg(&game); - - std::string condition("file(\"Blank.missing.esm\")"); - std::string::const_iterator begin = condition.begin(); - std::string::const_iterator end = condition.end(); - - EXPECT_NO_THROW(r = boost::spirit::qi::phrase_parse(begin, end, cg, skipper, eval)); - EXPECT_TRUE(r); - EXPECT_FALSE(eval); - } - - TEST_F(ConditionGrammarTest, UnsafePath) { - Game game(Game::tes5); - game.SetGamePath(dataPath.parent_path()); - - boost::spirit::qi::space_type skipper; - bool eval = false; - bool r = false; - Grammar cg(&game); - - std::string condition("file(\"../../Blank.esm\")"); - std::string::const_iterator begin = condition.begin(); - std::string::const_iterator end = condition.end(); - - EXPECT_THROW(r = boost::spirit::qi::phrase_parse(begin, end, cg, skipper, eval), error); - } - - TEST_F(ConditionGrammarTest, RegexConditionTrue) { - Game game(Game::tes5); - game.SetGamePath(dataPath.parent_path()); - - boost::spirit::qi::space_type skipper; - bool eval = false; - bool r = false; - Grammar cg(&game); - - std::string condition("regex(\"Blank.+\\.esm\")"); - std::string::const_iterator begin = condition.begin(); - std::string::const_iterator end = condition.end(); - - EXPECT_NO_THROW(r = boost::spirit::qi::phrase_parse(begin, end, cg, skipper, eval)); - EXPECT_TRUE(r); - EXPECT_TRUE(eval); - } - - TEST_F(ConditionGrammarTest, RegexConditionFalse) { - Game game(Game::tes5); - game.SetGamePath(dataPath.parent_path()); - - boost::spirit::qi::space_type skipper; - bool eval = true; - bool r = false; - Grammar cg(&game); - - std::string condition("regex(\"Blank\\.m.+\\.esm\")"); - std::string::const_iterator begin = condition.begin(); - std::string::const_iterator end = condition.end(); - - EXPECT_NO_THROW(r = boost::spirit::qi::phrase_parse(begin, end, cg, skipper, eval)); - EXPECT_TRUE(r); - EXPECT_FALSE(eval); - } - - TEST_F(ConditionGrammarTest, RegexCondition_Subfolder) { - Game game(Game::tes5); - game.SetGamePath(dataPath.parent_path()); - - boost::spirit::qi::space_type skipper; - bool eval = true; - bool r = false; - Grammar cg(&game); - - std::string condition("regex(\"resource\\\\detail\\\\resource\\.txt\")"); - std::string::const_iterator begin = condition.begin(); - std::string::const_iterator end = condition.end(); - - EXPECT_NO_THROW(r = boost::spirit::qi::phrase_parse(begin, end, cg, skipper, eval)); - EXPECT_TRUE(r); - EXPECT_TRUE(eval); - } - - TEST_F(ConditionGrammarTest, ManyConditionTrue) { - Game game(Game::tes5); - game.SetGamePath(dataPath.parent_path()); - - boost::spirit::qi::space_type skipper; - bool eval = false; - bool r = false; - Grammar cg(&game); - - std::string condition("many(\"Blank.+\\.esm\")"); - std::string::const_iterator begin = condition.begin(); - std::string::const_iterator end = condition.end(); - - EXPECT_NO_THROW(r = boost::spirit::qi::phrase_parse(begin, end, cg, skipper, eval)); - EXPECT_TRUE(r); - EXPECT_TRUE(eval); - } - - TEST_F(ConditionGrammarTest, ManyConditionFalse) { - Game game(Game::tes5); - game.SetGamePath(dataPath.parent_path()); - - boost::spirit::qi::space_type skipper; - bool eval = true; - bool r = false; - Grammar cg(&game); - - std::string condition("many(\"Blank\\.esm\")"); - std::string::const_iterator begin = condition.begin(); - std::string::const_iterator end = condition.end(); - - EXPECT_NO_THROW(r = boost::spirit::qi::phrase_parse(begin, end, cg, skipper, eval)); - EXPECT_TRUE(r); - EXPECT_FALSE(eval); - } - - TEST_F(ConditionGrammarTest, ChecksumConditionTrue) { - Game game(Game::tes5); - game.SetGamePath(dataPath.parent_path()); - ASSERT_NO_THROW(game.Init(false, localPath)); - - boost::spirit::qi::space_type skipper; - bool eval = false; - bool r = false; - Grammar cg(&game); - - std::string condition("checksum(\"Blank.esp\", 24F0E2A1)"); - std::string::const_iterator begin = condition.begin(); - std::string::const_iterator end = condition.end(); - - EXPECT_NO_THROW(r = boost::spirit::qi::phrase_parse(begin, end, cg, skipper, eval)); - EXPECT_TRUE(r); - EXPECT_TRUE(eval); - } - - TEST_F(ConditionGrammarTest, ChecksumConditionFalse) { - Game game(Game::tes5); - game.SetGamePath(dataPath.parent_path()); - ASSERT_NO_THROW(game.Init(false, localPath)); - - boost::spirit::qi::space_type skipper; - bool eval = true; - bool r = false; - Grammar cg(&game); - - std::string condition("checksum(\"Blank.esp\", DEADBEEF)"); - std::string::const_iterator begin = condition.begin(); - std::string::const_iterator end = condition.end(); - - EXPECT_NO_THROW(r = boost::spirit::qi::phrase_parse(begin, end, cg, skipper, eval)); - EXPECT_TRUE(r); - EXPECT_FALSE(eval); - } - - TEST_F(ConditionGrammarTest, VersionConditionEqualTrue) { - Game game(Game::tes5); - game.SetGamePath(dataPath.parent_path()); - ASSERT_NO_THROW(game.Init(false, localPath)); - ASSERT_NO_THROW(game.LoadPlugins(true)); - - boost::spirit::qi::space_type skipper; - bool eval = false; - bool r = false; - Grammar cg(&game); - - std::string condition("version(\"Blank.esm\", \"5.0\", ==)"); - std::string::const_iterator begin = condition.begin(); - std::string::const_iterator end = condition.end(); - - EXPECT_NO_THROW(r = boost::spirit::qi::phrase_parse(begin, end, cg, skipper, eval)); - EXPECT_TRUE(r); - EXPECT_TRUE(eval); - } - - TEST_F(ConditionGrammarTest, VersionConditionEqualFalse) { - Game game(Game::tes5); - game.SetGamePath(dataPath.parent_path()); - ASSERT_NO_THROW(game.Init(false, localPath)); - ASSERT_NO_THROW(game.LoadPlugins(true)); - - boost::spirit::qi::space_type skipper; - bool eval = true; - bool r = false; - Grammar cg(&game); - - std::string condition("version(\"Blank.esm\", \"6.0\", ==)"); - std::string::const_iterator begin = condition.begin(); - std::string::const_iterator end = condition.end(); - - EXPECT_NO_THROW(r = boost::spirit::qi::phrase_parse(begin, end, cg, skipper, eval)); - EXPECT_TRUE(r); - EXPECT_FALSE(eval); - } - - TEST_F(ConditionGrammarTest, VersionConditionNotEqualTrue) { - Game game(Game::tes5); - game.SetGamePath(dataPath.parent_path()); - ASSERT_NO_THROW(game.Init(false, localPath)); - ASSERT_NO_THROW(game.LoadPlugins(true)); - - boost::spirit::qi::space_type skipper; - bool eval = false; - bool r = false; - Grammar cg(&game); - - std::string condition("version(\"Blank.esm\", \"6.0\", !=)"); - std::string::const_iterator begin = condition.begin(); - std::string::const_iterator end = condition.end(); - - EXPECT_NO_THROW(r = boost::spirit::qi::phrase_parse(begin, end, cg, skipper, eval)); - EXPECT_TRUE(r); - EXPECT_TRUE(eval); - } - - TEST_F(ConditionGrammarTest, VersionConditionNotEqualFalse) { - Game game(Game::tes5); - game.SetGamePath(dataPath.parent_path()); - ASSERT_NO_THROW(game.Init(false, localPath)); - ASSERT_NO_THROW(game.LoadPlugins(true)); - - boost::spirit::qi::space_type skipper; - bool eval = true; - bool r = false; - Grammar cg(&game); - - std::string condition("version(\"Blank.esm\", \"5.0\", !=)"); - std::string::const_iterator begin = condition.begin(); - std::string::const_iterator end = condition.end(); - - EXPECT_NO_THROW(r = boost::spirit::qi::phrase_parse(begin, end, cg, skipper, eval)); - EXPECT_TRUE(r); - EXPECT_FALSE(eval); - } - - TEST_F(ConditionGrammarTest, VersionConditionLessThanTrue) { - Game game(Game::tes5); - game.SetGamePath(dataPath.parent_path()); - ASSERT_NO_THROW(game.Init(false, localPath)); - ASSERT_NO_THROW(game.LoadPlugins(true)); - - boost::spirit::qi::space_type skipper; - bool eval = false; - bool r = false; - Grammar cg(&game); - - std::string condition("version(\"Blank.esm\", \"6.0\", <)"); - std::string::const_iterator begin = condition.begin(); - std::string::const_iterator end = condition.end(); - - EXPECT_NO_THROW(r = boost::spirit::qi::phrase_parse(begin, end, cg, skipper, eval)); - EXPECT_TRUE(r); - EXPECT_TRUE(eval); - } - - TEST_F(ConditionGrammarTest, VersionConditionLessThanFalse) { - Game game(Game::tes5); - game.SetGamePath(dataPath.parent_path()); - ASSERT_NO_THROW(game.Init(false, localPath)); - ASSERT_NO_THROW(game.LoadPlugins(true)); - - boost::spirit::qi::space_type skipper; - bool eval = true; - bool r = false; - Grammar cg(&game); - - std::string condition("version(\"Blank.esm\", \"5.0\", <)"); - std::string::const_iterator begin = condition.begin(); - std::string::const_iterator end = condition.end(); - - EXPECT_NO_THROW(r = boost::spirit::qi::phrase_parse(begin, end, cg, skipper, eval)); - EXPECT_TRUE(r); - EXPECT_FALSE(eval); - } - - TEST_F(ConditionGrammarTest, VersionConditionGreaterThanTrue) { - Game game(Game::tes5); - game.SetGamePath(dataPath.parent_path()); - ASSERT_NO_THROW(game.Init(false, localPath)); - ASSERT_NO_THROW(game.LoadPlugins(true)); - - boost::spirit::qi::space_type skipper; - bool eval = false; - bool r = false; - Grammar cg(&game); - - std::string condition("version(\"Blank.esm\", \"4.0\", >)"); - std::string::const_iterator begin = condition.begin(); - std::string::const_iterator end = condition.end(); - - EXPECT_NO_THROW(r = boost::spirit::qi::phrase_parse(begin, end, cg, skipper, eval)); - EXPECT_TRUE(r); - EXPECT_TRUE(eval); - } - - TEST_F(ConditionGrammarTest, VersionConditionGreaterThanFalse) { - Game game(Game::tes5); - game.SetGamePath(dataPath.parent_path()); - ASSERT_NO_THROW(game.Init(false, localPath)); - ASSERT_NO_THROW(game.LoadPlugins(true)); - - boost::spirit::qi::space_type skipper; - bool eval = true; - bool r = false; - Grammar cg(&game); - - std::string condition("version(\"Blank.esm\", \"5.0\", >)"); - std::string::const_iterator begin = condition.begin(); - std::string::const_iterator end = condition.end(); - - EXPECT_NO_THROW(r = boost::spirit::qi::phrase_parse(begin, end, cg, skipper, eval)); - EXPECT_TRUE(r); - EXPECT_FALSE(eval); - } - - TEST_F(ConditionGrammarTest, VersionConditionLETrue) { - Game game(Game::tes5); - game.SetGamePath(dataPath.parent_path()); - ASSERT_NO_THROW(game.Init(false, localPath)); - ASSERT_NO_THROW(game.LoadPlugins(true)); - - boost::spirit::qi::space_type skipper; - bool eval = false; - bool r = false; - Grammar cg(&game); - - std::string condition("version(\"Blank.esm\", \"5.0\", <=)"); - std::string::const_iterator begin = condition.begin(); - std::string::const_iterator end = condition.end(); - - EXPECT_NO_THROW(r = boost::spirit::qi::phrase_parse(begin, end, cg, skipper, eval)); - EXPECT_TRUE(r); - EXPECT_TRUE(eval); - } - - TEST_F(ConditionGrammarTest, VersionConditionLEFalse) { - Game game(Game::tes5); - game.SetGamePath(dataPath.parent_path()); - ASSERT_NO_THROW(game.Init(false, localPath)); - ASSERT_NO_THROW(game.LoadPlugins(true)); - - boost::spirit::qi::space_type skipper; - bool eval = true; - bool r = false; - Grammar cg(&game); - - std::string condition("version(\"Blank.esm\", \"4.0\", <=)"); - std::string::const_iterator begin = condition.begin(); - std::string::const_iterator end = condition.end(); - - EXPECT_NO_THROW(r = boost::spirit::qi::phrase_parse(begin, end, cg, skipper, eval)); - EXPECT_TRUE(r); - EXPECT_FALSE(eval); - } - - TEST_F(ConditionGrammarTest, VersionConditionGETrue) { - Game game(Game::tes5); - game.SetGamePath(dataPath.parent_path()); - ASSERT_NO_THROW(game.Init(false, localPath)); - ASSERT_NO_THROW(game.LoadPlugins(true)); - - boost::spirit::qi::space_type skipper; - bool eval = false; - bool r = false; - Grammar cg(&game); - - std::string condition("version(\"Blank.esm\", \"5.0\", >=)"); - std::string::const_iterator begin = condition.begin(); - std::string::const_iterator end = condition.end(); - - EXPECT_NO_THROW(r = boost::spirit::qi::phrase_parse(begin, end, cg, skipper, eval)); - EXPECT_TRUE(r); - EXPECT_TRUE(eval); - } - - TEST_F(ConditionGrammarTest, VersionConditionGEFalse) { - Game game(Game::tes5); - game.SetGamePath(dataPath.parent_path()); - ASSERT_NO_THROW(game.LoadPlugins(true)); - - boost::spirit::qi::space_type skipper; - bool eval = true; - bool r = false; - Grammar cg(&game); - - std::string condition("version(\"Blank.esm\", \"6.0\", >=)"); - std::string::const_iterator begin = condition.begin(); - std::string::const_iterator end = condition.end(); - - EXPECT_NO_THROW(r = boost::spirit::qi::phrase_parse(begin, end, cg, skipper, eval)); - EXPECT_TRUE(r); - EXPECT_FALSE(eval); - } - - TEST_F(ConditionGrammarTest, ActiveConditionTrue) { - Game game(Game::tes5); - game.SetGamePath(dataPath.parent_path()); - ASSERT_NO_THROW(game.Init(false, localPath)); - - boost::spirit::qi::space_type skipper; - bool eval = false; - bool r = false; - Grammar cg(&game); - - std::string condition("active(\"Blank.esm\")"); - std::string::const_iterator begin = condition.begin(); - std::string::const_iterator end = condition.end(); - - EXPECT_NO_THROW(r = boost::spirit::qi::phrase_parse(begin, end, cg, skipper, eval)); - EXPECT_TRUE(r); - EXPECT_TRUE(eval); - } - - TEST_F(ConditionGrammarTest, ActiveConditionFalse) { - Game game(Game::tes5); - game.SetGamePath(dataPath.parent_path()); - ASSERT_NO_THROW(game.Init(false, localPath)); - - boost::spirit::qi::space_type skipper; - bool eval = true; - bool r = false; - Grammar cg(&game); - - std::string condition("active(\"Blank.esp\")"); - std::string::const_iterator begin = condition.begin(); - std::string::const_iterator end = condition.end(); - - EXPECT_NO_THROW(r = boost::spirit::qi::phrase_parse(begin, end, cg, skipper, eval)); - EXPECT_TRUE(r); - EXPECT_FALSE(eval); - } - - TEST_F(ConditionGrammarTest, NegatorTrue) { - Game game(Game::tes5); - game.SetGamePath(dataPath.parent_path()); - - boost::spirit::qi::space_type skipper; - bool eval = false; - bool r = false; - Grammar cg(&game); - - std::string condition("not file(\"Blank.missing.esm\")"); - std::string::const_iterator begin = condition.begin(); - std::string::const_iterator end = condition.end(); - - EXPECT_NO_THROW(r = boost::spirit::qi::phrase_parse(begin, end, cg, skipper, eval)); - EXPECT_TRUE(r); - EXPECT_TRUE(eval); - } - - TEST_F(ConditionGrammarTest, NegatorFalse) { - Game game(Game::tes5); - game.SetGamePath(dataPath.parent_path()); - - boost::spirit::qi::space_type skipper; - bool eval = true; - bool r = false; - Grammar cg(&game); - - std::string condition("not file(\"Blank.esm\")"); - std::string::const_iterator begin = condition.begin(); - std::string::const_iterator end = condition.end(); - - EXPECT_NO_THROW(r = boost::spirit::qi::phrase_parse(begin, end, cg, skipper, eval)); - EXPECT_TRUE(r); - EXPECT_FALSE(eval); - } - - TEST_F(ConditionGrammarTest, CompoundAndTrue) { - Game game(Game::tes5); - game.SetGamePath(dataPath.parent_path()); - - boost::spirit::qi::space_type skipper; - bool eval = true; - bool r = false; - Grammar cg(&game); - - std::string condition("file(\"Blank.esm\") and file(\"Blank.esp\")"); - std::string::const_iterator begin = condition.begin(); - std::string::const_iterator end = condition.end(); - - EXPECT_NO_THROW(r = boost::spirit::qi::phrase_parse(begin, end, cg, skipper, eval)); - EXPECT_TRUE(r); - EXPECT_TRUE(eval); - } - - TEST_F(ConditionGrammarTest, CompoundAndFalse) { - Game game(Game::tes5); - game.SetGamePath(dataPath.parent_path()); - - boost::spirit::qi::space_type skipper; - bool eval = true; - bool r = false; - Grammar cg(&game); - - std::string condition("file(\"Blank.esm\") and file(\"Blank.missing.esp\")"); - std::string::const_iterator begin = condition.begin(); - std::string::const_iterator end = condition.end(); - - EXPECT_NO_THROW(r = boost::spirit::qi::phrase_parse(begin, end, cg, skipper, eval)); - EXPECT_TRUE(r); - EXPECT_FALSE(eval); - } - - TEST_F(ConditionGrammarTest, CompoundOrTrue) { - Game game(Game::tes5); - game.SetGamePath(dataPath.parent_path()); - - boost::spirit::qi::space_type skipper; - bool eval = true; - bool r = false; - Grammar cg(&game); - - std::string condition("file(\"Blank.missing.esm\") or file(\"Blank.esp\")"); - std::string::const_iterator begin = condition.begin(); - std::string::const_iterator end = condition.end(); - - EXPECT_NO_THROW(r = boost::spirit::qi::phrase_parse(begin, end, cg, skipper, eval)); - EXPECT_TRUE(r); - EXPECT_TRUE(eval); - } - - TEST_F(ConditionGrammarTest, CompoundOrFalse) { - Game game(Game::tes5); - game.SetGamePath(dataPath.parent_path()); - - boost::spirit::qi::space_type skipper; - bool eval = true; - bool r = false; - Grammar cg(&game); - - std::string condition("file(\"Blank.missing.esm\") or file(\"Blank.missing.esp\")"); - std::string::const_iterator begin = condition.begin(); - std::string::const_iterator end = condition.end(); - - EXPECT_NO_THROW(r = boost::spirit::qi::phrase_parse(begin, end, cg, skipper, eval)); - EXPECT_TRUE(r); - EXPECT_FALSE(eval); - } - - TEST_F(ConditionGrammarTest, OrderOfEvaluation) { - Game game(Game::tes5); - game.SetGamePath(dataPath.parent_path()); - - boost::spirit::qi::space_type skipper; - bool eval = true; - bool r = false; - Grammar cg(&game); - - std::string condition("file(\"Blank.esm\") and ( not file(\"Blank.esm\") or file(\"Blank.esp\") ) or file(\"Blank.missing.esp\")"); - std::string::const_iterator begin = condition.begin(); - std::string::const_iterator end = condition.end(); - - EXPECT_NO_THROW(r = boost::spirit::qi::phrase_parse(begin, end, cg, skipper, eval)); - EXPECT_TRUE(r); - EXPECT_TRUE(eval); - } - } -} - -#endif diff --git a/src/tests/main.cpp b/src/tests/main.cpp index 0ce51e9c..21e5e72f 100644 --- a/src/tests/main.cpp +++ b/src/tests/main.cpp @@ -50,7 +50,7 @@ #include "backend/helpers/language_test.h" #include "backend/helpers/version_test.h" #include "backend/helpers/yaml_set_helpers_test.h" -#include "backend/metadata/test_condition_grammar.h" +#include "backend/metadata/condition_grammar_test.h" #include "backend/metadata/test_conditional_metadata.h" #include "backend/metadata/test_file.h" #include "backend/metadata/test_location.h"