146 lines
3.0 KiB
C
146 lines
3.0 KiB
C
#include "repl.h"
|
|
#include "testing.h"
|
|
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
|
|
static const char *input;
|
|
static int input_len;
|
|
|
|
static bool read_called;
|
|
static const char *read_input;
|
|
static int read_len;
|
|
static const expression_t *read_result;
|
|
|
|
static bool evaluate_called;
|
|
static const expression_t *evaluate_expression;
|
|
static int evaluate_result;
|
|
|
|
static bool print_called;
|
|
static const char *print_output;
|
|
static int print_len;
|
|
|
|
static void reset_fixtures(void)
|
|
{
|
|
read_called = false;
|
|
evaluate_called = false;
|
|
print_called = false;
|
|
}
|
|
|
|
static int test_get_byte(void)
|
|
{
|
|
if (0 == input_len)
|
|
return EOF;
|
|
--input_len;
|
|
return *input++;
|
|
}
|
|
|
|
static const expression_t *
|
|
test_read(memory_pool_t *pool, const char *input, int len)
|
|
{
|
|
(void)pool;
|
|
read_called = true;
|
|
read_input = input;
|
|
read_len = len;
|
|
return read_result;
|
|
}
|
|
|
|
static int test_evaluate(const expression_t *expression)
|
|
{
|
|
evaluate_called = true;
|
|
evaluate_expression = expression;
|
|
return evaluate_result;
|
|
}
|
|
|
|
static void test_print(const char *output, int len)
|
|
{
|
|
print_called = true;
|
|
print_output = output;
|
|
print_len = len;
|
|
}
|
|
|
|
static repl_t repl = {
|
|
.get_byte = test_get_byte,
|
|
.read = test_read,
|
|
.evaluate = test_evaluate,
|
|
.print = test_print,
|
|
};
|
|
|
|
static void set_up_valid_state(void)
|
|
{
|
|
static const expression_t expression
|
|
= { .is_number = true, .number = 1234 };
|
|
input = "foobar\nbarquux";
|
|
input_len = 14;
|
|
read_result = &expression;
|
|
evaluate_result = 4321;
|
|
}
|
|
|
|
static void read_is_called_on_first_line_line_in_input(void)
|
|
{
|
|
reset_fixtures();
|
|
set_up_valid_state();
|
|
step_repl(&repl);
|
|
ASSERT_TRUE(read_called);
|
|
ASSERT_EQUAL(6, read_len);
|
|
ASSERT_MEM_EQUAL("foobar", read_input, 6);
|
|
}
|
|
|
|
static void read_result_is_passed_to_evaluate(void)
|
|
{
|
|
reset_fixtures();
|
|
set_up_valid_state();
|
|
step_repl(&repl);
|
|
ASSERT_TRUE(evaluate_called);
|
|
ASSERT_TRUE(evaluate_expression->is_number);
|
|
ASSERT_EQUAL(1234, evaluate_expression->number);
|
|
}
|
|
|
|
static void result_of_evaluation_is_printed_with_a_newline(void)
|
|
{
|
|
reset_fixtures();
|
|
set_up_valid_state();
|
|
step_repl(&repl);
|
|
ASSERT_TRUE(print_called);
|
|
ASSERT_EQUAL(5, print_len);
|
|
ASSERT_MEM_EQUAL("4321\n", print_output, 5);
|
|
}
|
|
|
|
static void true_is_returned_on_successful_step(void)
|
|
{
|
|
reset_fixtures();
|
|
set_up_valid_state();
|
|
const bool result = step_repl(&repl);
|
|
ASSERT_TRUE(result);
|
|
}
|
|
|
|
static void false_is_returned_on_end_of_input(void)
|
|
{
|
|
reset_fixtures();
|
|
set_up_valid_state();
|
|
input_len = 0;
|
|
const bool result = step_repl(&repl);
|
|
ASSERT_FALSE(result);
|
|
}
|
|
|
|
static void evaluate_is_not_called_if_read_returns_null(void)
|
|
{
|
|
reset_fixtures();
|
|
set_up_valid_state();
|
|
read_result = NULL;
|
|
step_repl(&repl);
|
|
ASSERT_FALSE(evaluate_called);
|
|
}
|
|
|
|
int main(void)
|
|
{
|
|
TESTING_BEGIN();
|
|
RUN_TEST(read_is_called_on_first_line_line_in_input);
|
|
RUN_TEST(read_result_is_passed_to_evaluate);
|
|
RUN_TEST(result_of_evaluation_is_printed_with_a_newline);
|
|
RUN_TEST(true_is_returned_on_successful_step);
|
|
RUN_TEST(false_is_returned_on_end_of_input);
|
|
RUN_TEST(evaluate_is_not_called_if_read_returns_null);
|
|
TESTING_END();
|
|
}
|