yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
testing.h
Go to the documentation of this file.
1#ifndef YAZE_TEST_CORE_TESTING_H
2#define YAZE_TEST_CORE_TESTING_H
3
4#include <gmock/gmock.h>
5#include <gtest/gtest.h>
6
7#include "absl/status/status.h"
8#include "absl/status/statusor.h"
9
10#define EXPECT_OK(expr) EXPECT_EQ((expr), absl::OkStatus())
11
12#define ASSERT_OK(expr) ASSERT_EQ((expr), absl::OkStatus())
13
14#define ASSERT_OK_AND_ASSIGN(lhs, rexpr) \
15 if (auto rexpr_value = (rexpr); rexpr_value.ok()) { \
16 lhs = std::move(rexpr_value).value(); \
17 } else { \
18 FAIL() << "error: " << rexpr_value.status(); \
19 }
20
21namespace yaze {
22namespace test {
23
24// StatusIs is a matcher that matches a status that has the same code and
25// message as the expected status.
26MATCHER_P(StatusIs, status, "") { return arg.code() == status; }
27
28// Support for testing absl::StatusOr.
29template <typename T>
30::testing::AssertionResult IsOkAndHolds(const absl::StatusOr<T>& status_or,
31 const T& value) {
32 if (!status_or.ok()) {
33 return ::testing::AssertionFailure()
34 << "Expected status to be OK, but got: " << status_or.status();
35 }
36 if (status_or.value() != value) {
37 return ::testing::AssertionFailure() << "Expected value to be " << value
38 << ", but got: " << status_or.value();
39 }
40 return ::testing::AssertionSuccess();
41}
42
43MATCHER_P(IsOkAndHolds, value, "") { return IsOkAndHolds(arg, value); }
44
45// Helper to test if a StatusOr contains an error with a specific message
46MATCHER_P(StatusIsWithMessage, message, "") {
47 return !arg.ok() && arg.status().message() == message;
48}
49
50// Helper to test if a StatusOr contains an error with a specific code
51MATCHER_P(StatusIsWithCode, code, "") {
52 return !arg.ok() && arg.status().code() == code;
53}
54
55// Helper to test if a StatusOr is OK and contains a value that matches a
56// matcher
57template <typename T, typename Matcher>
58::testing::AssertionResult IsOkAndMatches(const absl::StatusOr<T>& status_or,
59 const Matcher& matcher) {
60 if (!status_or.ok()) {
61 return ::testing::AssertionFailure()
62 << "Expected status to be OK, but got: " << status_or.status();
63 }
64 if (!::testing::Matches(matcher)(status_or.value())) {
65 return ::testing::AssertionFailure()
66 << "Value does not match expected matcher";
67 }
68 return ::testing::AssertionSuccess();
69}
70
71// Helper to test if two StatusOr values are equal
72template <typename T>
73::testing::AssertionResult StatusOrEqual(const absl::StatusOr<T>& a,
74 const absl::StatusOr<T>& b) {
75 if (a.ok() != b.ok()) {
76 return ::testing::AssertionFailure()
77 << "One status is OK while the other is not";
78 }
79 if (!a.ok()) {
80 return ::testing::AssertionSuccess();
81 }
82 if (a.value() != b.value()) {
83 return ::testing::AssertionFailure()
84 << "Values are not equal: " << a.value() << " vs " << b.value();
85 }
86 return ::testing::AssertionSuccess();
87}
88
89} // namespace test
90} // namespace yaze
91
92#endif // YAZE_TEST_CORE_TESTING_H
MATCHER_P(StatusIs, status, "")
Definition testing.h:26
::testing::AssertionResult IsOkAndMatches(const absl::StatusOr< T > &status_or, const Matcher &matcher)
Definition testing.h:58
::testing::AssertionResult IsOkAndHolds(const absl::StatusOr< T > &status_or, const T &value)
Definition testing.h:30
::testing::AssertionResult StatusOrEqual(const absl::StatusOr< T > &a, const absl::StatusOr< T > &b)
Definition testing.h:73
Main namespace for the application.