Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ts-roundtrip.cpp « tests - github.com/windirstat/simpleini.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d4e663a9004dfe0f9572cde41201a6f6dd4fa373 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include "pch.h"
#include <algorithm>
#include "../SimpleIni.h"

class TestRoundTrip : public ::testing::Test {
protected:
	void SetUp() override;
	void TestMulti();
	void TestBOM(bool useBOM);

protected:
	CSimpleIniA ini;
	std::string input;
	std::string output;
};

void TestRoundTrip::SetUp() {
	ini.SetUnicode();
}

TEST_F(TestRoundTrip, TestStandard) {
	input =
		"; File comment\n"
		"\n"
		"\n"
		"; Section 1 comment\n"
		"[section1]\n"
		"\n"
		"\n"
		"; Section 2 comment\n"
		"[section2]\n"
		"key1 = string\n"
		"key2 = true\n"
		"key3 = 3.1415\n"
		;

	SI_Error rc = ini.LoadData(input);
	ASSERT_EQ(rc, SI_OK);

	const char* result = ini.GetValue("section2", "key1");
	ASSERT_STREQ(result, "string");

	rc = ini.Save(output);
	ASSERT_EQ(rc, SI_OK);

	output.erase(std::remove(output.begin(), output.end(), '\r'), output.end());
	ASSERT_STREQ(input.c_str(), output.c_str());
}

void TestRoundTrip::TestMulti() {
	input =
		"[section]\n"
		"key = string1\n"
		"key = string2\n"
		;

	SI_Error rc = ini.LoadData(input);
	ASSERT_EQ(rc, SI_OK);

	rc = ini.Save(output);
	ASSERT_EQ(rc, SI_OK);

	output.erase(std::remove(output.begin(), output.end(), '\r'), output.end());
}

TEST_F(TestRoundTrip, TestMultiGood) {
	ini.SetMultiKey(true);
	TestMulti();
	ASSERT_STREQ(input.c_str(), output.c_str());
}

TEST_F(TestRoundTrip, TestMultiBad) {
	std::string expected =
		"[section]\n"
		"key = string2\n";

	ini.SetMultiKey(false);
	TestMulti();
	ASSERT_STRNE(input.c_str(), output.c_str());
	ASSERT_STREQ(expected.c_str(), output.c_str());
}

TEST_F(TestRoundTrip, TestSpacesTrue) {
	input =
		"[section]\n"
		"key = string1\n";

	SI_Error rc = ini.LoadData(input);
	ASSERT_EQ(rc, SI_OK);

	ini.SetSpaces(true);
	rc = ini.Save(output);
	ASSERT_EQ(rc, SI_OK);

	output.erase(std::remove(output.begin(), output.end(), '\r'), output.end());
	
	ASSERT_STREQ(input.c_str(), output.c_str());
}

TEST_F(TestRoundTrip, TestSpacesFalse) {
	input =
		"[section]\n"
		"key = string1\n";

	SI_Error rc = ini.LoadData(input);
	ASSERT_EQ(rc, SI_OK);

	ini.SetSpaces(false);
	rc = ini.Save(output);
	ASSERT_EQ(rc, SI_OK);

	output.erase(std::remove(output.begin(), output.end(), '\r'), output.end());

	ASSERT_STRNE(input.c_str(), output.c_str());

	std::string expected =
		"[section]\n"
		"key=string1\n";

	ASSERT_STREQ(expected.c_str(), output.c_str());
}

void TestRoundTrip::TestBOM(bool useBOM) {
	const char bom[] = "\xEF\xBB\xBF";
	const char input8[] =
		u8"[テスト1]\n"
		u8"テスト2 = テスト3\n";

	input = bom;
	input += input8;

	ini.Reset();
	ini.SetUnicode(false);
	SI_Error rc = ini.LoadData(input);
	ASSERT_EQ(rc, SI_OK);

	const char tesuto1[] = u8"テスト1";
	const char tesuto2[] = u8"テスト2";
	const char tesuto3[] = u8"テスト3";

	const char* result = ini.GetValue(tesuto1, tesuto2);
	ASSERT_STREQ(result, tesuto3);

	rc = ini.Save(output, useBOM);
	ASSERT_EQ(rc, SI_OK);

	output.erase(std::remove(output.begin(), output.end(), '\r'), output.end());
}

TEST_F(TestRoundTrip, TestWithBOM) {
	TestBOM(true);

	ASSERT_STREQ(input.c_str(), output.c_str());
}

TEST_F(TestRoundTrip, TestWithoutBOM) {
	TestBOM(false);

	ASSERT_STRNE(input.c_str(), output.c_str());

	std::string expected(input, 3);
	ASSERT_STREQ(expected.c_str(), output.c_str());
}