Add mpc_checkf and mpc_check_withf combinators.

This commit is contained in:
Jerome M. BERGER
2018-04-03 14:53:13 +02:00
parent dfda9d3bd6
commit a63bedc74a
4 changed files with 72 additions and 0 deletions

View File

@@ -45,6 +45,42 @@ void test_check_with(void) {
mpc_delete(p);
}
void test_checkf(void) {
int success;
mpc_result_t r;
mpc_parser_t* p = mpc_checkf(mpc_or(2, mpc_char('a'), mpc_char('b')), check_is_a, "Expected '%s'", "a");
success = mpc_parse("test", "a", p, &r);
PT_ASSERT(success);
PT_ASSERT_STR_EQ(r.output, "a");
if (success) free(r.output); else mpc_err_delete(r.error);
success = mpc_parse("test", "b", p, &r);
PT_ASSERT(!success);
PT_ASSERT_STR_EQ(r.error->failure, "Expected 'a'");
if (success) free(r.output); else mpc_err_delete(r.error);
mpc_delete(p);
}
void test_check_withf(void) {
int success;
mpc_result_t r;
mpc_parser_t* p = mpc_check_withf(mpc_or(2, mpc_char('a'), mpc_char('b')), check_is, "a", "Expected '%s'", "a");
success = mpc_parse("test", "a", p, &r);
PT_ASSERT(success);
if (success) PT_ASSERT_STR_EQ(r.output, "a");
if (success) free(r.output); else mpc_err_delete(r.error);
success = mpc_parse("test", "b", p, &r);
PT_ASSERT(!success);
if (!success) PT_ASSERT_STR_EQ(r.error->failure, "Expected 'a'");
if (success) free(r.output); else mpc_err_delete(r.error);
mpc_delete(p);
}
void suite_combinators(void) {
pt_add_test(test_check, "Test Check", "Suite Combinators");
pt_add_test(test_check_with, "Test Check with", "Suite Combinators");