Skip to content

Commit 8c38078

Browse files
Added unit tests for Return Values
1 parent 0ea1d69 commit 8c38078

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ set(TEST_SRCS
5050
src/Common/test_max.cpp
5151
src/Common/test_min.cpp
5252
src/ErrorCodes/test_ErrorCodes.cpp
53+
src/ErrorCodes/test_ReturnValues.cpp
5354
src/IPAddress/test_toString.cpp
5455
src/IPAddress/test_fromString.cpp
5556
src/IPAddress/test_fromString6.cpp
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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+
using namespace arduino;
16+
17+
/**************************************************************************************
18+
* TEST CODE
19+
**************************************************************************************/
20+
21+
int test_int(int i) {
22+
return i;
23+
}
24+
25+
ReturnValue test(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+
}

0 commit comments

Comments
 (0)