diff options
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/justify.bats | 19 | ||||
| -rw-r--r-- | tests/test_word.c | 117 |
2 files changed, 95 insertions, 41 deletions
diff --git a/tests/justify.bats b/tests/justify.bats index a581c94..73db029 100644 --- a/tests/justify.bats +++ b/tests/justify.bats @@ -57,16 +57,15 @@ EOF assert_output "Hello world" } -# TODO: Handle this case properly -# @test "word longer than buffer is wrapped" { -# run "${JUSTIFY_BIN}" -w 60 <<EOF -# 123456789012345678901234567890123456789012345678901234567890EXTRA -# EOF -# assert_success -# expected_output="123456789012345678901234567890123456789012345678901234567890 -# EXTRA" -# assert_output "$expected_output" -# } +@test "word longer than buffer is wrapped" { + run "${JUSTIFY_BIN}" -w 60 <<EOF +123456789012345678901234567890123456789012345678901234567890EXTRA +EOF + assert_success + expected_output="123456789012345678901234567890123456789012345678901234567890 +EXTRA" + assert_output "$expected_output" +} @test "multiple spaces in input are collapsed when on last left-aligned line" { run "${JUSTIFY_BIN}" <<EOF diff --git a/tests/test_word.c b/tests/test_word.c index 2da3589..aa58a34 100644 --- a/tests/test_word.c +++ b/tests/test_word.c @@ -17,137 +17,192 @@ FILE *create_tmp_file(const char *data) { return fp; } -void test_read_word_simple(void **state) { +/** + * GIVEN the an input containing a single word + * WHEN we read a word from the file + * THEN we would expect to read in the single word + */ +void test_read_word_single_word(void **state) { const char *input = "hello"; const char *expected = "hello"; FILE *fp = create_tmp_file(input); - char word[100]; + int result = read_word(fp, word, sizeof(word)); + assert_int_equal(result, READ_WORD_SUCCESS); assert_string_equal(word, expected); - fclose(fp); } +/** + * GIVEN an input string with leading spaces + * WHEN we read a word from the file + * THEN we would expect to have discarded the leading spaces and + * read in the following word + */ void test_read_word_leading_spaces(void **state) { const char *input = " leading"; const char *expected = "leading"; FILE *fp = create_tmp_file(input); - char word[100]; + int result = read_word(fp, word, sizeof(word)); + assert_int_equal(result, READ_WORD_SUCCESS); assert_string_equal(word, expected); - fclose(fp); } +/** + * GIVEN an input string with trailing spaces + * WHEN we read a word from the file + * THEN we would expect to read in the word without + * the trailing spaces + * WHEN we read in another word + * THEN we expect to see the remaining spaces simply get + * consumed and get back EOF + */ void test_read_word_trailing_spaces(void **state) { const char *input = "trailing "; const char *expected = "trailing"; FILE *fp = create_tmp_file(input); - char word[100]; + int result = read_word(fp, word, sizeof(word)); assert_int_equal(result, READ_WORD_SUCCESS); assert_string_equal(word, expected); + result = read_word(fp, word, sizeof(word)); + assert_int_equal(result, READ_WORD_ERR_EOF); + assert_string_equal(word, ""); + fclose(fp); } +/** + * GIVEN an input string with multiple spaces between words + * WHEN we read a word from the file + * THEN we expect to read in the first word + * WHEN we read in another word from the file + * THEN we expect to skip over the multiple spaces + * and read in the last word + */ void test_read_word_multiple_spaces(void **state) { const char *input = "multiple spaces"; - const char *expected = "multiple"; FILE *fp = create_tmp_file(input); - char word[100]; + + char *expected = "multiple"; int result = read_word(fp, word, sizeof(word)); assert_int_equal(result, READ_WORD_SUCCESS); assert_string_equal(word, expected); - fclose(fp); -} - -void test_read_word_single_word(void **state) { - const char *input = "singleword"; - const char *expected = "singleword"; - FILE *fp = create_tmp_file(input); - - char word[100]; - int result = read_word(fp, word, sizeof(word)); + expected = "spaces"; + result = read_word(fp, word, sizeof(word)); assert_int_equal(result, READ_WORD_SUCCESS); - assert_string_equal(word, expected); + assert_string_equal(word, expected); fclose(fp); } +/** + * GIVEN an empty input string + * WHEN we read a word from the file + * THEN we expect to get EOF on read + */ void test_read_word_empty_input(void **state) { const char *input = ""; const char *expected = ""; FILE *fp = create_tmp_file(input); - char word[100]; + int result = read_word(fp, word, sizeof(word)); + assert_int_equal(result, READ_WORD_ERR_EOF); assert_string_equal(word, expected); - fclose(fp); } +/** + * GIVEN a NULL input + * WHEN we read a word from the file + * THEN we expect to get a read error + */ void test_read_word_null_fp(void **state) { char word[100]; int result = read_word(NULL, word, sizeof(word)); assert_int_equal(result, READ_WORD_ERR_READ); } +/** + * GIVEN a NULL word buffer + * WHEN we read a word from the file + * THEN we expect to get a read error + */ void test_read_word_null_word(void **state) { FILE *fp = tmpfile(); assert_non_null(fp); int result = read_word(fp, NULL, 100); - assert_int_equal(result, READ_WORD_ERR_READ); + assert_int_equal(result, READ_WORD_ERR_READ); fclose(fp); } +/** + * GIVEN a word buffer with a 0 length + * WHEN we read a word from the file + * THEN we expect to get EOF + */ void test_read_word_zero_length(void **state) { FILE *fp = tmpfile(); assert_non_null(fp); - char word[100]; + int result = read_word(fp, word, 0); - assert_int_equal(result, READ_WORD_ERR_EOF); + assert_int_equal(result, READ_WORD_ERR_EOF); fclose(fp); } -void test_read_word_buffer_overflow(void **state) { +/** + * GIVEN a word that is too long to fit into the word buffer + * WHEN we read a word from the file + * THEN we expect to get the partial word back + * WHEN we read in another word + * THEN we expect to get the remainder of the word + */ +void test_read_word_overflow(void **state) { const char *input = "thisisaverylongword"; - const char *expected = "thisisaver"; FILE *fp = create_tmp_file(input); + char word[11]; // Buffer size is 11 to include null terminator - char word[11]; // Buffer size is 11 including null terminator + char *expected = "thisisaver"; int result = read_word(fp, word, sizeof(word)); assert_int_equal(result, READ_WORD_SUCCESS); assert_string_equal(word, expected); + expected = "ylongword"; + result = read_word(fp, word, sizeof(word)); + assert_int_equal(result, READ_WORD_SUCCESS); + assert_string_equal(word, expected); + fclose(fp); } int main(void) { const struct CMUnitTest tests[] = { - cmocka_unit_test(test_read_word_simple), + cmocka_unit_test(test_read_word_single_word), cmocka_unit_test(test_read_word_leading_spaces), cmocka_unit_test(test_read_word_trailing_spaces), cmocka_unit_test(test_read_word_multiple_spaces), - cmocka_unit_test(test_read_word_single_word), cmocka_unit_test(test_read_word_empty_input), cmocka_unit_test(test_read_word_null_fp), cmocka_unit_test(test_read_word_null_word), cmocka_unit_test(test_read_word_zero_length), - cmocka_unit_test(test_read_word_buffer_overflow), + cmocka_unit_test(test_read_word_overflow), }; return cmocka_run_group_tests(tests, NULL, NULL); -}
\ No newline at end of file +} |