File size: 9,722 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 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 |
#include "../../unity/unity.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <errno.h>
/* Note:
- This test file is included into the nl source, so it has access to
all internal globals and functions, including proc_text, line_buf, etc.
- Do not use Unity assertions while stdout is redirected.
*/
/* Forward declare the target function for clarity (it is visible as it's in the same TU). */
/* static void proc_text(void); -- not redeclared to avoid conflicts */
/* Helper: begin capturing stdout to a temporary file. */
typedef struct {
int saved_fd;
int tmp_fd;
char path[64];
} capture_ctx;
static int begin_capture(capture_ctx *ctx) {
strcpy(ctx->path, "/tmp/nl_cap_XXXXXX");
ctx->tmp_fd = mkstemp(ctx->path);
if (ctx->tmp_fd < 0) return -1;
ctx->saved_fd = dup(fileno(stdout));
if (ctx->saved_fd < 0) {
close(ctx->tmp_fd);
unlink(ctx->path);
return -1;
}
fflush(stdout);
if (dup2(ctx->tmp_fd, fileno(stdout)) < 0) {
close(ctx->tmp_fd);
close(ctx->saved_fd);
unlink(ctx->path);
return -1;
}
return 0;
}
/* Helper: end capturing stdout and return the captured output as a malloc'd string. */
static char *end_capture(capture_ctx *ctx) {
fflush(stdout);
/* Restore stdout first, then read and clean up. */
dup2(ctx->saved_fd, fileno(stdout));
close(ctx->saved_fd);
/* Read back file */
struct stat st;
if (fstat(ctx->tmp_fd, &st) != 0) {
close(ctx->tmp_fd);
unlink(ctx->path);
return NULL;
}
off_t sz = st.st_size;
if (lseek(ctx->tmp_fd, 0, SEEK_SET) < 0) {
close(ctx->tmp_fd);
unlink(ctx->path);
return NULL;
}
char *buf = (char *)malloc((size_t)sz + 1);
if (!buf) {
close(ctx->tmp_fd);
unlink(ctx->path);
return NULL;
}
ssize_t rd = read(ctx->tmp_fd, buf, (size_t)sz);
if (rd < 0) {
free(buf);
close(ctx->tmp_fd);
unlink(ctx->path);
return NULL;
}
buf[rd] = '\0';
close(ctx->tmp_fd);
unlink(ctx->path);
return buf;
}
/* Helper: set the global line buffer to the given content (must include '\n').
Returns pointer that must be freed by caller after proc_text call. */
static char *set_line(const char *s) {
size_t len = strlen(s);
char *copy = (char *)malloc(len);
memcpy(copy, s, len);
line_buf.buffer = copy;
line_buf.length = len;
return copy;
}
/* Helper: call proc_text on provided content and capture output. */
static char *call_and_capture_proc_text(const char *content) {
capture_ctx ctx;
if (begin_capture(&ctx) != 0) {
return NULL;
}
char *mem = set_line(content);
/* Do not use Unity asserts while redirected */
proc_text();
char *out = end_capture(&ctx);
free(mem);
return out;
}
/* Reset the static blank_lines inside proc_text to a known state.
We do this by forcing an 'a' mode call on a non-empty line, which sets blank_lines = 0.
Output is discarded. */
static void reset_proc_text_static_state(void) {
const char *saved_type = current_type;
intmax_t saved_blank_join = blank_join;
char const *saved_sep = separator_str;
char const *saved_fmt = lineno_format;
int saved_width = lineno_width;
char const *saved_no_line_fmt = print_no_line_fmt;
intmax_t saved_line_no = line_no;
intmax_t saved_incr = page_incr;
current_type = "a";
blank_join = 2; /* >1 to ensure the path sets blank_lines=0 for non-empty line */
separator_str = "|";
lineno_format = FORMAT_RIGHT_NOLZ;
lineno_width = 2;
print_no_line_fmt = "";
line_no = 1;
page_incr = 1;
capture_ctx ctx;
if (begin_capture(&ctx) == 0) {
char *mem = set_line("R\n");
proc_text(); /* does a numbered non-empty line -> blank_lines becomes 0 */
char *d = end_capture(&ctx);
free(mem);
free(d);
}
/* restore */
current_type = saved_type;
blank_join = saved_blank_join;
separator_str = saved_sep;
lineno_format = saved_fmt;
lineno_width = saved_width;
print_no_line_fmt = saved_no_line_fmt;
line_no = saved_line_no;
page_incr = saved_incr;
}
void setUp(void) {
/* Set deterministic defaults; reset internal static state in proc_text */
separator_str = "|";
lineno_format = FORMAT_RIGHT_NOLZ;
lineno_width = 3;
page_incr = 1;
line_no = 1;
line_no_overflow = false;
print_no_line_fmt = "U";
current_type = "n";
blank_join = 1;
reset_proc_text_static_state();
}
void tearDown(void) {
/* Nothing to clean specifically */
}
/* Test: 'a' mode with blank_join == 1 numbers all lines, including blank. */
static void test_proc_text_a_blank_join_1(void) {
current_type = "a";
blank_join = 1;
separator_str = "|";
lineno_width = 3;
lineno_format = FORMAT_RIGHT_NOLZ;
print_no_line_fmt = "<>";
line_no = 7;
page_incr = 1;
char *out1 = call_and_capture_proc_text("X\n");
TEST_ASSERT_NOT_NULL(out1);
TEST_ASSERT_EQUAL_STRING(" 7|X\n", out1);
free(out1);
TEST_ASSERT_EQUAL_INT64(8, line_no);
char *out2 = call_and_capture_proc_text("\n");
TEST_ASSERT_NOT_NULL(out2);
TEST_ASSERT_EQUAL_STRING(" 8|\n", out2);
free(out2);
TEST_ASSERT_EQUAL_INT64(9, line_no);
}
/* Test: 'a' mode with blank_join > 1 groups consecutive blank lines.
For blank_join=3 and starting line_no=1:
- first blank -> unnumbered
- second blank -> unnumbered
- third blank -> numbered " 1|"
- fourth blank -> unnumbered
Then a non-empty line resets the blank counter and is numbered " 2|".
*/
static void test_proc_text_a_blank_join_gt1_sequence(void) {
current_type = "a";
blank_join = 3;
separator_str = "|";
lineno_width = 2;
lineno_format = FORMAT_RIGHT_NOLZ;
print_no_line_fmt = "U";
line_no = 1;
page_incr = 1;
char *o1 = call_and_capture_proc_text("\n");
TEST_ASSERT_NOT_NULL(o1);
TEST_ASSERT_EQUAL_STRING("U\n", o1);
free(o1);
TEST_ASSERT_EQUAL_INT64(1, line_no);
char *o2 = call_and_capture_proc_text("\n");
TEST_ASSERT_NOT_NULL(o2);
TEST_ASSERT_EQUAL_STRING("U\n", o2);
free(o2);
TEST_ASSERT_EQUAL_INT64(1, line_no);
char *o3 = call_and_capture_proc_text("\n");
TEST_ASSERT_NOT_NULL(o3);
TEST_ASSERT_EQUAL_STRING(" 1|\n", o3);
free(o3);
TEST_ASSERT_EQUAL_INT64(2, line_no);
char *o4 = call_and_capture_proc_text("\n");
TEST_ASSERT_NOT_NULL(o4);
TEST_ASSERT_EQUAL_STRING("U\n", o4);
free(o4);
TEST_ASSERT_EQUAL_INT64(2, line_no);
char *o5 = call_and_capture_proc_text("abc\n");
TEST_ASSERT_NOT_NULL(o5);
TEST_ASSERT_EQUAL_STRING(" 2|abc\n", o5);
free(o5);
TEST_ASSERT_EQUAL_INT64(3, line_no);
}
/* Test: 't' mode numbers only non-empty lines. */
static void test_proc_text_t_type(void) {
current_type = "t";
print_no_line_fmt = "X";
separator_str = "|";
lineno_width = 2;
lineno_format = FORMAT_RIGHT_NOLZ;
line_no = 42;
page_incr = 1;
char *o1 = call_and_capture_proc_text("hello\n");
TEST_ASSERT_NOT_NULL(o1);
TEST_ASSERT_EQUAL_STRING("42|hello\n", o1);
free(o1);
TEST_ASSERT_EQUAL_INT64(43, line_no);
char *o2 = call_and_capture_proc_text("\n");
TEST_ASSERT_NOT_NULL(o2);
TEST_ASSERT_EQUAL_STRING("X\n", o2);
free(o2);
/* line_no unchanged for unnumbered line */
TEST_ASSERT_EQUAL_INT64(43, line_no);
}
/* Test: 'n' mode numbers no lines. */
static void test_proc_text_n_type(void) {
current_type = "n";
print_no_line_fmt = "Z";
separator_str = "|";
lineno_width = 2;
lineno_format = FORMAT_RIGHT_NOLZ;
line_no = 5;
page_incr = 1;
char *o1 = call_and_capture_proc_text("q\n");
TEST_ASSERT_NOT_NULL(o1);
TEST_ASSERT_EQUAL_STRING("Zq\n", o1);
free(o1);
TEST_ASSERT_EQUAL_INT64(5, line_no);
char *o2 = call_and_capture_proc_text("\n");
TEST_ASSERT_NOT_NULL(o2);
TEST_ASSERT_EQUAL_STRING("Z\n", o2);
free(o2);
TEST_ASSERT_EQUAL_INT64(5, line_no);
}
/* Test: 'p' mode numbers only lines matching regex.
Use the program's regex engine and buffers. */
static void test_proc_text_p_type_regex(void) {
/* Prepare regex: "foo" */
current_type = "p";
current_regex = &body_regex;
/* Initialize pattern buffer similarly to build_type_arg. */
body_regex.buffer = NULL;
body_regex.allocated = 0;
body_regex.fastmap = body_fastmap;
body_regex.translate = NULL;
re_syntax_options =
RE_SYNTAX_POSIX_BASIC & ~RE_CONTEXT_INVALID_DUP & ~RE_NO_EMPTY_RANGES;
const char *errmsg = re_compile_pattern("foo", strlen("foo"), &body_regex);
TEST_ASSERT_NULL_MESSAGE(errmsg, "Failed to compile regex pattern");
print_no_line_fmt = "N";
separator_str = "|";
lineno_width = 2;
lineno_format = FORMAT_RIGHT_NOLZ;
line_no = 5;
page_incr = 1;
char *o1 = call_and_capture_proc_text("foo\n");
TEST_ASSERT_NOT_NULL(o1);
TEST_ASSERT_EQUAL_STRING(" 5|foo\n", o1);
free(o1);
TEST_ASSERT_EQUAL_INT64(6, line_no);
char *o2 = call_and_capture_proc_text("bar\n");
TEST_ASSERT_NOT_NULL(o2);
TEST_ASSERT_EQUAL_STRING("Nbar\n", o2);
free(o2);
TEST_ASSERT_EQUAL_INT64(6, line_no);
}
/* Test: page_incr affects line_no increment. */
static void test_proc_text_page_incr(void) {
current_type = "a";
blank_join = 1;
separator_str = "|";
lineno_width = 3;
lineno_format = FORMAT_RIGHT_NOLZ;
print_no_line_fmt = "";
line_no = 10;
page_incr = 10;
char *o = call_and_capture_proc_text("X\n");
TEST_ASSERT_NOT_NULL(o);
TEST_ASSERT_EQUAL_STRING(" 10|X\n", o);
free(o);
TEST_ASSERT_EQUAL_INT64(20, line_no);
}
int main(void) {
UNITY_BEGIN();
RUN_TEST(test_proc_text_a_blank_join_1);
RUN_TEST(test_proc_text_a_blank_join_gt1_sequence);
RUN_TEST(test_proc_text_t_type);
RUN_TEST(test_proc_text_n_type);
RUN_TEST(test_proc_text_p_type_regex);
RUN_TEST(test_proc_text_page_incr);
return UNITY_END();
} |