| #include "../../unity/unity.h" |
| #include <stdio.h> |
| #include <string.h> |
| #include <stdlib.h> |
| #include <stdbool.h> |
|
|
| |
| |
|
|
| |
| static FILE* make_file(const char* s) |
| { |
| FILE* f = tmpfile(); |
| TEST_ASSERT_NOT_NULL_MESSAGE(f, "tmpfile failed"); |
| size_t len = strlen(s); |
| if (len > 0) { |
| size_t n = fwrite(s, 1, len, f); |
| TEST_ASSERT_TRUE_MESSAGE(n == len, "failed to write all data to tmpfile"); |
| } |
| rewind(f); |
| return f; |
| } |
|
|
| void setUp(void) { |
| |
| } |
|
|
| void tearDown(void) { |
| |
| } |
|
|
| |
|
|
| void test_get_prefix_empty_prefix_with_leading_spaces(void) |
| { |
| char pfx[] = " "; |
| set_prefix(pfx); |
| tabs = false; |
|
|
| FILE* f = make_file(" abc\n"); |
| int c = get_prefix(f); |
|
|
| TEST_ASSERT_EQUAL_INT('a', c); |
| TEST_ASSERT_EQUAL_INT(3, in_column); |
| TEST_ASSERT_EQUAL_INT(2, next_prefix_indent); |
| TEST_ASSERT_FALSE(tabs); |
|
|
| int next = getc(f); |
| TEST_ASSERT_EQUAL_INT('b', next); |
| fclose(f); |
| } |
|
|
| void test_get_prefix_nonempty_prefix_exact_match_with_trailing_spaces(void) |
| { |
| char pfx[] = " abc "; |
| set_prefix(pfx); |
| tabs = false; |
|
|
| FILE* f = make_file(" abc xyz\n"); |
| int c = get_prefix(f); |
|
|
| TEST_ASSERT_EQUAL_INT('x', c); |
| TEST_ASSERT_EQUAL_INT(10, in_column); |
| TEST_ASSERT_EQUAL_INT(4, next_prefix_indent); |
| TEST_ASSERT_FALSE(tabs); |
|
|
| int next = getc(f); |
| TEST_ASSERT_EQUAL_INT('y', next); |
| fclose(f); |
| } |
|
|
| void test_get_prefix_mismatch_first_char(void) |
| { |
| char pfx[] = "abc"; |
| set_prefix(pfx); |
| tabs = false; |
|
|
| FILE* f = make_file(" Xdef\n"); |
| int c = get_prefix(f); |
|
|
| TEST_ASSERT_EQUAL_INT('X', c); |
| TEST_ASSERT_EQUAL_INT(3, in_column); |
| TEST_ASSERT_EQUAL_INT(3, next_prefix_indent); |
| TEST_ASSERT_FALSE(tabs); |
|
|
| int next = getc(f); |
| TEST_ASSERT_EQUAL_INT('d', next); |
| fclose(f); |
| } |
|
|
| void test_get_prefix_partial_match_then_mismatch_mid_prefix(void) |
| { |
| char pfx[] = "abz"; |
| set_prefix(pfx); |
| tabs = false; |
|
|
| FILE* f = make_file(" abXq\n"); |
| int c = get_prefix(f); |
|
|
| TEST_ASSERT_EQUAL_INT('X', c); |
| TEST_ASSERT_EQUAL_INT(3, in_column); |
| TEST_ASSERT_EQUAL_INT(1, next_prefix_indent); |
| TEST_ASSERT_FALSE(tabs); |
|
|
| int next = getc(f); |
| TEST_ASSERT_EQUAL_INT('q', next); |
| fclose(f); |
| } |
|
|
| void test_get_prefix_empty_prefix_uses_min_of_lead_and_indent(void) |
| { |
| char pfx[] = " "; |
| set_prefix(pfx); |
| tabs = false; |
|
|
| FILE* f = make_file(" a"); |
| int c = get_prefix(f); |
|
|
| TEST_ASSERT_EQUAL_INT('a', c); |
| TEST_ASSERT_EQUAL_INT(2, in_column); |
| TEST_ASSERT_EQUAL_INT(2, next_prefix_indent); |
| TEST_ASSERT_FALSE(tabs); |
|
|
| int next = getc(f); |
| |
| TEST_ASSERT_EQUAL_INT(EOF, next); |
| fclose(f); |
| } |
|
|
| void test_get_prefix_tabs_with_empty_prefix_sets_tabs_and_indent(void) |
| { |
| char pfx[] = ""; |
| set_prefix(pfx); |
| tabs = false; |
|
|
| FILE* f = make_file("\t\tA"); |
| int c = get_prefix(f); |
|
|
| TEST_ASSERT_EQUAL_INT('A', c); |
| TEST_ASSERT_TRUE(tabs); |
| TEST_ASSERT_EQUAL_INT(16, in_column); |
| TEST_ASSERT_EQUAL_INT(0, next_prefix_indent); |
|
|
| int next = getc(f); |
| TEST_ASSERT_EQUAL_INT(EOF, next); |
| fclose(f); |
| } |
|
|
| void test_get_prefix_tab_before_nonempty_prefix(void) |
| { |
| char pfx[] = "XYZ"; |
| set_prefix(pfx); |
| tabs = false; |
|
|
| FILE* f = make_file("\tXYZ QR"); |
| int c = get_prefix(f); |
|
|
| TEST_ASSERT_TRUE(tabs); |
| TEST_ASSERT_EQUAL_INT('Q', c); |
| TEST_ASSERT_EQUAL_INT(8, next_prefix_indent); |
| TEST_ASSERT_EQUAL_INT(13, in_column); |
|
|
| int next = getc(f); |
| TEST_ASSERT_EQUAL_INT('R', next); |
| fclose(f); |
| } |
|
|
| void test_get_prefix_eof_immediately(void) |
| { |
| char pfx[] = ""; |
| set_prefix(pfx); |
| tabs = false; |
|
|
| FILE* f = make_file(""); |
| int c = get_prefix(f); |
|
|
| TEST_ASSERT_EQUAL_INT(EOF, c); |
| TEST_ASSERT_EQUAL_INT(0, in_column); |
| TEST_ASSERT_EQUAL_INT(0, next_prefix_indent); |
| TEST_ASSERT_FALSE(tabs); |
| fclose(f); |
| } |
|
|
| void test_get_prefix_prefix_match_followed_by_tabs(void) |
| { |
| char pfx[] = "P"; |
| set_prefix(pfx); |
| tabs = false; |
|
|
| FILE* f = make_file(" P\t\tA"); |
| int c = get_prefix(f); |
|
|
| TEST_ASSERT_EQUAL_INT('A', c); |
| TEST_ASSERT_TRUE(tabs); |
| TEST_ASSERT_EQUAL_INT(1, next_prefix_indent); |
| TEST_ASSERT_EQUAL_INT(16, in_column); |
|
|
| int next = getc(f); |
| TEST_ASSERT_EQUAL_INT(EOF, next); |
| fclose(f); |
| } |
|
|
| void test_get_prefix_newline_immediately_nonempty_prefix(void) |
| { |
| char pfx[] = "x"; |
| set_prefix(pfx); |
| tabs = false; |
|
|
| FILE* f = make_file("\nA"); |
| int c = get_prefix(f); |
|
|
| TEST_ASSERT_EQUAL_INT('\n', c); |
| TEST_ASSERT_EQUAL_INT(0, in_column); |
| TEST_ASSERT_EQUAL_INT(0, next_prefix_indent); |
| TEST_ASSERT_FALSE(tabs); |
|
|
| int next = getc(f); |
| TEST_ASSERT_EQUAL_INT('A', next); |
| fclose(f); |
| } |
|
|
| int main(void) |
| { |
| UNITY_BEGIN(); |
|
|
| RUN_TEST(test_get_prefix_empty_prefix_with_leading_spaces); |
| RUN_TEST(test_get_prefix_nonempty_prefix_exact_match_with_trailing_spaces); |
| RUN_TEST(test_get_prefix_mismatch_first_char); |
| RUN_TEST(test_get_prefix_partial_match_then_mismatch_mid_prefix); |
| RUN_TEST(test_get_prefix_empty_prefix_uses_min_of_lead_and_indent); |
| RUN_TEST(test_get_prefix_tabs_with_empty_prefix_sets_tabs_and_indent); |
| RUN_TEST(test_get_prefix_tab_before_nonempty_prefix); |
| RUN_TEST(test_get_prefix_eof_immediately); |
| RUN_TEST(test_get_prefix_prefix_match_followed_by_tabs); |
| RUN_TEST(test_get_prefix_newline_immediately_nonempty_prefix); |
|
|
| return UNITY_END(); |
| } |