Improve coverage for Writer and PrettyWriter

This commit is contained in:
miloyip 2015-04-13 14:50:08 +08:00
parent 3c028685df
commit 18a8891f0d
4 changed files with 47 additions and 8 deletions

View File

@ -100,8 +100,9 @@ public:
Base::os_->Put('\n'); Base::os_->Put('\n');
WriteIndent(); WriteIndent();
} }
if (!Base::WriteEndObject()) bool ret = Base::WriteEndObject();
return false; (void)ret;
RAPIDJSON_ASSERT(ret == true);
if (Base::level_stack_.Empty()) // end of json text if (Base::level_stack_.Empty()) // end of json text
Base::os_->Flush(); Base::os_->Flush();
return true; return true;
@ -123,8 +124,9 @@ public:
Base::os_->Put('\n'); Base::os_->Put('\n');
WriteIndent(); WriteIndent();
} }
if (!Base::WriteEndArray()) bool ret = Base::WriteEndArray();
return false; (void)ret;
RAPIDJSON_ASSERT(ret == true);
if (Base::level_stack_.Empty()) // end of json text if (Base::level_stack_.Empty()) // end of json text
Base::os_->Flush(); Base::os_->Flush();
return true; return true;

View File

@ -272,7 +272,8 @@ protected:
os_->Put(hexDigits[(codepoint >> 4) & 15]); os_->Put(hexDigits[(codepoint >> 4) & 15]);
os_->Put(hexDigits[(codepoint ) & 15]); os_->Put(hexDigits[(codepoint ) & 15]);
} }
else if (codepoint >= 0x010000 && codepoint <= 0x10FFFF) { else {
RAPIDJSON_ASSERT(codepoint >= 0x010000 && codepoint <= 0x10FFFF);
// Surrogate pair // Surrogate pair
unsigned s = codepoint - 0x010000; unsigned s = codepoint - 0x010000;
unsigned lead = (s >> 10) + 0xD800; unsigned lead = (s >> 10) + 0xD800;
@ -288,8 +289,6 @@ protected:
os_->Put(hexDigits[(trail >> 4) & 15]); os_->Put(hexDigits[(trail >> 4) & 15]);
os_->Put(hexDigits[(trail ) & 15]); os_->Put(hexDigits[(trail ) & 15]);
} }
else
return false; // invalid code point
} }
else if ((sizeof(Ch) == 1 || (unsigned)c < 256) && escape[(unsigned char)c]) { else if ((sizeof(Ch) == 1 || (unsigned)c < 256) && escape[(unsigned char)c]) {
is.Take(); is.Take();

View File

@ -80,6 +80,15 @@ TEST(PrettyWriter, SetIndent) {
buffer.GetString()); buffer.GetString());
} }
TEST(PrettyWriter, String) {
StringBuffer buffer;
PrettyWriter<StringBuffer> writer(buffer);
EXPECT_TRUE(writer.StartArray());
EXPECT_TRUE(writer.String("Hello\n"));
EXPECT_TRUE(writer.EndArray());
EXPECT_STREQ("[\n \"Hello\\n\"\n]", buffer.GetString());
}
#if RAPIDJSON_HAS_STDSTRING #if RAPIDJSON_HAS_STDSTRING
TEST(PrettyWriter, String_STDSTRING) { TEST(PrettyWriter, String_STDSTRING) {
StringBuffer buffer; StringBuffer buffer;

View File

@ -344,3 +344,32 @@ TEST(Writer, InvalidEncoding) {
EXPECT_FALSE(writer.String(s)); EXPECT_FALSE(writer.String(s));
} }
} }
TEST(Writer, InvalidEventSequence) {
// {]
{
StringBuffer buffer;
Writer<StringBuffer> writer(buffer);
writer.StartObject();
EXPECT_THROW(writer.EndArray(), AssertException);
EXPECT_FALSE(writer.IsComplete());
}
// [}
{
StringBuffer buffer;
Writer<StringBuffer> writer(buffer);
writer.StartArray();
EXPECT_THROW(writer.EndObject(), AssertException);
EXPECT_FALSE(writer.IsComplete());
}
// { 1:
{
StringBuffer buffer;
Writer<StringBuffer> writer(buffer);
writer.StartObject();
EXPECT_THROW(writer.Int(1), AssertException);
EXPECT_FALSE(writer.IsComplete());
}
}