Skip to content

Commit e3d9398

Browse files
committed
test: add test for insert/update/write
1 parent 8fe007f commit e3d9398

File tree

1 file changed

+52
-1
lines changed

1 file changed

+52
-1
lines changed

test/tests.cpp

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#include <vector>
33

44
#include "gtest/gtest.h"
5-
#include "ini/INIReader.h"
5+
#include "ini/ini.h"
66

77
using namespace inih;
88

@@ -113,3 +113,54 @@ TEST(INIReader, dulicate_keys) {
113113
EXPECT_THROW(INIReader{"./fixtures/duplicate_keys.ini"},
114114
std::runtime_error);
115115
}
116+
117+
TEST(INIReader, InsertEntry) {
118+
INIReader r{"./fixtures/config.ini"};
119+
120+
// section exist, key not exist
121+
r.InsertEntry("section1", "my_custom_key", "hello world");
122+
123+
// section&key not exist
124+
r.InsertEntry("new_section", "key1", 5);
125+
126+
EXPECT_EQ("hello world", r.Get("section1", "my_custom_key"));
127+
EXPECT_EQ(5, r.Get<int>("new_section", "key1"));
128+
}
129+
130+
TEST(INIReader, UpdateEntry) {
131+
INIReader r{"./fixtures/config.ini"};
132+
r.InsertEntry("section1", "my_custom_key", "hello world");
133+
134+
r.UpdateEntry("section1", "my_custom_key", 123);
135+
EXPECT_EQ(123, r.Get<int>("section1", "my_custom_key"));
136+
137+
std::vector<double> ans1{0.1, 0.2, 0.3};
138+
r.UpdateEntry("section1", "my_custom_key", ans1);
139+
for (size_t i = 0; i < ans1.size(); ++i) {
140+
EXPECT_EQ(ans1[i], r.GetVector<double>("section1", "my_custom_key")[i]);
141+
}
142+
}
143+
144+
TEST(INIWriter, write) {
145+
INIReader r{"./fixtures/config.ini"};
146+
r.InsertEntry("new_section", "key1", "123");
147+
r.InsertEntry("new_section", "key2", 5.5);
148+
r.InsertEntry("new_section", "key3", std::vector<double>{0.1, 0.2, 0.3});
149+
r.InsertEntry("new_section", "key4", std::vector<std::string>{"a", "b"});
150+
151+
system("rm -rf ./fixtures/output.ini");
152+
INIWriter::write("./fixtures/output.ini", r);
153+
154+
INIReader r2{"./fixtures/output.ini"};
155+
for (const auto& section : r.Sections()) {
156+
for (const auto& key : r.Keys(section)) {
157+
EXPECT_EQ(r.Get(section, key), r2.Get(section, key));
158+
}
159+
}
160+
}
161+
162+
TEST(INIWriter, exception) {
163+
INIReader r{"./fixtures/config.ini"};
164+
EXPECT_THROW(INIWriter::write("./fixtures/config.ini", r),
165+
std::runtime_error);
166+
}

0 commit comments

Comments
 (0)