Allow whitespace in expressions

This commit is contained in:
2024-10-24 21:20:57 +01:00
parent 9f54c3552b
commit e5cd69c26b
2 changed files with 96 additions and 3 deletions

View File

@@ -180,6 +180,60 @@ static void valid_expression_followed_by_garbage_yeilds_null(void)
ASSERT_NULL(result);
}
static void sum_with_single_spaces_around_plus_is_parsed_successfully(void)
{
init_memory_pool(&pool);
const expression_t *result = read_expression(&pool, "1 + 2", 5);
ASSERT_NOT_NULL(result);
ASSERT_FALSE(result->is_number);
ASSERT_EQUAL(OPERATOR_ADD, result->application.operator);
}
static void sum_with_multiple_spaces_around_plus_is_parsed_successfully(void)
{
init_memory_pool(&pool);
const expression_t *result = read_expression(&pool, "1 + 2", 7);
ASSERT_NOT_NULL(result);
ASSERT_FALSE(result->is_number);
ASSERT_EQUAL(OPERATOR_ADD, result->application.operator);
}
static void product_with_spaces_around_times_is_parsed_successfully(void)
{
init_memory_pool(&pool);
const expression_t *result = read_expression(&pool, "1 * 2", 6);
ASSERT_NOT_NULL(result);
ASSERT_FALSE(result->is_number);
ASSERT_EQUAL(OPERATOR_MULTIPLY, result->application.operator);
}
static void spaces_inside_parens_are_ignored(void)
{
init_memory_pool(&pool);
const expression_t *result = read_expression(&pool, "( 14 )", 6);
ASSERT_NOT_NULL(result);
ASSERT_TRUE(result->is_number);
ASSERT_EQUAL(14, result->number);
}
static void leading_spaces_are_ignored(void)
{
init_memory_pool(&pool);
const expression_t *result = read_expression(&pool, " 14", 5);
ASSERT_NOT_NULL(result);
ASSERT_TRUE(result->is_number);
ASSERT_EQUAL(14, result->number);
}
static void trailing_spaces_are_ignored(void)
{
init_memory_pool(&pool);
const expression_t *result = read_expression(&pool, "14 ", 5);
ASSERT_NOT_NULL(result);
ASSERT_TRUE(result->is_number);
ASSERT_EQUAL(14, result->number);
}
int main(void)
{
TESTING_BEGIN();
@@ -196,5 +250,11 @@ int main(void)
RUN_TEST(paren_1_plus_2_times_3_close_paren_parses_as_left_nested);
RUN_TEST(garbage_input_yeilds_null);
RUN_TEST(valid_expression_followed_by_garbage_yeilds_null);
RUN_TEST(sum_with_single_spaces_around_plus_is_parsed_successfully);
RUN_TEST(sum_with_multiple_spaces_around_plus_is_parsed_successfully);
RUN_TEST(product_with_spaces_around_times_is_parsed_successfully);
RUN_TEST(spaces_inside_parens_are_ignored);
RUN_TEST(leading_spaces_are_ignored);
RUN_TEST(trailing_spaces_are_ignored);
TESTING_END();
}