Skip to content

Instantly share code, notes, and snippets.

@martinmoene
Created October 21, 2014 15:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martinmoene/9a231dc5b5be6a225011 to your computer and use it in GitHub Desktop.
Save martinmoene/9a231dc5b5be6a225011 to your computer and use it in GitHub Desktop.
// lest light-weight sections
// [1] Catch: Test cases and sections,
// https://github.com/philsquared/Catch/blob/develop/docs/tutorial.md#test-cases-and-sections
// [2] lest – lest errors escape testing,
// https://github.com/martinmoene/lest#lest--lest-errors-escape-testing---10
#include <cassert>
#include <iostream>
#include <string>
#include <vector>
#define EXPECT(...) assert(__VA_ARGS__)
#define MK_NAM3( name, line ) name##line
#define MK_NAM2( name, line ) MK_NAM3( name, line )
#define MK_NAME( name ) MK_NAM2( name, __LINE__ )
#define SETUP(...) \
for ( int $section = 0, $count = 1; $section < $count; ++$section )
#define SECTION( name ) \
static int MK_NAME( $id ) = 0; \
if ( $guard $run = $guard( MK_NAME( $id ), $section, $count, name ) ) \
for ( int $section = 0, $count = 1; $section < $count; ++$section )
struct $guard
{
int & id;
int const & section;
std::string const & name;
$guard( int & id, int const & section, int & count, std::string const & name )
: id( id ), section( section ), name( name )
{
if ( section == 0 )
{
id = count++;
}
}
operator bool() { return id == section; }
};
// single level of setup, teardown:
void CASE_A()
{
// SETUP
// first pass counts number of sections:
for ( int $section = 0, $count = 1; $section < $count; ++$section )
{
int i = 42;
std::cout << "case A start, i:" << i << "\n";
// SECTION
static int id1 = 0;
std::cout << "section A1 id1:" << id1 << "\n";
if ( $guard $run = $guard( id1, $section, $count, "Section A1" ) )
{
EXPECT( i == 42 );
std::cout << "section A1 start, i:" << i << "\n";
i = 66;
std::cout << "section A1 body, i:" << i << "\n";
}
// SECTION
static int id2 = 0;
std::cout << "section A2 id2:" << id2 << "\n";
if ( $guard $run = $guard( id2, $section, $count, "Section A2" ) )
{
EXPECT( i == 42 );
std::cout << "section A2 start, i:" << i << "\n";
i = 77;
std::cout << "section A2 body, i:" << i << "\n";
}
}
}
// setup, teardown at section level:
void CASE_B()
{
// SETUP
// first pass counts number of sections:
for ( int $section = 0, $count = 1; $section < $count; ++$section )
{
// SECTION
static int id1 = 0;
if ( $guard $run = $guard( id1, $section, $count, "Section A1" ) )
for ( int $section = 0, $count = 1; $section < $count; ++$section )
{
int i = 42;
// SECTION
static int id1 = 0;
if ( $guard $run = $guard( id1, $section, $count, "Section A2" ) )
for ( int $section = 0, $count = 1; $section < $count; ++$section )
{
i = 777;
}
// SECTION
static int id2 = 0;
if ( $guard $run = $guard( id2, $section, $count, "Section A2" ) )
for ( int $section = 0, $count = 1; $section < $count; ++$section )
{
EXPECT( i == 42 );
}
}
}
}
void CASE_C()
{
SETUP("Sections share setup and (scoped) teardown code")
{
int i = 42;
SECTION( "Section 1" )
{
i = 1;
int j = 42;
SECTION( "Section 11" )
{
i = j = 11;
int k = 42;
SECTION( "Section 111" )
{
i = j = k = 111;
}
SECTION( "Section 112" )
{
EXPECT( i == 11 );
EXPECT( j == 11 );
EXPECT( k == 42 );
}
}
SECTION( "Section 12" )
{
EXPECT( i == 1 );
EXPECT( j == 42 );
}
}
SECTION( "Section 2" )
{
EXPECT( i == 42 );
}
}
}
#define SCENARIO(...) void CASE_S()
#define GIVEN(name ) SETUP( std::string("Given: " ) + name )
#define WHEN( name ) SECTION( std::string("When: " ) + name )
#define THEN( name ) SECTION( std::string("Then: " ) + name )
SCENARIO( "vectors can be sized and resized", "[vector]" ) {
GIVEN( "A vector with some items" ) {
std::vector<int> v( 5 );
EXPECT( v.size() == 5 );
EXPECT( v.capacity() >= 5 );
WHEN( "the size is increased" ) {
v.resize( 10 );
THEN( "the size and capacity change" ) {
EXPECT( v.size() == 10 );
EXPECT( v.capacity() >= 10 );
}
continue;
}
WHEN( "the size is reduced" ) {
v.resize( 0 );
THEN( "the size changes but not capacity" ) {
EXPECT( v.size() == 0 );
EXPECT( v.capacity() >= 5 );
}
continue;
}
WHEN( "more capacity is reserved" ) {
v.reserve( 10 );
THEN( "the capacity changes but not the size" ) {
EXPECT( v.size() == 5 );
EXPECT( v.capacity() >= 10 );
}
continue;
}
WHEN( "less capacity is reserved" ) {
v.reserve( 0 );
THEN( "neither size nor capacity are changed" ) {
EXPECT( v.size() == 5 );
EXPECT( v.capacity() >= 5 );
}
continue;
}
}
}
int main()
{
CASE_A();
CASE_B();
CASE_C();
CASE_S();
}
// cl -nologo -W3 -EHsc -I.. lest_sections.cpp && lest_sections
// g++ -Wall -Wextra -Wmissing-include-dirs -std=c++11 -I.. -o lest_sections.exe lest_sections.cpp && lest_sections
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment