• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "pw_result/result.h"
17 #include "pw_status/status.h"
18 #include "pw_unit_test/framework.h"
19 
20 // TODO(b/338094795): Remove when these are available in the light framework.
21 
22 #ifndef ASSERT_OK
23 #define ASSERT_OK(expr) ASSERT_EQ(expr, pw::OkStatus())
24 #endif  // ASSERT_OK
25 
26 #ifndef EXPECT_OK
27 #define EXPECT_OK(expr) EXPECT_EQ(expr, pw::OkStatus())
28 #endif  // EXPECT_OK
29 
30 #ifndef ASSERT_OK_AND_ASSIGN
31 
32 #define ASSERT_OK_AND_ASSIGN(lhs, rexpr) \
33   ASSERT_OK_AND_ASSIGN_DETAIL(UNIQUE_IDENTIFIER_DETAIL(__LINE__), lhs, rexpr)
34 
35 // NOLINTBEGIN(bugprone-macro-parentheses)
36 // The suggestion would produce bad code.
37 #define ASSERT_OK_AND_ASSIGN_DETAIL(result, lhs, rexpr) \
38   auto result = (rexpr);                                \
39   ASSERT_EQ(result.status(), pw::OkStatus());           \
40   lhs = ::pw::internal::ConvertToValue(result)
41 
42 #define UNIQUE_IDENTIFIER_DETAIL(line) UNIQUE_IDENTIFIER_EXPANDED_DETAIL(line)
43 #define UNIQUE_IDENTIFIER_EXPANDED_DETAIL(line) \
44   _assert_ok_and_assign_unique_name_##line
45 
46 #endif  // ASSERT_OK_AND_ASSIGN
47