aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLoic Guegan <manzerbredes@mailbox.org>2022-01-24 16:41:02 +0100
committerLoic Guegan <manzerbredes@mailbox.org>2022-01-24 16:41:02 +0100
commite6e2c5a8bf5c99119799c9ac706c4428fec6b5aa (patch)
tree90a5d47ea2c7bb46fce75d5ed5fe667aa5b05637
parent55b2c72108b1206d8b2f8565e2bc6f398a74cd2c (diff)
Improve tests
-rw-r--r--src/pgnp.cpp14
-rw-r--r--src/pgnp.hpp17
-rw-r--r--tests/tests.cpp49
3 files changed, 77 insertions, 3 deletions
diff --git a/src/pgnp.cpp b/src/pgnp.cpp
index bb9fd29..5336cd6 100644
--- a/src/pgnp.cpp
+++ b/src/pgnp.cpp
@@ -239,7 +239,12 @@ int PGN::ParseNextTag(int start_loc) {
tags[key] = value;
tagkeys.push_back(key);
- // TODO: Check that caracters if a ]
+ EOF_CHECK(valueloc + 1);
+ c = pgn_content[valueloc + 1];
+ if (c != ']') {
+ throw UnexpectedCharacter(c, ']', valueloc + 1);
+ }
+
return (valueloc + 1); // +1 For the last char of the tag which is ']'
}
@@ -247,7 +252,12 @@ HalfMove *PGN::GetMoves() { return (moves); }
std::vector<std::string> PGN::GetTagList() { return tagkeys; }
-std::string PGN::GetTagValue(std::string key) { return tags[key]; }
+std::string PGN::GetTagValue(std::string key) {
+ if (tags.find(key) == tags.end()) {
+ throw InvalidTagName();
+ }
+ return tags[key];
+}
void PGN::Dump() {
std::cout << "---------- PGN DUMP ----------" << std::endl;
diff --git a/src/pgnp.hpp b/src/pgnp.hpp
index a89de73..0e9c7a5 100644
--- a/src/pgnp.hpp
+++ b/src/pgnp.hpp
@@ -2,6 +2,7 @@
#include <exception>
#include <fstream>
#include <iostream>
+#include <sstream>
#include <streambuf>
#include <string>
#include <unordered_map>
@@ -24,6 +25,7 @@ public:
HalfMove();
~HalfMove();
+ /// @brief Get number of HalfMove in the MailLine
int GetLength();
/// @brief Dump move and all its variations
void Dump();
@@ -66,6 +68,21 @@ struct UnexpectedEOF : public std::exception {
const char *what() const throw() { return "Unexpected end of pgn file"; }
};
+struct InvalidTagName : public std::exception {
+ const char *what() const throw() { return "Invalid tag name"; }
+};
+
+struct UnexpectedCharacter : public std::exception {
+ std::string msg;
+ UnexpectedCharacter(char actual, char required, int loc) {
+ std::stringstream ss;
+ ss << "Expected \'" << required << "\' at location " << loc
+ << " but read \'" << actual << "\'";
+ msg = ss.str();
+ }
+ const char *what() const throw() { return msg.c_str(); }
+};
+
struct STRCheckFailed : public std::exception {
const char *what() const throw() {
return "Seven Tag Roster compliance check failed";
diff --git a/tests/tests.cpp b/tests/tests.cpp
index 9c65975..3838ef6 100644
--- a/tests/tests.cpp
+++ b/tests/tests.cpp
@@ -7,7 +7,54 @@ TEST_CASE("Valid PGN", "[pgn1]") {
PGN pgn;
REQUIRE_NOTHROW(pgn.FromFile("pgn_files/valid/pgn1.pgn"));
REQUIRE_THROWS(pgn.STRCheck());
- REQUIRE(pgn.GetMoves()->GetLength() == 6);
+
+ HalfMove *m = pgn.GetMoves();
+ REQUIRE(m->GetLength() == 6);
+
+ SECTION("Main line move checks") {
+ CHECK(m->move == "g3");
+
+ m = m->MainLine;
+ CHECK(m->move == "d5");
+
+ m = m->MainLine;
+ CHECK(m->move == "Bg2");
+
+ m = m->MainLine;
+ CHECK(m->move == "Nf6");
+
+ m = m->MainLine;
+ CHECK(m->move == "c4");
+
+ m = m->MainLine;
+ CHECK(m->move == "c6");
+ }
+
+ SECTION("Main line color checks") {
+ m = pgn.GetMoves();
+ CHECK_FALSE(m->isBlack);
+
+ m = m->MainLine;
+ CHECK(m->isBlack);
+
+ m = m->MainLine;
+ CHECK_FALSE(m->isBlack);
+
+ m = m->MainLine;
+ CHECK(m->isBlack);
+ }
+
+ SECTION("Tag exists checks") {
+ CHECK(pgn.HasTag("WhiteElo"));
+ CHECK_FALSE(pgn.HasTag("Round"));
+ CHECK(pgn.HasTag("TimeControl"));
+ }
+
+ SECTION("Tag values checks") {
+ CHECK(pgn.GetTagValue("WhiteElo") == "1830");
+ CHECK(pgn.GetTagValue("TimeControl") == "600+5");
+ CHECK_THROWS_AS(pgn.GetTagValue("InvalidTagName"), InvalidTagName);
+ }
}
TEST_CASE("Valid PGN", "[pgn2]") {