|
| 1 | +/* |
| 2 | + * Copyright (c) 2020 Arduino. All rights reserved. |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: LGPL-2.1-or-later |
| 5 | +*/ |
| 6 | + |
| 7 | +/************************************************************************************** |
| 8 | + * INCLUDE |
| 9 | + **************************************************************************************/ |
| 10 | + |
| 11 | +#include<catch2/catch_test_macros.hpp> |
| 12 | + |
| 13 | +#include<api/ErrorCodes.h> |
| 14 | + |
| 15 | +usingnamespacearduino; |
| 16 | + |
| 17 | +/************************************************************************************** |
| 18 | + * TEST CODE |
| 19 | + **************************************************************************************/ |
| 20 | + |
| 21 | +inttest_int(int i) { |
| 22 | +return i; |
| 23 | +} |
| 24 | + |
| 25 | +ReturnValuetest(int i) { |
| 26 | +return i; |
| 27 | +} |
| 28 | + |
| 29 | +TEST_CASE ("A ReturnValue can be evaluated as boolean","[ReturnValues-Evaluation]") { |
| 30 | +// Default constructor takes an integer implicitly, if negative it means error |
| 31 | +REQUIRE((bool)ReturnValue(1) ==true); |
| 32 | +REQUIRE((bool)ReturnValue(-12) ==false); |
| 33 | +} |
| 34 | + |
| 35 | +TEST_CASE ("A ReturnValue can be substituted to int in a function return value","[ReturnValues-Evaluation]") { |
| 36 | +REQUIRE((bool)test(1) ==true); |
| 37 | +REQUIRE((bool)test(-123) ==false); |
| 38 | +REQUIRE(test_int(1) == (int)test(1)); |
| 39 | +REQUIRE(test_int(-123) == (int)test(-123)); |
| 40 | +} |
| 41 | + |
| 42 | +TEST_CASE ("A ReturnValue can be instantiated with explicit error code and value","[ReturnValues-Evaluation]") { |
| 43 | +THEN("It can take a negative value as value and still be successful") { |
| 44 | +auto rv =ReturnValue(-1231, ArduinoSuccess); |
| 45 | + |
| 46 | +REQUIRE((bool)rv ==true); |
| 47 | + } |
| 48 | +THEN("It can take both a value and an error code") { |
| 49 | +auto rv =ReturnValue(-1231, ArduinoError); |
| 50 | + |
| 51 | +REQUIRE((bool)rv ==false); |
| 52 | +REQUIRE(rv.value == -1231); |
| 53 | +REQUIRE(rv.error == ArduinoError); |
| 54 | + } |
| 55 | +} |