| 1 | #include <cgreen/cgreen.h> |
| 2 | |
| 3 | #include "../src/utils.h" |
| 4 | |
| 5 | #ifdef __cplusplus |
| 6 | using namespace cgreen; |
| 7 | #endif |
| 8 | |
| 9 | #include "runner.c" |
| 10 | |
| 11 | #include "test_item.h" |
| 12 | |
| 13 | #include "discoverer.mock" |
| 14 | #include "io.mock" |
| 15 | |
| 16 | |
| 17 | Describe(Runner); |
| 18 | |
| 19 | BeforeEach(Runner){} |
| 20 | |
| 21 | AfterEach(Runner){} |
| 22 | |
| 23 | #define CONTEXT_NAME "context" |
| 24 | #define TEST_NAME "test" |
| 25 | |
| 26 | #define STRINGIFY_X(x) #x |
| 27 | #define STRINGIFY(x) STRINGIFY_X(x) |
| 28 | |
| 29 | Ensure(Runner, can_get_context_name_of_name) { |
| 30 | char *context_name = context_name_of("Context:Test"); |
| 31 | assert_that(context_name, is_equal_to_string("Context")); |
| 32 | free(context_name); |
| 33 | |
| 34 | context_name = context_name_of("Test"); |
| 35 | assert_that(context_name, is_equal_to_string(CGREEN_DEFAULT_SUITE)); |
| 36 | free(context_name); |
| 37 | } |
| 38 | |
| 39 | |
| 40 | Ensure(Runner, can_get_test_name_of_symbolic_name) { |
| 41 | char *test_name = test_name_of("Context:Test"); |
| 42 | assert_that(test_name, is_equal_to_string("Test")); |
| 43 | free(test_name); |
| 44 | |
| 45 | test_name = test_name_of("Test"); |
| 46 | assert_that(test_name, is_equal_to_string("Test")); |
| 47 | free(test_name); |
| 48 | } |
| 49 | |
| 50 | |
| 51 | static void add_test_items_to_vector(TestItem items[], CgreenVector *test_items, int count) { |
| 52 | for (int i=0; i < count; i++) |
| 53 | cgreen_vector_add(test_items, &items[i]); |
| 54 | } |
| 55 | |
| 56 | |
| 57 | Ensure(Runner, can_ensure_test_exists_from_context_and_name) { |
| 58 | TestItem test_items[5] = { |
| 59 | {(char *)"", (char *)"Context1", (char *)"Test1"}, |
| 60 | {(char *)"", (char *)"Context1", (char *)"Test2"}, |
| 61 | {(char *)"", (char *)"Context2", (char *)"Test1"}, |
| 62 | {(char *)"", (char *)"Context2", (char *)"Test2"}}; |
| 63 | CgreenVector *tests = create_cgreen_vector(NULL); |
| 64 | add_test_items_to_vector(test_items, tests, 5); |
| 65 | |
| 66 | assert_that(matching_test_exists("Context1:Test1", tests)); |
| 67 | } |
| 68 | |
| 69 | Ensure(Runner, can_match_test_name) { |
| 70 | TestItem test_item = {(char *)"", (char *)"Context1", (char *)"Test1"}; |
| 71 | |
| 72 | assert_that(test_matches_pattern("Context1:Test1", &test_item)); |
| 73 | assert_that(test_matches_pattern("Context*:Test1", &test_item)); |
| 74 | assert_that(test_matches_pattern("*:Test1", &test_item)); |
| 75 | |
| 76 | assert_that(test_matches_pattern("Context*:Test1", &test_item)); |
| 77 | assert_that(test_matches_pattern("*:Test1", &test_item)); |
| 78 | |
| 79 | assert_that(test_matches_pattern("Context1:Test*", &test_item)); |
| 80 | assert_that(test_matches_pattern("Context*:Test*", &test_item)); |
| 81 | assert_that(test_matches_pattern("Context*:*", &test_item)); |
| 82 | assert_that(test_matches_pattern("*:Test*", &test_item)); |
| 83 | assert_that(test_matches_pattern("*:*", &test_item)); |
| 84 | } |
| 85 | |
| 86 | Ensure(Runner, can_add_test_to_the_suite_for_its_context) { |
| 87 | ContextSuite *suite_list = NULL; |
| 88 | CgreenTest *test = (CgreenTest *)&test; |
| 89 | TestSuite *parent_suite = create_test_suite(); |
| 90 | TestSuite *first_suite, *second_suite; |
| 91 | TestItem test_item1 = {"", "TheFirstContext", "TheName"}; |
| 92 | TestItem test_item2 = {"", "TheSecondContext", "TheName"}; |
| 93 | |
| 94 | assert_that(suite_list, is_null); |
| 95 | |
| 96 | add_test_to_context(parent_suite, &suite_list, &test_item1, test); |
| 97 | first_suite = find_suite_for_context(suite_list, "TheFirstContext"); |
| 98 | assert_that(first_suite, is_non_null); |
| 99 | assert_that(first_suite->size, is_equal_to(1)); |
| 100 | |
| 101 | second_suite = find_suite_for_context(suite_list, "TheSecondContext"); |
| 102 | assert_that(second_suite, is_null); |
| 103 | |
| 104 | add_test_to_context(parent_suite, &suite_list, &test_item2, test); |
| 105 | assert_that(find_suite_for_context(suite_list, "TheFirstContext")->size, is_equal_to(1)); |
| 106 | assert_that(find_suite_for_context(suite_list, "TheSecondContext")->size, is_equal_to(1)); |
| 107 | |
| 108 | destroy_test_suite(parent_suite); |
| 109 | destroy_context_suites(suite_list); |
| 110 | } |
| 111 | |
| 112 | Ensure(Runner, can_sort_an_empty_list_of_tests) { |
| 113 | CgreenVector *test_items = create_cgreen_vector(NULL); |
| 114 | |
| 115 | test_items = sorted_test_items_from(test_items); |
| 116 | |
| 117 | assert_that(cgreen_vector_size(test_items) == 0); |
| 118 | } |
| 119 | |
| 120 | Ensure(Runner, can_sort_a_list_of_a_single_tests) { |
| 121 | TestItem test_item = { |
| 122 | (char *)"", (char *)"Context1", (char *)"Test1", |
| 123 | }; |
| 124 | CgreenVector *test_items = create_cgreen_vector(NULL); |
| 125 | cgreen_vector_add(test_items, &test_item); |
| 126 | |
| 127 | test_items = sorted_test_items_from(test_items); |
| 128 | assert_that(((TestItem *)cgreen_vector_get(test_items, 0))->test_name, |
| 129 | is_equal_to_string("Test1")); |
| 130 | } |
| 131 | |
| 132 | Ensure(Runner, can_sort_a_list_of_two_unordered_tests) { |
| 133 | TestItem test_items[] = { |
| 134 | {(char *)"", (char *)"Context1", (char *)"Test2"}, |
| 135 | {(char *)"", (char *)"Context1", (char *)"Test1"}, |
| 136 | }; |
| 137 | |
| 138 | CgreenVector *tests = create_cgreen_vector(NULL); |
| 139 | add_test_items_to_vector(test_items, tests, 2); |
| 140 | |
| 141 | tests = sorted_test_items_from(tests); |
| 142 | |
| 143 | assert_that(((TestItem *)cgreen_vector_get(tests, 0))->test_name, |
| 144 | is_equal_to_string("Test1")); |
| 145 | assert_that(((TestItem *)cgreen_vector_get(tests, 1))->test_name, |
| 146 | is_equal_to_string("Test2")); |
| 147 | } |
| 148 | |
| 149 | Ensure(Runner, can_sort_an_ordered_list_of_two_tests) { |
| 150 | TestItem test_item[] = { |
| 151 | {(char *)"", (char *)"Context1", (char *)"Test1"}, |
| 152 | {(char *)"", (char *)"Context1", (char *)"Test2"} |
| 153 | }; |
| 154 | |
| 155 | CgreenVector *test_items = create_cgreen_vector(NULL); |
| 156 | cgreen_vector_add(test_items, &test_item[0]); |
| 157 | cgreen_vector_add(test_items, &test_item[1]); |
| 158 | |
| 159 | test_items = sorted_test_items_from(test_items); |
| 160 | |
| 161 | assert_that(((TestItem *)cgreen_vector_get(test_items, 0))->test_name, |
| 162 | is_equal_to_string("Test1")); |
| 163 | assert_that(((TestItem *)cgreen_vector_get(test_items, 1))->test_name, |
| 164 | is_equal_to_string("Test2")); |
| 165 | } |
| 166 | |
| 167 | Ensure(Runner, can_sort_an_unordered_list_of_tests) { |
| 168 | TestItem unordered_test_items[] = { |
| 169 | {(char *)"", (char *)"Context1", (char *)"Test9"}, |
| 170 | {(char *)"", (char *)"Context1", (char *)"Test6"}, |
| 171 | {(char *)"", (char *)"Context1", (char *)"Test3"}, |
| 172 | {(char *)"", (char *)"Context1", (char *)"Test1"}, |
| 173 | {(char *)"", (char *)"Context1", (char *)"Test5"}, |
| 174 | {(char *)"", (char *)"Context1", (char *)"Test8"}, |
| 175 | {(char *)"", (char *)"Context1", (char *)"Test7"}, |
| 176 | {(char *)"", (char *)"Context1", (char *)"Test4"}, |
| 177 | {(char *)"", (char *)"Context1", (char *)"Test2"}, |
| 178 | }; |
| 179 | const char *expected_test_name[] = { |
| 180 | "Test1", "Test2", "Test3", "Test4", "Test5", "Test6", "Test7", "Test8", "Test9" }; |
| 181 | |
| 182 | CgreenVector *tests = create_cgreen_vector(NULL); |
| 183 | add_test_items_to_vector(unordered_test_items, tests, |
| 184 | sizeof(unordered_test_items)/sizeof(unordered_test_items[0])); |
| 185 | |
| 186 | tests = sorted_test_items_from(tests); |
| 187 | |
| 188 | for (int i=0; i<cgreen_vector_size(tests); i++) |
| 189 | assert_that(((TestItem *)cgreen_vector_get(tests, i))->test_name, |
| 190 | is_equal_to_string(expected_test_name[i])); |
| 191 | } |
| 192 | |
| 193 | /* vim: set ts=4 sw=4 et cindent: */ |
| 194 | /* Local variables: */ |
| 195 | /* tab-width: 4 */ |
| 196 | /* End: */ |