// C++11 - report a user-defined data type.
#define CATCH_CONFIG_MAIN // This tells Catch to provide a main() - only do this in one cpp file
#include "catch.hpp"
namespace ns {
struct book
{
using text = std::string;
text author;
text title;
text isbn;
book( text author, text title, text isbn_ )
: author( author ), title( title ), isbn( isbn_ ) {}
friend bool operator==( book const & lhs, book const & rhs )
{
return lhs.author == rhs.author
&& lhs.title == rhs.title
&& lhs.isbn == rhs.isbn;
}
friend bool operator!=( book const & lhs, book const & rhs )
{
return ! ( lhs == rhs );
}
};
} // namespace ns
// provide stream operator for ns::book:
namespace Catch {
std::ostream & operator<<( std::ostream & os, ::ns::book const & b )
{
return os << "[book: " << toString(b.author) << ", " << toString(b.title) << ", " << toString(b.isbn) << "]";
}
} // namespace Catch
ns::book atocpp{ "Bjarne Stroustrup", "A Tour of C++.", "978-0-321-95831-0" };
ns::book tcpppl{ "Bjarne Stroustrup", "The C++ Programming Language.", "978-0-321-56384-2" };
TEST_CASE( "A book reports via the book-specific operator<<()" )
{
REQUIRE( atocpp != tcpppl );
REQUIRE( atocpp == tcpppl );
}
TEST_CASE( "A collection of books reports via the book-specific operator<<()" )
{
std::vector<ns::book> less = { atocpp }, more = { tcpppl };
REQUIRE( less == more );
}
// set CATCH_HOME=D:\Users\Martin\Documents\00_MJM\00_ACCU\PhilNash\Catch\single_include
// cl -nologo -W3 -EHsc -I%CATCH_HOME% catch_udt.cpp && catch_udt
// g++ -Wall -Wextra -Wmissing-include-dirs -std=c++11 -I%CATCH_HOME% -o catch_udt.exe catch_udt.cpp && catch_udt