From 26d2836487847bec34adce7d80009ab6c89fa2c3 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Wed, 4 Apr 2018 11:22:44 +0100 Subject: Add rules parsing tests --- src/test/deal_parser_test.cpp | 148 ++++++++++++++-- src/test/rules_parser_test.cpp | 377 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 508 insertions(+), 17 deletions(-) create mode 100644 src/test/rules_parser_test.cpp (limited to 'src') diff --git a/src/test/deal_parser_test.cpp b/src/test/deal_parser_test.cpp index 77a33ff..750913d 100644 --- a/src/test/deal_parser_test.cpp +++ b/src/test/deal_parser_test.cpp @@ -4,26 +4,140 @@ #include -#include "test_helper.h" -#include "../main/game/game_state.h" -#include "../main/game/global_cache.h" +#include +#include -typedef sol_rules::build_policy pol; +#include "../main/input-output/input/json-parsing/deal_parser.h" +#include "../main/input-output/input/json-parsing/json_helper.h" -TEST(FoundationsDominance, SameSuit) { - test_helper::run_foundations_dominance_test(pol::SAME_SUIT, { - "AC","2C","3C","4C","AH","2H","3H","4H","AS","2S","3S","4S","AD", - "2D","3D","4D"}); +using namespace std; +using namespace rapidjson; + +void run_deal_parser_test(bool, const char*); + +TEST(DealParser, SimpleTableauLetter) { + run_deal_parser_test(true, R"( +{ + "tableau piles": [ + ["AS"] + ] +} +)"); +} + +TEST(DealParser, SimpleTableauNumber) { + run_deal_parser_test(true, R"( +{ + "tableau piles": [ + ["1S"] + ] +} +)"); +} + +TEST(DealParser, IllegalTableauNumber) { + run_deal_parser_test(false, R"( +{ + "tableau piles": [ + ["14S"] + ] +} +)"); +} + +TEST(DealParser, IllegalTableauSuit) { + run_deal_parser_test(false, R"( +{ + "tableau piles": [ + ["14A"] + ] +} +)"); +} + +TEST(DealParser, SimpleTableauCase) { + run_deal_parser_test(true, R"( +{ + "tableau piles": [ + ["kc"] + ] +} +)"); } -TEST(FoundationsDominance, RedBlack) { - test_helper::run_foundations_dominance_test(pol::RED_BLACK, { - "AC","2C","AH","2H","AS","3H","2S","AD","3C","4H","3S","2D","4C", - "4S","3D","4D"}); +TEST(DealParser, AdvancedTableau) { + run_deal_parser_test(true, R"( +{ + "tableau piles": [ + ["5S","13C","4C","3C","2S","2c"], + ["5D","qD","3S","1d","2D"], + ["5H","4S","JH","3h","2H"] + ] +} +)"); +} + +TEST(DealParser, Hole) { + run_deal_parser_test(true, R"( +{ + "tableau piles": [ + ["5S","13C","4C"] + ], + "hole": "qD" +} +)"); } -TEST(FoundationsDominance, AnySuit) { - test_helper::run_foundations_dominance_test(pol::ANY_SUIT, { - "AC","2C","AH","2H","AS","2S","AD","3C","3H","3S","2D","4C","4H", - "4S","3D","4D"}); -} \ No newline at end of file +TEST(DealParser, Stock) { + run_deal_parser_test(true, R"( +{ + "tableau piles": [ + ["5S","13C","4C"] + ], + "stock": ["1S","JC","7H"] +} +)"); +} + +TEST(DealParser, Waste) { + run_deal_parser_test(true, R"( +{ + "tableau piles": [ + ["5S","13C","4C"] + ], + "waste": ["1S","JC","7H"] +} +)"); +} + +TEST(DealParser, IllegalPileType) { + run_deal_parser_test(false, R"( +{ + "tableau piles": [ + ["5S","13C","4C"] + ], + "other": ["1S","JC","7H"] +} +)"); +} + +void run_deal_parser_test(bool validity, const char* json) { + Document in_doc; + in_doc.Parse(json); + if (in_doc.HasParseError()) { + throw runtime_error("invalid json"); + } + + Document val_doc; + val_doc.Parse(deal_parser::deal_schema_json().c_str()); + SchemaDocument sd(val_doc); + SchemaValidator validator(sd); + bool acc = in_doc.Accept(validator); + if (acc) { + ASSERT_TRUE(validity == acc) << "JSON accepted when it should not have " + "been:\n" << json; + } else { + ASSERT_TRUE(validity == acc) << json_helper::schema_err_str(validator) + << "\n" << json; + } +} diff --git a/src/test/rules_parser_test.cpp b/src/test/rules_parser_test.cpp new file mode 100644 index 0000000..f5f3f76 --- /dev/null +++ b/src/test/rules_parser_test.cpp @@ -0,0 +1,377 @@ +// +// Created by thecharlesblake on 1/11/18. +// + +#include + +#include +#include + +#include "../main/input-output/input/json-parsing/rules_parser.h" +#include "../main/input-output/input/json-parsing/json_helper.h" + +using namespace std; +using namespace rapidjson; + +void run_rules_parser_test(bool, const char*); + +// invalid int, build pol, bool, other + +TEST(RulesParser, DefaultGameRules) { + run_rules_parser_test(true, R"( +{ + "tableau piles": { + "count": 8, + "build policy": "any-suit", + "spaces policy": "any", + "diagonal deal": false, + "move built group": false, + "move built group policy": "same-as-build" + }, + "max rank": 13, + "two decks": false, + "hole": false, + "foundations": true, + "foundations initial card": false, + "foundations removable": false, + "foundations complete piles": false, + "cells": 0, + "stock size": 0, + "stock deal type": "waste", + "reserve size": 0, + "reserve stacked": false +} +)"); +} + +TEST(RulesParser, InvalidType) { + run_rules_parser_test(false, R"( +{ + "tableau piles": { + "count": "13" + } +} +)"); +} + +TEST(RulesParser, InvalidNumber) { + run_rules_parser_test(false, R"( +{ + "tableau piles": { + "count": 13 + }, + "max rank": 20 +} +)"); +} + +TEST(RulesParser, InvalidEnum) { + run_rules_parser_test(false, R"( +{ + "tableau piles": { + "count": 13, + "build policy": "red-black-green" + } +} +)"); +} + +TEST(RulesParser, InvalidRuleName) { + run_rules_parser_test(false, R"( +{ + "tableau piles": { + "count number": 13 + } +} +)"); +} + +TEST(RulesParser, SpanishPatience) { + run_rules_parser_test(true, R"( +{ + "tableau piles": { + "count": 13 + }, + "foundations removable": true +} +)"); +} + +TEST(RulesParser, SimpleSpanishPatience) { + run_rules_parser_test(true, R"( +{ + "tableau piles": { + "count": 3 + }, + "max rank": 2, + "foundations removable": true +} +)"); +} + +TEST(RulesParser, FreeCell) { + run_rules_parser_test(true, R"( +{ + "tableau piles": { + "build policy": "red-black" + }, + "cells": 4 +} +)"); +} + +TEST(RulesParser, SimpleFreeCell) { + run_rules_parser_test(true, R"( +{ + "tableau piles": { + "count": 4, + "build policy": "red-black" + }, + "max rank": 4, + "cells": 1 +} +)"); +} + +TEST(RulesParser, BakersDozen) { + run_rules_parser_test(true, R"( +{ + "tableau piles": { + "count": 13, + "spaces policy": "no-build" + }, + "foundations removable": true +} +)"); +} + +TEST(RulesParser, SimpleBakersDozen) { + run_rules_parser_test(true, R"( +{ + "tableau piles": { + "count": 3, + "spaces policy": "no-build" + }, + "max rank": 3, + "foundations removable": true +} +)"); +} + +TEST(RulesParser, BlackHole) { + run_rules_parser_test(true, R"( +{ + "tableau piles": { + "count": 17, + "build policy": "no-build" + }, + "hole": true, + "foundations": false +} +)"); +} + +TEST(RulesParser, SimpleBlackHole) { + run_rules_parser_test(true, R"( +{ + "tableau piles": { + "count": 4, + "build policy": "no-build" + }, + "max rank": 5, + "hole": true, + "foundations": false +} +)"); +} + +TEST(RulesParser, FortunesFavor) { + run_rules_parser_test(true, R"( +{ + "tableau piles": { + "count": 12, + "build policy": "same-suit" + }, + "foundations initial card": true, + "stock size": 36 +} +)"); +} + +TEST(RulesParser, SimpleFortunesFavor) { + run_rules_parser_test(true, R"( +{ + "tableau piles": { + "count": 3, + "build policy": "same-suit" + }, + "max rank": 4, + "foundations initial card": true, + "stock size": 3 +} +)"); +} + +TEST(RulesParser, FlowerGarden) { + run_rules_parser_test(true, R"( +{ + "tableau piles": { + "count": 6 + }, + "reserve size": 16, + "foundations removable": true +} +)"); +} + +TEST(RulesParser, SimpleFlowerGarden) { + run_rules_parser_test(true, R"( +{ + "tableau piles": { + "count": 3 + }, + "max rank": 3, + "reserve size": 3, + "foundations removable": true +} +)"); +} + +TEST(RulesParser, Canfield) { + run_rules_parser_test(true, R"( +{ + "tableau piles": { + "count": 4, + "build policy": "red-black" + }, + "stock size": 34, + "reserve size": 14, + "reserve stacked": true +} +)"); +} + +TEST(RulesParser, SimpleCanfield) { + run_rules_parser_test(true, R"( +{ + "tableau piles": { + "count": 3, + "build policy": "red-black" + }, + "max rank": 3, + "stock size": 1, + "reserve size": 2, + "reserve stacked": true +} +)"); +} + +TEST(RulesParser, Somerset) { + run_rules_parser_test(true, R"( +{ + "tableau piles": { + "count": 10, + "build policy": "red-black", + "diagonal deal": true + }, + "foundations removable": true +} +)"); +} + +TEST(RulesParser, SimpleSomerset) { + run_rules_parser_test(true, R"( +{ + "tableau piles": { + "count": 4, + "build policy": "red-black", + "diagonal deal": true + }, + "max rank": 2, + "foundations removable": true +} +)"); +} + +TEST(RulesParser, AlphaStar) { + run_rules_parser_test(true, R"( +{ + "tableau piles": { + "count": 12, + "build policy": "same-suit", + "move built group": true + }, + "foundations initial card": true +} +)"); +} + +TEST(RulesParser, SimpleAlphaStar) { + run_rules_parser_test(true, R"( +{ + "tableau piles": { + "count": 3, + "build policy": "same-suit", + "move built group": true + }, + "max rank": 5, + "foundations initial card": true +} +)"); +} + +TEST(RulesParser, Spider) { + run_rules_parser_test(true, R"( +{ + "tableau piles": { + "count": 10, + "build policy": "any-suit", + "move built group": true, + "move built group policy": "same-suit" + }, + "foundations complete piles": true, + "two decks": true, + "stock size": 50, + "stock deal type": "tableau piles" +} +)"); +} + +TEST(RulesParser, SimpleSpider) { + run_rules_parser_test(true, R"( +{ + "tableau piles": { + "count": 4, + "build policy": "any-suit", + "move built group": true, + "move built group policy": "same-suit" + }, + "foundations complete piles": true, + "two decks": true, + "max rank": 3, + "stock size": 12, + "stock deal type": "tableau piles" +} +)"); +} + +void run_rules_parser_test(bool validity, const char* json) { + Document in_doc; + in_doc.Parse(json); + if (in_doc.HasParseError()) { + throw runtime_error("invalid json"); + } + + Document val_doc; + val_doc.Parse(rules_parser::rules_schema_json().c_str()); + SchemaDocument sd(val_doc); + SchemaValidator validator(sd); + bool acc = in_doc.Accept(validator); + if (acc) { + ASSERT_TRUE(validity == acc) << "JSON accepted when it should not have " + "been:\n" << json; + } else { + ASSERT_TRUE(validity == acc) << json_helper::schema_err_str(validator) + << "\n" << json; + } +} -- cgit v1.2.1