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 /// Verifies that `expr` is OkStatus() 17 /// 18 /// Converts `expr` to a Status value and checks that it is OkStatus(). 19 /// 20 /// @param[in] expr The expression to check. 21 #define PW_TEST_EXPECT_OK(expr) \ 22 EXPECT_EQ(::pw::internal::ConvertToStatus(expr), pw::OkStatus()) 23 24 /// See `PW_TEST_EXPECT_OK`. 25 #define PW_TEST_ASSERT_OK(expr) \ 26 ASSERT_EQ(::pw::internal::ConvertToStatus(expr), pw::OkStatus()) 27 28 /// Executes an expression that returns a `pw::Result` or `pw::StatusWithSize` 29 /// and assigns or moves that value to lhs if the error code is OK. If the 30 /// status is non-OK, generates a test failure and returns from the current 31 /// function, which must have a void return type. 32 /// 33 /// Example: Declaring and initializing a new value. E.g.: 34 /// PW_TEST_ASSERT_OK_AND_ASSIGN(auto value, MaybeGetValue(arg)); 35 /// PW_TEST_ASSERT_OK_AND_ASSIGN(const ValueType value, MaybeGetValue(arg)); 36 /// 37 /// Example: Assigning to an existing value 38 /// ValueType value; 39 /// PW_TEST_ASSERT_OK_AND_ASSIGN(value, MaybeGetValue(arg)); 40 /// 41 /// The value assignment example would expand into something like: 42 /// auto status_or_value = MaybeGetValue(arg); 43 /// PW_TEST_ASSERT_OK(status_or_value.status()); 44 /// value = status_or_value.ValueOrDie(); 45 /// 46 /// WARNING: PW_TEST_ASSERT_OK_AND_ASSIGN expand into multiple statements; it 47 /// cannot be used in a single statement (e.g. as the body of an if statement 48 /// without {})! 49 /// 50 /// @param [in] lhs Variable to assign unwrapped value to 51 /// @param [in] rexpr Expression to unwrap. Must be a `Result` or 52 /// `StatusWithSize`. 53 #define PW_TEST_ASSERT_OK_AND_ASSIGN(lhs, rexpr) \ 54 _PW_TEST_ASSERT_OK_AND_ASSIGN_DETAIL( \ 55 _PW_UNIQUE_IDENTIFIER_DETAIL(__LINE__), lhs, rexpr) 56 57 #define _PW_TEST_ASSERT_OK_AND_ASSIGN_DETAIL(result, lhs, rexpr) \ 58 auto result = (rexpr); \ 59 PW_TEST_ASSERT_OK(result); \ 60 lhs = ::pw::internal::ConvertToValue(result) 61 62 #define _PW_UNIQUE_IDENTIFIER_DETAIL(line) \ 63 _PW_UNIQUE_IDENTIFIER_EXPANDED_DETAIL(line) 64 #define _PW_UNIQUE_IDENTIFIER_EXPANDED_DETAIL(line) \ 65 _assert_pw_test_assert_ok_and_assign_unique_name_##line 66