Kevin Rocard | f035788 | 2017-02-10 16:19:28 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Kevin Rocard | 20e7af6 | 2017-03-10 17:10:43 -0800 | [diff] [blame] | 16 | |
Kevin Rocard | 20e7af6 | 2017-03-10 17:10:43 -0800 | [diff] [blame] | 17 | #include <algorithm> |
Kevin Rocard | 72e50e2 | 2017-05-05 14:02:55 -0700 | [diff] [blame] | 18 | #include <vector> |
Kevin Rocard | 20e7af6 | 2017-03-10 17:10:43 -0800 | [diff] [blame] | 19 | |
Kevin Rocard | f035788 | 2017-02-10 16:19:28 -0800 | [diff] [blame] | 20 | #include <hidl/Status.h> |
| 21 | |
| 22 | namespace detail { |
| 23 | |
Kevin Rocard | 72e50e2 | 2017-05-05 14:02:55 -0700 | [diff] [blame] | 24 | // This is a detail namespace, thus it is OK to import a class as nobody else is |
| 25 | // allowed to use it |
Kevin Rocard | 20e7af6 | 2017-03-10 17:10:43 -0800 | [diff] [blame] | 26 | using ::android::hardware::Return; |
| 27 | using ::android::hardware::audio::V2_0::Result; |
| 28 | |
Kevin Rocard | f26f67a | 2017-05-02 19:21:58 -0700 | [diff] [blame] | 29 | template <class T> |
| 30 | inline ::testing::AssertionResult assertIsOk(const char* expr, |
| 31 | const Return<T>& ret) { |
| 32 | return ::testing::AssertionResult(ret.isOk()) |
| 33 | << "Expected: " << expr |
| 34 | << "\n to be an OK Return but it is not: " << ret.description(); |
Kevin Rocard | 20e7af6 | 2017-03-10 17:10:43 -0800 | [diff] [blame] | 35 | } |
| 36 | |
Kevin Rocard | f26f67a | 2017-05-02 19:21:58 -0700 | [diff] [blame] | 37 | // Call continuation if the provided result isOk |
| 38 | template <class T, class Continuation> |
| 39 | inline ::testing::AssertionResult continueIfIsOk(const char* expr, |
| 40 | const Return<T>& ret, |
| 41 | Continuation continuation) { |
| 42 | auto isOkStatus = assertIsOk(expr, ret); |
| 43 | return !isOkStatus ? isOkStatus : continuation(); |
Kevin Rocard | 20e7af6 | 2017-03-10 17:10:43 -0800 | [diff] [blame] | 44 | } |
| 45 | |
Kevin Rocard | f26f67a | 2017-05-02 19:21:58 -0700 | [diff] [blame] | 46 | // Expect two equal Results |
| 47 | inline ::testing::AssertionResult assertResult(const char* e_expr, |
| 48 | const char* r_expr, |
| 49 | Result expected, Result result) { |
| 50 | return ::testing::AssertionResult(expected == result) |
| 51 | << "Value of: " << r_expr |
| 52 | << "\n Actual: " << ::testing::PrintToString(result) |
| 53 | << "\nExpected: " << e_expr |
| 54 | << "\nWhich is: " << ::testing::PrintToString(expected); |
| 55 | } |
| 56 | |
| 57 | // Expect two equal Results one being wrapped in an OK Return |
| 58 | inline ::testing::AssertionResult assertResult(const char* e_expr, |
| 59 | const char* r_expr, |
| 60 | Result expected, |
| 61 | const Return<Result>& ret) { |
| 62 | return continueIfIsOk(r_expr, ret, [&] { |
| 63 | return assertResult(e_expr, r_expr, expected, Result{ret}); |
| 64 | }); |
| 65 | } |
| 66 | |
| 67 | // Expect a Result to be part of a list of Results |
| 68 | inline ::testing::AssertionResult assertResult( |
| 69 | const char* e_expr, const char* r_expr, const std::vector<Result>& expected, |
| 70 | Result result) { |
Kevin Rocard | 20e7af6 | 2017-03-10 17:10:43 -0800 | [diff] [blame] | 71 | if (std::find(expected.begin(), expected.end(), result) != expected.end()) { |
Kevin Rocard | f26f67a | 2017-05-02 19:21:58 -0700 | [diff] [blame] | 72 | return ::testing::AssertionSuccess(); // result is in expected |
Kevin Rocard | 20e7af6 | 2017-03-10 17:10:43 -0800 | [diff] [blame] | 73 | } |
Kevin Rocard | f26f67a | 2017-05-02 19:21:58 -0700 | [diff] [blame] | 74 | return ::testing::AssertionFailure() |
| 75 | << "Value of: " << r_expr |
| 76 | << "\n Actual: " << ::testing::PrintToString(result) |
| 77 | << "\nExpected one of: " << e_expr |
| 78 | << "\n Which is: " << ::testing::PrintToString(expected); |
Kevin Rocard | 20e7af6 | 2017-03-10 17:10:43 -0800 | [diff] [blame] | 79 | } |
| 80 | |
Kevin Rocard | f26f67a | 2017-05-02 19:21:58 -0700 | [diff] [blame] | 81 | // Expect a Result wrapped in an OK Return to be part of a list of Results |
| 82 | inline ::testing::AssertionResult assertResult( |
| 83 | const char* e_expr, const char* r_expr, const std::vector<Result>& expected, |
| 84 | const Return<Result>& ret) { |
| 85 | return continueIfIsOk(r_expr, ret, [&] { |
| 86 | return assertResult(e_expr, r_expr, expected, Result{ret}); |
| 87 | }); |
Kevin Rocard | 20e7af6 | 2017-03-10 17:10:43 -0800 | [diff] [blame] | 88 | } |
| 89 | |
Kevin Rocard | f26f67a | 2017-05-02 19:21:58 -0700 | [diff] [blame] | 90 | inline ::testing::AssertionResult assertOk(const char* expr, |
| 91 | const Return<void>& ret) { |
| 92 | return assertIsOk(expr, ret); |
Kevin Rocard | f035788 | 2017-02-10 16:19:28 -0800 | [diff] [blame] | 93 | } |
| 94 | |
Kevin Rocard | f26f67a | 2017-05-02 19:21:58 -0700 | [diff] [blame] | 95 | inline ::testing::AssertionResult assertOk(const char* expr, Result result) { |
| 96 | return ::testing::AssertionResult(result == Result::OK) |
| 97 | << "Expected success: " << expr |
| 98 | << "\nActual: " << ::testing::PrintToString(result); |
Kevin Rocard | f035788 | 2017-02-10 16:19:28 -0800 | [diff] [blame] | 99 | } |
| 100 | |
Kevin Rocard | f26f67a | 2017-05-02 19:21:58 -0700 | [diff] [blame] | 101 | inline ::testing::AssertionResult assertOk(const char* expr, |
| 102 | const Return<Result>& ret) { |
| 103 | return continueIfIsOk(expr, ret, |
| 104 | [&] { return assertOk(expr, Result{ret}); }); |
Kevin Rocard | f035788 | 2017-02-10 16:19:28 -0800 | [diff] [blame] | 105 | } |
Kevin Rocard | f035788 | 2017-02-10 16:19:28 -0800 | [diff] [blame] | 106 | } |
| 107 | |
Kevin Rocard | f26f67a | 2017-05-02 19:21:58 -0700 | [diff] [blame] | 108 | #define ASSERT_IS_OK(ret) ASSERT_PRED_FORMAT1(detail::assertIsOk, ret) |
| 109 | #define EXPECT_IS_OK(ret) EXPECT_PRED_FORMAT1(detail::assertIsOk, ret) |
| 110 | |
Kevin Rocard | f035788 | 2017-02-10 16:19:28 -0800 | [diff] [blame] | 111 | // Test anything provided is and contains only OK |
Kevin Rocard | f26f67a | 2017-05-02 19:21:58 -0700 | [diff] [blame] | 112 | #define ASSERT_OK(ret) ASSERT_PRED_FORMAT1(detail::assertOk, ret) |
| 113 | #define EXPECT_OK(ret) EXPECT_PRED_FORMAT1(detail::assertOk, ret) |
Kevin Rocard | 3c405a7 | 2017-03-08 16:46:51 -0800 | [diff] [blame] | 114 | |
Kevin Rocard | 72e50e2 | 2017-05-05 14:02:55 -0700 | [diff] [blame] | 115 | #define ASSERT_RESULT(expected, ret) \ |
Kevin Rocard | f26f67a | 2017-05-02 19:21:58 -0700 | [diff] [blame] | 116 | ASSERT_PRED_FORMAT2(detail::assertResult, expected, ret) |
Kevin Rocard | 72e50e2 | 2017-05-05 14:02:55 -0700 | [diff] [blame] | 117 | #define EXPECT_RESULT(expected, ret) \ |
Kevin Rocard | f26f67a | 2017-05-02 19:21:58 -0700 | [diff] [blame] | 118 | EXPECT_PRED_FORMAT2(detail::assertResult, expected, ret) |