|
| 1 | +// Copyright 2016 Katherine Whitlock |
| 2 | + |
| 3 | +#include "cppspec.hpp" |
| 4 | + |
| 5 | +describe a_suite("A suite", $ { |
| 6 | + it("contains a spec with an expectation", _ { |
| 7 | + expect(true).to_be_true(); |
| 8 | + }); |
| 9 | +}); |
| 10 | + |
| 11 | +describe suite_object("A suite is just an object wrapping a lambda", $ { |
| 12 | + bool a = false; |
| 13 | + |
| 14 | + it("and so is a spec", _ { |
| 15 | + a = true; |
| 16 | + expect(a).to_be_true(); |
| 17 | + }); |
| 18 | +}); |
| 19 | + |
| 20 | +describe to_be_compare("The 'to_satisfy' matcher compares equality via evaluation", |
| 21 | + $ { |
| 22 | + it("and has a positive case", _ { |
| 23 | + expect(true).to_satisfy([](const bool &b) { return b == true; }); |
| 24 | + }); |
| 25 | + |
| 26 | + it("and can have a negative case", _ { |
| 27 | + expect(false).not_().to_satisfy([](const bool &b) { return b == true; }); |
| 28 | + }); |
| 29 | +}); |
| 30 | + |
| 31 | + |
| 32 | +describe to_equal_compare("The 'to_equal' matcher compares with '=='", $ { |
| 33 | + it("and has a positive case", _ { |
| 34 | + expect(true).to_equal(true); |
| 35 | + }); |
| 36 | + |
| 37 | + it("and can have a negative case", _ { |
| 38 | + expect(false).not_().to_equal(true); |
| 39 | + }); |
| 40 | +}); |
| 41 | + |
| 42 | +struct AB { int a; int b; }; |
| 43 | +inline bool operator==(const AB& lhs, const AB&rhs) { |
| 44 | + return lhs.a == rhs.a && lhs.b == rhs.b; |
| 45 | +} |
| 46 | + |
| 47 | + |
| 48 | +describe included_matchers("Included matchers:", $ { |
| 49 | + it("The 'to_be' matcher compares equality via lambda evaluation", _ { |
| 50 | + int a = 12; |
| 51 | + int b = a; |
| 52 | + |
| 53 | + expect(a).to_satisfy([b](const int &value) -> bool { return value == b;}); |
| 54 | + // to_equal is shorter for things that can be compared |
| 55 | + // with operator== |
| 56 | + expect(a).to_equal(b); |
| 57 | + }); |
| 58 | + |
| 59 | + context("The 'to_equal' matcher", _ { |
| 60 | + it("works for simple literals and variables", _ { |
| 61 | + auto a = 12; |
| 62 | + expect(a).to_equal(12); |
| 63 | + }); |
| 64 | + |
| 65 | + it("should work for objects with operator== defined", _ { |
| 66 | + AB foo; |
| 67 | + foo.a = 12; |
| 68 | + foo.b = 34; |
| 69 | + |
| 70 | + AB bar; |
| 71 | + bar.a = 12; |
| 72 | + bar.b = 34; |
| 73 | + |
| 74 | + expect(foo).to_equal(bar); |
| 75 | + }); |
| 76 | + }); |
| 77 | + // Not yet implemented |
| 78 | +// it("The 'to_match' matcher is for regular expressions", _ { |
| 79 | +// std::string message = "foo bar baz"; |
| 80 | +// |
| 81 | +// expect(message).to_match(std::regex("bar")); |
| 82 | +// expect(message).to_match("bar"); |
| 83 | +// expect(message).not_().to_match(std::regex("quux")); |
| 84 | +// }); |
| 85 | + |
| 86 | + it("The 'to_be_truthy' matcher is for boolean casting testing", _ { |
| 87 | + void* a = nullptr; |
| 88 | + //auto foo = "foo"; |
| 89 | + |
| 90 | +// expect(foo).to_be_truthy(); |
| 91 | + expect(a).not_().to_be_truthy(); |
| 92 | + }); |
| 93 | + |
| 94 | + it("The 'to_be_falsy' matcher is for boolean casting testing", _ { |
| 95 | + void* a = nullptr; |
| 96 | + //auto foo = "foo"; |
| 97 | + |
| 98 | + expect(a).to_be_falsy(); |
| 99 | +// expect(foo).not_().to_be_falsy(); |
| 100 | + }); |
| 101 | + |
| 102 | + it("The 'to_include' matcher is for finding an item in a container", _ { |
| 103 | + auto a = {"foo", "bar", "baz"}; |
| 104 | + |
| 105 | + expect(a).to_contain("bar"); |
| 106 | + expect(a).not_().to_contain("quux"); |
| 107 | + }); |
| 108 | + |
| 109 | + it("The 'to_be_less_than' matcher is for mathematical comparisons", _ { |
| 110 | + auto pi = 3.1415926, |
| 111 | + e = 2.78; |
| 112 | + |
| 113 | + expect(e).to_be_less_than(pi); |
| 114 | + expect(pi).not_().to_be_less_than(e); |
| 115 | + }); |
| 116 | + |
| 117 | + it("The 'to_be_greater_than' matcher is for mathematical comparisons", _ { |
| 118 | + auto pi = 3.1415926, |
| 119 | + e = 2.78; |
| 120 | + |
| 121 | + expect(pi).to_be_greater_than(e); |
| 122 | + expect(e).not_().to_be_greater_than(pi); |
| 123 | + }); |
| 124 | + |
| 125 | +// TODO: take another look at Jasmine's toBeCloseTo |
| 126 | +// it("The 'to_be_within' matcher is for precision match comparison", _ { |
| 127 | +// auto pi = 3.1415926, |
| 128 | +// e = 2.78; |
| 129 | +// |
| 130 | +// expect(pi).not_().to_be_within(0.5).of(e); |
| 131 | +// }); |
| 132 | + |
| 133 | + it("The 'to_throw' matcher is for testing if a function throws an exception", _ { |
| 134 | + std::function<int()> foo = [] { |
| 135 | + return 1 + 2; |
| 136 | + }; |
| 137 | + std::function<void*()> bar = [] { |
| 138 | + throw(std::exception()); |
| 139 | + return nullptr; |
| 140 | + }; |
| 141 | + |
| 142 | + expect(foo).not_().to_throw(); |
| 143 | + expect(bar).to_throw(); |
| 144 | + }); |
| 145 | +}); |
| 146 | + |
| 147 | + |
| 148 | +//describe fail_function_spec("A spec using the fail function", $ { |
| 149 | +// auto foo = [](auto &x, auto &callback) { |
| 150 | +// if (x) { |
| 151 | +// callback(); |
| 152 | +// } |
| 153 | +// }; |
| 154 | +// |
| 155 | +// it("should not call the callback", _ { |
| 156 | +// foo(false, [] -> { |
| 157 | +// fail("Callback has been called"); |
| 158 | +// }); |
| 159 | +// }); |
| 160 | +//}); |
| 161 | + |
| 162 | +describe a_spec("A spec", $ { |
| 163 | + it("is just a function, so it can contain any code", _ { |
| 164 | + auto foo = 0; |
| 165 | + foo += 1; |
| 166 | + expect(foo).to_equal(1); |
| 167 | + }); |
| 168 | + |
| 169 | + it("can have more than one expectation", _ { |
| 170 | + auto foo = 0; |
| 171 | + foo += 1; |
| 172 | + |
| 173 | + expect(foo).to_equal(1); |
| 174 | + expect(true).to_equal(true); |
| 175 | + }); |
| 176 | +}); |
| 177 | + |
| 178 | +describe a_spec_using_be_ae("A spec using before_each and after_each", $ { |
| 179 | + auto foo = 0; |
| 180 | + |
| 181 | + before_all([&foo]{ foo = 1; }); |
| 182 | + after_all([&foo]{ foo = 0; }); |
| 183 | + |
| 184 | + it("sets the initial value of foo before specs run", [&](auto &self) { |
| 185 | + expect(foo).to_equal(1); |
| 186 | + foo += 1; |
| 187 | + }); |
| 188 | + |
| 189 | + it("does not reset foo between specs", _ { |
| 190 | + expect(foo).to_equal(2); |
| 191 | + }); |
| 192 | +}); |
| 193 | + |
| 194 | +describe a_spec_before_each("A spec", $ { |
| 195 | + int foo; |
| 196 | + before_each([&foo]{ foo = 0; }); |
| 197 | + |
| 198 | + it("can use a variable to share state", _ { |
| 199 | + expect(foo).to_equal(0); |
| 200 | + foo += 1; |
| 201 | + }); |
| 202 | + |
| 203 | + it("prevents test pollution by starting with a clean test environment", _ { |
| 204 | + expect(foo).to_equal(0); |
| 205 | + }); |
| 206 | +}); |
| 207 | + |
| 208 | +describe a_spec_nesting("A spec", $ { |
| 209 | + int foo; |
| 210 | + |
| 211 | + before_each([&foo] { |
| 212 | + foo = 0; |
| 213 | + foo += 1; |
| 214 | + }); |
| 215 | + |
| 216 | + after_each([&foo] { |
| 217 | + foo = 0; |
| 218 | + }); |
| 219 | + |
| 220 | + it("is just a function, so it can contain any code", _ { |
| 221 | + expect(foo).to_equal(1); |
| 222 | + }); |
| 223 | + |
| 224 | + it("can have more than one expectation", _ { |
| 225 | + expect(foo).to_equal(1); |
| 226 | + expect(true).to_equal(true); |
| 227 | + }); |
| 228 | + |
| 229 | + explain("nested contexts", _ { |
| 230 | + int bar; |
| 231 | + |
| 232 | + before_each([&bar] { |
| 233 | + bar = 1; |
| 234 | + }); |
| 235 | + |
| 236 | + it("can reference both scopes as needed", _ { |
| 237 | + expect(foo).to_equal(bar); |
| 238 | + }); |
| 239 | + }); |
| 240 | +}); |
| 241 | + |
| 242 | +// xdescribes are unnecessary, as test are simply not run or |
| 243 | +// added to the Runner object. |
| 244 | + |
| 245 | +// TODO: pending specs |
| 246 | + |
| 247 | +describe to_include_matcher("to contain", $ { |
| 248 | + let(foo, ([]() -> std::vector<int> { return {1,2,3,4}; })); |
| 249 | + |
| 250 | + it("matches arrays with some of the values", _ { |
| 251 | + expect(foo).to_contain({3,1}); |
| 252 | + expect(foo).not_().to_contain(6); |
| 253 | + }); |
| 254 | +}); |
| 255 | + |
| 256 | +int main() { |
| 257 | + return CppSpec::Runner(CppSpec::Formatters::verbose) |
| 258 | + .add_spec(a_suite) |
| 259 | + .add_spec(suite_object) |
| 260 | + .add_spec(to_be_compare) |
| 261 | + .add_spec(to_equal_compare) |
| 262 | + .add_spec(included_matchers) |
| 263 | + .add_spec(a_spec) |
| 264 | + .add_spec(a_spec_using_be_ae) |
| 265 | + .add_spec(a_spec_before_each) |
| 266 | + .add_spec(a_spec_nesting) |
| 267 | + .add_spec(to_include_matcher) |
| 268 | + .exec() ? EXIT_SUCCESS : EXIT_FAILURE; |
| 269 | +} |
0 commit comments