File size: 6,206 Bytes
78d2150 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 |
#include "../../unity/unity.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
/* 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();
} |