|
| 1 | +# Testing |
| 2 | + |
| 3 | +Running tests requires a `Description` object to provide the spec. Each spec is independent. |
| 4 | + |
| 5 | +Tests are added to a `Runner` manually, which is passed a `Formatter` object on instantiation. |
| 6 | + |
| 7 | +Once a spec has been added to a `Runner`, the runner is then executed, returning a `Result` object. |
| 8 | + |
| 9 | +`Result` objects are able to be implicitly casted to `bool`, and can thus be used for returning |
| 10 | +directly from the `main` function. |
| 11 | + |
| 12 | + |
| 13 | +## Formatters |
| 14 | + |
| 15 | +There are a number of `Formatter` subclasses for printing to a terminal, including `Verbose`, `Progress`, and `TAP`. `Progress` prints out as a serious of periods, while `Verbose` prints a |
| 16 | +fully RSpec-like list of tests, coloring them to show their status and result. |
| 17 | + |
| 18 | +## Example |
| 19 | + |
| 20 | +Here's an example spec and the associated runner: |
| 21 | + |
| 22 | +```c++ |
| 23 | +#include <cstdlib> |
| 24 | +#include "cppspec.hpp" |
| 25 | + |
| 26 | +describe strcmp_spec("int strcmp ( const char * str1, const char * str2 )", $ { |
| 27 | + auto greater_than_zero = [](int i){return i>=0;}; |
| 28 | + auto less_than_zero = [](int i){return i<0;}; |
| 29 | + |
| 30 | + it("returns 0 only when strings are equal", _ { |
| 31 | + expect(strcmp("hello", "hello")).to_equal(0); |
| 32 | + }); |
| 33 | + |
| 34 | + it("returns a negative integer when str1 is less than str2", _ { |
| 35 | + expect(strcmp("hello", "world")).to_satisfy(less_than_zero); |
| 36 | + expect(strcmp("0123", "1321431")).to_satisfy(less_than_zero); |
| 37 | + }); |
| 38 | + |
| 39 | + it("returns a positive integer if str1 is greater than str2", _ { |
| 40 | + expect(strcmp("yellow", "world")).to_satisfy(greater_than_zero); |
| 41 | + expect(strcmp("9", "789")).to_satisfy(greater_than_zero); |
| 42 | + }); |
| 43 | +}); |
| 44 | + |
| 45 | + |
| 46 | +int main(){ |
| 47 | + return CppSpec::Runner(CppSpec::Formatters::verbose) |
| 48 | + .add_spec(strcmp_spec) |
| 49 | + .exec() ? EXIT_SUCCESS : EXIT_FAILURE; |
| 50 | +} |
| 51 | + |
| 52 | +``` |
0 commit comments