From 68d39818074116f6b4568bc991453ec608127553 Mon Sep 17 00:00:00 2001 From: kazk Date: Tue, 2 Aug 2022 22:00:21 -0700 Subject: [PATCH 1/8] Fix exit code --- workspace/codewars_reporter.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/workspace/codewars_reporter.c b/workspace/codewars_reporter.c index d2ec3c4..465141e 100644 --- a/workspace/codewars_reporter.c +++ b/workspace/codewars_reporter.c @@ -68,6 +68,10 @@ static void codewars_reporter_start_suite(TestReporter *reporter, const char *na static void codewars_reporter_start_test(TestReporter *reporter, const char *name) { printf("\n%s\n", name); reporter_start_test(reporter, name); + reporter->passes = 0; + reporter->failures = 0; + reporter->skips = 0; + reporter->exceptions = 0; push_ts((struct ts_node **)&reporter->memo); } @@ -85,7 +89,13 @@ static void codewars_show_fail(TestReporter *reporter, const char *file, int lin static void codewars_reporter_finish_test(TestReporter *reporter, const char *filename, int line, const char *message) { clock_t ts_diff = clock() - pop_ts((struct ts_node **)&reporter->memo); + // This function increments passes/failures counts. reporter_finish_test(reporter, filename, line, message); + // Increment the totals. `total_failures` is used to determine the exit code. + reporter->total_passes += reporter->passes; + reporter->total_failures += reporter->failures; + reporter->total_skips += reporter->skips; + reporter->total_exceptions += reporter->exceptions; printf("\n%ld\n", 1000 * ts_diff / CLOCKS_PER_SEC); } From d1304b34199e7c17f9ffd99e331d6f2a2495cd62 Mon Sep 17 00:00:00 2001 From: kazk Date: Tue, 2 Aug 2022 22:09:04 -0700 Subject: [PATCH 2/8] Fix function names --- workspace/codewars_reporter.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/workspace/codewars_reporter.c b/workspace/codewars_reporter.c index 465141e..46e82c0 100644 --- a/workspace/codewars_reporter.c +++ b/workspace/codewars_reporter.c @@ -75,11 +75,11 @@ static void codewars_reporter_start_test(TestReporter *reporter, const char *nam push_ts((struct ts_node **)&reporter->memo); } -static void codewars_show_pass(TestReporter *reporter, const char *file, int line, const char *message, va_list arguments) { +static void codewars_reporter_show_pass(TestReporter *reporter, const char *file, int line, const char *message, va_list arguments) { printf("\nTest Passed\n"); } -static void codewars_show_fail(TestReporter *reporter, const char *file, int line, const char *message, va_list arguments) { +static void codewars_reporter_show_fail(TestReporter *reporter, const char *file, int line, const char *message, va_list arguments) { printf("\n"); char *escaped_message = create_codewars_escape_message(message); vprintf(escaped_message, arguments); @@ -109,8 +109,8 @@ TestReporter *create_codewars_reporter() { TestReporter *reporter = create_reporter(); reporter->start_suite = &codewars_reporter_start_suite; reporter->start_test = &codewars_reporter_start_test; - reporter->show_pass = &codewars_show_pass; - reporter->show_fail = &codewars_show_fail; + reporter->show_pass = &codewars_reporter_show_pass; + reporter->show_fail = &codewars_reporter_show_fail; reporter->finish_test = &codewars_reporter_finish_test; reporter->finish_suite = &codewars_reporter_finish_suite; reporter->memo = NULL; From 1d4447cf63733da1867e4a1ea8f2514fd91a1f2c Mon Sep 17 00:00:00 2001 From: kazk Date: Tue, 2 Aug 2022 22:29:54 -0700 Subject: [PATCH 3/8] Only output one `` per test --- workspace/codewars_reporter.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/workspace/codewars_reporter.c b/workspace/codewars_reporter.c index 46e82c0..2186a9f 100644 --- a/workspace/codewars_reporter.c +++ b/workspace/codewars_reporter.c @@ -75,10 +75,6 @@ static void codewars_reporter_start_test(TestReporter *reporter, const char *nam push_ts((struct ts_node **)&reporter->memo); } -static void codewars_reporter_show_pass(TestReporter *reporter, const char *file, int line, const char *message, va_list arguments) { - printf("\nTest Passed\n"); -} - static void codewars_reporter_show_fail(TestReporter *reporter, const char *file, int line, const char *message, va_list arguments) { printf("\n"); char *escaped_message = create_codewars_escape_message(message); @@ -91,6 +87,9 @@ static void codewars_reporter_finish_test(TestReporter *reporter, const char *fi clock_t ts_diff = clock() - pop_ts((struct ts_node **)&reporter->memo); // This function increments passes/failures counts. reporter_finish_test(reporter, filename, line, message); + if (reporter->passes > 0 && reporter->failures == 0 && reporter->exceptions == 0 && reporter->skips == 0) { + printf("\nTest Passed\n"); + } // Increment the totals. `total_failures` is used to determine the exit code. reporter->total_passes += reporter->passes; reporter->total_failures += reporter->failures; @@ -109,7 +108,6 @@ TestReporter *create_codewars_reporter() { TestReporter *reporter = create_reporter(); reporter->start_suite = &codewars_reporter_start_suite; reporter->start_test = &codewars_reporter_start_test; - reporter->show_pass = &codewars_reporter_show_pass; reporter->show_fail = &codewars_reporter_show_fail; reporter->finish_test = &codewars_reporter_finish_test; reporter->finish_suite = &codewars_reporter_finish_suite; From a0549fceb0b7df26560204a1f0918c1ac11edb7e Mon Sep 17 00:00:00 2001 From: kazk Date: Tue, 2 Aug 2022 22:31:40 -0700 Subject: [PATCH 4/8] Use assertion with custom message in random tests --- examples/multiply-failing/solution_tests.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/examples/multiply-failing/solution_tests.c b/examples/multiply-failing/solution_tests.c index 08c60a1..479faac 100644 --- a/examples/multiply-failing/solution_tests.c +++ b/examples/multiply-failing/solution_tests.c @@ -24,7 +24,8 @@ Ensure(Multiply, works_for_100_random_tests) { int a = rand() % 100; int b = rand() % 100; int expected = a * b; - assert_that(multiply(a, b), is_equal_to(expected)); + // assert_that(multiply(a, b), is_equal_to(expected)); + assert_equal_with_message(multiply(a, b), expected, "multiply(%d, %d) == %d", a, b, expected); } } From 29edffb75a302a22401858c492ca5499e15dbff5 Mon Sep 17 00:00:00 2001 From: kazk Date: Tue, 2 Aug 2022 22:43:49 -0700 Subject: [PATCH 5/8] Report unexpected test termination --- workspace/codewars_reporter.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/workspace/codewars_reporter.c b/workspace/codewars_reporter.c index 2186a9f..4e5234c 100644 --- a/workspace/codewars_reporter.c +++ b/workspace/codewars_reporter.c @@ -83,6 +83,19 @@ static void codewars_reporter_show_fail(TestReporter *reporter, const char *file printf("\n"); } +// When a test fails to complete +static void codewars_reporter_show_incomplete(TestReporter *reporter, const char *file, int line, const char *message, va_list arguments) { + printf("\n"); + if (message == NULL) { + printf("Test Terminated Unexpectedly"); + } else { + char *escaped_message = create_codewars_escape_message(message); + vprintf(escaped_message, arguments); + free(escaped_message); + } + printf("\n"); +} + static void codewars_reporter_finish_test(TestReporter *reporter, const char *filename, int line, const char *message) { clock_t ts_diff = clock() - pop_ts((struct ts_node **)&reporter->memo); // This function increments passes/failures counts. @@ -109,6 +122,7 @@ TestReporter *create_codewars_reporter() { reporter->start_suite = &codewars_reporter_start_suite; reporter->start_test = &codewars_reporter_start_test; reporter->show_fail = &codewars_reporter_show_fail; + reporter->show_incomplete = &codewars_reporter_show_incomplete; reporter->finish_test = &codewars_reporter_finish_test; reporter->finish_suite = &codewars_reporter_finish_suite; reporter->memo = NULL; From 6bb2fbcab84c61f53c80712188a079c07776fad3 Mon Sep 17 00:00:00 2001 From: kazk Date: Tue, 2 Aug 2022 22:45:38 -0700 Subject: [PATCH 6/8] Fail missing assertions --- examples/multiply-failing/solution_tests.c | 4 ++++ workspace/codewars_reporter.c | 8 ++++++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/examples/multiply-failing/solution_tests.c b/examples/multiply-failing/solution_tests.c index 479faac..3bcf9b8 100644 --- a/examples/multiply-failing/solution_tests.c +++ b/examples/multiply-failing/solution_tests.c @@ -9,6 +9,9 @@ Describe(Multiply); BeforeEach(Multiply) {} AfterEach(Multiply) {} +Ensure(Multiply, missing_assertion) { +} + Ensure(Multiply, works_for_some_fixed_tests) { assert_that(multiply(3, 5), is_equal_to(15)); assert_that(multiply(5, 3), is_equal_to(15)); @@ -31,6 +34,7 @@ Ensure(Multiply, works_for_100_random_tests) { TestSuite *solution_tests() { TestSuite *suite = create_test_suite(); + add_test_with_context(suite, Multiply, missing_assertion); add_test_with_context(suite, Multiply, works_for_some_fixed_tests); add_test_with_context(suite, Multiply, works_for_100_random_tests); return suite; diff --git a/workspace/codewars_reporter.c b/workspace/codewars_reporter.c index 4e5234c..9074fdf 100644 --- a/workspace/codewars_reporter.c +++ b/workspace/codewars_reporter.c @@ -100,8 +100,12 @@ static void codewars_reporter_finish_test(TestReporter *reporter, const char *fi clock_t ts_diff = clock() - pop_ts((struct ts_node **)&reporter->memo); // This function increments passes/failures counts. reporter_finish_test(reporter, filename, line, message); - if (reporter->passes > 0 && reporter->failures == 0 && reporter->exceptions == 0 && reporter->skips == 0) { - printf("\nTest Passed\n"); + if (reporter->failures == 0 && reporter->exceptions == 0 && reporter->skips == 0) { + if (reporter->passes > 0) { + printf("\nTest Passed\n"); + } else { + printf("\nMissing Assertions\n"); + } } // Increment the totals. `total_failures` is used to determine the exit code. reporter->total_passes += reporter->passes; From d191a3a35cec6b3194bd5b20addba8abf5566ea4 Mon Sep 17 00:00:00 2001 From: kazk Date: Tue, 2 Aug 2022 23:10:17 -0700 Subject: [PATCH 7/8] Fix CI --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 6371660..cca9bef 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -40,7 +40,7 @@ jobs: run: bin/run multiply - name: Run multiply-failing example - run: bin/run multiply-failing + run: bin/run multiply-failing || true - name: Run syntax-error example run: bin/run syntax-error || true From d54f019fe1e972492f378a51a1198670274e272a Mon Sep 17 00:00:00 2001 From: kazk Date: Tue, 2 Aug 2022 23:14:04 -0700 Subject: [PATCH 8/8] Remove unnecessary permissions --- .github/workflows/ci.yml | 3 --- 1 file changed, 3 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index cca9bef..5f8bda2 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -10,9 +10,6 @@ jobs: build_and_test: runs-on: ubuntu-latest if: ${{ github.repository == 'codewars/riscv' }} - permissions: - contents: read - packages: write steps: - uses: actions/checkout@v3