#include "../../unity/unity.h" #include #include #include #include /* Forward declarations of tested/internal functions and globals are not needed since this file is textually included into the same translation unit. */ /* Helper to create a temporary file with given contents. */ 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) { /* Nothing to set up by default */ } void tearDown(void) { /* Nothing to tear down by default */ } /* Tests start here */ void test_get_prefix_empty_prefix_with_leading_spaces(void) { char pfx[] = " "; /* trimmed -> prefix_length=0, prefix_lead_space=2 */ 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 "; /* trimmed -> "abc", lead=2 */ 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); /* 4 spaces + 3 + 3 trailing spaces */ 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); /* 1 space + 'a' + 'b' */ 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[] = " "; /* lead=3, prefix_length=0 */ 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); /* min(3,2)=2 */ TEST_ASSERT_FALSE(tabs); int next = getc(f); /* After returning 'a', stream advanced; next should be EOF here */ TEST_ASSERT_EQUAL_INT(EOF, next); fclose(f); } void test_get_prefix_tabs_with_empty_prefix_sets_tabs_and_indent(void) { char pfx[] = ""; /* lead=0, prefix_length=0 */ 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); /* two tabs -> 16 columns */ 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); /* tab to column 8 */ TEST_ASSERT_EQUAL_INT(13, in_column); /* 8 + 3 + 2 spaces */ int next = getc(f); TEST_ASSERT_EQUAL_INT('R', next); fclose(f); } void test_get_prefix_eof_immediately(void) { char pfx[] = ""; /* empty; lead=0 */ 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); /* one leading space */ TEST_ASSERT_EQUAL_INT(16, in_column); /* after 'P' (col 2), two tabs -> col 16 */ 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(); }