| 1 | #include <cgreen/filename.h> |
| 2 | #include <cgreen/suite.h> |
| 3 | #include <cgreen/unit.h> |
| 4 | #include <cgreen/vector.h> |
| 5 | #ifndef __cplusplus |
| 6 | #include <stdbool.h> |
| 7 | #endif |
| 8 | #include <stddef.h> |
| 9 | #include <stdlib.h> |
| 10 | #include <string.h> |
| 11 | |
| 12 | #include "parameters.h" |
| 13 | |
| 14 | |
| 15 | CgreenContext defaultContext = { |
| 16 | /* name */ "", |
| 17 | /* filename */ FILENAME, |
| 18 | /* setup */ &do_nothing, |
| 19 | /* teardown */ &do_nothing |
| 20 | }; |
| 21 | |
| 22 | bool has_test(TestSuite *suite, const char *name) { |
| 23 | int i; |
| 24 | for (i = 0; i < suite->size; i++) { |
| 25 | if (suite->tests[i].type == test_function) { |
| 26 | if (strcmp(suite->tests[i].name, name) == 0) { |
| 27 | return true; |
| 28 | } |
| 29 | } else if (has_test(suite->tests[i].Runnable.suite, name)) { |
| 30 | return true; |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | return false; |
| 35 | } |
| 36 | |
| 37 | bool has_setup(TestSuite *suite) { |
| 38 | return (suite->setup != &do_nothing); |
| 39 | } |
| 40 | |
| 41 | bool has_teardown(TestSuite *suite) { |
| 42 | return (suite->teardown != &do_nothing); |
| 43 | } |
| 44 | |
| 45 | void do_nothing(void) { |
| 46 | } |
| 47 | |
| 48 | TestSuite *create_named_test_suite_(const char *name, const char *filename, int line) { |
| 49 | TestSuite *suite = (TestSuite *)malloc(sizeof(TestSuite)); |
| 50 | suite->name = name; |
| 51 | suite->filename = filename; |
| 52 | suite->line = line; |
| 53 | suite->tests = NULL; |
| 54 | suite->setup = &do_nothing; |
| 55 | suite->teardown = &do_nothing; |
| 56 | suite->size = 0; |
| 57 | return suite; |
| 58 | } |
| 59 | |
| 60 | void destroy_test_suite(TestSuite *suiteToDestroy) { |
| 61 | int i; |
| 62 | for (i = 0; i < suiteToDestroy->size; i++) { |
| 63 | UnitTest test = suiteToDestroy->tests[i]; |
| 64 | TestSuite* suite = test.Runnable.suite; |
| 65 | if (test_suite == test.type && suite != NULL) { |
| 66 | suiteToDestroy->tests[i].Runnable.suite = NULL; |
| 67 | destroy_test_suite(suite); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | if (suiteToDestroy->tests != NULL) |
| 72 | free(suiteToDestroy->tests); |
| 73 | |
| 74 | free(suiteToDestroy); |
| 75 | } |
| 76 | |
| 77 | void add_test_(TestSuite *suite, const char *name, CgreenTest *test) { |
| 78 | suite->size++; |
| 79 | suite->tests = (UnitTest *)realloc(suite->tests, sizeof(UnitTest) * suite->size); |
| 80 | suite->tests[suite->size - 1].type = test_function; |
| 81 | suite->tests[suite->size - 1].name = name; |
| 82 | suite->tests[suite->size - 1].Runnable.test = test; |
| 83 | } |
| 84 | |
| 85 | void add_tests_(TestSuite *suite, const char *names, ...) { |
| 86 | CgreenVector *test_names = create_vector_of_names(names); |
| 87 | int i; |
| 88 | va_list tests; |
| 89 | va_start(tests, names); |
| 90 | for (i = 0; i < cgreen_vector_size(test_names); i++) { |
| 91 | add_test_(suite, (char *)(cgreen_vector_get(test_names, i)), va_arg(tests, CgreenTest *)); |
| 92 | } |
| 93 | va_end(tests); |
| 94 | destroy_cgreen_vector(test_names); |
| 95 | } |
| 96 | |
| 97 | void add_suite_(TestSuite *owner, const char *name, TestSuite *suite) { |
| 98 | owner->size++; |
| 99 | owner->tests = (UnitTest *)realloc(owner->tests, sizeof(UnitTest) * owner->size); |
| 100 | owner->tests[owner->size - 1].type = test_suite; |
| 101 | owner->tests[owner->size - 1].name = name; |
| 102 | owner->tests[owner->size - 1].Runnable.suite = suite; |
| 103 | } |
| 104 | |
| 105 | void set_setup(TestSuite *suite, void (*set_up)(void)) { |
| 106 | suite->setup = set_up; |
| 107 | } |
| 108 | |
| 109 | void set_teardown(TestSuite *suite, void (*tear_down)(void)) { |
| 110 | suite->teardown = tear_down; |
| 111 | } |
| 112 | |
| 113 | int count_tests(TestSuite *suite) { |
| 114 | int count = 0; |
| 115 | int i; |
| 116 | for (i = 0; i < suite->size; i++) { |
| 117 | if (suite->tests[i].type == test_function) { |
| 118 | count++; |
| 119 | } else { |
| 120 | count += count_tests(suite->tests[i].Runnable.suite); |
| 121 | } |
| 122 | } |
| 123 | return count; |
| 124 | } |
| 125 |