Skip to content

Commit 0c8dbd6

Browse files
Properly print Arduino String objects in tests
This ensures that checks like: REQUIRE(some_str == "ABC"); Actually print the value of some_str instead of just `1` (which is the String object converted to StringIfHelperType). This is implemented by adding a specialization of the Catch StringMaker template, so this only takes effect when StringPrinter.h is included. This commit includes it in all the String testcases (even ones that do not strictly use it now) for good measure.
1 parent 3caa039 commit 0c8dbd6

19 files changed

+60
-0
lines changed

Diff for: test/src/String/StringPrinter.h

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#pragma once
2+
3+
#include <catch.hpp>
4+
#include <String.h>
5+
6+
namespace Catch {
7+
/**
8+
* Template specialization that makes sure Catch can properly print
9+
* Arduino Strings when used in comparisons directly.
10+
*
11+
* Note that without this, String objects are printed as 0 and 1,
12+
* because they are implicitly convertible to StringIfHelperType,
13+
* which is a dummy pointer.
14+
*/
15+
template<>
16+
struct StringMaker<arduino::String> {
17+
static std::string convert(const arduino::String& str) {
18+
if (str)
19+
return ::Catch::Detail::stringify(std::string(str.c_str(), str.length()));
20+
else
21+
return "{invalid String}";
22+
}
23+
};
24+
} // namespace Catch

Diff for: test/src/String/test_String.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
#include <String.h>
1414

15+
#include "StringPrinter.h"
16+
1517
/**************************************************************************************
1618
* TEST CODE
1719
**************************************************************************************/

Diff for: test/src/String/test_characterAccessFunc.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
#include <String.h>
1212

13+
#include "StringPrinter.h"
14+
1315
/**************************************************************************************
1416
* TEST CODE
1517
**************************************************************************************/

Diff for: test/src/String/test_compareTo.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
#include <String.h>
1212

13+
#include "StringPrinter.h"
14+
1315
/**************************************************************************************
1416
* TEST CODE
1517
**************************************************************************************/

Diff for: test/src/String/test_comparisonFunc.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
#include <String.h>
1212

13+
#include "StringPrinter.h"
14+
1315
/**************************************************************************************
1416
* TEST CODE
1517
**************************************************************************************/

Diff for: test/src/String/test_concat.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
#include <String.h>
1212

13+
#include "StringPrinter.h"
14+
1315
/**************************************************************************************
1416
* TEST CODE
1517
**************************************************************************************/

Diff for: test/src/String/test_indexOf.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
#include <String.h>
1212

13+
#include "StringPrinter.h"
14+
1315
/**************************************************************************************
1416
* TEST CODE
1517
**************************************************************************************/

Diff for: test/src/String/test_lastIndexOf.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
#include <String.h>
1212

13+
#include "StringPrinter.h"
14+
1315
/**************************************************************************************
1416
* TEST CODE
1517
**************************************************************************************/

Diff for: test/src/String/test_length.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
#include <String.h>
1212

13+
#include "StringPrinter.h"
14+
1315
/**************************************************************************************
1416
* TEST CODE
1517
**************************************************************************************/

Diff for: test/src/String/test_operators.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
#include <String.h>
1212

13+
#include "StringPrinter.h"
14+
1315
/**************************************************************************************
1416
* TEST CODE
1517
**************************************************************************************/

Diff for: test/src/String/test_remove.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
#include <String.h>
1212

13+
#include "StringPrinter.h"
14+
1315
/**************************************************************************************
1416
* TEST CODE
1517
**************************************************************************************/

Diff for: test/src/String/test_replace.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
#include <String.h>
1212

13+
#include "StringPrinter.h"
14+
1315
/**************************************************************************************
1416
* TEST CODE
1517
**************************************************************************************/

Diff for: test/src/String/test_substring.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
#include <String.h>
1212

13+
#include "StringPrinter.h"
14+
1315
/**************************************************************************************
1416
* TEST CODE
1517
**************************************************************************************/

Diff for: test/src/String/test_toDouble.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
#include <String.h>
1212

13+
#include "StringPrinter.h"
14+
1315
/**************************************************************************************
1416
* TEST CODE
1517
**************************************************************************************/

Diff for: test/src/String/test_toFloat.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
#include <String.h>
1212

13+
#include "StringPrinter.h"
14+
1315
/**************************************************************************************
1416
* TEST CODE
1517
**************************************************************************************/

Diff for: test/src/String/test_toInt.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
#include <String.h>
1212

13+
#include "StringPrinter.h"
14+
1315
/**************************************************************************************
1416
* TEST CODE
1517
**************************************************************************************/

Diff for: test/src/String/test_toLowerCase.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
#include <String.h>
1212

13+
#include "StringPrinter.h"
14+
1315
/**************************************************************************************
1416
* TEST CODE
1517
**************************************************************************************/

Diff for: test/src/String/test_toUpperCase.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
#include <String.h>
1212

13+
#include "StringPrinter.h"
14+
1315
/**************************************************************************************
1416
* TEST CODE
1517
**************************************************************************************/

Diff for: test/src/String/test_trim.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
#include <String.h>
1212

13+
#include "StringPrinter.h"
14+
1315
/**************************************************************************************
1416
* TEST CODE
1517
**************************************************************************************/

0 commit comments

Comments
 (0)