Skip to content

Commit 7ce8890

Browse files
committed
feat: Implement flag 'compare_outputs' and its tests
1 parent a56cc83 commit 7ce8890

5 files changed

Lines changed: 24 additions & 24 deletions

File tree

creal.c

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
#include "funcs.c"
88

9-
#define DEFAULT_FLAGS (TRIM_COMMAND_OUTPUT)
9+
#define DEFAULT_FLAGS (TRIM_COMMAND_OUTPUT | COMPARE_OUTPUTS)
1010

1111
static uint32_t flags = DEFAULT_FLAGS;
1212
static const char *COMMENT_STR_LEFT = "[[!";
@@ -16,8 +16,8 @@ static const struct {
1616
Action act;
1717
char *act_str;
1818
} act_map[] = {
19-
{ COMMAND, "command" }, { RETURNCODE, "returncode" }, { NAME, "name" },
20-
{ ACTION_SIZE, "action_size" }, { INVALID_ACTION, NULL }, { SINGLE_LINE_OUTPUT, NULL },
19+
{ COMMAND, "command" }, { RETURNCODE, "returncode" }, { NAME, "name" },
20+
{ ACTION_SIZE, NULL }, { INVALID_ACTION, NULL }, { SINGLE_LINE_OUTPUT, NULL },
2121
{ MULTI_LINE_OUTPUT, NULL },
2222
};
2323

@@ -35,6 +35,8 @@ static const struct {
3535
{ APPEND_RELATIVE, "append_relative" },
3636
{ ALWAYS_SHOW_OUTPUT, "always_show_output" },
3737
{ SET_COMMENT_STRING, "set_comment_string" },
38+
{ COMPARE_OUTPUTS, "compare_outputs" },
39+
{ FLAG_SIZE, NULL },
3840
{ INVALID_FLAG, NULL },
3941
};
4042

@@ -193,11 +195,11 @@ int parse_flag(const char *unparsed_flag)
193195
char *value = copy_sub_str_offset(unparsed_flag, value_idx + 1);
194196
int flag_on = flag_is_true(value, -1);
195197
Flags e_flag = INVALID_FLAG;
198+
debug_printf("flag: %s, value: %s\n", flag, value);
196199

197200
FOR_ALL_FLAGS(i) {
198201
if (strcmp(flag, flag_map[i].flag_str) == 0) {
199202
e_flag = flag_map[i].flag;
200-
debug_print_c(CYAN, "found flag '%s'\n", flag_map[i].flag_str);
201203
break;
202204
}
203205
}
@@ -291,23 +293,14 @@ void add_line(Creal *creal, const char *line)
291293

292294
void print_flags()
293295
{
294-
verbose_printf("active flags for runner:\n");
295-
if (flags & FAIL_UNEXPECTED_NEWLINES) {
296-
verbose_print_c(CYAN, "\tfail_unexpected_newlines\n");
297-
}
298-
if (flags & STRICT) {
299-
verbose_print_c(CYAN, "\tstrict\n");
300-
}
301296
if (flags & VERBOSE) {
302-
verbose_print_c(CYAN, "\tverbose\n");
303-
}
304-
if (flags & TRIM_COMMAND_OUTPUT) {
305-
verbose_print_c(CYAN, "\ttrim_command_output\n");
306-
}
307-
if (flags & COLOR_OFF) {
308-
verbose_print_c(CYAN, "\tcolor_off\n");
297+
FOR_ALL_FLAGS(i) {
298+
if (flags & flag_map[i].flag) {
299+
print_c(CYAN, "\t%s\n", flag_map[i].flag_str);
300+
}
301+
}
302+
printf("\n");
309303
}
310-
verbose_printf("\n");
311304
}
312305

313306
void print_diff(const Creal *expected, const Creal *actual, size_t start_of_diff)
@@ -360,7 +353,7 @@ int compare_creals(const Creal *expected, const Creal *actual)
360353
verbose_print_c(YELLOW, "testing runner '%s'\n", expected->name);
361354
print_flags();
362355
size_t num_lines = expected->lines;
363-
if (expected->lines != 0) {
356+
if (flags & COMPARE_OUTPUTS) {
364357
if (actual->lines != expected->lines) {
365358
if (flags & STRICT) {
366359
print_c(RED, "The output contains diffrent number of lines than "

creal.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ typedef struct {
1010
char *command;
1111
char **output;
1212
char *name;
13-
1413
} Creal;
1514

1615
/// To Add a new Flag follow these steps
@@ -46,13 +45,15 @@ typedef enum {
4645
ALWAYS_SHOW_OUTPUT = (1 << 8),
4746
/// Unimplemented.
4847
SET_COMMENT_STRING = (1 << 9),
48+
/// Default flag. If set, compares the output between the runner and the result
49+
COMPARE_OUTPUTS = (1 << 10),
4950
/// Internal use only. Signifies the end of the user flags.
50-
FLAG_SIZE = (1 << 10),
51+
FLAG_SIZE = (1 << 11),
5152
/// Signifies that a flag in the creal file is invalid
52-
INVALID_FLAG = (1 << 11),
53+
INVALID_FLAG = (1 << 12),
5354
} Flags;
5455

55-
#define FOR_ALL_FLAGS(i) for (int i = NONE; (i << i) < FLAG_SIZE; i++)
56+
#define FOR_ALL_FLAGS(i) for (int i = NONE; 1 << i < FLAG_SIZE; i++)
5657

5758
/// To Add a new Action follow these steps
5859
/// 1. Create a member with a similar name convention as below

tests/mocks/bad_flag.creal

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#debug
2+
#verbose
13
#non_existing_flag
24
name: bad flag without strict
35
command: echo hello

tests/testmocks/tests.creal

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ output: |
66
'test hello' passed.
77
|
88
---
9+
#compare_outputs=false
910
name: test invalid runner
1011
command: creal ./tests/mocks/invalid.creal
1112
returncode: 1
@@ -26,6 +27,7 @@ name: test bad flag w/ strict
2627
command: creal ./tests/mocks/bad_flag_strict.creal
2728
returncode: 1
2829
---
30+
#compare_outputs=true
2931
name: test diffs
3032
command: creal ./tests/mocks/diff.creal
3133
output: |

tests/testmocks/unix_tests.creal

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ output: |
66
'test hello' passed.
77
|
88
---
9+
#compare_outputs=false
910
name: test invalid runner
1011
command: ./creal ./tests/mocks/invalid.creal
1112
returncode: 1
@@ -26,6 +27,7 @@ name: test bad flag w/ strict
2627
command: ./creal ./tests/mocks/bad_flag_strict.creal
2728
returncode: 1
2829
---
30+
#compare_outputs=true
2931
name: test diffs
3032
command: ./creal ./tests/mocks/unix_diff.creal
3133
output: |

0 commit comments

Comments
 (0)