Skip to content

Simplify string comparisons in tests and fix bug in String::compareTo #137

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 25, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
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.
  • Loading branch information
matthijskooijman committed Dec 31, 2020
commit 0c8dbd64a5d05e558185d7f85df40e328e23a9af
24 changes: 24 additions & 0 deletions test/src/String/StringPrinter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#pragma once

#include <catch.hpp>
#include <String.h>

namespace Catch {
/**
* Template specialization that makes sure Catch can properly print
* Arduino Strings when used in comparisons directly.
*
* Note that without this, String objects are printed as 0 and 1,
* because they are implicitly convertible to StringIfHelperType,
* which is a dummy pointer.
*/
template<>
struct StringMaker<arduino::String> {
static std::string convert(const arduino::String& str) {
if (str)
return ::Catch::Detail::stringify(std::string(str.c_str(), str.length()));
else
return "{invalid String}";
}
};
} // namespace Catch
2 changes: 2 additions & 0 deletions test/src/String/test_String.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

#include <String.h>

#include "StringPrinter.h"

/**************************************************************************************
* TEST CODE
**************************************************************************************/
Expand Down
2 changes: 2 additions & 0 deletions test/src/String/test_characterAccessFunc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

#include <String.h>

#include "StringPrinter.h"

/**************************************************************************************
* TEST CODE
**************************************************************************************/
Expand Down
2 changes: 2 additions & 0 deletions test/src/String/test_compareTo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

#include <String.h>

#include "StringPrinter.h"

/**************************************************************************************
* TEST CODE
**************************************************************************************/
Expand Down
2 changes: 2 additions & 0 deletions test/src/String/test_comparisonFunc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

#include <String.h>

#include "StringPrinter.h"

/**************************************************************************************
* TEST CODE
**************************************************************************************/
Expand Down
2 changes: 2 additions & 0 deletions test/src/String/test_concat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

#include <String.h>

#include "StringPrinter.h"

/**************************************************************************************
* TEST CODE
**************************************************************************************/
Expand Down
2 changes: 2 additions & 0 deletions test/src/String/test_indexOf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

#include <String.h>

#include "StringPrinter.h"

/**************************************************************************************
* TEST CODE
**************************************************************************************/
Expand Down
2 changes: 2 additions & 0 deletions test/src/String/test_lastIndexOf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

#include <String.h>

#include "StringPrinter.h"

/**************************************************************************************
* TEST CODE
**************************************************************************************/
Expand Down
2 changes: 2 additions & 0 deletions test/src/String/test_length.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

#include <String.h>

#include "StringPrinter.h"

/**************************************************************************************
* TEST CODE
**************************************************************************************/
Expand Down
2 changes: 2 additions & 0 deletions test/src/String/test_operators.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

#include <String.h>

#include "StringPrinter.h"

/**************************************************************************************
* TEST CODE
**************************************************************************************/
Expand Down
2 changes: 2 additions & 0 deletions test/src/String/test_remove.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

#include <String.h>

#include "StringPrinter.h"

/**************************************************************************************
* TEST CODE
**************************************************************************************/
Expand Down
2 changes: 2 additions & 0 deletions test/src/String/test_replace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

#include <String.h>

#include "StringPrinter.h"

/**************************************************************************************
* TEST CODE
**************************************************************************************/
Expand Down
2 changes: 2 additions & 0 deletions test/src/String/test_substring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

#include <String.h>

#include "StringPrinter.h"

/**************************************************************************************
* TEST CODE
**************************************************************************************/
Expand Down
2 changes: 2 additions & 0 deletions test/src/String/test_toDouble.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

#include <String.h>

#include "StringPrinter.h"

/**************************************************************************************
* TEST CODE
**************************************************************************************/
Expand Down
2 changes: 2 additions & 0 deletions test/src/String/test_toFloat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

#include <String.h>

#include "StringPrinter.h"

/**************************************************************************************
* TEST CODE
**************************************************************************************/
Expand Down
2 changes: 2 additions & 0 deletions test/src/String/test_toInt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

#include <String.h>

#include "StringPrinter.h"

/**************************************************************************************
* TEST CODE
**************************************************************************************/
Expand Down
2 changes: 2 additions & 0 deletions test/src/String/test_toLowerCase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

#include <String.h>

#include "StringPrinter.h"

/**************************************************************************************
* TEST CODE
**************************************************************************************/
Expand Down
2 changes: 2 additions & 0 deletions test/src/String/test_toUpperCase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

#include <String.h>

#include "StringPrinter.h"

/**************************************************************************************
* TEST CODE
**************************************************************************************/
Expand Down
2 changes: 2 additions & 0 deletions test/src/String/test_trim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

#include <String.h>

#include "StringPrinter.h"

/**************************************************************************************
* TEST CODE
**************************************************************************************/
Expand Down