Skip to content

Commit 709020f

Browse files
authored
Merge pull request #4 from codewars/improve-reporter
2 parents 309fcb7 + d54f019 commit 709020f

File tree

3 files changed

+40
-12
lines changed

3 files changed

+40
-12
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@ jobs:
1010
build_and_test:
1111
runs-on: ubuntu-latest
1212
if: ${{ github.repository == 'codewars/riscv' }}
13-
permissions:
14-
contents: read
15-
packages: write
1613
steps:
1714
- uses: actions/checkout@v3
1815

@@ -40,7 +37,7 @@ jobs:
4037
run: bin/run multiply
4138

4239
- name: Run multiply-failing example
43-
run: bin/run multiply-failing
40+
run: bin/run multiply-failing || true
4441

4542
- name: Run syntax-error example
4643
run: bin/run syntax-error || true

examples/multiply-failing/solution_tests.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ Describe(Multiply);
99
BeforeEach(Multiply) {}
1010
AfterEach(Multiply) {}
1111

12+
Ensure(Multiply, missing_assertion) {
13+
}
14+
1215
Ensure(Multiply, works_for_some_fixed_tests) {
1316
assert_that(multiply(3, 5), is_equal_to(15));
1417
assert_that(multiply(5, 3), is_equal_to(15));
@@ -24,12 +27,14 @@ Ensure(Multiply, works_for_100_random_tests) {
2427
int a = rand() % 100;
2528
int b = rand() % 100;
2629
int expected = a * b;
27-
assert_that(multiply(a, b), is_equal_to(expected));
30+
// assert_that(multiply(a, b), is_equal_to(expected));
31+
assert_equal_with_message(multiply(a, b), expected, "multiply(%d, %d) == %d", a, b, expected);
2832
}
2933
}
3034

3135
TestSuite *solution_tests() {
3236
TestSuite *suite = create_test_suite();
37+
add_test_with_context(suite, Multiply, missing_assertion);
3338
add_test_with_context(suite, Multiply, works_for_some_fixed_tests);
3439
add_test_with_context(suite, Multiply, works_for_100_random_tests);
3540
return suite;

workspace/codewars_reporter.c

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,24 +68,50 @@ static void codewars_reporter_start_suite(TestReporter *reporter, const char *na
6868
static void codewars_reporter_start_test(TestReporter *reporter, const char *name) {
6969
printf("\n<IT::>%s\n", name);
7070
reporter_start_test(reporter, name);
71+
reporter->passes = 0;
72+
reporter->failures = 0;
73+
reporter->skips = 0;
74+
reporter->exceptions = 0;
7175
push_ts((struct ts_node **)&reporter->memo);
7276
}
7377

74-
static void codewars_show_pass(TestReporter *reporter, const char *file, int line, const char *message, va_list arguments) {
75-
printf("\n<PASSED::>Test Passed\n");
76-
}
77-
78-
static void codewars_show_fail(TestReporter *reporter, const char *file, int line, const char *message, va_list arguments) {
78+
static void codewars_reporter_show_fail(TestReporter *reporter, const char *file, int line, const char *message, va_list arguments) {
7979
printf("\n<FAILED::>");
8080
char *escaped_message = create_codewars_escape_message(message);
8181
vprintf(escaped_message, arguments);
8282
free(escaped_message);
8383
printf("\n");
8484
}
8585

86+
// When a test fails to complete
87+
static void codewars_reporter_show_incomplete(TestReporter *reporter, const char *file, int line, const char *message, va_list arguments) {
88+
printf("\n<ERROR::>");
89+
if (message == NULL) {
90+
printf("Test Terminated Unexpectedly");
91+
} else {
92+
char *escaped_message = create_codewars_escape_message(message);
93+
vprintf(escaped_message, arguments);
94+
free(escaped_message);
95+
}
96+
printf("\n");
97+
}
98+
8699
static void codewars_reporter_finish_test(TestReporter *reporter, const char *filename, int line, const char *message) {
87100
clock_t ts_diff = clock() - pop_ts((struct ts_node **)&reporter->memo);
101+
// This function increments passes/failures counts.
88102
reporter_finish_test(reporter, filename, line, message);
103+
if (reporter->failures == 0 && reporter->exceptions == 0 && reporter->skips == 0) {
104+
if (reporter->passes > 0) {
105+
printf("\n<PASSED::>Test Passed\n");
106+
} else {
107+
printf("\n<ERROR::>Missing Assertions\n");
108+
}
109+
}
110+
// Increment the totals. `total_failures` is used to determine the exit code.
111+
reporter->total_passes += reporter->passes;
112+
reporter->total_failures += reporter->failures;
113+
reporter->total_skips += reporter->skips;
114+
reporter->total_exceptions += reporter->exceptions;
89115
printf("\n<COMPLETEDIN::>%ld\n", 1000 * ts_diff / CLOCKS_PER_SEC);
90116
}
91117

@@ -99,8 +125,8 @@ TestReporter *create_codewars_reporter() {
99125
TestReporter *reporter = create_reporter();
100126
reporter->start_suite = &codewars_reporter_start_suite;
101127
reporter->start_test = &codewars_reporter_start_test;
102-
reporter->show_pass = &codewars_show_pass;
103-
reporter->show_fail = &codewars_show_fail;
128+
reporter->show_fail = &codewars_reporter_show_fail;
129+
reporter->show_incomplete = &codewars_reporter_show_incomplete;
104130
reporter->finish_test = &codewars_reporter_finish_test;
105131
reporter->finish_suite = &codewars_reporter_finish_suite;
106132
reporter->memo = NULL;

0 commit comments

Comments
 (0)