C/C++ API Reference
Loading...
Searching...
No Matches
status_macros.h
1// Copyright 2024 The Pigweed Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may not
4// use this file except in compliance with the License. You may obtain a copy of
5// the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12// License for the specific language governing permissions and limitations under
13// the License.
14#pragma once
15
16#include "lib/stdcompat/type_traits.h"
17#include "pw_status/status.h"
18
20
26#define PW_TEST_EXPECT_OK(expr) \
27 if (cpp20::is_constant_evaluated()) { \
28 ::pw::unit_test::internal::Constexpr_EXPECT_OK( \
29 ::pw::internal::ConvertToStatus(expr)); \
30 } else \
31 EXPECT_EQ(::pw::internal::ConvertToStatus(expr), ::pw::OkStatus())
32
34
36
38#define PW_TEST_ASSERT_OK(expr) \
39 if (cpp20::is_constant_evaluated()) { \
40 if (!::pw::unit_test::internal::Constexpr_EXPECT_OK( \
41 ::pw::internal::ConvertToStatus(expr))) { \
42 return; \
43 } \
44 } else \
45 ASSERT_EQ(::pw::internal::ConvertToStatus(expr), ::pw::OkStatus())
46
72#define PW_TEST_ASSERT_OK_AND_ASSIGN(lhs, rexpr) \
73 _PW_TEST_ASSERT_OK_AND_ASSIGN_DETAIL( \
74 _PW_UNIQUE_IDENTIFIER_DETAIL(__LINE__), lhs, rexpr)
75
77
78#define _PW_TEST_ASSERT_OK_AND_ASSIGN_DETAIL(result, lhs, rexpr) \
79 auto result = (rexpr); \
80 PW_TEST_ASSERT_OK(result); \
81 lhs = ::pw::internal::ConvertToValue(result)
82
83#define _PW_UNIQUE_IDENTIFIER_DETAIL(line) \
84 _PW_UNIQUE_IDENTIFIER_EXPANDED_DETAIL(line)
85#define _PW_UNIQUE_IDENTIFIER_EXPANDED_DETAIL(line) \
86 _assert_pw_test_assert_ok_and_assign_unique_name_##line
87
88namespace pw::unit_test::internal {
89
90// Functions for PW_CONSTEXPR_TEST compatibility. See pw_unit_test/constexpr.h.
91bool EXPECT_OK_FAILED();
92
93constexpr bool Constexpr_EXPECT_OK(Status status) {
94 return status.ok() ? true : EXPECT_OK_FAILED();
95}
96
97} // namespace pw::unit_test::internal