catchorg / Catch2

A modern, C++-native, test framework for unit-tests, TDD and BDD - using C++14, C++17 and later (C++11 support is in v2.x branch, and C++03 on the Catch1.x branch)
https://discord.gg/4CWS9zD
Boost Software License 1.0
18.62k stars 3.05k forks source link

[Documentation suggestion] Provide executable examples and link to them #1037

Open martinmoene opened 7 years ago

martinmoene commented 7 years ago

Readers of Catch' documentation may benefit from readily available, complete and executable examples that show a limited and coherent set of features and steps needed for compilation. The tutorial and reference documentation can then link to relevant examples that the reader can try, perhaps even online.

Compilation of these examples must be integrated with the continuous integration process. As such the examples become executable documentation geared towards the user.

For example:

// Assertions

// Compile:
// - g++ -I$(CATCH_SINGLE_INCLUDE) -o assertions assertions.cpp && assertions --success
// - cl -EHsc -I%CATCH_SINGLE_INCLUDE% assertions.cpp && assertions --success

// Let Catch provide main():
#define CATCH_CONFIG_MAIN

#include "catch.hpp"

std::string one() {
    return "1";
}

TEST_CASE( "Assert that something is true (pass)", "[require]" ) {
    REQUIRE( one() == "1" );
}

TEST_CASE( "Assert that something is true (fail)", "[require]" ) {
    REQUIRE( one() == "x" );
}

TEST_CASE( "Assert that something is true (stop at first failure)", "[require]" ) {
    REQUIRE( one() == "x" );
    REQUIRE( one() == "1" );
}

TEST_CASE( "Assert that something is true (continue after failure)", "[check]" ) {
    CHECK(   one() == "x" );
    REQUIRE( one() == "1" );
}

Subjects that come to mind:

horenmar commented 7 years ago

Basically, you want parts of SelfTest to become self-contained and hosted on Wandbox or similar, correct?

martinmoene commented 7 years ago

Indeed, with the difference that SelfTest may be geared (too much) towards implementers, complete and overwhelming where mere users may need another approach to support their understanding.

Key:

See for example the examples linked from section Usage of lest.

horenmar commented 7 years ago

Yeah, SelfTest's primary aim is to (mostly) guarantee that things didn't get broken by a change, secondary aim is a reference for someone who already knows Catch well and documentation for newbies is a very distant third.

Anyway, I can see some value in adding these, but honestly don't have much time (and what little I do, I'd like to use for different things), so this will likely either get put on a backburner, or someone else needs to put in a PR (hint, hint).

martinmoene commented 7 years ago

I can see some value in adding these

is what I liked to know ;)

martinmoene commented 7 years ago

Plan (in progress):

Overall, part 1:

Overall, part 2 (in progress):

Examples, part 1:

Examples, part 2 (in progress):

Examples, rest:

ahadji05 commented 4 years ago

I installed the Catch2 framework using the CMake installation and when I try to compile and run any example I get this error: CATCH_SINGLE_INCLUDE: command not found

this is how I compile an example: $ g++ -std=c++11 -Wall -I$(CATCH_SINGLE_INCLUDE) -c 000-CatchMain.cpp

what is the purpose of the macro CATCH_SINGLE_INCLUDE ?

horenmar commented 4 years ago

@ahadji05 It should contain path to the single-include version of catch.hpp.