| 1 |
#include <cgreen/cgreen.h> |
| 2 |
#include <cgreen/mocks.h> |
| 3 |
|
| 4 |
#include "discoverer.h" |
| 5 |
#include "test_item.h" |
| 6 |
|
| 7 |
#include <stdbool.h> |
| 8 |
#include <stdio.h> |
| 9 |
#include <string.h> |
| 10 |
|
| 11 |
#include "io.h" |
| 12 |
#include "io.mocks" |
| 13 |
|
| 14 |
|
| 15 |
Describe(Discoverer); |
| 16 |
BeforeEach(Discoverer) {} |
| 17 |
AfterEach(Discoverer) {} |
| 18 |
|
| 19 |
|
| 20 |
static bool verbose = false; |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
static void expect_open_file(const char *filename, void *result) { |
| 25 |
expect(open_file, when(filename, |
| 26 |
is_equal_to_string(filename)), |
| 27 |
will_return(result)); |
| 28 |
} |
| 29 |
|
| 30 |
static void expect_open_process(const char *partial_command, void *result) { |
| 31 |
expect(open_process, when(command, contains_string(partial_command)), |
| 32 |
will_return(result)); |
| 33 |
} |
| 34 |
|
| 35 |
static void given_a_file_with_no_lines(const char *filename) { |
| 36 |
expect_open_file(filename, (void *)1); |
| 37 |
expect(close_file, when(file, is_equal_to(1))); |
| 38 |
expect_open_process("nm ", (void *)2); |
| 39 |
expect(read_line, when(file, is_equal_to(2)), |
| 40 |
will_return(EOF)); |
| 41 |
expect(close_process, when(file, is_equal_to(2))); |
| 42 |
} |
| 43 |
|
| 44 |
static void expect_read_line_from(int file_id, const char *line) { |
| 45 |
if (line == NULL) |
| 46 |
expect(read_line, when(file, is_equal_to(file_id)), |
| 47 |
will_return(EOF)); |
| 48 |
else |
| 49 |
expect(read_line, when(file, is_equal_to(file_id)), |
| 50 |
will_set_contents_of_output_parameter(buffer, line, strlen(line)+1), |
| 51 |
will_return(strlen(line)+1)); |
| 52 |
} |
| 53 |
|
| 54 |
static void given_a_file_with_two_lines(const char *filename, const char *line1, const char *line2) { |
| 55 |
static char command[100]; |
| 56 |
expect_open_file(filename, (void *)1); |
| 57 |
expect(close_file, when(file, is_equal_to(1))); |
| 58 |
sprintf(command, "nm '%s'", filename); |
| 59 |
expect_open_process(command, (void *)2); |
| 60 |
expect_read_line_from(2, line1); |
| 61 |
expect_read_line_from(2, line2); |
| 62 |
expect_read_line_from(2, NULL); |
| 63 |
expect(close_process, when(file, is_equal_to(2))); |
| 64 |
} |
| 65 |
|
| 66 |
static void given_a_file_with_one_line(const char *filename, const char *line) { |
| 67 |
expect_open_file(filename, (void *)1); |
| 68 |
expect(close_file, when(file, is_equal_to(1))); |
| 69 |
expect_open_process("nm ", (void *)2); |
| 70 |
expect_read_line_from(2, line); |
| 71 |
expect_read_line_from(2, NULL); |
| 72 |
expect(close_process, when(file, is_equal_to(2))); |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
Ensure(Discoverer, should_find_no_tests_in_non_existing_file) { |
| 79 |
expect_open_file("non-existing-file", NULL); |
| 80 |
|
| 81 |
CgreenVector *tests = discover_tests_in("non-existing-file", verbose); |
| 82 |
|
| 83 |
assert_that(tests, is_null); |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
Ensure(Discoverer, should_find_no_tests_in_existing_empty_file) { |
| 89 |
given_a_file_with_no_lines("empty-file"); |
| 90 |
|
| 91 |
CgreenVector *tests = discover_tests_in("empty-file", verbose); |
| 92 |
|
| 93 |
assert_that(cgreen_vector_size(tests), is_equal_to(0)); |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
Ensure(Discoverer, should_find_one_test_in_file_with_one_line_containing_testname_pattern) { |
| 99 |
given_a_file_with_one_line("some-file", |
| 100 |
"0000000000202160 D CgreenSpec__A_context__a_test__\n"); |
| 101 |
|
| 102 |
CgreenVector *tests = discover_tests_in("some-file", verbose); |
| 103 |
|
| 104 |
assert_that(cgreen_vector_size(tests), is_equal_to(1)); |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
Ensure(Discoverer, should_find_two_test_in_two_line_file_with_two_lines_containing_testname_pattern) { |
| 110 |
given_a_file_with_two_lines("some-file", |
| 111 |
"0000000000202160 D CgreenSpec__Context1__test1__\n", |
| 112 |
"0000000000202160 D CgreenSpec__Context2__test2__\n"); |
| 113 |
|
| 114 |
CgreenVector *tests = discover_tests_in("some-file", verbose); |
| 115 |
|
| 116 |
assert_that(cgreen_vector_size(tests), is_equal_to(2)); |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
Ensure(Discoverer, should_find_one_test_in_two_line_file_with_one_line_containing_testname_pattern) { |
| 122 |
given_a_file_with_two_lines("some-file", |
| 123 |
"0000000000202160 D CgreenSpec__Discoverer__test1__\n", |
| 124 |
"0000000000202160 D ID\n"); |
| 125 |
|
| 126 |
CgreenVector *tests = discover_tests_in("some-file", verbose); |
| 127 |
|
| 128 |
assert_that(cgreen_vector_size(tests), is_equal_to(1)); |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
Ensure(Discoverer, should_find_no_test_in_file_with_no_definiton) { |
| 134 |
given_a_file_with_one_line("some-file", |
| 135 |
"0000000000202160 U CgreenSpec__Discoverer__test1__\n"); |
| 136 |
|
| 137 |
CgreenVector *tests = discover_tests_in("some-file", verbose); |
| 138 |
|
| 139 |
assert_that(cgreen_vector_size(tests), is_equal_to(0)); |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
Ensure(Discoverer, should_strip_newline_at_end_of_specification_name) { |
| 145 |
given_a_file_with_one_line("some-file", |
| 146 |
"0000000000202160 U CgreenSpec__Discoverer__test1__\n"); |
| 147 |
|
| 148 |
CgreenVector *tests = discover_tests_in("some-file", verbose); |
| 149 |
|
| 150 |
assert_that(cgreen_vector_size(tests), is_equal_to(0)); |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
Ensure(Discoverer, should_return_valid_test_item_for_a_line_containing_testname_pattern) { |
| 156 |
given_a_file_with_one_line("some-file", |
| 157 |
"0000000000202160 D CgreenSpec__Context1__test_1__\n"); |
| 158 |
|
| 159 |
CgreenVector *tests = discover_tests_in("some-file", verbose); |
| 160 |
|
| 161 |
assert_that(cgreen_vector_size(tests), is_equal_to(1)); |
| 162 |
|
| 163 |
TestItem *test_item = (TestItem*)cgreen_vector_get(tests, 0); |
| 164 |
assert_that(test_item->specification_name, is_equal_to_string("CgreenSpec__Context1__test_1__")); |
| 165 |
assert_that(test_item->context_name, is_equal_to_string("Context1")); |
| 166 |
assert_that(test_item->test_name, is_equal_to_string("test_1")); |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
Ensure(Discoverer, can_handle_truncated_testnames) { |
| 171 |
given_a_file_with_one_line("some-file", "0000000000202160 D CgreenSpec__Context1__a\n"); |
| 172 |
|
| 173 |
CgreenVector *tests = discover_tests_in("some-file", verbose); |
| 174 |
|
| 175 |
TestItem *test_item = (TestItem*)cgreen_vector_get(tests, 0); |
| 176 |
assert_that(test_item->test_name, is_equal_to_string("a")); |
| 177 |
} |