From 42851bcaf841d398ac9a6a2f13e94c99822d4def Mon Sep 17 00:00:00 2001 From: Jan Date: Fri, 19 Feb 2021 23:23:46 +0100 Subject: [PATCH] add unit tests for game --- .../Commands/Sequence/SequenceActionTests.cpp | 24 ++++---- .../Sequence/SequenceArchitectureTests.cpp | 6 +- .../Commands/Sequence/SequenceGameTests.cpp | 56 +++++++++++++++++++ 3 files changed, 71 insertions(+), 15 deletions(-) diff --git a/test/ZoneCodeGeneratorLibTests/Parsing/Commands/Sequence/SequenceActionTests.cpp b/test/ZoneCodeGeneratorLibTests/Parsing/Commands/Sequence/SequenceActionTests.cpp index 8149dc18..0488aba2 100644 --- a/test/ZoneCodeGeneratorLibTests/Parsing/Commands/Sequence/SequenceActionTests.cpp +++ b/test/ZoneCodeGeneratorLibTests/Parsing/Commands/Sequence/SequenceActionTests.cpp @@ -122,7 +122,7 @@ namespace test::parsing::commands::sequence::sequence_action } }; - TEST_CASE("SequenceAction: Ensure can parse simple action directive", "[parsing][parsingstream]") + TEST_CASE("SequenceAction: Ensure can parse simple action directive", "[parsing][sequence]") { CommandsSequenceTestsHelper helper; const TokenPos pos; @@ -146,7 +146,7 @@ namespace test::parsing::commands::sequence::sequence_action REQUIRE(helper.m_test_struct->m_post_load_action->m_parameter_types.empty()); } - TEST_CASE("SequenceAction: Ensure can parse simple action directive with one arg", "[parsing][parsingstream]") + TEST_CASE("SequenceAction: Ensure can parse simple action directive with one arg", "[parsing][sequence]") { CommandsSequenceTestsHelper helper; const TokenPos pos; @@ -172,7 +172,7 @@ namespace test::parsing::commands::sequence::sequence_action REQUIRE(helper.m_test_struct->m_post_load_action->m_parameter_types[0] == helper.m_arg_struct_raw); } - TEST_CASE("SequenceAction: Ensure can parse simple action directive with two args", "[parsing][parsingstream]") + TEST_CASE("SequenceAction: Ensure can parse simple action directive with two args", "[parsing][sequence]") { CommandsSequenceTestsHelper helper; const TokenPos pos; @@ -201,7 +201,7 @@ namespace test::parsing::commands::sequence::sequence_action REQUIRE(helper.m_test_struct->m_post_load_action->m_parameter_types[1] == helper.m_arg_struct2_raw); } - TEST_CASE("SequenceAction: Ensure can parse simple action directive with three args", "[parsing][parsingstream]") + TEST_CASE("SequenceAction: Ensure can parse simple action directive with three args", "[parsing][sequence]") { CommandsSequenceTestsHelper helper; const TokenPos pos; @@ -233,7 +233,7 @@ namespace test::parsing::commands::sequence::sequence_action REQUIRE(helper.m_test_struct->m_post_load_action->m_parameter_types[2] == helper.m_arg_struct3_raw); } - TEST_CASE("SequenceAction: Fails if base typename does not exist", "[parsing][parsingstream]") + TEST_CASE("SequenceAction: Fails if base typename does not exist", "[parsing][sequence]") { CommandsSequenceTestsHelper helper; const TokenPos pos; @@ -252,7 +252,7 @@ namespace test::parsing::commands::sequence::sequence_action REQUIRE(helper.m_test_struct->m_post_load_action == nullptr); } - TEST_CASE("SequenceAction: Fails if arg typename does not exist", "[parsing][parsingstream]") + TEST_CASE("SequenceAction: Fails if arg typename does not exist", "[parsing][sequence]") { CommandsSequenceTestsHelper helper; const TokenPos pos; @@ -276,7 +276,7 @@ namespace test::parsing::commands::sequence::sequence_action REQUIRE(helper.m_test_struct->m_post_load_action == nullptr); } - TEST_CASE("SequenceAction: Fails if no type and no use", "[parsing][parsingstream]") + TEST_CASE("SequenceAction: Fails if no type and no use", "[parsing][sequence]") { CommandsSequenceTestsHelper helper; const TokenPos pos; @@ -294,7 +294,7 @@ namespace test::parsing::commands::sequence::sequence_action REQUIRE(helper.m_test_struct->m_post_load_action == nullptr); } - TEST_CASE("SequenceAction: Ensure can parse action directive with used type", "[parsing][parsingstream]") + TEST_CASE("SequenceAction: Ensure can parse action directive with used type", "[parsing][sequence]") { CommandsSequenceTestsHelper helper; const TokenPos pos; @@ -318,7 +318,7 @@ namespace test::parsing::commands::sequence::sequence_action REQUIRE(helper.m_test_struct->m_post_load_action->m_parameter_types.empty()); } - TEST_CASE("SequenceAction: Ensure can parse action directive with type from member", "[parsing][parsingstream]") + TEST_CASE("SequenceAction: Ensure can parse action directive with type from member", "[parsing][sequence]") { CommandsSequenceTestsHelper helper; const TokenPos pos; @@ -345,7 +345,7 @@ namespace test::parsing::commands::sequence::sequence_action REQUIRE(helper.m_test_struct->m_post_load_action->m_parameter_types.empty()); } - TEST_CASE("SequenceAction: Ensure can parse action directive with type from member and in use", "[parsing][parsingstream]") + TEST_CASE("SequenceAction: Ensure can parse action directive with type from member and in use", "[parsing][sequence]") { CommandsSequenceTestsHelper helper; const TokenPos pos; @@ -370,7 +370,7 @@ namespace test::parsing::commands::sequence::sequence_action REQUIRE(helper.m_test_struct->m_post_load_action->m_parameter_types.empty()); } - TEST_CASE("SequenceAction: Ensure can use different struct even though something is used", "[parsing][parsingstream]") + TEST_CASE("SequenceAction: Ensure can use different struct even though something is used", "[parsing][sequence]") { CommandsSequenceTestsHelper helper; const TokenPos pos; @@ -398,7 +398,7 @@ namespace test::parsing::commands::sequence::sequence_action REQUIRE(helper.m_test_struct->m_post_load_action->m_parameter_types.empty()); } - TEST_CASE("SequenceAction: Ensure member must be type with members", "[parsing][parsingstream]") + TEST_CASE("SequenceAction: Ensure member must be type with members", "[parsing][sequence]") { CommandsSequenceTestsHelper helper; const TokenPos pos; diff --git a/test/ZoneCodeGeneratorLibTests/Parsing/Commands/Sequence/SequenceArchitectureTests.cpp b/test/ZoneCodeGeneratorLibTests/Parsing/Commands/Sequence/SequenceArchitectureTests.cpp index a00f2118..997f401c 100644 --- a/test/ZoneCodeGeneratorLibTests/Parsing/Commands/Sequence/SequenceArchitectureTests.cpp +++ b/test/ZoneCodeGeneratorLibTests/Parsing/Commands/Sequence/SequenceArchitectureTests.cpp @@ -36,7 +36,7 @@ namespace test::parsing::commands::sequence::sequence_architecture } }; - TEST_CASE("SequenceAction: Ensure can set x86", "[parsing][parsingstream]") + TEST_CASE("SequenceArchitecture: Ensure can set x86", "[parsing][sequence]") { CommandsSequenceTestsHelper helper; const TokenPos pos; @@ -54,7 +54,7 @@ namespace test::parsing::commands::sequence::sequence_architecture REQUIRE(helper.m_repository->GetArchitecture() == Architecture::X86); } - TEST_CASE("SequenceAction: Ensure can set x64", "[parsing][parsingstream]") + TEST_CASE("SequenceArchitecture: Ensure can set x64", "[parsing][sequence]") { CommandsSequenceTestsHelper helper; const TokenPos pos; @@ -72,7 +72,7 @@ namespace test::parsing::commands::sequence::sequence_architecture REQUIRE(helper.m_repository->GetArchitecture() == Architecture::X86); } - TEST_CASE("SequenceAction: Ensure cannot match unknown value", "[parsing][parsingstream]") + TEST_CASE("SequenceArchitecture: Ensure cannot match unknown value", "[parsing][sequence]") { CommandsSequenceTestsHelper helper; const TokenPos pos; diff --git a/test/ZoneCodeGeneratorLibTests/Parsing/Commands/Sequence/SequenceGameTests.cpp b/test/ZoneCodeGeneratorLibTests/Parsing/Commands/Sequence/SequenceGameTests.cpp index e69de29b..2279f2ea 100644 --- a/test/ZoneCodeGeneratorLibTests/Parsing/Commands/Sequence/SequenceGameTests.cpp +++ b/test/ZoneCodeGeneratorLibTests/Parsing/Commands/Sequence/SequenceGameTests.cpp @@ -0,0 +1,56 @@ +#include + +#include "Utils/ClassUtils.h" +#include "Parsing/Commands/Sequence/SequenceGame.h" +#include "Parsing/Mock/MockLexer.h" +#include "Persistence/InMemory/InMemoryRepository.h" + +namespace test::parsing::commands::sequence::sequence_game +{ + class CommandsSequenceTestsHelper + { + public: + std::unique_ptr m_repository; + std::unique_ptr m_state; + std::unique_ptr> m_lexer; + + unsigned m_consumed_token_count; + + CommandsSequenceTestsHelper() + : m_repository(std::make_unique()), + m_state(std::make_unique(m_repository.get())), + m_consumed_token_count(0u) + { + } + + void Tokens(std::initializer_list> tokens) + { + m_lexer = std::make_unique>(tokens, CommandsParserValue::EndOfFile(TokenPos())); + } + + bool PerformTest() + { + REQUIRE(m_lexer); + const auto sequence = std::make_unique(); + return sequence->MatchSequence(m_lexer.get(), m_state.get(), m_consumed_token_count); + } + }; + + TEST_CASE("SequenceGame: Ensure can set game", "[parsing][sequence]") + { + CommandsSequenceTestsHelper helper; + const TokenPos pos; + helper.Tokens({ + CommandsParserValue::Identifier(pos, new std::string("game")), + CommandsParserValue::Identifier(pos, new std::string("very_cool_game")), + CommandsParserValue::Character(pos, ';'), + CommandsParserValue::EndOfFile(pos) + }); + + auto result = helper.PerformTest(); + + REQUIRE(result); + REQUIRE(helper.m_consumed_token_count == 3); + REQUIRE(helper.m_repository->GetGameName() == "very_cool_game"); + } +}