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", "false", __FILE__, __LINE__, __func__))

#define EXPECT_FAILURE(result) \
  ((result).has_failure() \
  ? static_cast<void>(0) \
  : crucible::testing::expect::fail((#result), (result.get_success()), "Failure", "Success", __FILE__, __LINE__, __func__))

#define EXPECT_SUCCESS(result) \
  ((result).has_success() \
  ? static_cast<void>(0) \
  : crucible::testing::expect::fail((#result), (result.get_failure()), "Success", "Failure", __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((lhs).begin(), (lhs).end(), (rhs).begin(), (rhs).end()) \
  ? static_cast<void>(0) \
  : crucible::testing::expect::fail_comparison((#lhs), (#rhs), (lhs), (rhs), "==", __FILE__, __LINE__, __func__))

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

namespace crucible::testing::expect {
  struct ExpectFailed : 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 *actual, 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