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

#include <stdexcept>

#define EXPECT(expression) \
  ((expression) \
  ? static_cast<void>(0) \
  : crucible::test::expect::fail(("`" #expression "`"), __FILE__, __LINE__, __FUNCTION__))

#define EXPECT_EQ(lhs, rhs) \
  (((lhs) == (rhs)) \
  ? static_cast<void>(0) \
  : crucible::test::expect::fail(("`" #lhs "` == `" #rhs "`"), __FILE__, __LINE__, __FUNCTION__))

#define EXPECT_NE(lhs, rhs) \
  (((lhs) != (rhs)) \
  ? static_cast<void>(0) \
  : crucible::test::expect::fail(("`" #lhs "` != `" #rhs "`"), __FILE__, __LINE__, __FUNCTION__))

#define EXPECT_LT(lhs, rhs) \
  (((lhs) < (rhs)) \
  ? static_cast<void>(0) \
  : crucible::test::expect::fail(("`" #lhs "` < `" #rhs "`"), __FILE__, __LINE__, __FUNCTION__))

#define EXPECT_LE(lhs, rhs) \
  (((lhs) <= (rhs)) \
  ? static_cast<void>(0) \
  : crucible::test::expect::fail(("`" #lhs "` <= `" #rhs "`"), __FILE__, __LINE__, __FUNCTION__))

#define EXPECT_GT(lhs, rhs) \
  (((lhs) > (rhs)) \
  ? static_cast<void>(0) \
  : crucible::test::expect::fail(("`" #lhs "` > `" #rhs "`"), __FILE__, __LINE__, __FUNCTION__))

#define EXPECT_GE(lhs, rhs) \
  (((lhs) >= (rhs)) \
  ? static_cast<void>(0) \
  : crucible::test::expect::fail(("`" #lhs "` >= `" #rhs "`"), __FILE__, __LINE__, __FUNCTION__))

#define EXPECT_FAILURE(result) \
  ((result).has_failure() \
  ? static_cast<void>(0) \
  : crucible::test::expect::fail(("`" #result ".has_failure()`"), __FILE__, __LINE__, __FUNCTION__))

#define EXPECT_SUCCESS(result) \
  ((result).has_success() \
  ? static_cast<void>(0) \
  : crucible::test::expect::fail(("`" #result ".has_success()`"), __FILE__, __LINE__, __FUNCTION__))

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

  [[noreturn]]
  auto fail(char const *message, char const *file, int const line, char const *function) -> void;
}

#endif // CRUCIBLE_TEST_EXPECT_HPP