Add tests for mpc_check and mpc_check_with.

This commit is contained in:
Jerome M. BERGER
2018-04-03 10:07:54 +02:00
parent 6ac5594c4f
commit dfda9d3bd6
2 changed files with 53 additions and 0 deletions

51
tests/combinators.c Normal file
View File

@@ -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");
}

View File

@@ -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();
}