From dfda9d3bd61b95bf6f5686e18c97b47869e80444 Mon Sep 17 00:00:00 2001 From: "Jerome M. BERGER" Date: Tue, 3 Apr 2018 10:07:54 +0200 Subject: [PATCH] Add tests for `mpc_check` and `mpc_check_with`. --- tests/combinators.c | 51 +++++++++++++++++++++++++++++++++++++++++++++ tests/test.c | 2 ++ 2 files changed, 53 insertions(+) create mode 100644 tests/combinators.c diff --git a/tests/combinators.c b/tests/combinators.c new file mode 100644 index 0000000..871e89e --- /dev/null +++ b/tests/combinators.c @@ -0,0 +1,51 @@ +#include "ptest.h" +#include "../mpc.h" + +static int check_is_a(mpc_val_t** x) { + return strcmp(*x, "a") == 0; +} + +static int check_is(mpc_val_t** x, void* t) { + return strcmp(*x, t) == 0; +} + +void test_check(void) { + int success; + mpc_result_t r; + mpc_parser_t* p = mpc_check(mpc_or(2, mpc_char('a'), mpc_char('b')), check_is_a, "Expected '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_with(void) { + int success; + mpc_result_t r; + mpc_parser_t* p = mpc_check_with(mpc_or(2, mpc_char('a'), mpc_char('b')), check_is, "a", "Expected '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"); +} diff --git a/tests/test.c b/tests/test.c index 4a2cc62..1b78000 100644 --- a/tests/test.c +++ b/tests/test.c @@ -3,12 +3,14 @@ void suite_core(void); void suite_regex(void); void suite_grammar(void); +void suite_combinators(void); int main(int argc, char** argv) { (void) argc; (void) argv; pt_add_suite(suite_core); pt_add_suite(suite_regex); pt_add_suite(suite_grammar); + pt_add_suite(suite_combinators); return pt_run(); }