-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e92b774
commit df29445
Showing
8 changed files
with
495 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* Copyright (c) 2020 Arduino. All rights reserved. | ||
* | ||
* SPDX-License-Identifier: LGPL-2.1-or-later | ||
*/ | ||
|
||
#ifndef PRINT_MOCK_H_ | ||
#define PRINT_MOCK_H_ | ||
|
||
/************************************************************************************** | ||
* INCLUDE | ||
**************************************************************************************/ | ||
|
||
#include <WString.h> | ||
|
||
#include <Print.h> | ||
|
||
/************************************************************************************** | ||
* CLASS DECLARATION | ||
**************************************************************************************/ | ||
|
||
class PrintMock : public Print | ||
{ | ||
public: | ||
String _str; | ||
size_t write(uint8_t b) { _str += static_cast<char>(b); return 1;}; | ||
void mock_setWriteError() { setWriteError(); } | ||
void mock_setWriteError(int err) { setWriteError(err); } | ||
}; | ||
|
||
#endif /* PRINT_MOCK_H_ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* Copyright (c) 2020 Arduino. All rights reserved. | ||
* | ||
* SPDX-License-Identifier: LGPL-2.1-or-later | ||
*/ | ||
|
||
#ifndef PRINTABLE_MOCK_H_ | ||
#define PRINTABLE_MOCK_H_ | ||
|
||
/************************************************************************************** | ||
* CLASS DECLARATION | ||
**************************************************************************************/ | ||
|
||
class PrintableMock : public Printable | ||
{ | ||
public: | ||
int _i; | ||
virtual size_t printTo(Print& p) const override | ||
{ | ||
size_t written = 0; | ||
written += p.print("PrintableMock i = "); | ||
written += p.print(_i); | ||
return written; | ||
} | ||
}; | ||
|
||
#endif /* PRINTABLE_MOCK_H_ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/* | ||
* Copyright (c) 2020 Arduino. All rights reserved. | ||
* | ||
* SPDX-License-Identifier: LGPL-2.1-or-later | ||
*/ | ||
|
||
/************************************************************************************** | ||
* TEST CODE | ||
**************************************************************************************/ | ||
|
||
TEST_CASE ("Print::availableForWrite() should return 0 if not overwritten by derived class", "[Print-availableForWrite-01]") | ||
{ | ||
PrintMock mock; | ||
REQUIRE(mock.availableForWrite() == 0); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/* | ||
* Copyright (c) 2020 Arduino. All rights reserved. | ||
* | ||
* SPDX-License-Identifier: LGPL-2.1-or-later | ||
*/ | ||
|
||
/************************************************************************************** | ||
* TEST CODE | ||
**************************************************************************************/ | ||
|
||
TEST_CASE ("Clear write error should set the error code back to 0", "[Print-clearWriteError-01]") | ||
{ | ||
PrintMock mock; | ||
mock.mock_setWriteError(5); | ||
mock.clearWriteError(); | ||
REQUIRE(mock.getWriteError() == 0); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* | ||
* Copyright (c) 2020 Arduino. All rights reserved. | ||
* | ||
* SPDX-License-Identifier: LGPL-2.1-or-later | ||
*/ | ||
|
||
/************************************************************************************** | ||
* TEST CODE | ||
**************************************************************************************/ | ||
|
||
TEST_CASE ("No write error has been set", "[Print-getWriteError-01]") | ||
{ | ||
PrintMock mock; | ||
REQUIRE(mock.getWriteError() == 0); | ||
} | ||
|
||
TEST_CASE ("A write error has been set", "[Print-getWriteError-02]") | ||
{ | ||
PrintMock mock; | ||
mock.mock_setWriteError(5); | ||
REQUIRE(mock.getWriteError() == 5); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,186 @@ | ||
/* | ||
* Copyright (c) 2020 Arduino. All rights reserved. | ||
* | ||
* SPDX-License-Identifier: LGPL-2.1-or-later | ||
*/ | ||
|
||
/************************************************************************************** | ||
* TEST CODE | ||
**************************************************************************************/ | ||
|
||
TEST_CASE ("Print::print(char)", "[Print-print-01]") | ||
{ | ||
PrintMock mock; | ||
|
||
mock.print('A'); | ||
|
||
REQUIRE(mock._str == "A"); | ||
} | ||
|
||
TEST_CASE ("Print::print(const String &)", "[Print-print-02]") | ||
{ | ||
PrintMock mock; | ||
String const str("Test String"); | ||
|
||
mock.print(str); | ||
|
||
REQUIRE(mock._str == "Test String"); | ||
} | ||
|
||
TEST_CASE ("Print::print(const char str[])", "[Print-print-03]") | ||
{ | ||
PrintMock mock; | ||
const char str[] = "Test String"; | ||
|
||
mock.print(str); | ||
|
||
REQUIRE(mock._str == "Test String"); | ||
} | ||
|
||
TEST_CASE ("Print::print(int, int = DEC|HEX|OCT|BIN)", "[Print-print-04]") | ||
{ | ||
PrintMock mock; | ||
|
||
int const val = -1; | ||
|
||
WHEN("DEC") { mock.print(val, DEC); REQUIRE(mock._str == "-1"); } | ||
WHEN("HEX") { mock.print(val, HEX); REQUIRE(mock._str == "FFFFFFFFFFFFFFFF"); } | ||
WHEN("OCT") { mock.print(val, OCT); REQUIRE(mock._str == "1777777777777777777777"); } | ||
WHEN("BIN") { mock.print(val, BIN); REQUIRE(mock._str == "1111111111111111111111111111111111111111111111111111111111111111"); } | ||
} | ||
|
||
TEST_CASE ("Print::print(unsigned int, int = DEC|HEX|OCT|BIN)", "[Print-print-05]") | ||
{ | ||
PrintMock mock; | ||
|
||
unsigned int const val = 17; | ||
|
||
WHEN("DEC") { mock.print(val, DEC); REQUIRE(mock._str == "17"); } | ||
WHEN("HEX") { mock.print(val, HEX); REQUIRE(mock._str == "11"); } | ||
WHEN("OCT") { mock.print(val, OCT); REQUIRE(mock._str == "21"); } | ||
WHEN("BIN") { mock.print(val, BIN); REQUIRE(mock._str == "10001"); } | ||
} | ||
|
||
TEST_CASE ("Print::print(long, int = DEC|HEX|OCT|BIN)", "[Print-print-06]") | ||
{ | ||
PrintMock mock; | ||
|
||
long const val = -1; | ||
|
||
WHEN("DEC") { mock.print(val, DEC); REQUIRE(mock._str == "-1"); } | ||
WHEN("HEX") { mock.print(val, HEX); REQUIRE(mock._str == "FFFFFFFFFFFFFFFF"); } | ||
WHEN("OCT") { mock.print(val, OCT); REQUIRE(mock._str == "1777777777777777777777"); } | ||
WHEN("BIN") { mock.print(val, BIN); REQUIRE(mock._str == "1111111111111111111111111111111111111111111111111111111111111111"); } | ||
} | ||
|
||
TEST_CASE ("Print::print(unsigned long, int = DEC|HEX|OCT|BIN)", "[Print-print-07]") | ||
{ | ||
PrintMock mock; | ||
|
||
unsigned long const val = 17; | ||
|
||
WHEN("DEC") { mock.print(val, DEC); REQUIRE(mock._str == "17"); } | ||
WHEN("HEX") { mock.print(val, HEX); REQUIRE(mock._str == "11"); } | ||
WHEN("OCT") { mock.print(val, OCT); REQUIRE(mock._str == "21"); } | ||
WHEN("BIN") { mock.print(val, BIN); REQUIRE(mock._str == "10001"); } | ||
} | ||
|
||
TEST_CASE ("Print::print(long long, int = DEC|HEX|OCT|BIN)", "[Print-print-08]") | ||
{ | ||
PrintMock mock; | ||
|
||
long long const val = -1; | ||
|
||
WHEN("DEC") { mock.print(val, DEC); REQUIRE(mock._str == "-1"); } | ||
WHEN("HEX") { mock.print(val, HEX); REQUIRE(mock._str == "FFFFFFFFFFFFFFFF"); } | ||
WHEN("OCT") { mock.print(val, OCT); REQUIRE(mock._str == "1777777777777777777777"); } | ||
WHEN("BIN") { mock.print(val, BIN); REQUIRE(mock._str == "1111111111111111111111111111111111111111111111111111111111111111"); } | ||
} | ||
|
||
TEST_CASE ("Print::print(unsigned long long, int = DEC|HEX|OCT|BIN)", "[Print-print-09]") | ||
{ | ||
PrintMock mock; | ||
|
||
GIVEN("a value of zero ...") | ||
{ | ||
unsigned long long const val = 0; | ||
|
||
WHEN("DEC") { mock.print(val, DEC); REQUIRE(mock._str == "0"); } | ||
WHEN("HEX") { mock.print(val, HEX); REQUIRE(mock._str == "0"); } | ||
WHEN("OCT") { mock.print(val, OCT); REQUIRE(mock._str == "0"); } | ||
WHEN("BIN") { mock.print(val, BIN); REQUIRE(mock._str == "0"); } | ||
} | ||
GIVEN("a non-zero value ...") | ||
{ | ||
unsigned long long const val = 17; | ||
|
||
WHEN("DEC") { mock.print(val, DEC); REQUIRE(mock._str == "17"); } | ||
WHEN("HEX") { mock.print(val, HEX); REQUIRE(mock._str == "11"); } | ||
WHEN("OCT") { mock.print(val, OCT); REQUIRE(mock._str == "21"); } | ||
WHEN("BIN") { mock.print(val, BIN); REQUIRE(mock._str == "10001"); } | ||
} | ||
} | ||
|
||
TEST_CASE ("Print::print(double, int = 2)", "[Print-print-10]") | ||
{ | ||
PrintMock mock; | ||
|
||
WHEN ("val is a positive floating point value") | ||
{ | ||
double const val = 3.1459; | ||
WHEN("digits = 0") { mock.print(val, 0); REQUIRE(mock._str == "3"); } | ||
WHEN("digits = 1") { mock.print(val, 1); REQUIRE(mock._str == "3.1"); } | ||
WHEN("digits = 2 (default)") { mock.print(val); REQUIRE(mock._str == "3.15"); } | ||
WHEN("digits = 3") { mock.print(val, 3); REQUIRE(mock._str == "3.146"); } | ||
WHEN("digits = 4") { mock.print(val, 4); REQUIRE(mock._str == "3.1459"); } | ||
WHEN("digits = 5") { mock.print(val, 5); REQUIRE(mock._str == "3.14590"); } | ||
} | ||
|
||
WHEN ("digits are negative") | ||
{ | ||
double const val = 3.1459; | ||
WHEN("digits = -1") { mock.print(val, -1); REQUIRE(mock._str == "3.15"); } | ||
} | ||
|
||
WHEN ("val is a negative floating point value") | ||
{ | ||
double const val = -3.1459; | ||
WHEN("digits = 2 (default)") { mock.print(val); REQUIRE(mock._str == "-3.15"); } | ||
} | ||
|
||
WHEN ("val is NAN") { mock.print(NAN); REQUIRE(mock._str == "nan"); } | ||
WHEN ("val is INFINITY") { mock.print(INFINITY); REQUIRE(mock._str == "inf"); } | ||
} | ||
|
||
TEST_CASE ("Print::print(Printable)", "[Print-print-11]") | ||
{ | ||
PrintMock mock; | ||
|
||
PrintableMock printable; | ||
printable._i = 1; | ||
|
||
mock.print(printable); | ||
|
||
REQUIRE(mock._str == "PrintableMock i = 1"); | ||
} | ||
|
||
TEST_CASE ("Print::print(unsigned char, int)", "[Print-print-12]") | ||
{ | ||
PrintMock mock; | ||
|
||
WHEN("DEC") { mock.print('A', DEC); REQUIRE(mock._str == "65"); } | ||
WHEN("HEX") { mock.print('A', HEX); REQUIRE(mock._str == "41"); } | ||
WHEN("OCT") { mock.print('A', OCT); REQUIRE(mock._str == "101"); } | ||
WHEN("BIN") { mock.print('A', BIN); REQUIRE(mock._str == "1000001"); } | ||
} | ||
|
||
TEST_CASE ("Testing Print::print(const __FlashStringHelper *)", "[Print-print-13]") | ||
{ | ||
#undef F | ||
#define F(string_literal) (reinterpret_cast<const __FlashStringHelper *>(PSTR(string_literal))) | ||
PrintMock mock; | ||
|
||
mock.print(F("Hello flash string")); | ||
|
||
REQUIRE(mock._str == "Hello flash string"); | ||
} |
Oops, something went wrong.