Echo Writes Code

expect.inl

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

#include "crucible/testing/expect.hpp"

#include "crucible/core/format.inl"

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

namespace crucible::testing::expect {

#ifdef _WIN32
  constexpr char SOURCE_FILE_PATH_SEPARATOR { '\\' };
#else
  constexpr char SOURCE_FILE_PATH_SEPARATOR { '/' };
#endif // _WIN32

  template<typename T>
  auto fail(char const *expression, T const &value, char const *expected, char const *file, int const line, char const *function) -> void {
    char const *final_path_separator { std::strrchr(file, SOURCE_FILE_PATH_SEPARATOR) };
    char const *truncated_file { final_path_separator ? final_path_separator + 1 : file };

    std::ostringstream buffer;

    auto const formatting_context {
      core::format::build_context()
        .set_indent_size(2)
        .get()
    };

    buffer
      << "Location: " << truncated_file << ":" << line << " [" << function << "]\n"
      << "Expected: " << expression << " to be " << expected << "\n"
      << "Actual: " << core::format::format_anything(value, formatting_context);

    throw ExpectFailed(buffer.str());
  }

  template<typename LHS, typename RHS>
  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 {
    char const *final_path_separator { std::strrchr(file, SOURCE_FILE_PATH_SEPARATOR) };
    char const *truncated_file { final_path_separator ? final_path_separator + 1 : file };

    std::ostringstream buffer;

    auto const formatting_context {
      core::format::build_context()
        .set_indent_size(2)
        .get()
    };

    buffer
      << "Location: " << truncated_file << ":" << line << " [" << function << "]\n"
      << "Expected: " << lhs_expression << " " << operation << " " << rhs_expression << "\n"
      << "Actual LHS: " << core::format::format_anything(lhs_value, formatting_context) << "\n"
      << "Actual RHS: " << core::format::format_anything(rhs_value, formatting_context);

    throw ExpectFailed(buffer.str());
  }
}

#endif // CRUCIBLE_TESTING_EXPECT_INL