From 75c89e7b353e68e7d61578139eac8ba2e460c10e Mon Sep 17 00:00:00 2001 From: Benoit Blanchon Date: Mon, 25 Aug 2014 09:52:42 +0200 Subject: [PATCH] Test an object with one member --- JsonGenerator/IndentedPrintDecorator.cpp | 24 ++++++++++++++++++++++- JsonGenerator/IndentedPrintDecorator.h | 6 ++++-- JsonGeneratorTests/IntentedPrintTests.cpp | 8 ++++++++ 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/JsonGenerator/IndentedPrintDecorator.cpp b/JsonGenerator/IndentedPrintDecorator.cpp index b790036e..ca15c543 100644 --- a/JsonGenerator/IndentedPrintDecorator.cpp +++ b/JsonGenerator/IndentedPrintDecorator.cpp @@ -7,5 +7,27 @@ size_t IndentedPrintDecorator::write(uint8_t c) { - return sink.write(c); + switch (c) + { + case '{': + indent++; + return sink.write(c) + writeln(); + + case '}': + indent--; + return writeln() + sink.write(c); + + default: + return sink.write(c); + } +} + +size_t IndentedPrintDecorator::writeln() +{ + size_t n = sink.write('\n'); + + for (int i = 0; i < indent; i++) + n += sink.write(' '); + + return n; } \ No newline at end of file diff --git a/JsonGenerator/IndentedPrintDecorator.h b/JsonGenerator/IndentedPrintDecorator.h index 6800e00f..e876b44a 100644 --- a/JsonGenerator/IndentedPrintDecorator.h +++ b/JsonGenerator/IndentedPrintDecorator.h @@ -13,14 +13,16 @@ class IndentedPrintDecorator : public Print public: IndentedPrintDecorator(Print& p) - : currentLevel(0), sink(p) + : indent(0), sink(p) { } virtual size_t write(uint8_t); private: - int currentLevel; + int indent; Print& sink; + + size_t writeln(); }; diff --git a/JsonGeneratorTests/IntentedPrintTests.cpp b/JsonGeneratorTests/IntentedPrintTests.cpp index 60727282..5839224f 100644 --- a/JsonGeneratorTests/IntentedPrintTests.cpp +++ b/JsonGeneratorTests/IntentedPrintTests.cpp @@ -37,6 +37,14 @@ namespace JsonGeneratorTests outputMustBe("[]"); } + TEST_METHOD(ObjectWithOneMember) + { + whenInputIs("{\"key\":\"value\"}"); + outputMustBe( + "{\n" + " \"key\":\"value\"\n" + "}"); + } private: