Add tests for mpc_check and mpc_check_with.
				
					
				
			This commit is contained in:
		
							
								
								
									
										51
									
								
								tests/combinators.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										51
									
								
								tests/combinators.c
									
									
									
									
									
										Normal 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"); | ||||
| } | ||||
| @@ -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(); | ||||
| } | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Jerome M. BERGER
					Jerome M. BERGER