Tuesday, December 20, 2016

C++ Unit Test Framework Catch


1. class comparison in REQUIRE need operator == and  toString/template specialization/insertion operator
2. CATCH_TRANSLATE_EXCEPTION customize ex.message

 #define CATCH_CONFIG_MAIN  in stdafx.h

#include "stdafx.h"
#include "c:\__tools\catch.hpp"
#include <ostream>
#include <exception>

 class Book
{
public:
 int Id;
 bool operator==(const Book& b)
 {
  return b.Id == Id;
 }

};

//std::ostream& operator<<(std::ostream& os, Book const& b)
// {
// os << "overloading OS";
// return os;
//};
namespace Catch {
 template<> struct StringMaker<Book> {
  static std::string convert(Book const& value)
  {
   return "String Maker Specialization";
  }
 };

 //std::string toString(Book const& i)
 //{
 // return "to string for Book";
 //}
}
class myException : public std::exception
{
public:
 virtual const char* what() const throw()
 {
  return " my exception";
 }
};

CATCH_TRANSLATE_EXCEPTION(myException& ex)
{
 return ex.what();
}

TEST_CASE("This is a catch test","[Equal][LOG]")
{
 Book b1, b2;
 b1.Id = 90;
 INFO("Start Testing");
 REQUIRE(b1 == b2);
 WARN("One assertion failed");
 REQUIRE_FALSE(1 == 1);

}

TEST_CASE("This is another catch test","[CHK][EXP]")
{
 CHECK_FALSE(1 == 1);
 throw myException();
}



No comments:

Post a Comment