Echo Writes Code

expect.hpp

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
#ifndef CRUCIBLE_TESTING_EXPECT_HPP
#define CRUCIBLE_TESTING_EXPECT_HPP

#include <algorithm>
#include <stdexcept>

#define EXPECT(expression) \
  ((expression) \
  ? static_cast<void>(0) \
  : crucible::testing::expect::fail((#expression), (expression), "true", __FILE__, __LINE__, __func__))

#define EXPECT_LHS(either) \
  ((either).has_lhs() \
  ? static_cast<void>(0) \
  : crucible::testing::expect::fail((#either), (either), "Lhs { ... }", __FILE__, __LINE__, __func__))

#define EXPECT_RHS(either) \
  ((either).has_rhs() \
  ? static_cast<void>(0) \
  : crucible::testing::expect::fail((#either), (either), "Rhs { ... }", __FILE__, __LINE__, __func__))

#define EXPECT_EQ(lhs, rhs) \
  (((lhs) == (rhs)) \
  ? static_cast<void>(0) \
  : crucible::testing::expect::fail_comparison((#lhs), (#rhs), (lhs), (rhs), "==", __FILE__, __LINE__, __func__))

#define EXPECT_NE(lhs, rhs) \
  (((lhs) != (rhs)) \
  ? static_cast<void>(0) \
  : crucible::testing::expect::fail_comparison((#lhs), (#rhs), (lhs), (rhs), "!=", __FILE__, __LINE__, __func__))

#define EXPECT_LT(lhs, rhs) \
  (((lhs) < (rhs)) \
  ? static_cast<void>(0) \
  : crucible::testing::expect::fail_comparison((#lhs), (#rhs), (lhs), (rhs), "<", __FILE__, __LINE__, __func__))

#define EXPECT_GT(lhs, rhs) \
  (((lhs) > (rhs)) \
  ? static_cast<void>(0) \
  : crucible::testing::expect::fail_comparison((#lhs), (#rhs), (lhs), (rhs), ">", __FILE__, __LINE__, __func__))

#define EXPECT_LE(lhs, rhs) \
  (((lhs) <= (rhs)) \
  ? static_cast<void>(0) \
  : crucible::testing::expect::fail_comparison((#lhs), (#rhs), (lhs), (rhs), "<=", __FILE__, __LINE__, __func__))

#define EXPECT_GE(lhs, rhs) \
  (((lhs) >= (rhs)) \
  ? static_cast<void>(0) \
  : crucible::testing::expect::fail_comparison((#lhs), (#rhs), (lhs), (rhs), ">=", __FILE__, __LINE__, __func__))

#define EXPECT_CONTAINER_EQ(lhs, rhs) \
  (std::equal(std::begin(lhs), std::end(lhs), std::begin(rhs), std::end(rhs)) \
  ? static_cast<void>(0) \
  : crucible::testing::expect::fail_comparison((#lhs), (#rhs), (lhs), (rhs), "==", __FILE__, __LINE__, __func__))

#define EXPECT_CONTAINER_NE(lhs, rhs) \
  (!std::equal(std::begin(lhs), std::end(lhs), std::begin(rhs), std::end(rhs)) \
  ? static_cast<void>(0) \
  : crucible::testing::expect::fail_comparison((#lhs), (#rhs), (lhs), (rhs), "!=", __FILE__, __LINE__, __func__))

namespace crucible::testing::expect {
  struct ExpectFailed final : std::logic_error {
    using logic_error::logic_error;
  };

  template<typename T>
  [[noreturn]]
  auto fail(char const *expression, T const &value, char const *expected, char const *file, int const line, char const *function) -> void;

  template<typename LHS, typename RHS>
  [[noreturn]]
  auto fail_comparison(char const *lhs_expression, char const *rhs_expression, LHS const &lhs_value, RHS const &rhs_value, char const *operation, char const *file, int const line, char const *function) -> void;
}

#endif // CRUCIBLE_TESTING_EXPECT_HPP