This repository has been archived on 2023-07-19. You can view files and clone it, but cannot push or open issues or pull requests.
plasp/tests/TestUtils.cpp

333 lines
9.1 KiB
C++
Raw Normal View History

2016-10-08 16:03:14 +02:00
#include <catch.hpp>
2016-05-27 03:58:59 +02:00
#include <plasp/utils/Parser.h>
#include <plasp/utils/ParserException.h>
////////////////////////////////////////////////////////////////////////////////////////////////////
2016-10-08 16:03:14 +02:00
TEST_CASE("[parser] Simple strings are parsed correctly", "[parser]")
2016-05-24 02:23:56 +02:00
{
2016-08-07 16:46:48 +02:00
std::stringstream s(" identifier 5 \n-51\t 0 1 100 200 -300 -400");
plasp::utils::Parser<> p("input", s);
2016-05-27 03:58:59 +02:00
2016-10-08 16:03:14 +02:00
REQUIRE(p.parse<std::string>() == "identifier");
REQUIRE(p.parse<size_t>() == 5u);
REQUIRE(p.parse<int>() == -51);
REQUIRE(p.parse<bool>() == false);
REQUIRE(p.parse<bool>() == true);
2016-05-27 03:58:59 +02:00
2016-10-08 16:03:14 +02:00
REQUIRE(p.parse<int>() == 100);
REQUIRE(p.parse<size_t>() == 200u);
REQUIRE(p.parse<int>() == -300);
REQUIRE_THROWS_AS(p.parse<size_t>(), plasp::utils::ParserException);
2016-05-27 03:58:59 +02:00
}
////////////////////////////////////////////////////////////////////////////////////////////////////
2016-10-08 16:03:14 +02:00
TEST_CASE("[parser] Parsing exceptions are correctly reported", "[parser]")
2016-05-27 03:58:59 +02:00
{
2016-08-07 16:46:48 +02:00
std::stringstream s(" identifier 5 \n-51\t 0 1 100 200 -300 -400");
plasp::utils::Parser<> p("input", s);
2016-05-27 03:58:59 +02:00
2016-10-08 16:03:14 +02:00
REQUIRE_NOTHROW(p.expect<std::string>("identifier"));
REQUIRE_NOTHROW(p.expect<size_t>(5u));
REQUIRE_NOTHROW(p.expect<int>(-51));
REQUIRE_NOTHROW(p.expect<bool>(false));
REQUIRE_NOTHROW(p.expect<bool>(true));
2016-08-07 16:46:48 +02:00
2016-10-08 16:03:14 +02:00
REQUIRE_NOTHROW(p.expect<int>(100));
REQUIRE_NOTHROW(p.expect<size_t>(200u));
REQUIRE_NOTHROW(p.expect<int>(-300));
REQUIRE_THROWS_AS(p.expect<size_t>(-400), plasp::utils::ParserException);
2016-08-07 16:46:48 +02:00
p.seek(0);
2016-10-08 16:03:14 +02:00
REQUIRE_THROWS_AS(p.expect<std::string>("error"), plasp::utils::ParserException);
2016-08-07 16:46:48 +02:00
p.seek(14);
2016-10-08 16:03:14 +02:00
REQUIRE_THROWS_AS(p.expect<size_t>(6u), plasp::utils::ParserException);
2016-08-07 16:46:48 +02:00
p.seek(17);
2016-10-08 16:03:14 +02:00
REQUIRE_THROWS_AS(p.expect<int>(-50), plasp::utils::ParserException);
2016-08-07 16:46:48 +02:00
p.seek(24);
2016-10-08 16:03:14 +02:00
REQUIRE_THROWS_AS(p.expect<bool>(true), plasp::utils::ParserException);
2016-08-07 16:46:48 +02:00
p.seek(26);
2016-10-08 16:03:14 +02:00
REQUIRE_THROWS_AS(p.expect<bool>(false), plasp::utils::ParserException);
2016-08-07 16:46:48 +02:00
p.seek(28);
2016-10-08 16:03:14 +02:00
REQUIRE_THROWS_AS(p.expect<int>(101), plasp::utils::ParserException);
2016-08-07 16:46:48 +02:00
p.seek(31);
2016-10-08 16:03:14 +02:00
REQUIRE_THROWS_AS(p.expect<size_t>(201), plasp::utils::ParserException);
2016-08-07 16:46:48 +02:00
p.seek(34);
2016-10-08 16:03:14 +02:00
REQUIRE_THROWS_AS(p.expect<int>(-299), plasp::utils::ParserException);
2016-05-27 03:58:59 +02:00
}
////////////////////////////////////////////////////////////////////////////////////////////////////
2016-10-08 16:03:14 +02:00
TEST_CASE("[parser] While parsing, the cursor position is as expected", "[parser]")
2016-08-08 12:40:02 +02:00
{
std::stringstream s(" identifier 5 \n-51\t 0 1");
plasp::utils::Parser<> p("input", s);
plasp::utils::Parser<>::Position pos;
pos = p.position();
2016-10-08 16:03:14 +02:00
REQUIRE(p.testAndReturn<std::string>("error") == false);
REQUIRE(p.position() == pos);
REQUIRE(p.testAndReturn<std::string>("identifier") == true);
REQUIRE(p.position() == pos);
REQUIRE(p.testAndSkip<std::string>("error") == false);
REQUIRE(p.position() == pos);
REQUIRE(p.testAndSkip<std::string>("identifier") == true);
REQUIRE(p.position() == 12);
2016-08-08 12:40:02 +02:00
pos = p.position();
2016-10-08 16:03:14 +02:00
REQUIRE(p.testAndReturn<size_t>(6u) == false);
REQUIRE(p.position() == pos);
REQUIRE(p.testAndReturn<size_t>(5u) == true);
REQUIRE(p.position() == pos);
REQUIRE(p.testAndSkip<size_t>(6u) == false);
REQUIRE(p.position() == pos);
REQUIRE(p.testAndSkip<size_t>(5u) == true);
REQUIRE(p.position() == 15);
2016-08-08 12:40:02 +02:00
pos = p.position();
2016-10-08 16:03:14 +02:00
REQUIRE(p.testAndReturn<int>(-50) == false);
REQUIRE(p.position() == pos);
REQUIRE(p.testAndReturn<int>(-51) == true);
REQUIRE(p.position() == pos);
REQUIRE(p.testAndSkip<int>(-50) == false);
REQUIRE(p.position() == pos);
REQUIRE(p.testAndSkip<int>(-51) == true);
REQUIRE(p.position() == 22);
2016-08-08 12:40:02 +02:00
pos = p.position();
2016-10-08 16:03:14 +02:00
REQUIRE(p.testAndReturn<bool>(true) == false);
REQUIRE(p.position() == pos);
REQUIRE(p.testAndReturn<bool>(false) == true);
REQUIRE(p.position() == pos);
REQUIRE(p.testAndSkip<bool>(true) == false);
REQUIRE(p.position() == pos);
REQUIRE(p.testAndSkip<bool>(false) == true);
REQUIRE(p.position() == 25);
2016-08-08 12:40:02 +02:00
pos = p.position();
2016-10-08 16:03:14 +02:00
REQUIRE(p.testAndReturn<bool>(false) == false);
REQUIRE(p.position() == pos);
REQUIRE(p.testAndReturn<bool>(true) == true);
REQUIRE(p.position() == pos);
REQUIRE(p.testAndSkip<bool>(false) == false);
REQUIRE(p.position() == pos);
REQUIRE(p.testAndSkip<bool>(true) == true);
REQUIRE(p.position() == 27);
2016-08-08 12:40:02 +02:00
}
////////////////////////////////////////////////////////////////////////////////////////////////////
2016-10-08 16:03:14 +02:00
TEST_CASE("[parser] The end of the input stream is correctly handled", "[parser]")
2016-05-27 03:58:59 +02:00
{
std::stringstream s1("test");
plasp::utils::Parser<> p1("input", s1);
2016-05-27 03:58:59 +02:00
2016-10-08 16:03:14 +02:00
REQUIRE_NOTHROW(p1.expect<std::string>("test"));
REQUIRE_THROWS_AS(p1.parse<std::string>(), plasp::utils::ParserException);
2016-05-27 03:58:59 +02:00
std::stringstream s2("test1 test2 test3");
plasp::utils::Parser<> p2("input", s2);
2016-05-27 03:58:59 +02:00
2016-10-08 16:03:14 +02:00
REQUIRE_NOTHROW(p2.expect<std::string>("test1"));
REQUIRE_NOTHROW(p2.expect<std::string>("test2"));
REQUIRE_NOTHROW(p2.expect<std::string>("test3"));
REQUIRE_THROWS_AS(p2.parse<std::string>(), plasp::utils::ParserException);
2016-05-27 03:58:59 +02:00
std::stringstream s3("-127");
plasp::utils::Parser<> p3("input", s3);
2016-05-27 03:58:59 +02:00
p3.expect<int>(-127);
2016-10-08 16:03:14 +02:00
REQUIRE_THROWS_AS(p3.parse<int>(), plasp::utils::ParserException);
2016-05-27 03:58:59 +02:00
std::stringstream s4("128 -1023 -4095");
plasp::utils::Parser<> p4("input", s4);
2016-05-27 03:58:59 +02:00
2016-10-08 16:03:14 +02:00
REQUIRE_NOTHROW(p4.expect<size_t>(128));
REQUIRE_NOTHROW(p4.expect<int>(-1023));
REQUIRE_NOTHROW(p4.expect<int>(-4095));
REQUIRE_THROWS_AS(p4.parse<int>(), plasp::utils::ParserException);
2016-05-27 03:58:59 +02:00
std::stringstream s5("0");
plasp::utils::Parser<> p5("input", s5);
2016-05-27 03:58:59 +02:00
p5.expect<bool>(false);
2016-10-08 16:03:14 +02:00
REQUIRE_THROWS_AS(p5.parse<bool>(), plasp::utils::ParserException);
2016-05-27 03:58:59 +02:00
std::stringstream s6("0 1 0");
plasp::utils::Parser<> p6("input", s6);
2016-05-27 03:58:59 +02:00
2016-10-08 16:03:14 +02:00
REQUIRE_NOTHROW(p6.expect<bool>(false));
REQUIRE_NOTHROW(p6.expect<bool>(true));
REQUIRE_NOTHROW(p6.expect<bool>(false));
REQUIRE_THROWS_AS(p6.parse<bool>(), plasp::utils::ParserException);
2016-05-24 02:23:56 +02:00
}
////////////////////////////////////////////////////////////////////////////////////////////////////
2016-10-08 16:03:14 +02:00
TEST_CASE("[parser] While parsing, the cursor coordinate is as expcected", "[parser]")
{
std::stringstream s("123 \n4\ntest1\n test2\ntest3 \ntest4\n\n\n\n");
plasp::utils::Parser<> p("input", s);
2016-06-22 10:02:46 +02:00
const auto startPosition = p.position();
plasp::utils::StreamCoordinate c;
c = p.coordinate();
2016-10-08 16:03:14 +02:00
REQUIRE(c.row == 1u);
REQUIRE(c.column == 1u);
REQUIRE(p.currentCharacter() == '1');
2016-10-08 16:03:14 +02:00
REQUIRE_NOTHROW(p.advance());
c = p.coordinate();
2016-10-08 16:03:14 +02:00
REQUIRE(c.row == 1u);
REQUIRE(c.column == 2u);
REQUIRE(p.currentCharacter() == '2');
2016-10-08 16:03:14 +02:00
REQUIRE_NOTHROW(p.advance());
c = p.coordinate();
2016-10-08 16:03:14 +02:00
REQUIRE(c.row == 1u);
REQUIRE(c.column == 3u);
REQUIRE(p.currentCharacter() == '3');
2016-10-08 16:03:14 +02:00
REQUIRE_NOTHROW(p.advance());
c = p.coordinate();
2016-10-08 16:03:14 +02:00
REQUIRE(c.row == 1u);
REQUIRE(c.column == 4u);
REQUIRE(p.currentCharacter() == ' ');
2016-10-08 16:03:14 +02:00
REQUIRE_NOTHROW(p.advance());
c = p.coordinate();
2016-10-08 16:03:14 +02:00
REQUIRE(c.row == 1u);
REQUIRE(c.column == 5u);
REQUIRE(p.currentCharacter() == '\n');
2016-10-08 16:03:14 +02:00
REQUIRE_NOTHROW(p.advance());
c = p.coordinate();
2016-10-08 16:03:14 +02:00
REQUIRE(c.row == 2u);
REQUIRE(c.column == 1u);
REQUIRE(p.currentCharacter() == '4');
2016-10-08 16:03:14 +02:00
REQUIRE_NOTHROW(p.advance());
2016-10-08 16:03:14 +02:00
REQUIRE_NOTHROW(p.expect<std::string>("test1"));
c = p.coordinate();
2016-10-08 16:03:14 +02:00
REQUIRE(c.row == 3u);
REQUIRE(c.column == 6u);
2016-10-08 16:03:14 +02:00
REQUIRE_NOTHROW(p.expect<std::string>("test2"));
c = p.coordinate();
2016-10-08 16:03:14 +02:00
REQUIRE(c.row == 4u);
REQUIRE(c.column == 7u);
2016-10-08 16:03:14 +02:00
REQUIRE_NOTHROW(p.expect<std::string>("test3"));
c = p.coordinate();
2016-10-08 16:03:14 +02:00
REQUIRE(c.row == 5u);
REQUIRE(c.column == 6u);
2016-10-08 16:03:14 +02:00
REQUIRE_NOTHROW(p.skipLine());
c = p.coordinate();
2016-10-08 16:03:14 +02:00
REQUIRE(c.row == 6u);
REQUIRE(c.column == 1u);
2016-10-08 16:03:14 +02:00
REQUIRE_NOTHROW(p.skipLine());
c = p.coordinate();
2016-10-08 16:03:14 +02:00
REQUIRE(c.row == 7u);
REQUIRE(c.column == 1u);
2016-10-08 16:03:14 +02:00
REQUIRE_NOTHROW(p.skipWhiteSpace());
c = p.coordinate();
2016-10-08 16:03:14 +02:00
REQUIRE(c.row == 10u);
REQUIRE(c.column == 1u);
REQUIRE(p.atEnd());
2016-06-22 10:02:46 +02:00
p.reset();
2016-10-08 16:03:14 +02:00
REQUIRE(p.position() == startPosition);
REQUIRE_FALSE(p.atEnd());
2016-06-22 10:02:46 +02:00
for (size_t i = 0; i < 5; i++)
p.advance();
2016-10-08 16:03:14 +02:00
REQUIRE(p.position() == static_cast<std::istream::pos_type>(5));
p.seek(static_cast<std::istream::pos_type>(7));
2016-10-08 16:03:14 +02:00
REQUIRE(p.position() == static_cast<std::istream::pos_type>(7));
2016-10-08 16:03:14 +02:00
REQUIRE_NOTHROW(p.expect<std::string>("test1"));
// TODO: test parser with multiple sections
}
////////////////////////////////////////////////////////////////////////////////////////////////////
2016-10-08 16:03:14 +02:00
TEST_CASE("[parser] Comments are correctly removed", "[parser]")
{
std::stringstream s1("; comment at beginning\ntest1; comment in between\ntest2; comment at end");
plasp::utils::Parser<> p1("input", s1);
p1.removeComments(";", "\n", false);
plasp::utils::StreamCoordinate c;
2016-10-08 16:03:14 +02:00
REQUIRE_NOTHROW(p1.expect<std::string>("test1"));
c = p1.coordinate();
2016-10-08 16:03:14 +02:00
REQUIRE(c.row == 2u);
REQUIRE(c.column == 6u);
2016-10-08 16:03:14 +02:00
REQUIRE_NOTHROW(p1.expect<std::string>("test2"));
c = p1.coordinate();
2016-10-08 16:03:14 +02:00
REQUIRE(c.row == 3u);
REQUIRE(c.column == 6u);
p1.skipWhiteSpace();
2016-10-08 16:03:14 +02:00
REQUIRE(p1.atEnd());
std::stringstream s2("test;");
plasp::utils::Parser<> p2("input", s2);
p2.removeComments(";", "\n", false);
2016-10-08 16:03:14 +02:00
REQUIRE_NOTHROW(p2.expect<std::string>("test"));
p2.skipWhiteSpace();
2016-10-08 16:03:14 +02:00
REQUIRE(p2.atEnd());
std::stringstream s3("/* comment at start */ test1 /* comment in between */ test2 /*");
plasp::utils::Parser<> p3("input", s3);
p3.removeComments("/*", "*/", true);
2016-10-08 16:03:14 +02:00
REQUIRE_NOTHROW(p3.expect<std::string>("test1"));
REQUIRE_NOTHROW(p3.expect<std::string>("test2"));
p3.skipWhiteSpace();
2016-10-08 16:03:14 +02:00
REQUIRE(p3.atEnd());
}