Extract line reading logic to reader and remove REPL tests

Reading a line into the buffer was the only logic complex enough to be
worth testing really, and exposing the necessary parts of it to test
it effectively was a pain.  Makes more sense to move read_line out and
throw away most of the tests.
This commit is contained in:
2024-10-24 21:46:59 +01:00
parent ecd50c76c5
commit acb4cd38d7
9 changed files with 81 additions and 192 deletions

View File

@@ -1,8 +1,21 @@
#include "reader.h"
#include "testing.h"
#include <stdio.h>
static memory_pool_t pool;
static char *test_input;
static int test_input_len;
static int test_get_byte(void)
{
if (test_input_len < 0)
return EOF;
--test_input_len;
return *test_input++;
}
static void input_1234_is_read_as_number_with_expected_value(void)
{
init_memory_pool(&pool);
@@ -234,6 +247,41 @@ static void trailing_spaces_are_ignored(void)
ASSERT_EQUAL(14, result->number);
}
static void read_line_copies_line_into_buffer(void)
{
test_input = "foobar\nbazquux";
test_input_len = strlen(test_input);
char buffer[10];
read_line(test_get_byte, buffer, sizeof(buffer));
ASSERT_MEM_EQUAL("foobar", buffer, 6);
}
static void read_line_returns_length_of_line(void)
{
test_input = "foobar\nbazquux";
test_input_len = strlen(test_input);
char buffer[10];
const int len = read_line(test_get_byte, buffer, sizeof(buffer));
ASSERT_EQUAL(6, len);
}
static void read_line_returns_minus_1_on_eof(void)
{
test_input_len = 0;
char buffer[10];
const int len = read_line(test_get_byte, buffer, sizeof(buffer));
ASSERT_EQUAL(-1, len);
}
static void read_line_returns_buffer_size_if_no_newline(void)
{
test_input = "foobarbazquux";
test_input_len = strlen(test_input);
char buffer[10];
const int len = read_line(test_get_byte, buffer, sizeof(buffer));
ASSERT_EQUAL(10, len);
}
int main(void)
{
TESTING_BEGIN();
@@ -256,5 +304,9 @@ int main(void)
RUN_TEST(spaces_inside_parens_are_ignored);
RUN_TEST(leading_spaces_are_ignored);
RUN_TEST(trailing_spaces_are_ignored);
RUN_TEST(read_line_copies_line_into_buffer);
RUN_TEST(read_line_returns_length_of_line);
RUN_TEST(read_line_returns_minus_1_on_eof);
RUN_TEST(read_line_returns_buffer_size_if_no_newline);
TESTING_END();
}