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

#include "crucible/core/format.hpp"

#include <algorithm>
#include <cstdint>
#include <cstring>
#include <iomanip>
#include <memory>
#include <ostream>
#include <sstream>
#include <stdexcept>
#include <string>
#include <type_traits>

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

#define CRUCIBLE_EXPECT_NOT(expression) \
  (!(expression) \
  ? static_cast<void>(0) \
  : crucible::test::expect::fail((#expression), (expression), "false", __FILE__, __LINE__, __func__))

#define CRUCIBLE_EXPECT_FAILURE(expression) \
  ((expression).is_failure() \
  ? static_cast<void>(0) \
  : crucible::test::expect::fail((#expression), (expression), "failure", __FILE__, __LINE__, __func__))

#define CRUCIBLE_EXPECT_SUCCESS(expression) \
  ((expression).is_success() \
  ? static_cast<void>(0) \
  : crucible::test::expect::fail((#expression), (expression), "success", __FILE__, __LINE__, __func__))

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

#define CRUCIBLE_EXPECT_PROPERTY_NOT(expression, context) \
  (!(expression) \
  ? static_cast<void>(0) \
  : crucible::test::expect::fail((#expression), (context), "false", __FILE__, __LINE__, __func__))

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

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

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

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

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

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

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

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

namespace crucible::test::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 *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_TEST_EXPECT_HPP

#include "crucible/test/expect.inl"