forked from bblanchon/ArduinoJson
Added a test that stores a long in a JsonValue
This commit is contained in:
@ -14,13 +14,23 @@ size_t JsonValue::printBoolTo(Print& p) const
|
|||||||
|
|
||||||
size_t JsonValue::printDoubleTo(Print& p) const
|
size_t JsonValue::printDoubleTo(Print& p) const
|
||||||
{
|
{
|
||||||
char tmp[16];
|
char tmp[32];
|
||||||
|
|
||||||
sprintf(tmp, "%lg", content.asDouble);
|
sprintf(tmp, "%lg", content.asDouble);
|
||||||
|
|
||||||
return p.write(tmp);
|
return p.write(tmp);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
size_t JsonValue::printLongTo(Print& p) const
|
||||||
|
{
|
||||||
|
char tmp[32];
|
||||||
|
|
||||||
|
sprintf(tmp, "%ld", content.asLong);
|
||||||
|
|
||||||
|
return p.write(tmp);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
size_t JsonValue::printPrintableTo(Print& p) const
|
size_t JsonValue::printPrintableTo(Print& p) const
|
||||||
{
|
{
|
||||||
if (content.asPrintable)
|
if (content.asPrintable)
|
||||||
|
@ -16,10 +16,10 @@ public:
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
JsonValue(const char* value)
|
JsonValue(bool value)
|
||||||
: implementation(&JsonValue::printStringTo)
|
: implementation(&JsonValue::printBoolTo)
|
||||||
{
|
{
|
||||||
content.asString = value;
|
content.asBool = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
JsonValue(double value)
|
JsonValue(double value)
|
||||||
@ -28,10 +28,10 @@ public:
|
|||||||
content.asDouble = value;
|
content.asDouble = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
JsonValue(bool value)
|
JsonValue(long value)
|
||||||
: implementation(&JsonValue::printBoolTo)
|
: implementation(&JsonValue::printLongTo)
|
||||||
{
|
{
|
||||||
content.asBool = value;
|
content.asLong = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
JsonValue(Printable& value)
|
JsonValue(Printable& value)
|
||||||
@ -40,6 +40,12 @@ public:
|
|||||||
content.asPrintable = &value;
|
content.asPrintable = &value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
JsonValue(const char* value)
|
||||||
|
: implementation(&JsonValue::printStringTo)
|
||||||
|
{
|
||||||
|
content.asString = value;
|
||||||
|
}
|
||||||
|
|
||||||
virtual size_t printTo(Print& p) const
|
virtual size_t printTo(Print& p) const
|
||||||
{
|
{
|
||||||
// handmade polymorphism
|
// handmade polymorphism
|
||||||
@ -52,6 +58,7 @@ private:
|
|||||||
{
|
{
|
||||||
bool asBool;
|
bool asBool;
|
||||||
double asDouble;
|
double asDouble;
|
||||||
|
long asLong;
|
||||||
Printable* asPrintable;
|
Printable* asPrintable;
|
||||||
const char* asString;
|
const char* asString;
|
||||||
};
|
};
|
||||||
@ -62,6 +69,7 @@ private:
|
|||||||
|
|
||||||
size_t printBoolTo(Print& p) const;
|
size_t printBoolTo(Print& p) const;
|
||||||
size_t printDoubleTo(Print& p) const;
|
size_t printDoubleTo(Print& p) const;
|
||||||
|
size_t printLongTo(Print& p) const;
|
||||||
size_t printPrintableTo(Print& p) const;
|
size_t printPrintableTo(Print& p) const;
|
||||||
size_t printStringTo(Print& p) const;
|
size_t printStringTo(Print& p) const;
|
||||||
};
|
};
|
@ -76,13 +76,20 @@ namespace JsonGeneratorTests
|
|||||||
assertResultIs("\"\\\\\\\"\\b\\f\\n\\r\\t\"");
|
assertResultIs("\"\\\\\\\"\\b\\f\\n\\r\\t\"");
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_METHOD(Number)
|
TEST_METHOD(Double)
|
||||||
{
|
{
|
||||||
write(3.14);
|
write(3.14);
|
||||||
assertReturns(4);
|
assertReturns(4);
|
||||||
assertResultIs("3.14");
|
assertResultIs("3.14");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_METHOD(Long)
|
||||||
|
{
|
||||||
|
write(314L);
|
||||||
|
assertReturns(3);
|
||||||
|
assertResultIs("314");
|
||||||
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
void write(T value)
|
void write(T value)
|
||||||
{
|
{
|
||||||
|
Reference in New Issue
Block a user