summaryrefslogtreecommitdiff
path: root/vendor/bandit/bandit/assertion_frameworks/snowhouse/example/custom_matchers_test.cpp
blob: c5437f9f27283ab45c7f9ad433cad680abb29142 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//          Copyright Joakim Karlsson & Kim Gräsman 2010-2013.
// 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)

#include <snowhouse/snowhouse.h>
using namespace snowhouse;
#include "tests.h"

struct IsEvenNumberNoStreamOperator
{
  bool Matches(const int actual) const
  {
    return (actual % 2) == 0; 
  }
};

struct IsEvenNumberWithStreamOperator
{
  bool Matches(const int actual) const
  {
    return (actual % 2) == 0; 
  }

  friend std::ostream& operator<<(std::ostream& stm, 
      const IsEvenNumberWithStreamOperator& );
};

std::ostream& operator<<(std::ostream& stm, 
    const IsEvenNumberWithStreamOperator& )
{
  stm << "An even number";
  return stm;
}

void CustomMatchers()
{
  std::cout << "================================================" << std::endl;
  std::cout << "   CustomMatchersNoStreamOperator" << std::endl;
  std::cout << "================================================" << std::endl;

  std::cout << "CanHandleCustomMatcher" << std::endl;
  {
    Assert::That(2, Fulfills(IsEvenNumberNoStreamOperator()));
  }

  std::cout << "CustomMatcherWithFluent" << std::endl;
  {
    Assert::That(2, Is().Fulfilling(IsEvenNumberNoStreamOperator()));
  }

  std::cout << "OutputsCorrectMessageWhenFails" << std::endl;
  {
    AssertTestFails(Assert::That(3, Fulfills(IsEvenNumberNoStreamOperator())),
        "Expected: [unsupported type]\nActual: 3");
  }


  std::cout << "================================================" << std::endl;
  std::cout << "CustomMatcherWithStreamOperator" << std::endl;
  std::cout << "================================================" << std::endl;

  std::cout << "ErrorMessageUsesCustomStreamOperatorIfAvailable" << std::endl;
  {
    AssertTestFails(Assert::That(3, Fulfills(IsEvenNumberWithStreamOperator())), 
        "Expected: An even number\nActual: 3");
  }
}