Skip to content

Commit

Permalink
Use new TempFile implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Master92 committed Nov 14, 2024
1 parent 460477f commit 2607aa2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 29 deletions.
19 changes: 7 additions & 12 deletions tests/CInterfaceTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,20 +57,15 @@ TEST_CASE("Try to read a non-existing entry")

TEST_CASE("Change a value")
{
constexpr auto tempFileName = "tmp.ini";
std::filesystem::copy_file(fileName, tempFileName);
constexpr auto newValue = 1337;

{
constexpr auto newValue = 1337;
auto file = utils::ScopeGuard<void*, cppIni_close>(cppIni_open(tempFileName));
utils::TempFile tmpFile(fileName);
auto file = utils::ScopeGuard<void*, cppIni_close>(cppIni_open(tmpFile.filename().data()));

const auto previousValue = cppIni_geti(*file, "Section1", "IntEntry");
CHECK_NE(previousValue, newValue);
cppIni_set(*file, "Section1", "IntEntry", std::to_string(newValue).c_str());
CHECK_EQ(cppIni_geti(*file, "Section1", "IntEntry"), newValue);
}

std::filesystem::remove(tempFileName);
const auto previousValue = cppIni_geti(*file, "Section1", "IntEntry");
CHECK_NE(previousValue, newValue);
cppIni_set(*file, "Section1", "IntEntry", std::to_string(newValue).c_str());
CHECK_EQ(cppIni_geti(*file, "Section1", "IntEntry"), newValue);
}

TEST_CASE("Read an integer entry")
Expand Down
23 changes: 6 additions & 17 deletions tests/FileTest.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* cppIni - A C++20 library for reading and writing INI files
* Copyright (C) 2023 Nils Hofmann <nils.friedchen@googlemail.com>
* Copyright (C) 2023-2024 Nils Hofmann <nils.friedchen@googlemail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand All @@ -21,26 +21,14 @@
#include <doctest/doctest.h>

#include <cppIni/File.h>
#include "utils.h"

using namespace std::literals;

static const std::string fileName = std::format("{}{}", WORKING_DIR, "/res/test.ini");

TEST_SUITE_BEGIN("File");

class FileFixture
{
public:
FileFixture() {
std::filesystem::copy_file(::fileName, fileName);
}
~FileFixture() {
std::filesystem::remove(fileName);
}
protected:
const std::string fileName = std::format("{}{}", WORKING_DIR, "/res/tmp.ini");
};

TEST_CASE("Failing construction of an empty File object")
{
CHECK_THROWS(File{""});
Expand Down Expand Up @@ -145,15 +133,16 @@ TEST_CASE("Equality operator")
CHECK_EQ(f, f2);
}

TEST_CASE_FIXTURE(FileFixture, "Change a value with set")
TEST_CASE("Change a value with set")
{
constexpr auto newValue = "NewValue"sv;

auto f = File{fileName};
utils::TempFile tmpFile(fileName);
auto f = File{tmpFile.filename()};
f.set("Section1", "Entry1", "NewValue");
CHECK_EQ(f.get<std::string_view>("Section1", "Entry1"), newValue);

const auto f2 = File{fileName};
const auto f2 = File{tmpFile.filename()};
CHECK_EQ(f.get<std::string_view>("Section1", "Entry1"), newValue);
}

Expand Down

0 comments on commit 2607aa2

Please sign in to comment.