mirror of
https://github.com/mat007/turtle.git
synced 2026-06-22 12:13:43 +00:00
Turned documentation code into example code
git-svn-id: https://svn.code.sf.net/p/turtle/code/trunk@578 860be788-9bd5-4423-9f1e-828f051e677b
This commit is contained in:
parent
af5f82a29d
commit
d29397417e
10 changed files with 437 additions and 224 deletions
24
build/boost/doc/example/calculator.hpp
Normal file
24
build/boost/doc/example/calculator.hpp
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
// http://turtle.sourceforge.net
|
||||
//
|
||||
// Copyright Mathieu Champlon 2012
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0.
|
||||
// (See accompanying file LICENSE_1_0.txt or copy at
|
||||
// http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef CALCULATOR
|
||||
#define CALCULATOR
|
||||
|
||||
class view;
|
||||
|
||||
//[ calculator
|
||||
class calculator
|
||||
{
|
||||
public:
|
||||
calculator( view& v );
|
||||
|
||||
void add( int a, int b ); // the result will be sent to the view 'v'
|
||||
};
|
||||
//]
|
||||
|
||||
#endif // CALCULATOR
|
||||
174
build/boost/doc/example/customisation.cpp
Normal file
174
build/boost/doc/example/customisation.cpp
Normal file
|
|
@ -0,0 +1,174 @@
|
|||
// http://turtle.sourceforge.net
|
||||
//
|
||||
// Copyright Mathieu Champlon 2012
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0.
|
||||
// (See accompanying file LICENSE_1_0.txt or copy at
|
||||
// http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
//[ mock_use_conversions
|
||||
#define MOCK_USE_CONVERSIONS
|
||||
#include <turtle/mock.hpp>
|
||||
//]
|
||||
#include "calculator.hpp"
|
||||
#include "mock_view.hpp"
|
||||
|
||||
//[ mock_stream_user_type
|
||||
namespace user_namespace
|
||||
{
|
||||
struct user_type
|
||||
{};
|
||||
|
||||
inline mock::stream& operator<<( mock::stream& s, const user_type& )
|
||||
{
|
||||
return s << "user_type";
|
||||
}
|
||||
}
|
||||
//]
|
||||
|
||||
namespace custom_constraint_free_function_test
|
||||
{
|
||||
//[ custom_constraint_free_function
|
||||
bool custom_constraint( int actual )
|
||||
{
|
||||
return actual == 42;
|
||||
}
|
||||
//]
|
||||
|
||||
//[ custom_constraint_free_function_test
|
||||
BOOST_AUTO_TEST_CASE( forty_one_plus_one_is_forty_two )
|
||||
{
|
||||
mock_view v;
|
||||
calculator c( v );
|
||||
MOCK_EXPECT( v.display ).with( &custom_constraint );
|
||||
c.add( 41, 1 );
|
||||
}
|
||||
//]
|
||||
}
|
||||
|
||||
namespace custom_constraint_functor_test
|
||||
{
|
||||
//[ custom_constraint_functor
|
||||
struct custom_constraint
|
||||
{
|
||||
friend bool operator==( int actual, const custom_constraint& )
|
||||
{
|
||||
return actual == 42;
|
||||
}
|
||||
|
||||
friend std::ostream& operator<<( std::ostream& s, const custom_constraint& )
|
||||
{
|
||||
return s << "_ == 42";
|
||||
}
|
||||
};
|
||||
//]
|
||||
|
||||
//[ custom_constraint_functor_test
|
||||
BOOST_AUTO_TEST_CASE( forty_one_plus_one_is_forty_two )
|
||||
{
|
||||
mock_view v;
|
||||
calculator c( v );
|
||||
MOCK_EXPECT( v.display ).with( custom_constraint() );
|
||||
c.add( 41, 1 );
|
||||
}
|
||||
//]
|
||||
}
|
||||
|
||||
//[ near_constraint
|
||||
template< typename Expected >
|
||||
struct near_constraint
|
||||
{
|
||||
near_constraint( Expected expected, Expected threshold )
|
||||
: expected_( expected )
|
||||
, threshold_( threshold )
|
||||
{}
|
||||
|
||||
template< typename Actual >
|
||||
bool operator()( Actual actual ) const
|
||||
{
|
||||
return std::abs( actual - boost::unwrap_ref( expected_ ) )
|
||||
< boost::unwrap_ref( threshold_ );
|
||||
}
|
||||
|
||||
friend std::ostream& operator<<( std::ostream& s, const near_constraint& c )
|
||||
{
|
||||
return s << "near( " << mock::format( c.expected_ )
|
||||
<< ", " << mock::format( c.threshold_ ) << " )";
|
||||
}
|
||||
|
||||
Expected expected_, threshold_;
|
||||
};
|
||||
|
||||
template< typename Expected >
|
||||
mock::constraint< near_constraint< Expected > > near( Expected expected, Expected threshold )
|
||||
{
|
||||
return near_constraint< Expected >( expected, threshold );
|
||||
}
|
||||
//]
|
||||
|
||||
namespace near_constraint_test
|
||||
{
|
||||
//[ near_constraint_test
|
||||
BOOST_AUTO_TEST_CASE( forty_one_plus_one_is_forty_two_plus_or_minus_one )
|
||||
{
|
||||
mock_view v;
|
||||
calculator c( v );
|
||||
MOCK_EXPECT( v.display ).with( near( 42, 1 ) );
|
||||
c.add( 41, 1 );
|
||||
}
|
||||
//]
|
||||
}
|
||||
|
||||
namespace near_constraint_cref_test
|
||||
{
|
||||
//[ near_constraint_cref_test
|
||||
BOOST_AUTO_TEST_CASE( forty_one_plus_one_is_forty_two_plus_or_minus_one )
|
||||
{
|
||||
mock_view v;
|
||||
calculator c( v );
|
||||
int expected, threshold;
|
||||
MOCK_EXPECT( v.display ).with( near( boost::cref( expected ), boost::cref( threshold ) ) );
|
||||
expected = 42;
|
||||
threshold = 1;
|
||||
c.add( 41, 1 );
|
||||
}
|
||||
//]
|
||||
}
|
||||
|
||||
#undef MOCK_MAX_ARGS
|
||||
//[ max_args
|
||||
#define MOCK_MAX_ARGS 20
|
||||
#include <turtle/mock.hpp>
|
||||
//]
|
||||
|
||||
//[ custom_policy
|
||||
template< typename Result >
|
||||
struct custom_policy
|
||||
{
|
||||
static Result abort()
|
||||
{
|
||||
// ...
|
||||
}
|
||||
template< typename Context >
|
||||
static void fail( const char* message, const Context&, const char* file = "unknown location", int line = 0 )
|
||||
{
|
||||
// ...
|
||||
}
|
||||
template< typename Context >
|
||||
static void call( const Context& context, const char* file, int line )
|
||||
{
|
||||
// ...
|
||||
}
|
||||
template< typename Context >
|
||||
static void pass( const char* file, int line )
|
||||
{
|
||||
// ...
|
||||
}
|
||||
};
|
||||
//]
|
||||
|
||||
#undef MOCK_ERROR_POLICY
|
||||
//[ define_custom_policy
|
||||
#define MOCK_ERROR_POLICY custom_policy
|
||||
#include <turtle/mock.hpp>
|
||||
//]
|
||||
88
build/boost/doc/example/getting_started.cpp
Normal file
88
build/boost/doc/example/getting_started.cpp
Normal file
|
|
@ -0,0 +1,88 @@
|
|||
//[ prerequisite
|
||||
#define BOOST_AUTO_TEST_MAIN
|
||||
#include <boost/test/auto_unit_test.hpp>
|
||||
#include <turtle/mock.hpp>
|
||||
//]
|
||||
#include "calculator.hpp"
|
||||
#include "mock_view.hpp"
|
||||
|
||||
namespace phases
|
||||
{
|
||||
//[ phases
|
||||
BOOST_AUTO_TEST_CASE( zero_plus_zero_is_zero )
|
||||
{
|
||||
mock_view v; // create mock objects
|
||||
calculator c( v ); // create object under test
|
||||
MOCK_EXPECT( v.display ).once().with( 0 ); // configure mock objects
|
||||
c.add( 0, 0 ); // exercise object under test
|
||||
} // verify mock objects
|
||||
//]
|
||||
}
|
||||
|
||||
namespace verify_reset
|
||||
{
|
||||
//[ verify_reset
|
||||
BOOST_AUTO_TEST_CASE( zero_plus_zero_is_zero )
|
||||
{
|
||||
mock_view v;
|
||||
calculator c( v );
|
||||
MOCK_EXPECT( v.display ).once().with( 0 );
|
||||
c.add( 0, 0 );
|
||||
MOCK_VERIFY( v.display ); // verify all expectations are fulfilled for the 'display' method
|
||||
mock::verify( v ); // verify all expectations are fulfilled for all methods of 'v'
|
||||
mock::verify(); // verify all expectations are fulfilled for all existing mock objects
|
||||
MOCK_RESET( v.display ); // reset all expectations for the 'display' method
|
||||
mock::reset( v ); // reset all expectations for all methods of 'v'
|
||||
mock::reset(); // reset all expectations for all existing mock objects
|
||||
} // automatically verify all expectations are fulfilled for all mock objects going out of scope
|
||||
//]
|
||||
}
|
||||
|
||||
namespace expectations
|
||||
{
|
||||
//[ expectations
|
||||
BOOST_AUTO_TEST_CASE( zero_plus_zero_is_zero )
|
||||
{
|
||||
mock_view v;
|
||||
calculator c( v );
|
||||
MOCK_EXPECT( v.display ).once().with( 0 ); // this call must occur once (and only once)
|
||||
MOCK_EXPECT( v.display ).with( 1 ); // this call can occur any number of times (including never)
|
||||
c.add( 0, 0 );
|
||||
}
|
||||
//]
|
||||
}
|
||||
|
||||
namespace sequence
|
||||
{
|
||||
//[ sequence
|
||||
BOOST_AUTO_TEST_CASE( zero_plus_zero_is_zero )
|
||||
{
|
||||
mock_view v;
|
||||
calculator c( v );
|
||||
mock::sequence s;
|
||||
MOCK_EXPECT( v.display ).once().with( 0 ).in( s ); // add this expectation to the sequence
|
||||
MOCK_EXPECT( v.display ).with( 1 ).in( s ); // add this expectation to the sequence after the previous one
|
||||
c.add( 0, 0 );
|
||||
c.add( 1, 0 );
|
||||
}
|
||||
//]
|
||||
}
|
||||
|
||||
namespace several_sequences
|
||||
{
|
||||
//[ several_sequences
|
||||
BOOST_AUTO_TEST_CASE( zero_plus_zero_is_zero )
|
||||
{
|
||||
mock_view v;
|
||||
calculator c( v );
|
||||
mock::sequence s1, s2;
|
||||
MOCK_EXPECT( v.display ).once().with( 0 ).in( s1 );
|
||||
MOCK_EXPECT( v.display ).once().with( 1 ).in( s2 );
|
||||
MOCK_EXPECT( v.display ).with( 2 ).in( s1 ).in( s2 ); // add this expectation to both sequences after the previous ones
|
||||
c.add( 0, 0 );
|
||||
c.add( 1, 0 );
|
||||
c.add( 1, 1 );
|
||||
c.add( 2, 0 );
|
||||
}
|
||||
//]
|
||||
}
|
||||
22
build/boost/doc/example/mock_view.hpp
Normal file
22
build/boost/doc/example/mock_view.hpp
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
// http://turtle.sourceforge.net
|
||||
//
|
||||
// Copyright Mathieu Champlon 2012
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0.
|
||||
// (See accompanying file LICENSE_1_0.txt or copy at
|
||||
// http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef MOCK_VIEW
|
||||
#define MOCK_VIEW
|
||||
|
||||
#include <turtle/mock.hpp>
|
||||
#include "view.hpp"
|
||||
|
||||
//[ mock_view
|
||||
MOCK_BASE_CLASS( mock_view, view ) // declare a 'mock_view' class implementing 'view'
|
||||
{
|
||||
MOCK_METHOD( display, 1 ) // implement the 'display' method from 'view' (taking 1 argument)
|
||||
};
|
||||
//]
|
||||
|
||||
#endif // MOCK_VIEW
|
||||
68
build/boost/doc/example/motivation.cpp
Normal file
68
build/boost/doc/example/motivation.cpp
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
#define BOOST_AUTO_TEST_MAIN
|
||||
#include <boost/test/auto_unit_test.hpp>
|
||||
#include <boost/mock/mock.hpp>
|
||||
#include "calculator.hpp"
|
||||
#include "mock_view.hpp"
|
||||
|
||||
namespace simple
|
||||
{
|
||||
//[ simple_calculator
|
||||
class calculator
|
||||
{
|
||||
public:
|
||||
int add( int a, int b );
|
||||
};
|
||||
//]
|
||||
|
||||
//[ simple_zero_plus_zero_is_zero
|
||||
BOOST_AUTO_TEST_CASE( zero_plus_zero_is_zero )
|
||||
{
|
||||
calculator c;
|
||||
BOOST_CHECK_EQUAL( 0, c.add( 0, 0 ) );
|
||||
}
|
||||
//]
|
||||
}
|
||||
|
||||
namespace without_mock_object
|
||||
{
|
||||
//[ my_view
|
||||
class my_view : public view
|
||||
{
|
||||
public:
|
||||
my_view()
|
||||
: called( false )
|
||||
{}
|
||||
virtual void display( int result )
|
||||
{
|
||||
called = true;
|
||||
value = result;
|
||||
}
|
||||
bool called;
|
||||
int value;
|
||||
};
|
||||
//]
|
||||
|
||||
//[ zero_plus_zero_is_zero_without_mock_object
|
||||
BOOST_AUTO_TEST_CASE( zero_plus_zero_is_zero )
|
||||
{
|
||||
my_view v;
|
||||
calculator c( v );
|
||||
c.add( 0, 0 );
|
||||
BOOST_REQUIRE( v.called );
|
||||
BOOST_CHECK_EQUAL( 0, v.value );
|
||||
}
|
||||
//]
|
||||
}
|
||||
|
||||
namespace with_mock_object
|
||||
{
|
||||
//[ zero_plus_zero_is_zero_with_mock_object
|
||||
BOOST_AUTO_TEST_CASE( zero_plus_zero_is_zero )
|
||||
{
|
||||
mock_view v;
|
||||
calculator c( v );
|
||||
MOCK_EXPECT( v.display ).once().with( 0 ); // expect the 'display' method to be called once (and only once) with a parameter value equal to 0
|
||||
c.add( 0, 0 );
|
||||
}
|
||||
//]
|
||||
}
|
||||
20
build/boost/doc/example/view.hpp
Normal file
20
build/boost/doc/example/view.hpp
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
// http://turtle.sourceforge.net
|
||||
//
|
||||
// Copyright Mathieu Champlon 2012
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0.
|
||||
// (See accompanying file LICENSE_1_0.txt or copy at
|
||||
// http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef VIEW
|
||||
#define VIEW
|
||||
|
||||
//[ view
|
||||
class view
|
||||
{
|
||||
public:
|
||||
virtual void display( int result ) = 0;
|
||||
};
|
||||
//]
|
||||
|
||||
#endif // VIEW
|
||||
Loading…
Add table
Add a link
Reference in a new issue