Skip to content

Commit 52685d5

Browse files
add history tracking
1 parent 51686ea commit 52685d5

File tree

3 files changed

+54
-4
lines changed

3 files changed

+54
-4
lines changed

code/include/fossil/unittest/internal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ typedef struct {
224224
bool shoudl_timeout; /**< Flag indicating whether the test case should timeout (1 for true, 0 for false). */
225225
bool should_fail; /**< Flag indicating whether the test case should fail (1 for true, 0 for false). */
226226
bool has_assert; /**< Flag indicating if an assertion occurred (1 for true, 0 for false). */
227+
bool same_assert; /**< Flag indicating if the test case is the same (1 for true, 0 for false). */
227228
int32_t num_asserts; /**< Number of assertions that occurred. */
228229
int32_t line; /**< Line number where the assertion occurred. */
229230
char *func; /**< Function name where the assertion occurred. */

code/source/unittest/console.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,8 @@ void fossil_test_io_unittest_step(xassert_info *assume) {
409409
} else if (_CLI.verbose_level == 1) {
410410
fossil_test_cout("blue", "[intro] has_assert : ");
411411
fossil_test_cout("cyan", "%s\n", assume->has_assert ? COLOR_GREEN "yes" COLOR_RESET : COLOR_RED "no" COLOR_RESET);
412+
fossil_test_cout("blue", "[intro] same_assert: ");
413+
fossil_test_cout("cyan", "%s\n", assume->same_assert ? COLOR_RED "yes" COLOR_RESET : COLOR_GREEN "no" COLOR_RESET);
412414
fossil_test_cout("blue", "[intro] num_asserts: ");
413415
fossil_test_cout("cyan", COLOR_GREEN "%3i\n" COLOR_RESET , assume->num_asserts);
414416
}

code/source/unittest/unittest.c

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,20 @@ Organization: Fossil Logic
1616
#include "fossil/unittest/commands.h"
1717
#include <stdarg.h>
1818

19+
#define MAX_ASSERT_HISTORY 100
20+
21+
typedef struct {
22+
bool expression;
23+
xassert_type_t behavior;
24+
char* message;
25+
char* file;
26+
int line;
27+
char* func;
28+
} assert_history_t;
29+
30+
static assert_history_t assert_history[MAX_ASSERT_HISTORY];
31+
static int assert_history_count = 0;
32+
1933
fossil_env_t _TEST_ENV;
2034
xassert_info _ASSERT_INFO;
2135

@@ -392,6 +406,7 @@ void fossil_test_run_testcase(fossil_test_t *test) {
392406
_ASSERT_INFO.should_fail = false;
393407
_ASSERT_INFO.shoudl_timeout = false;
394408
_ASSERT_INFO.num_asserts = 0;
409+
_ASSERT_INFO.same_assert = false;
395410

396411
if (_TEST_ENV.rule.skipped && strcmp(test->marks, "skip") == 0) {
397412
return;
@@ -708,19 +723,51 @@ void fossil_test_assert_impl_expect(bool expression, xassert_info *assume) {
708723
}
709724
} // end of func
710725

711-
void _fossil_test_assert_class(bool expression, xassert_type_t behavor, char* message, char* file, int line, char* func) {
726+
bool is_assert_in_history(bool expression, xassert_type_t behavior, char* message, char* file, int line, char* func) {
727+
for (int i = 0; i < assert_history_count; i++) {
728+
if (assert_history[i].expression == expression &&
729+
assert_history[i].behavior == behavior &&
730+
strcmp(assert_history[i].message, message) == 0 &&
731+
strcmp(assert_history[i].file, file) == 0 &&
732+
assert_history[i].line == line &&
733+
strcmp(assert_history[i].func, func) == 0) {
734+
return true;
735+
}
736+
}
737+
return false;
738+
}
739+
740+
void _fossil_test_assert_class(bool expression, xassert_type_t behavior, char* message, char* file, int line, char* func) {
741+
if (is_assert_in_history(expression, behavior, message, file, line, func)) {
742+
// Skip the assertion as it has already been executed
743+
_ASSERT_INFO.same_assert = true;
744+
return;
745+
}
746+
712747
_ASSERT_INFO.func = func;
713748
_ASSERT_INFO.file = file;
714749
_ASSERT_INFO.line = line;
715750
_ASSERT_INFO.message = message;
716751

717-
if (behavor == TEST_ASSERT_AS_CLASS_ASSUME) {
752+
if (behavior == TEST_ASSERT_AS_CLASS_ASSUME) {
718753
fossil_test_assert_impl_assume(expression, &_ASSERT_INFO);
719-
} else if (behavor == TEST_ASSERT_AS_CLASS_ASSERT) {
754+
} else if (behavior == TEST_ASSERT_AS_CLASS_ASSERT) {
720755
fossil_test_assert_impl_assert(expression, &_ASSERT_INFO);
721-
} else if (behavor == TEST_ASSERT_AS_CLASS_EXPECT) {
756+
} else if (behavior == TEST_ASSERT_AS_CLASS_EXPECT) {
722757
fossil_test_assert_impl_expect(expression, &_ASSERT_INFO);
723758
}
759+
724760
_ASSERT_INFO.num_asserts++; // increment the number of asserts
725761
_ASSERT_INFO.has_assert = true; // Make note of an assert being added in a given test case
762+
763+
// Add the assertion to the history
764+
if (assert_history_count < MAX_ASSERT_HISTORY) {
765+
assert_history[assert_history_count].expression = expression;
766+
assert_history[assert_history_count].behavior = behavior;
767+
assert_history[assert_history_count].message = message;
768+
assert_history[assert_history_count].file = file;
769+
assert_history[assert_history_count].line = line;
770+
assert_history[assert_history_count].func = func;
771+
assert_history_count++;
772+
}
726773
}

0 commit comments

Comments
 (0)