Menu

Spec++ 0.0.5 Released

Spec++ is a framework for practicing Behavior Driven Development(BDD) in C++.

The example below demonstrates how a Spec++ specification could look like. In
this short example we are developing a stack container. Our mission is to test
it's behavior. We start by setting up a helper class (stack_helper) that are
going to contain setup, teardown, helper methods and objects needed to test the
behavior. We can now decide what context we are testing our behavior in "An
empty stack" is the context in our example. The next step is to specify the
behavior, "should not be empty after push". We can now do a push to our stack
and then write our expectation, value( stack.empty() ).should.be( false ).

-----8<----------
#include <thebc/spec++.hpp>
#include "stack.hpp"
using namespace spec;

struct stack_helper
{
    stack<int> stack;
    // Setup
    stack_helper()
    {
    }

    // Teardown
    ~stack_helper()
    {
    }
// Helper method
void push_one_item_to_stack()
{
stack.push( 1 );
}
};

context("An empty stack", stack_helper)
{
    specify("should not be empty after push")
    {
push_one_item_to_stack();
        value( stack.empty() ).should.be( false );
    }
    specify("should complain when poping empty stack")
    {
        method stack.pop(); should_throw;
    }

}

SPECPP_MAIN
-----8<---------

Here is a list of other expectations statements you can make with
value(<actual>):

Equal and Not equal:
.should.be( <expected> );       
.should.equal( <expected> );     
.should.not_be( <expected> );   
.should.not_equal( <expected> );
.should == <expected>;           
.should != <expected>;
More than, less than or equal:
.should.be_more_than( <expected> );               
.should.be_less_than( <expected> );               
.should.be_more_than_or_equal_to( <expected> );
.should.be_less_than_or_equal_to( <expected> );
.should <<expected>;                               
.should> <expected>;                               
.should <= <expected>;                             
.should>= <expected>;
Between or equal to:
.should.be_between( <lower_bound> ).And( <upper_bound> );
.should.not_be_between( <lower_bound> ).And( <upper_bound> ); 
.should.be_between( <lower_bound> ) && <upper_bound>; 
.should.not_be_between( <lower_bound> ) && <upper_bound>; 
.should.be_between_or_equal_to( <lower_bound> ).And( <upper_bound> );
.should.not_be_between_or_equal_to( <lower_bound> ).And( <upper_bound> );
.should.be_between_or_equal_to( <lower_bound> ) && <upper_bound>; 
.should.not_be_between_or_equal_to( <lower_bound> ) && <upper_bound>;
Floating point comparison:
.should.be_close( <expected>, <tolerance> );           
.should.not_be_close( <expected>, <tolerance> );     
.should.be_within( <tolerance> ).of( <expected> );     
.should.not_be_within( <tolerance> ).of( <expected> );
Regular Expression pattern matching:
.should.match( <regex pattern> );     
.should.not_match( <regex pattern> );
Throwing:
function f() should_throw;           
function f() should_not_throw;       
function f() should_throw_a( <type> );
method f() should_throw;             
method f() should_not_throw;         
method f() should_throw_a( <type> );

When compiled every Spec++ executable have command line options, this is so that
you can choose output methods format of output, or what context or specifier to
execute.

Allowed options:

General options:
-h [ --help ] Print Help (this message) and exit
-v [ --version ] Print version information and exit

Output format options:
--output-method arg (=stdout) Choose one output method (stdout, stderr, or
file)
--filename arg (=specpp) If file has been choose as output method arg
will be used as filename, file extension is
added automatically
-f [ --format ] arg (=compiler) Use arg as output

Specification options:
-c [ --context ] arg Specify by description the context you want to run.
-s [ --specify ] arg Specify by description the specifier you want to run.

Third party dependencies:
o Boost.Regex (lib)
o Boost.Program_options (lib)
o Boost.Foreach (header only)
o Boost.Share_ptr (header only)

The next release:

For the next release I will add a more extended exception api with support for
containers and iterators. I will also add an installation guide and
documentation.

Fredrik.Eriksson at thebc.se

Posted by Fredrik 2007-02-27

Log in to post a comment.