Add tests for duplicate sections

This commit is contained in:
Michał Janiszewski
2017-02-24 20:12:02 +01:00
parent 0a77374899
commit 94f0b39f0f
2 changed files with 50 additions and 1 deletions

View File

@@ -9,6 +9,7 @@ class IniReaderTest : public testing::Test
{
protected:
static const utf8 predefined[];
static const utf8 duplicate[];
};
static auto Enum_Currency = ConfigEnum<sint32>({});
@@ -52,11 +53,38 @@ TEST_F(IniReaderTest, read_prepared)
const utf8 * str = ir->GetCString("path", nullptr);
ASSERT_STREQ(str, u8"C:'\\some/dir\\here/神鷹暢遊");
Memory::Free(str);
// go backa section
// go back a section
ASSERT_EQ(ir->ReadSection("int"), true);
ASSERT_EQ(ir->GetSint32("one", 42), 1);
delete ir;
}
TEST_F(IniReaderTest, read_duplicate)
{
MemoryStream ms(duplicate, 99);
ASSERT_EQ(ms.CanRead(), true);
ASSERT_EQ(ms.CanWrite(), false);
IIniReader * ir = CreateIniReader(&ms);
ASSERT_NE(ir, nullptr);
// there should only be data from the last section
ASSERT_EQ(ir->ReadSection("section"), true);
ASSERT_EQ(ir->GetBoolean("one", false), false);
ASSERT_EQ(ir->GetBoolean("two", false), false);
ASSERT_EQ(ir->GetBoolean("three", false), true);
ASSERT_EQ(ir->ReadSection("section"), true);
// try switching to another section
ASSERT_EQ(ir->ReadSection("doesnt_exist"), false);
// make sure we are still in the same section
ASSERT_EQ(ir->GetBoolean("one", false), false);
ASSERT_EQ(ir->GetBoolean("two", false), false);
ASSERT_EQ(ir->GetBoolean("three", false), true);
ASSERT_EQ(ir->ReadSection("section"), true);
// test 4 times, there are only 3 sections
ASSERT_EQ(ir->ReadSection("section"), true);
delete ir;
}
const utf8 IniReaderTest::predefined[] = "[bool]\nboolval = true\n\n[int]\none = 1\nzero = 0\n\n[string]\npath = "
"\"C:'\\\\some/dir\\\\here/\xE7\xA5\x9E\xE9\xB7\xB9\xE6\x9A\xA2\xE9\x81\x8A\"\n";
const utf8 IniReaderTest::duplicate[] = "[section]\none = true\n[section]\ntwo = true\n[section]\nthree = true\n";

View File

@@ -186,3 +186,24 @@ TEST_F(IniWriterTest, create_multiple_section_with_values)
Memory::Free(ini);
delete iw;
}
TEST_F(IniWriterTest, create_duplicate_sections)
{
MemoryStream ms(1000);
IIniWriter * iw = CreateIniWriter(&ms);
ASSERT_NE(iw, nullptr);
iw->WriteSection("section");
iw->WriteSection("section");
iw->WriteSection("section");
int8_t null_terminator = 0;
ms.Write(&null_terminator, 1);
ASSERT_GE(ms.GetPosition(), 33);
ASSERT_LE(ms.GetPosition(), 43); // Accomodate for varying-sized newline (Windows)
ASSERT_EQ(ms.GetLength(), ms.GetPosition());
ms.SetPosition(0);
const char * ini = (const char *)ms.ReadString();
ASSERT_STREQ(ini, "[section]" PLATFORM_NEWLINE PLATFORM_NEWLINE "[section]" PLATFORM_NEWLINE PLATFORM_NEWLINE
"[section]" PLATFORM_NEWLINE);
Memory::Free(ini);
delete iw;
}