2016-05-22 20:19:45 +02:00
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
2016-05-27 03:58:59 +02:00
|
|
|
#include <plasp/utils/IO.h>
|
|
|
|
#include <plasp/utils/Parser.h>
|
|
|
|
#include <plasp/utils/ParserException.h>
|
2016-05-22 20:19:45 +02:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-08-08 12:40:02 +02:00
|
|
|
TEST(UtilsTests, ParserParse)
|
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");
|
2016-08-02 19:58:54 +02:00
|
|
|
plasp::utils::Parser<> p("input", s);
|
2016-05-27 03:58:59 +02:00
|
|
|
|
|
|
|
ASSERT_EQ(p.parse<std::string>(), "identifier");
|
|
|
|
ASSERT_EQ(p.parse<size_t>(), 5u);
|
|
|
|
ASSERT_EQ(p.parse<int>(), -51);
|
|
|
|
ASSERT_EQ(p.parse<bool>(), false);
|
|
|
|
ASSERT_EQ(p.parse<bool>(), true);
|
|
|
|
|
2016-08-07 16:46:48 +02:00
|
|
|
ASSERT_EQ(p.parse<int>(), 100);
|
|
|
|
ASSERT_EQ(p.parse<size_t>(), 200u);
|
|
|
|
ASSERT_EQ(p.parse<int>(), -300);
|
|
|
|
ASSERT_THROW(p.parse<size_t>(), plasp::utils::ParserException);
|
2016-05-27 03:58:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-08-07 16:46:48 +02:00
|
|
|
TEST(UtilsTests, ParserExpect)
|
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");
|
2016-08-02 19:58:54 +02:00
|
|
|
plasp::utils::Parser<> p("input", s);
|
2016-05-27 03:58:59 +02:00
|
|
|
|
2016-08-07 16:46:48 +02:00
|
|
|
ASSERT_NO_THROW(p.expect<std::string>("identifier"));
|
|
|
|
ASSERT_NO_THROW(p.expect<size_t>(5u));
|
|
|
|
ASSERT_NO_THROW(p.expect<int>(-51));
|
|
|
|
ASSERT_NO_THROW(p.expect<bool>(false));
|
|
|
|
ASSERT_NO_THROW(p.expect<bool>(true));
|
|
|
|
|
|
|
|
ASSERT_NO_THROW(p.expect<int>(100));
|
|
|
|
ASSERT_NO_THROW(p.expect<size_t>(200u));
|
|
|
|
ASSERT_NO_THROW(p.expect<int>(-300));
|
|
|
|
ASSERT_THROW(p.expect<size_t>(-400), plasp::utils::ParserException);
|
|
|
|
|
|
|
|
p.seek(0);
|
|
|
|
ASSERT_THROW(p.expect<std::string>("error"), plasp::utils::ParserException);
|
|
|
|
|
|
|
|
p.seek(14);
|
|
|
|
ASSERT_THROW(p.expect<size_t>(6u), plasp::utils::ParserException);
|
|
|
|
|
|
|
|
p.seek(17);
|
|
|
|
ASSERT_THROW(p.expect<int>(-50), plasp::utils::ParserException);
|
|
|
|
|
|
|
|
p.seek(24);
|
|
|
|
ASSERT_THROW(p.expect<bool>(true), plasp::utils::ParserException);
|
|
|
|
|
|
|
|
p.seek(26);
|
|
|
|
ASSERT_THROW(p.expect<bool>(false), plasp::utils::ParserException);
|
|
|
|
|
|
|
|
p.seek(28);
|
|
|
|
ASSERT_THROW(p.expect<int>(101), plasp::utils::ParserException);
|
|
|
|
|
|
|
|
p.seek(31);
|
|
|
|
ASSERT_THROW(p.expect<size_t>(201), plasp::utils::ParserException);
|
|
|
|
|
|
|
|
p.seek(34);
|
|
|
|
ASSERT_THROW(p.expect<int>(-299), plasp::utils::ParserException);
|
2016-05-27 03:58:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-08-08 12:40:02 +02:00
|
|
|
TEST(UtilsTests, ParserTest)
|
|
|
|
{
|
|
|
|
std::stringstream s(" identifier 5 \n-51\t 0 1");
|
|
|
|
plasp::utils::Parser<> p("input", s);
|
|
|
|
|
|
|
|
plasp::utils::Parser<>::Position pos;
|
|
|
|
|
|
|
|
pos = p.position();
|
|
|
|
ASSERT_EQ(p.testAndReturn<std::string>("error"), false);
|
|
|
|
ASSERT_EQ(p.position(), pos);
|
|
|
|
ASSERT_EQ(p.testAndReturn<std::string>("identifier"), true);
|
|
|
|
ASSERT_EQ(p.position(), pos);
|
|
|
|
ASSERT_EQ(p.testAndSkip<std::string>("error"), false);
|
|
|
|
ASSERT_EQ(p.position(), pos);
|
|
|
|
ASSERT_EQ(p.testAndSkip<std::string>("identifier"), true);
|
|
|
|
ASSERT_EQ(p.position(), 12);
|
|
|
|
|
|
|
|
pos = p.position();
|
|
|
|
ASSERT_EQ(p.testAndReturn<size_t>(6u), false);
|
|
|
|
ASSERT_EQ(p.position(), pos);
|
|
|
|
ASSERT_EQ(p.testAndReturn<size_t>(5u), true);
|
|
|
|
ASSERT_EQ(p.position(), pos);
|
|
|
|
ASSERT_EQ(p.testAndSkip<size_t>(6u), false);
|
|
|
|
ASSERT_EQ(p.position(), pos);
|
|
|
|
ASSERT_EQ(p.testAndSkip<size_t>(5u), true);
|
|
|
|
ASSERT_EQ(p.position(), 15);
|
|
|
|
|
|
|
|
pos = p.position();
|
|
|
|
ASSERT_EQ(p.testAndReturn<int>(-50), false);
|
|
|
|
ASSERT_EQ(p.position(), pos);
|
|
|
|
ASSERT_EQ(p.testAndReturn<int>(-51), true);
|
|
|
|
ASSERT_EQ(p.position(), pos);
|
|
|
|
ASSERT_EQ(p.testAndSkip<int>(-50), false);
|
|
|
|
ASSERT_EQ(p.position(), pos);
|
|
|
|
ASSERT_EQ(p.testAndSkip<int>(-51), true);
|
|
|
|
ASSERT_EQ(p.position(), 22);
|
|
|
|
|
|
|
|
pos = p.position();
|
|
|
|
ASSERT_EQ(p.testAndReturn<bool>(true), false);
|
|
|
|
ASSERT_EQ(p.position(), pos);
|
|
|
|
ASSERT_EQ(p.testAndReturn<bool>(false), true);
|
|
|
|
ASSERT_EQ(p.position(), pos);
|
|
|
|
ASSERT_EQ(p.testAndSkip<bool>(true), false);
|
|
|
|
ASSERT_EQ(p.position(), pos);
|
|
|
|
ASSERT_EQ(p.testAndSkip<bool>(false), true);
|
|
|
|
ASSERT_EQ(p.position(), 25);
|
|
|
|
|
|
|
|
pos = p.position();
|
|
|
|
ASSERT_EQ(p.testAndReturn<bool>(false), false);
|
|
|
|
ASSERT_EQ(p.position(), pos);
|
|
|
|
ASSERT_EQ(p.testAndReturn<bool>(true), true);
|
|
|
|
ASSERT_EQ(p.position(), pos);
|
|
|
|
ASSERT_EQ(p.testAndSkip<bool>(false), false);
|
|
|
|
ASSERT_EQ(p.position(), pos);
|
|
|
|
ASSERT_EQ(p.testAndSkip<bool>(true), true);
|
|
|
|
ASSERT_EQ(p.position(), 27);
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-05-27 03:58:59 +02:00
|
|
|
TEST(UtilsTests, ParseEndOfFile)
|
|
|
|
{
|
|
|
|
std::stringstream s1("test");
|
2016-08-02 19:58:54 +02:00
|
|
|
plasp::utils::Parser<> p1("input", s1);
|
2016-05-27 03:58:59 +02:00
|
|
|
|
|
|
|
ASSERT_NO_THROW(p1.expect<std::string>("test"));
|
|
|
|
ASSERT_THROW(p1.parse<std::string>(), plasp::utils::ParserException);
|
|
|
|
|
|
|
|
std::stringstream s2("test1 test2 test3");
|
2016-08-02 19:58:54 +02:00
|
|
|
plasp::utils::Parser<> p2("input", s2);
|
2016-05-27 03:58:59 +02:00
|
|
|
|
|
|
|
ASSERT_NO_THROW(p2.expect<std::string>("test1"));
|
|
|
|
ASSERT_NO_THROW(p2.expect<std::string>("test2"));
|
|
|
|
ASSERT_NO_THROW(p2.expect<std::string>("test3"));
|
|
|
|
ASSERT_THROW(p2.parse<std::string>(), plasp::utils::ParserException);
|
|
|
|
|
|
|
|
std::stringstream s3("-127");
|
2016-08-02 19:58:54 +02:00
|
|
|
plasp::utils::Parser<> p3("input", s3);
|
2016-05-27 03:58:59 +02:00
|
|
|
|
|
|
|
p3.expect<int>(-127);
|
|
|
|
ASSERT_THROW(p3.parse<int>(), plasp::utils::ParserException);
|
|
|
|
|
|
|
|
std::stringstream s4("128 -1023 -4095");
|
2016-08-02 19:58:54 +02:00
|
|
|
plasp::utils::Parser<> p4("input", s4);
|
2016-05-27 03:58:59 +02:00
|
|
|
|
|
|
|
ASSERT_NO_THROW(p4.expect<size_t>(128));
|
|
|
|
ASSERT_NO_THROW(p4.expect<int>(-1023));
|
|
|
|
ASSERT_NO_THROW(p4.expect<int>(-4095));
|
|
|
|
ASSERT_THROW(p4.parse<int>(), plasp::utils::ParserException);
|
|
|
|
|
|
|
|
std::stringstream s5("0");
|
2016-08-02 19:58:54 +02:00
|
|
|
plasp::utils::Parser<> p5("input", s5);
|
2016-05-27 03:58:59 +02:00
|
|
|
|
|
|
|
p5.expect<bool>(false);
|
|
|
|
ASSERT_THROW(p5.parse<bool>(), plasp::utils::ParserException);
|
|
|
|
|
|
|
|
std::stringstream s6("0 1 0");
|
2016-08-02 19:58:54 +02:00
|
|
|
plasp::utils::Parser<> p6("input", s6);
|
2016-05-27 03:58:59 +02:00
|
|
|
|
|
|
|
ASSERT_NO_THROW(p6.expect<bool>(false));
|
|
|
|
ASSERT_NO_THROW(p6.expect<bool>(true));
|
|
|
|
ASSERT_NO_THROW(p6.expect<bool>(false));
|
|
|
|
ASSERT_THROW(p6.parse<bool>(), plasp::utils::ParserException);
|
2016-05-24 02:23:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-05-30 12:32:13 +02:00
|
|
|
TEST(UtilsTests, ParserPosition)
|
|
|
|
{
|
2016-05-30 13:00:55 +02:00
|
|
|
std::stringstream s("123 \n4\ntest1\n test2\ntest3 \ntest4\n\n\n\n");
|
2016-08-02 19:58:54 +02:00
|
|
|
plasp::utils::Parser<> p("input", s);
|
2016-06-07 18:41:01 +02:00
|
|
|
|
2016-06-22 10:02:46 +02:00
|
|
|
const auto startPosition = p.position();
|
|
|
|
|
2016-08-02 19:58:54 +02:00
|
|
|
plasp::utils::StreamCoordinate c;
|
2016-05-30 12:32:13 +02:00
|
|
|
|
2016-06-07 18:41:01 +02:00
|
|
|
c = p.coordinate();
|
|
|
|
ASSERT_EQ(c.row, 1u);
|
|
|
|
ASSERT_EQ(c.column, 1u);
|
2016-05-30 12:32:13 +02:00
|
|
|
ASSERT_EQ(p.currentCharacter(), '1');
|
|
|
|
|
2016-05-30 12:58:10 +02:00
|
|
|
ASSERT_NO_THROW(p.advance());
|
2016-05-30 12:32:13 +02:00
|
|
|
|
2016-06-07 18:41:01 +02:00
|
|
|
c = p.coordinate();
|
|
|
|
ASSERT_EQ(c.row, 1u);
|
|
|
|
ASSERT_EQ(c.column, 2u);
|
2016-05-30 12:32:13 +02:00
|
|
|
ASSERT_EQ(p.currentCharacter(), '2');
|
|
|
|
|
2016-05-30 12:58:10 +02:00
|
|
|
ASSERT_NO_THROW(p.advance());
|
2016-05-30 12:32:13 +02:00
|
|
|
|
2016-06-07 18:41:01 +02:00
|
|
|
c = p.coordinate();
|
|
|
|
ASSERT_EQ(c.row, 1u);
|
|
|
|
ASSERT_EQ(c.column, 3u);
|
2016-05-30 12:32:13 +02:00
|
|
|
ASSERT_EQ(p.currentCharacter(), '3');
|
|
|
|
|
2016-05-30 12:58:10 +02:00
|
|
|
ASSERT_NO_THROW(p.advance());
|
2016-05-30 12:32:13 +02:00
|
|
|
|
2016-06-07 18:41:01 +02:00
|
|
|
c = p.coordinate();
|
|
|
|
ASSERT_EQ(c.row, 1u);
|
|
|
|
ASSERT_EQ(c.column, 4u);
|
2016-05-30 12:56:30 +02:00
|
|
|
ASSERT_EQ(p.currentCharacter(), ' ');
|
|
|
|
|
2016-05-30 12:58:10 +02:00
|
|
|
ASSERT_NO_THROW(p.advance());
|
2016-05-30 12:56:30 +02:00
|
|
|
|
2016-06-07 18:41:01 +02:00
|
|
|
c = p.coordinate();
|
|
|
|
ASSERT_EQ(c.row, 1u);
|
|
|
|
ASSERT_EQ(c.column, 5u);
|
2016-05-30 12:32:13 +02:00
|
|
|
ASSERT_EQ(p.currentCharacter(), '\n');
|
|
|
|
|
2016-05-30 12:58:10 +02:00
|
|
|
ASSERT_NO_THROW(p.advance());
|
2016-05-30 12:32:13 +02:00
|
|
|
|
2016-06-07 18:41:01 +02:00
|
|
|
c = p.coordinate();
|
|
|
|
ASSERT_EQ(c.row, 2u);
|
|
|
|
ASSERT_EQ(c.column, 1u);
|
2016-05-30 12:32:13 +02:00
|
|
|
ASSERT_EQ(p.currentCharacter(), '4');
|
|
|
|
|
2016-05-30 12:58:10 +02:00
|
|
|
ASSERT_NO_THROW(p.advance());
|
2016-05-30 12:32:13 +02:00
|
|
|
|
2016-05-30 12:58:10 +02:00
|
|
|
ASSERT_NO_THROW(p.expect<std::string>("test1"));
|
2016-05-30 12:32:13 +02:00
|
|
|
|
2016-06-07 18:41:01 +02:00
|
|
|
c = p.coordinate();
|
|
|
|
ASSERT_EQ(c.row, 3u);
|
|
|
|
ASSERT_EQ(c.column, 6u);
|
2016-05-30 12:32:13 +02:00
|
|
|
|
2016-05-30 12:58:10 +02:00
|
|
|
ASSERT_NO_THROW(p.expect<std::string>("test2"));
|
2016-05-30 12:32:13 +02:00
|
|
|
|
2016-06-07 18:41:01 +02:00
|
|
|
c = p.coordinate();
|
|
|
|
ASSERT_EQ(c.row, 4u);
|
|
|
|
ASSERT_EQ(c.column, 7u);
|
2016-05-30 12:32:13 +02:00
|
|
|
|
2016-05-30 12:58:10 +02:00
|
|
|
ASSERT_NO_THROW(p.expect<std::string>("test3"));
|
2016-05-30 12:32:13 +02:00
|
|
|
|
2016-06-07 18:41:01 +02:00
|
|
|
c = p.coordinate();
|
|
|
|
ASSERT_EQ(c.row, 5u);
|
|
|
|
ASSERT_EQ(c.column, 6u);
|
2016-05-30 12:32:13 +02:00
|
|
|
|
2016-05-30 13:00:55 +02:00
|
|
|
ASSERT_NO_THROW(p.skipLine());
|
|
|
|
|
2016-06-07 18:41:01 +02:00
|
|
|
c = p.coordinate();
|
|
|
|
ASSERT_EQ(c.row, 6u);
|
|
|
|
ASSERT_EQ(c.column, 1u);
|
2016-05-30 13:00:55 +02:00
|
|
|
|
|
|
|
ASSERT_NO_THROW(p.skipLine());
|
|
|
|
|
2016-06-07 18:41:01 +02:00
|
|
|
c = p.coordinate();
|
|
|
|
ASSERT_EQ(c.row, 7u);
|
|
|
|
ASSERT_EQ(c.column, 1u);
|
2016-05-30 13:00:55 +02:00
|
|
|
|
2016-05-30 12:58:10 +02:00
|
|
|
ASSERT_NO_THROW(p.skipWhiteSpace());
|
2016-05-30 12:32:13 +02:00
|
|
|
|
2016-06-07 18:41:01 +02:00
|
|
|
c = p.coordinate();
|
|
|
|
ASSERT_EQ(c.row, 10u);
|
|
|
|
ASSERT_EQ(c.column, 1u);
|
2016-08-02 19:58:54 +02:00
|
|
|
ASSERT_TRUE(p.atEnd());
|
2016-06-07 18:41:01 +02:00
|
|
|
|
2016-06-22 10:02:46 +02:00
|
|
|
p.reset();
|
|
|
|
ASSERT_EQ(p.position(), startPosition);
|
2016-08-02 19:58:54 +02:00
|
|
|
ASSERT_FALSE(p.atEnd());
|
2016-06-22 10:02:46 +02:00
|
|
|
|
2016-06-22 10:07:19 +02:00
|
|
|
for (size_t i = 0; i < 5; i++)
|
|
|
|
p.advance();
|
|
|
|
|
|
|
|
ASSERT_EQ(p.position(), static_cast<std::istream::pos_type>(5));
|
|
|
|
|
|
|
|
p.seek(static_cast<std::istream::pos_type>(7));
|
|
|
|
|
|
|
|
ASSERT_EQ(p.position(), static_cast<std::istream::pos_type>(7));
|
|
|
|
|
|
|
|
ASSERT_NO_THROW(p.expect<std::string>("test1"));
|
|
|
|
|
2016-06-07 18:41:01 +02:00
|
|
|
// TODO: test parser with multiple sections
|
2016-05-30 12:32:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-06-10 16:40:43 +02:00
|
|
|
TEST(UtilsTests, ParserRemoveComments)
|
|
|
|
{
|
|
|
|
std::stringstream s1("; comment at beginning\ntest1; comment in between\ntest2; comment at end");
|
2016-08-02 19:58:54 +02:00
|
|
|
plasp::utils::Parser<> p1("input", s1);
|
2016-06-10 16:40:43 +02:00
|
|
|
|
|
|
|
p1.removeComments(";", "\n", false);
|
|
|
|
|
2016-08-02 19:58:54 +02:00
|
|
|
plasp::utils::StreamCoordinate c;
|
2016-06-10 16:40:43 +02:00
|
|
|
|
|
|
|
ASSERT_NO_THROW(p1.expect<std::string>("test1"));
|
|
|
|
|
|
|
|
c = p1.coordinate();
|
|
|
|
ASSERT_EQ(c.row, 2u);
|
|
|
|
ASSERT_EQ(c.column, 6u);
|
|
|
|
|
|
|
|
ASSERT_NO_THROW(p1.expect<std::string>("test2"));
|
|
|
|
|
|
|
|
c = p1.coordinate();
|
|
|
|
ASSERT_EQ(c.row, 3u);
|
|
|
|
ASSERT_EQ(c.column, 6u);
|
|
|
|
|
|
|
|
p1.skipWhiteSpace();
|
|
|
|
|
2016-08-02 19:58:54 +02:00
|
|
|
ASSERT_TRUE(p1.atEnd());
|
2016-06-10 16:40:43 +02:00
|
|
|
|
|
|
|
std::stringstream s2("test;");
|
2016-08-02 19:58:54 +02:00
|
|
|
plasp::utils::Parser<> p2("input", s2);
|
2016-06-10 16:40:43 +02:00
|
|
|
|
|
|
|
p2.removeComments(";", "\n", false);
|
|
|
|
|
|
|
|
ASSERT_NO_THROW(p2.expect<std::string>("test"));
|
|
|
|
|
|
|
|
p2.skipWhiteSpace();
|
|
|
|
|
2016-08-02 19:58:54 +02:00
|
|
|
ASSERT_TRUE(p2.atEnd());
|
2016-06-10 16:40:43 +02:00
|
|
|
|
|
|
|
std::stringstream s3("/* comment at start */ test1 /* comment in between */ test2 /*");
|
2016-08-02 19:58:54 +02:00
|
|
|
plasp::utils::Parser<> p3("input", s3);
|
2016-06-10 16:40:43 +02:00
|
|
|
|
|
|
|
p3.removeComments("/*", "*/", true);
|
|
|
|
|
|
|
|
ASSERT_NO_THROW(p3.expect<std::string>("test1"));
|
|
|
|
ASSERT_NO_THROW(p3.expect<std::string>("test2"));
|
|
|
|
|
|
|
|
p3.skipWhiteSpace();
|
|
|
|
|
2016-08-02 19:58:54 +02:00
|
|
|
ASSERT_TRUE(p3.atEnd());
|
2016-06-10 16:40:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-05-22 20:19:45 +02:00
|
|
|
TEST(UtilsTests, EscapeASP)
|
|
|
|
{
|
2016-05-23 17:13:11 +02:00
|
|
|
const std::string predicate = "action(stack_on(block-1, block-2, value@3, value@4))";
|
2016-05-22 20:19:45 +02:00
|
|
|
|
|
|
|
const auto escaped = plasp::utils::escapeASP(predicate);
|
|
|
|
const auto unescaped = plasp::utils::unescapeASP(escaped);
|
|
|
|
|
|
|
|
ASSERT_EQ(escaped.find("-"), std::string::npos);
|
2016-05-23 17:13:11 +02:00
|
|
|
ASSERT_EQ(escaped.find("@"), std::string::npos);
|
2016-05-22 20:19:45 +02:00
|
|
|
ASSERT_EQ(predicate, unescaped);
|
|
|
|
}
|