| 1 | #include <stdexcept> |
| 2 | #include <memory> |
| 3 | #include <stdio.h> |
| 4 | #include <stdlib.h> |
| 5 | #include <string> |
| 6 | #include <string.h> |
| 7 | #include <typeinfo> |
| 8 | #include <iostream> |
| 9 | |
| 10 | #include <cgreen/cgreen.h> |
| 11 | |
| 12 | using namespace cgreen; |
| 13 | |
| 14 | class MyType |
| 15 | { |
| 16 | public: |
| 17 | double x_; |
| 18 | double y_; |
| 19 | uint64_t z_; |
| 20 | MyType(double x, double y, uint64_t z) : x_(x), y_(y), z_(z) {} |
| 21 | bool operator==(MyType& other) |
| 22 | { |
| 23 | return this->z_ == other.z_; |
| 24 | } |
| 25 | bool operator!=(MyType& other) |
| 26 | { |
| 27 | return this->z_ != other.z_; |
| 28 | } |
| 29 | |
| 30 | std::ostream& operator<<(std::ostream& stream) |
| 31 | { |
| 32 | return stream << "x: " << x_ << " " << "y: " << y_ << " " << "z: " << z_ << std::endl; |
| 33 | } |
| 34 | }; |
| 35 | |
| 36 | static std::string* bob_pointer = NULL; |
| 37 | static std::string* alice_pointer = NULL; |
| 38 | static std::string* exception_pointer = NULL; |
| 39 | |
| 40 | void setup(void) { |
| 41 | bob_pointer = alice_pointer = exception_pointer = NULL; |
| 42 | // for testing output of exceptions during setup |
| 43 | // throw std::logic_error("this is why we fail"); |
| 44 | } |
| 45 | |
| 46 | // TODO: future featre to use stream operators to print expected/actual |
| 47 | //Ensure(outputs_expected_value_using_stream_operator) { |
| 48 | // MyType first = { 1.1, 2.2, 31337 }; |
| 49 | // MyType second = { 1.1, 2.2, 65536 }; |
| 50 | // assert_that(first, is_not_equal_to(second)); |
| 51 | //} |
| 52 | |
| 53 | Ensure(custom_large_classes_are_equal) { |
| 54 | MyType my_class(3.1337f, 3.14159f, 90210); |
| 55 | MyType other(0.0f, 0.0f, 90210); |
| 56 | assert_that(my_class == other); |
| 57 | } |
| 58 | |
| 59 | Ensure(custom_large_classes_are_not_equal) { |
| 60 | MyType my_class(3.1337f, 3.14159f, 90210); |
| 61 | MyType other(3.1337f, 3.14159f, 90211); |
| 62 | assert_that(my_class != other); |
| 63 | } |
| 64 | |
| 65 | Ensure(same_stl_string_references_are_equal) { |
| 66 | std::string bob("bob"); |
| 67 | assert_that(bob, is_equal_to_string(bob)); |
| 68 | } |
| 69 | |
| 70 | Ensure(stl_string_contains_string) { |
| 71 | std::string bobbob("bobbob"); |
| 72 | assert_that(bobbob, contains_string("obbo")); |
| 73 | } |
| 74 | |
| 75 | Ensure(string_contains_stl_string) { |
| 76 | std::string obbo("obbo"); |
| 77 | assert_that("bobbob", contains_string(obbo)); |
| 78 | } |
| 79 | |
| 80 | Ensure(string_contains_stl_string_pointer) { |
| 81 | bob_pointer = new std::string("obbo"); |
| 82 | assert_that("bobbob", contains_string(bob_pointer)); |
| 83 | } |
| 84 | |
| 85 | Ensure(stl_string_does_not_contain_string) { |
| 86 | std::string bobbob("bobbob"); |
| 87 | assert_that(bobbob, does_not_contain_string("alice")); |
| 88 | } |
| 89 | |
| 90 | Ensure(string_does_not_contain_stl_string) { |
| 91 | std::string obbo("obbo"); |
| 92 | assert_that("alice", does_not_contain_string(obbo)); |
| 93 | } |
| 94 | |
| 95 | Ensure(string_does_not_contain_stl_string_pointer) { |
| 96 | bob_pointer = new std::string("bob"); |
| 97 | assert_that("alice", does_not_contain_string(bob_pointer)); |
| 98 | } |
| 99 | |
| 100 | Ensure(stl_string_begins_with_string) { |
| 101 | std::string bobbob("bobbob"); |
| 102 | assert_that(bobbob, begins_with_string("bob")); |
| 103 | } |
| 104 | |
| 105 | Ensure(string_begins_with_stl_string) { |
| 106 | std::string bob("bob"); |
| 107 | assert_that("bobbob", begins_with_string(bob)); |
| 108 | } |
| 109 | |
| 110 | Ensure(string_begins_with_stl_string_pointer) { |
| 111 | bob_pointer = new std::string("bob"); |
| 112 | assert_that("bobbob", begins_with_string(bob_pointer)); |
| 113 | } |
| 114 | |
| 115 | Ensure(different_stl_string_references_are_not_equal) { |
| 116 | std::string bob("bob"); |
| 117 | std::string alice("alice"); |
| 118 | assert_that(bob, is_not_equal_to_string(alice)); |
| 119 | } |
| 120 | |
| 121 | Ensure(different_stl_string_pointers_are_not_equal) { |
| 122 | bob_pointer = new std::string("bob"); |
| 123 | alice_pointer = new std::string("alice"); |
| 124 | |
| 125 | assert_that(bob_pointer, is_not_equal_to_string(alice_pointer)); |
| 126 | } |
| 127 | |
| 128 | Ensure(different_stl_string_pointer_and_reference_are_not_equal) { |
| 129 | bob_pointer = new std::string("bob"); |
| 130 | std::string alice("alice"); |
| 131 | |
| 132 | assert_that(bob_pointer, is_not_equal_to_string(alice)); |
| 133 | } |
| 134 | |
| 135 | Ensure(different_stl_string_reference_and_pointer_are_not_equal) { |
| 136 | bob_pointer = new std::string("bob"); |
| 137 | std::string alice("alice"); |
| 138 | |
| 139 | assert_that(alice, is_not_equal_to_string(bob_pointer)); |
| 140 | } |
| 141 | |
| 142 | Ensure(same_stl_string_pointer_and_reference_are_equal) { |
| 143 | alice_pointer = new std::string("alice"); |
| 144 | std::string alice_reference("alice"); |
| 145 | |
| 146 | assert_that(alice_pointer, is_equal_to_string(alice_reference)); |
| 147 | } |
| 148 | |
| 149 | Ensure(same_stl_string_reference_and_pointer_are_equal) { |
| 150 | alice_pointer = new std::string("alice"); |
| 151 | std::string alice_reference("alice"); |
| 152 | |
| 153 | assert_that(alice_reference, is_equal_to_string(alice_pointer)); |
| 154 | } |
| 155 | |
| 156 | Ensure(stl_string_pointer_is_not_null) { |
| 157 | bob_pointer = new std::string("bob"); |
| 158 | |
| 159 | assert_that(bob_pointer, is_non_null); |
| 160 | } |
| 161 | |
| 162 | Ensure(stl_string_pointer_is_null) { |
| 163 | std::string *null_string = (std::string *)NULL; |
| 164 | assert_that(null_string, is_null); |
| 165 | } |
| 166 | |
| 167 | // for quickly iterating on failure message formatting improvements |
| 168 | Ensure(stl_string_length_assertion_failure_is_readable) { |
| 169 | // auto bob = new std::string("bob"); |
| 170 | // assert_that(bob->length(), is_not_equal_to(strlen("bob"))); |
| 171 | } |
| 172 | |
| 173 | Ensure(std_exception_what_is_output) { |
| 174 | // throw std::logic_error("this is why we fail"); |
| 175 | } |
| 176 | |
| 177 | static void throwing_function(void) { |
| 178 | throw std::string("throwing_function test"); |
| 179 | } |
| 180 | |
| 181 | Ensure(assert_throws_macro_passes_basic_type) { |
| 182 | assert_throws(std::string, throwing_function()); |
| 183 | } |
| 184 | |
| 185 | static void pointer_throwing_function(void) { |
| 186 | exception_pointer = new std::string("pointer_throwing_function test"); |
| 187 | throw exception_pointer; |
| 188 | } |
| 189 | |
| 190 | Ensure(assert_throws_macro_passes_pointer_type) { |
| 191 | assert_throws(std::string, pointer_throwing_function()); |
| 192 | } |
| 193 | |
| 194 | void teardown(void) { |
| 195 | delete bob_pointer; |
| 196 | delete alice_pointer; |
| 197 | delete exception_pointer; |
| 198 | } |
| 199 | |
| 200 | TestSuite* cpp_assertion_tests() { |
| 201 | TestSuite *suite = create_test_suite(); |
| 202 | set_setup(suite, setup); |
| 203 | set_teardown(suite, teardown); |
| 204 | add_test(suite, custom_large_classes_are_equal); |
| 205 | add_test(suite, custom_large_classes_are_not_equal); |
| 206 | add_test(suite, same_stl_string_references_are_equal); |
| 207 | add_test(suite, stl_string_contains_string); |
| 208 | add_test(suite, string_contains_stl_string); |
| 209 | add_test(suite, string_contains_stl_string_pointer); |
| 210 | add_test(suite, stl_string_does_not_contain_string); |
| 211 | add_test(suite, string_does_not_contain_stl_string); |
| 212 | add_test(suite, string_does_not_contain_stl_string_pointer); |
| 213 | add_test(suite, stl_string_begins_with_string); |
| 214 | add_test(suite, string_begins_with_stl_string); |
| 215 | add_test(suite, string_begins_with_stl_string_pointer); |
| 216 | add_test(suite, different_stl_string_references_are_not_equal); |
| 217 | add_test(suite, different_stl_string_pointers_are_not_equal); |
| 218 | add_test(suite, different_stl_string_pointer_and_reference_are_not_equal); |
| 219 | add_test(suite, different_stl_string_reference_and_pointer_are_not_equal); |
| 220 | add_test(suite, same_stl_string_pointer_and_reference_are_equal); |
| 221 | add_test(suite, same_stl_string_reference_and_pointer_are_equal); |
| 222 | add_test(suite, stl_string_pointer_is_not_null); |
| 223 | add_test(suite, stl_string_pointer_is_null); |
| 224 | add_test(suite, stl_string_length_assertion_failure_is_readable); |
| 225 | add_test(suite, std_exception_what_is_output); |
| 226 | add_test(suite, assert_throws_macro_passes_basic_type); |
| 227 | add_test(suite, assert_throws_macro_passes_pointer_type); |
| 228 | return suite; |
| 229 | } |