Skip to content

Commit

Permalink
Add Formatter test module, fix signed character issue.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikee47 committed Aug 15, 2024
1 parent ed32ec6 commit 8d52a32
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 4 deletions.
4 changes: 2 additions & 2 deletions Sming/Core/Data/Format/Formatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ unsigned escapeControls(String& value)
for(auto& c : value) {
if(escapeChar(c)) {
extra += 1; // "\"
} else if(c < 0x20) {
} else if(uint8_t(c) < 0x20) {
extra += 3; // "\xnn"
}
}
Expand All @@ -78,7 +78,7 @@ unsigned escapeControls(String& value)
memmove(out + extra, in, len);
in += extra;
while(len--) {
auto c = *in++;
uint8_t c = *in++;
auto esc = escapeChar(c);
if(esc) {
*out++ = '\\';
Expand Down
2 changes: 0 additions & 2 deletions Sming/Core/Data/Format/Json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@ bool IsValidUtf8(const char* str, unsigned length)
*
* This can occur if filenames become corrupted, so here we just
* substitute an underscore _ for anything which fails to match UTF8.
*
* TODO: Perform ANSI -> UTF8 conversion?
*/
void Json::escape(String& value) const
{
Expand Down
1 change: 1 addition & 0 deletions tests/HostTests/include/modules.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
XX(CStringArray) \
XX(Stream) \
XX(TemplateStream) \
XX(Formatter) \
XX(Serial) \
XX(ObjectMap) \
XX_NET(Base64) \
Expand Down
30 changes: 30 additions & 0 deletions tests/HostTests/modules/Formatter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <HostTests.h>
#include <Data/Format/Json.h>

class FormatterTest : public TestGroup
{
public:
FormatterTest() : TestGroup(_F("Formatter"))
{
}

void execute() override
{
DEFINE_FSTR_LOCAL(text1, "A JSON\ntest string\twith escapes\x12\0\n"
"Worth maybe \xc2\xa3"
"0.53.")
DEFINE_FSTR_LOCAL(text1b, "A JSON\\ntest string\\twith escapes\\x12\\0\\n"
"Worth maybe \xc2\xa3"
"0.53.")

Serial << text1 << endl;
String s(text1);
Format::json.escape(s);
REQUIRE_EQ(s, text1b);
}
};

void REGISTER_TEST(Formatter)
{
registerGroup<FormatterTest>();
}

0 comments on commit 8d52a32

Please sign in to comment.