1- // cppIni - A C++20 library for reading and writing INI files
2- // Copyright (C) 2023-2024 Nils Hofmann <nils.friedchen@googlemail.com>
3- //
4- // This program is free software: you can redistribute it and/or modify
5- // it under the terms of the GNU General Public License as published by
6- // the Free Software Foundation, either version 3 of the License, or
7- // (at your option) any later version.
8- //
9- // This program is distributed in the hope that it will be useful,
10- // but WITHOUT ANY WARRANTY; without even the implied warranty of
11- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12- // GNU General Public License for more details.
13- //
14- // You should have received a copy of the GNU General Public License
15- // along with this program. If not, see <https://www.gnu.org/licenses/>.
1+ /*
2+ * cppIni - A C++20 library for reading and writing INI files
3+ * Copyright (C) 2023-2024 Nils Hofmann <nils.friedchen@googlemail.com>
4+ *
5+ * This program is free software: you can redistribute it and/or modify
6+ * it under the terms of the GNU General Public License as published by
7+ * the Free Software Foundation, either version 3 of the License, or
8+ * (at your option) any later version.
9+ *
10+ * This program is distributed in the hope that it will be useful,
11+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+ * GNU General Public License for more details.
14+ *
15+ * You should have received a copy of the GNU General Public License
16+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
17+ */
1618
1719#include < array>
1820#include < filesystem>
1921#include < format>
2022
2123#include < cppIni/cppIni_c.h>
2224#include < doctest/doctest.h>
25+ #include " utils.h"
2326
2427static const std::string fileName = std::format(" {}{}" , WORKING_DIR, " /res/test.ini" );;
2528
@@ -33,67 +36,50 @@ TEST_CASE("Construction of File object")
3336 CHECK_NOTHROW (cppIni_close (&file));
3437}
3538
36- struct ScopeGuard
37- {
38- ScopeGuard (pFile file) : file(file){};
39- ~ScopeGuard (){ cppIni_close (&file); };
40- pFile file;
41- };
42-
4339TEST_CASE (" Read a string entry" )
4440{
45- void * file = cppIni_open (fileName.c_str ());
46- ScopeGuard guard{file};
41+ auto file = utils::ScopeGuard<void *, cppIni_close>(cppIni_open (fileName.c_str ()));
4742
4843 std::array<char , 64 > buffer{0 };
49- cppIni_gets (file, " Section1" , " Entry1" , buffer.data (), buffer.size ());
44+ cppIni_gets (* file, " Section1" , " Entry1" , buffer.data (), buffer.size ());
5045
5146 CHECK_EQ (std::string_view{buffer.data ()}, " Value1" );
5247}
5348
5449TEST_CASE (" Try to read a non-existing entry" )
5550{
56- void * file = cppIni_open (fileName.c_str ());
57- ScopeGuard guard{file};
51+ auto file = utils::ScopeGuard<void *, cppIni_close>(cppIni_open (fileName.c_str ()));
5852
5953 std::array<char , 64 > buffer{0 };
60- CHECK_EQ (cppIni_gets (file, " Section1" , " NonExistingEntry" , buffer.data (), buffer.size ()), buffer.data ());
54+ CHECK_EQ (cppIni_gets (* file, " Section1" , " NonExistingEntry" , buffer.data (), buffer.size ()), buffer.data ());
6155 CHECK_EQ (buffer[0 ], ' \0 ' );
6256}
6357
6458TEST_CASE (" Change a value" )
6559{
66- constexpr auto tempFileName = " tmp.ini" ;
67- std::filesystem::copy_file (fileName, tempFileName);
68-
69- {
70- constexpr auto newValue = 1337 ;
71- void * file = cppIni_open (tempFileName);
72- ScopeGuard guard{file};
60+ constexpr auto newValue = 1337 ;
7361
74- const auto previousValue = cppIni_geti (file, " Section1" , " IntEntry" );
75- CHECK_NE (previousValue, newValue);
76- cppIni_set (file, " Section1" , " IntEntry" , std::to_string (newValue).c_str ());
77- CHECK_EQ (cppIni_geti (file, " Section1" , " IntEntry" ), newValue);
78- }
62+ utils::TempFile tmpFile (fileName);
63+ auto file = utils::ScopeGuard<void *, cppIni_close>(cppIni_open (tmpFile.filename ().data ()));
7964
80- std::filesystem::remove (tempFileName);
65+ const auto previousValue = cppIni_geti (*file, " Section1" , " IntEntry" );
66+ CHECK_NE (previousValue, newValue);
67+ cppIni_set (*file, " Section1" , " IntEntry" , std::to_string (newValue).c_str ());
68+ CHECK_EQ (cppIni_geti (*file, " Section1" , " IntEntry" ), newValue);
8169}
8270
8371TEST_CASE (" Read an integer entry" )
8472{
85- void * file = cppIni_open (fileName.c_str ());
86- ScopeGuard guard{file};
73+ auto file = utils::ScopeGuard<void *, cppIni_close>(cppIni_open (fileName.c_str ()));
8774
88- CHECK_EQ (cppIni_geti (file, " Section1" , " IntEntry" ), 42 );
75+ CHECK_EQ (cppIni_geti (* file, " Section1" , " IntEntry" ), 42 );
8976}
9077
9178TEST_CASE (" Read a floating point value entry" )
9279{
93- void * file = cppIni_open (fileName.c_str ());
94- ScopeGuard guard{file};
80+ auto file = utils::ScopeGuard<void *, cppIni_close>(cppIni_open (fileName.c_str ()));
9581
96- CHECK_LT (std::abs (cppIni_getf (file, " Section1.Subsection1" , " DoubleEntry" ) - 3.1415 ), 0.001 );
82+ CHECK_LT (std::abs (cppIni_getf (* file, " Section1.Subsection1" , " DoubleEntry" ) - 3.1415 ), 0.001 );
9783}
9884
9985TEST_SUITE_END ();
0 commit comments