Skip to content

Commit b216a9c

Browse files
committed
Adding latest test code of ArduinoCloudThing
1 parent 4c2c5d5 commit b216a9c

18 files changed

+2137
-17
lines changed

extras/test/CMakeLists.txt

+32
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ project(testArduinoIoTCloud)
99
##########################################################################
1010

1111
include_directories(include)
12+
include_directories(../../src/cbor)
1213
include_directories(../../src/utility/ota)
1314
include_directories(external/catch/v2.12.1/include)
1415
include_directories(external/fakeit/v2.0.5/include)
@@ -24,11 +25,42 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
2425
set(TEST_TARGET testArduinoIoTCloud)
2526

2627
set(TEST_SRCS
28+
src/Arduino.cpp
29+
2730
src/test_main.cpp
31+
2832
src/test_OTALogic.cpp
33+
34+
src/test_addPropertyReal.cpp
35+
src/test_callback.cpp
36+
src/test_CloudColor.cpp
37+
src/test_CloudLocation.cpp
38+
src/test_decode.cpp
39+
src/test_encode.cpp
40+
src/test_publishEvery.cpp
41+
src/test_publishOnChange.cpp
42+
src/test_publishOnChangeRateLimit.cpp
43+
src/test_readOnly.cpp
44+
src/test_writeOnly.cpp
45+
2946
src/OTATestData.cpp
47+
src/TestUtil.cpp
48+
3049
../../src/utility/ota/crc.cpp
3150
../../src/utility/ota/OTALogic.cpp
51+
52+
../../src/cbor/ArduinoCloudThing.cpp
53+
../../src/cbor/ArduinoCloudProperty.cpp
54+
../../src/cbor/lib/tinycbor/src/cborencoder.c
55+
../../src/cbor/lib/tinycbor/src/cborencoder_close_container_checked.c
56+
../../src/cbor/lib/tinycbor/src/cborerrorstrings.c
57+
../../src/cbor/lib/tinycbor/src/cborparser.c
58+
../../src/cbor/lib/tinycbor/src/cborparser_dup_string.c
59+
../../src/cbor/lib/tinycbor/src/cborpretty.c
60+
../../src/cbor/lib/tinycbor/src/cborpretty_stdio.c
61+
../../src/cbor/lib/tinycbor/src/cbortojson.c
62+
../../src/cbor/lib/tinycbor/src/cborvalidation.c
63+
../../src/cbor/lib/tinycbor/src/open_memstream.c
3264
)
3365

3466
##########################################################################

extras/test/include/Arduino.h

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
Copyright (c) 2019 Arduino. All rights reserved.
3+
*/
4+
5+
#ifndef TEST_ARDUINO_H_
6+
#define TEST_ARDUINO_H_
7+
8+
/******************************************************************************
9+
INCLUDE
10+
******************************************************************************/
11+
12+
#include <string>
13+
14+
/******************************************************************************
15+
TYPEDEF
16+
******************************************************************************/
17+
18+
typedef std::string String;
19+
20+
/******************************************************************************
21+
FUNCTION PROTOTYPES
22+
******************************************************************************/
23+
24+
void set_millis(unsigned long const millis);
25+
unsigned long millis();
26+
27+
#endif /* TEST_ARDUINO_H_ */

extras/test/include/TestUtil.h

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
Copyright (c) 2019 Arduino. All rights reserved.
3+
*/
4+
5+
#ifndef INCLUDE_TESTUTIL_H_
6+
#define INCLUDE_TESTUTIL_H_
7+
8+
/**************************************************************************************
9+
INCLUDE
10+
**************************************************************************************/
11+
12+
#include <ArduinoCloudThing.h>
13+
14+
#include <vector>
15+
16+
/**************************************************************************************
17+
PROTOTYPES
18+
**************************************************************************************/
19+
20+
std::vector<uint8_t> encode(ArduinoCloudThing & thing, bool lightPayload = false);
21+
void print(std::vector<uint8_t> const & vect);
22+
23+
#endif /* INCLUDE_TESTUTIL_H_ */

extras/test/src/Arduino.cpp

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
Copyright (c) 2019 Arduino. All rights reserved.
3+
*/
4+
5+
/******************************************************************************
6+
INCLUDE
7+
******************************************************************************/
8+
9+
#include <Arduino.h>
10+
11+
/******************************************************************************
12+
GLOBAL VARIABLES
13+
******************************************************************************/
14+
15+
static unsigned long current_millis = 0;
16+
17+
/******************************************************************************
18+
PUBLIC FUNCTIONS
19+
******************************************************************************/
20+
21+
void set_millis(unsigned long const millis) {
22+
current_millis = millis;
23+
}
24+
25+
unsigned long millis() {
26+
return current_millis;
27+
}

