summaryrefslogtreecommitdiff
path: root/vendor/bandit/bandit/assertion_frameworks/snowhouse/example/stringize_tests.cpp
blob: a0971274eafb713baeedd63e971aa08af000797a (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <snowhouse/snowhouse.h>
using namespace snowhouse;
#include "tests.h"

namespace
{
  // No overload for operator<<(std::ostream&) or specialization of igloo::Stringizer
  struct WithoutStreamOperator
  {
    WithoutStreamOperator(int id)
    : m_id(id)
    {
    }

    bool operator==(const WithoutStreamOperator& rhs) const
    {
      return m_id == rhs.m_id;
    }

    int m_id;
  };

  // Has operator<<(std::ostream&)
  struct WithStreamOperator : public WithoutStreamOperator
  {
    WithStreamOperator(int id)
    : WithoutStreamOperator(id)
    {
    }
  };
   
  std::ostream& operator<<(std::ostream& stream, const WithStreamOperator& a)
  {
    stream << a.m_id;
    return stream;
  }

  // Has no operator<<(std::ostream&), but a specialization of igloo::Stringizer
  struct WithoutStreamOperatorButWithStringizer : public WithoutStreamOperator
  {
    WithoutStreamOperatorButWithStringizer(int id)
    : WithoutStreamOperator(id)
    {
    }
  };
}

namespace snowhouse {

  template<>
  struct Stringizer< WithoutStreamOperatorButWithStringizer >
  {
    static std::string ToString(const WithoutStreamOperatorButWithStringizer& value)
    {
      return snowhouse::Stringize(value.m_id);
    }
  };
}

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

  std::cout << "ShouldHandleTypesWithStreamOperators" << std::endl;
  {
    WithStreamOperator a(12);
    WithStreamOperator b(13);
    AssertTestFails(Assert::That(a, Is().EqualTo(b)), "Expected: equal to 13\nActual: 12");
  }

  std::cout << "ShouldHandleTypesWithoutStreamOperators" << std::endl;
  {
    WithoutStreamOperator a(12);
    WithoutStreamOperator b(13);
    AssertTestFails(Assert::That(a, Is().EqualTo(b)), "Expected: equal to [unsupported type]\nActual: [unsupported type]");
  }

  std::cout << "ShouldHandleTypesWithTraits" << std::endl;
  {
    WithoutStreamOperatorButWithStringizer a(12);
    WithoutStreamOperatorButWithStringizer b(13);
    AssertTestFails(Assert::That(a, Is().EqualTo(b)), "Expected: equal to 13\nActual: 12");
  }

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

  std::cout << "ShouldHandleTypesWithStreamOperators" << std::endl;
  {
    WithStreamOperator a(12);
    WithStreamOperator b(13);
    AssertTestFails(Assert::That(a, Equals(b)), "Expected: equal to 13\nActual: 12");
  }

  std::cout << "ShouldHandleTypesWithoutStreamOperators" << std::endl;
  {
    WithoutStreamOperator a(12);
    WithoutStreamOperator b(13);
    AssertTestFails(Assert::That(a, Equals(b)), "Expected: equal to [unsupported type]\nActual: [unsupported type]");
  }

  std::cout << "ShouldHandleTypesWithTraits" << std::endl;
  {
    WithoutStreamOperatorButWithStringizer a(12);
    WithoutStreamOperatorButWithStringizer b(13);
    AssertTestFails(Assert::That(a, Is().EqualTo(b)), "Expected: equal to 13\nActual: 12");
  }
}