Skip to content

Commit

Permalink
New tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
graninas committed Apr 13, 2019
1 parent 1fda393 commit fd620c7
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 41 deletions.
14 changes: 9 additions & 5 deletions cpp_parsec/ps/free/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ ParserT<Many<A>> parseMany(const ParserL<A>& p)
{
ParserT<ParserResult<A>> pt = safeP(p);

return parseMany(pt);
return parseMany<A>(pt);
}

template <typename A>
Expand Down Expand Up @@ -388,23 +388,27 @@ const auto lit = [](const std::string& s) {
return evalP<std::string>(litThrowPL(s));
};

const ParserT<Many<Char>> spaces = parseMany<Char>(spaceThrowPL);
//const ParserT<Many<Char>> spaces = manyPL<Char>(spaceThrowPL);
ParserT<Many<Char>> spaces()
{
return manyPL<Char>(spaceThrowPL);
}

// dummy

ParserT<std::string> parseString()
{
throw std::runtime_error("parseString is not implemented yet.");

}

ParserT<int> parseInt()
{
throw std::runtime_error("parseInt is not implemented yet.");

}

ParserT<double> parseDouble()
{
throw std::runtime_error("parseDouble is not implemented yet.");

}

const ParserT<std::string> strP = parseString();
Expand Down
105 changes: 69 additions & 36 deletions cpp_parsecTest/tst_parsec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ private Q_SLOTS:
void applicativeTest();
void sequencedParsersTest();
void forgetCombinatorsTest();
void seqCombinatorsTest();
void binarySeqCombinatorsTest();

void alt1Test();
void alt2Test();
Expand Down Expand Up @@ -86,7 +88,7 @@ void PSTest::litParserTest()

QVERIFY(isRight(result));
std::string r = getParsed<std::string>(result);
QVERIFY(r == "abc");
QVERIFY(r == "str");
}

void PSTest::lowerCaseCharParserTest()
Expand Down Expand Up @@ -313,6 +315,38 @@ void PSTest::forgetCombinatorsTest()
QVERIFY(getParsed<Char>(result2) == 'a');
}

void PSTest::seqCombinatorsTest()
{
using namespace ps;

ParserResult<Char> result1 = parse(seq(upper, lower), "Aa");
ParserResult<Char> result2 = parse(seq(upper, lower, digit), "Aa1");
ParserResult<Char> result3 = parse(seq(upper, lower, digit, symbol('!')), "Aa1!");

QVERIFY(isRight(result1));
QVERIFY(isRight(result2));
QVERIFY(isRight(result3));
QVERIFY(getParsed<Char>(result1) == 'a');
QVERIFY(getParsed<Char>(result2) == '1');
QVERIFY(getParsed<Char>(result3) == '!');
}

void PSTest::binarySeqCombinatorsTest()
{
using namespace ps;

ParserResult<Char> result1 = parse(upper >> lower, "Aa");
ParserResult<Char> result2 = parse(upper >> lower >> digit, "Aa1");
ParserResult<Char> result3 = parse(upper >> (lower << digit), "Aa1");

QVERIFY(isRight(result1));
QVERIFY(isRight(result2));
QVERIFY(isRight(result3));
QVERIFY(getParsed<Char>(result1) == 'a');
QVERIFY(getParsed<Char>(result2) == '1');
QVERIFY(getParsed<Char>(result3) == 'a');
}

void PSTest::alt1Test()
{
using namespace ps;
Expand Down Expand Up @@ -367,47 +401,46 @@ void PSTest::internalParsersTest()
struct Employee
{
int age;
std::string surname;
std::string forename;
std::string firstName;
std::string lastName;
double salary;
};

void PSTest::employeeTest()
{
using namespace ps;

auto mkEmployee = [](
int a, const std::string& sn, const std::string& fn, double s) {
return Employee {a, sn, fn, s};
};
// using namespace ps;

auto quotedString = between(symbol('"'), strP);
auto prefix = lit("employee") >> between(spaces, symbol('{'));
auto postfix = between(spaces, symbol('}'));
auto comma = between(spaces, symbol(','));

auto age = intP;
auto firstName = quotedString;
auto lastName = quotedString;
auto salary = doubleP;

ParserT<Employee> employeeParser =
app<Employee, int, std::string, std::string, double>(
mkEmployee,
prefix >> intP, // age
comma >> quotedString, // first name
comma >> quotedString, // last name
comma >> (doubleP << postfix)); // salary

auto s = "employee {35, “Jane”, “Street”, 50000.0}";
ParserResult<Employee> result = parse(employeeParser, s);

if (isLeft(result)) {
std::cout << "Parse error: " << getError(result).message;
}
else {
Employee employee = getParsed(result);
}
// auto mkEmployee = [](
// int a, const std::string& sn, const std::string& fn, double s) {
// return Employee {a, sn, fn, s};
// };

// auto quotedString = between(symbol('"'), strP);
// auto prefix = lit("employee") >> between(spaces(), symbol('{'));
// auto postfix = between(spaces(), symbol('}'));
// auto comma = between(spaces(), symbol(','));

// ParserT<Employee> employeeParser =
// app<Employee, int, std::string, std::string, double>(
// mkEmployee,
// prefix >> intP, // age
// comma >> quotedString, // first name
// comma >> quotedString, // last name
// comma >> (doubleP << postfix)); // salary

// auto s = "employee {35, “Jane”, “Street”, 50000.0}";
// ParserResult<Employee> result = parse(employeeParser, s);

// if (isLeft(result)) {
// std::cout << "Parse error: " << getError(result).message;
// }
// else {
// Employee employee = getParsed(result);
// QVERIFY(employee.age == 35);
// QVERIFY(employee.firstName == "Jane");
// QVERIFY(employee.lastName == "Street");
// QVERIFY(employee.salary >= 50000.0 && employee.salary <= 50000.1);
// }
}


Expand Down

0 comments on commit fd620c7

Please sign in to comment.