extras/test/src/TestUtil.cpp

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
Copyright (c) 2019 Arduino. All rights reserved.
3+
*/
4+
5+
/**************************************************************************************
6+
INCLUDE
7+
**************************************************************************************/
8+
9+
#include <TestUtil.h>
10+
11+
#include <iomanip>
12+
#include <iostream>
13+
14+
/**************************************************************************************
15+
PUBLIC FUNCTIONS
16+
**************************************************************************************/
17+
18+
std::vector<uint8_t> encode(ArduinoCloudThing & thing, bool lightPayload) {
19+
uint8_t buf[200] = {0};
20+
int const bytes_buf = thing.encode(buf, 200, lightPayload);
21+
if (bytes_buf == -1) {
22+
return std::vector<uint8_t>();
23+
} else {
24+
return std::vector<uint8_t>(buf, buf + bytes_buf);
25+
}
26+
}
27+
28+
void print(std::vector<uint8_t> const & vect) {
29+
for (auto i = vect.begin(); i != vect.end(); i++) {
30+
std::cout << std::uppercase
31+
<< std::hex
32+
<< std::setw(2)
33+
<< std::setfill('0')
34+
<< static_cast<size_t>(*i)
35+
<< std::dec
36+
<< std::nouppercase
37+
<< " ";
38+
}
39+
std::cout << std::endl;
40+
}

extras/test/src/test_CloudColor.cpp

+123
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/*
2+
Copyright (c) 2019 Arduino. All rights reserved.
3+
*/
4+
5+
/**************************************************************************************
6+
INCLUDE
7+
**************************************************************************************/
8+
9+
#include <catch.hpp>
10+
11+
#include <TestUtil.h>
12+
#include <ArduinoCloudThing.h>
13+
14+
/**************************************************************************************
15+
TEST CODE
16+
**************************************************************************************/
17+
18+
/************************************************************************************/
19+
SCENARIO("Arduino Cloud Properties ", "[ArduinoCloudThing::CloudColor]") {
20+
WHEN("Set invalid color HSB") {
21+
GIVEN("CloudProtocol::V2") {
22+
23+
24+
CloudColor color_test = CloudColor(0.0, 0.0, 0.0);
25+
26+
Color value_color_test = color_test.getValue();
27+
REQUIRE(value_color_test.setColorHSB(500.0, 20.0, 30.0) == false);
28+
29+
}
30+
}
31+
32+
WHEN("Set and Get different RGB colors") {
33+
GIVEN("CloudProtocol::V2") {
34+
35+
uint8_t r, g, b;
36+
37+
CloudColor color_test = CloudColor(0.0, 0.0, 0.0);
38+
39+
Color value_color_test = color_test.getValue();
40+
41+
value_color_test.setColorRGB(128, 64, 64);
42+
value_color_test.getRGB(r, g, b);
43+
44+
REQUIRE(r == 128);
45+
REQUIRE(g == 64);
46+
REQUIRE(b == 64);
47+
48+
value_color_test.setColorRGB(126, 128, 64);
49+
value_color_test.getRGB(r, g, b);
50+
51+
REQUIRE(r == 126);
52+
REQUIRE(g == 128);
53+
REQUIRE(b == 64);
54+
55+
value_color_test.setColorRGB(64, 128, 64);
56+
value_color_test.getRGB(r, g, b);
57+
58+
REQUIRE(r == 64);
59+
REQUIRE(g == 128);
60+
REQUIRE(b == 64);
61+
62+
value_color_test.setColorRGB(64, 64, 128);
63+
value_color_test.getRGB(r, g, b);
64+
65+
REQUIRE(r == 64);
66+
REQUIRE(g == 64);
67+
REQUIRE(b == 128);
68+
69+
value_color_test.setColorRGB(255, 0, 255);
70+
value_color_test.getRGB(r, g, b);
71+
72+
REQUIRE(r == 255);
73+
REQUIRE(g == 0);
74+
REQUIRE(b == 255);
75+
76+
value_color_test.setColorRGB(0, 0, 0);
77+
value_color_test.getRGB(r, g, b);
78+
79+
REQUIRE(r == 0);
80+
REQUIRE(g == 0);
81+
REQUIRE(b == 0);
82+
83+
value_color_test.setColorRGB(50, 100, 20);
84+
value_color_test.getRGB(r, g, b);
85+
86+
REQUIRE(r == 50);
87+
REQUIRE(g == 100);
88+
REQUIRE(b == 20);
89+
90+
value_color_test.setColorRGB(20, 50, 70);
91+
value_color_test.getRGB(r, g, b);
92+
93+
REQUIRE(r == 20);
94+
REQUIRE(g == 50);
95+
REQUIRE(b == 70);
96+
97+
}
98+
}
99+
100+
WHEN("Set HSB colors and get RGB") {
101+
GIVEN("CloudProtocol::V2") {
102+
bool verify;
103+
uint8_t r, g, b;
104+
105+
CloudColor color_test = CloudColor(0.0, 0.0, 0.0);
106+
107+
Color value_color_test = color_test.getValue();
108+
109+
value_color_test.setColorHSB(240, 50, 50);
110+
value_color_test.getRGB(r, g, b);
111+
verify = r == 64 && g == 64 && b == 128;
112+
113+
REQUIRE(verify);
114+
115+
value_color_test.setColorHSB(120, 50, 50);
116+
value_color_test.getRGB(r, g, b);
117+
verify = r == 64 && g == 128 && b == 64;
118+
119+
REQUIRE(verify);
120+
121+
}
122+
}
123+
}

0 commit comments

Comments
 (0)