Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "testy.h"
- using namespace std;
- using testy::Expect;
- int main() {
- // --------------------------------------------
- // Basic Usage
- // How to run a standalone test.
- // --------------------------------------------
- //
- // testy::immediate(std::string name, std::function<void(testy::Expect)> lambda);
- // --> Runs a test function.
- //
- // name : The test name.
- // lambda : The test function.
- //
- testy::immediate("Running Tests", [](Expect expect) {
- // Test code goes here.
- });
- // --------------------------------------------
- // Creating Expectations
- // How to actually test things.
- // --------------------------------------------
- //
- // Expect<T>(T value)
- // --> Creates an expectation for a value type.
- //
- // <T> : The expectation type. This can be inferred by the compiler.
- // value : The unknown value to assert.
- //
- testy::immediate("Expectations", [](Expect expect) {
- int unknown = /* do something that returns an int */;
- expect(unknown); // Does nothing yet.
- });
- //
- // Expect.function(std::function<void()> function)
- // --> Creates an expectation for a function type.
- //
- // function : The function to assert.
- //
- testy::immediate("Your Expectations Are Running", [](Expect expect) {
- int unknown = /* do something that returns an int */;
- expect.function([] {
- // Does nothing until asserted.
- });
- });
- // --------------------------------------------
- // Expectations
- // The syntactic sugar of assertions.
- // --------------------------------------------
- //
- // ValueExpectation<T>.equals(T value)
- // --> Assert two values using the == operator.
- //
- // value : The expected value.
- //
- // NOTE -- The ==(T, T) operator must be implemented.
- //
- testy::immediate("Equality", [](Expect expect) {
- int three = 3;
- int five = 5;
- expect(three).equals(five); // Test failed.
- });
- //
- // Expectation<T>.never().[...]
- // --> Negate an assertion.
- //
- // WARNING -- This is reset after each assertion.
- //
- testy::immediate("Negation", [](Expect expect) {
- int three = 3;
- int five = 5;
- expect(three).never().equals(five); // Passed.
- });
- //
- // Expectation<T>.that(std::string description).[...]
- // --> Describe an assertion.
- //
- // description : The description.
- //
- testy::immediate("Descriptions", [](Expect expect) {
- expect(2*2).that("Two times two is four.").equals(3);
- });
- //
- // FunctionExpectation.couts(std::string value)
- // --> Assert that the function prints a specific string to std::cout.
- //
- // value : The expected string.
- //
- testy::immediate("Couting", [](Expect expect) {
- expect.function([] {
- cout << "I'm sad\n";
- }).couts("I'm happy\n"); // Test failed.
- });
- //
- // FunctionExpectation.cerrs(std::string value)
- // --> Assert that the function prints a specific string to std::cerr.
- //
- // value : The expected string.
- //
- testy::immediate("Couting, With Cerr", [](Expect expect) {
- expect.function([] {
- cerr << "This went wrong.\n";
- }).cerrs(""); // Test failed.
- });
- //
- // FunctionExpectation.throws<T>()
- // --> Assert that the function throws a specific exception.
- //
- // <t> : The expected exception type.
- // value : The expected string.
- //
- testy::immediate("Except", [](Expect expect) {
- expect.function([] {
- throw runtime_error("Javelin");
- }).throws<runtime_error>(); // Test passed.
- });
- // --------------------------------------------
- // Logging
- // Where did it go wrong?
- // --------------------------------------------
- //
- // testy will tell you how things went wrong.
- // It's better than leaving a bunch of
- // std::cout << statements everywhere.
- //
- testy::immediate("Oops", [](Expect expect) {
- expect(true).that("Contradiction").equals(!true);
- // Oops: .................................... [failed]
- // ! Failure: Contradiction
- // ! Expected: 1
- // ! Received: 0
- });
- //
- // testy will automatically record everything
- // that went into std::cout and std::cerr. If
- // the test doesn't fail, you won't be bothered.
- //
- testy::immediate("Logging", [](Expect expect) {
- cout << "Good." << endl;
- cerr << "Bad." << endl;
- expect(true).that("Tautology").equals(true);
- // Logging: ................................. [passed]
- });
- //
- // Sometimes, you have a bunch of things to test,
- // but you don't want 50 different tests. Thanks to
- // Expectation.suppress, you can test everything in one.
- //
- testy::immediate("Suppression", [](Expect expect) {
- expect.suppress = true;
- cout << "Hello from the beginning." << endl;
- expect(true).that("Tautology").equals(true);
- expect(true).that("Contradiction").equals(false);
- expect(static_cast<string>("typo")).that("Spelling").equals("ytpo"); // C++ likes to default to const char* ...
- cerr << "Hello from the end." << endl;
- // Suppression: ............................. [failed]
- // ! Failure: Contradiction
- // ! Expected: 1
- // ! Received: 0
- // ! Failure: Spelling
- // ! Expected: typo
- // ! Received: ytpo
- // | Hello from the beginning.
- // | Hello from the end.
- });
- return 0;
- }
Add Comment
Please, Sign In to add comment