From 94f4fdbf726aac8c453a8bac7e5b0c0f3cfb5553 Mon Sep 17 00:00:00 2001 From: "Keidan (K. Billonneau)" Date: Wed, 3 Jan 2024 13:50:08 +0100 Subject: [PATCH] The test doesn't systematically delete the temporary file (+ use of string_view) --- src/main_test.cpp | 75 +++++++++++++++++++++++++++-------------------- 1 file changed, 43 insertions(+), 32 deletions(-) diff --git a/src/main_test.cpp b/src/main_test.cpp index 945b908..1c4065e 100644 --- a/src/main_test.cpp +++ b/src/main_test.cpp @@ -6,6 +6,7 @@ /* Includes -----------------------------------------------------------------*/ #include "Hex2Bin.hpp" #include "gtest/gtest.h" +#include /* Private defines ----------------------------------------------------------*/ static constexpr auto* SAMPLE1 = "samples/sample1.txt"; @@ -13,29 +14,40 @@ static constexpr auto* SAMPLE2 = "samples/sample2.txt"; static constexpr auto* SAMPLE3 = "samples/sample3.txt"; static constexpr auto* SAMPLE_TEMP = "samples/sample.txt.temp"; +using h2b::Hex2Bin; +using h2b::Hex2BinOpenResult; +using h2b::Hex2BinIsOpen; +namespace fs = std::filesystem; + /* Tests --------------------------------------------------------------------*/ +static auto cleanup(Hex2Bin& hex2bin, std::string_view path) -> void +{ + hex2bin.close(); + fs::remove(path); +} + TEST(Hex2BinTest, TestOpenInput) { - h2b::Hex2Bin hex2bin; + Hex2Bin hex2bin{}; const auto file = SAMPLE1; const auto ret = hex2bin.openInput(file); - EXPECT_EQ(1, ret == h2b::Hex2BinOpenResult::Success); + EXPECT_EQ(1, ret == Hex2BinOpenResult::Success); } TEST(Hex2BinTest, TestOpenOutput) { - h2b::Hex2Bin hex2bin{}; + Hex2Bin hex2bin{}; const auto file = SAMPLE_TEMP; const auto ret = hex2bin.openOutput(file); const auto oretInput = hex2bin.isFilesOpen(); - EXPECT_EQ(1, ret == h2b::Hex2BinOpenResult::Success); - EXPECT_EQ(1, oretInput == h2b::Hex2BinIsOpen::Input); - std::remove(SAMPLE_TEMP); + cleanup(hex2bin, SAMPLE_TEMP); + EXPECT_EQ(1, ret == Hex2BinOpenResult::Success); + EXPECT_EQ(1, oretInput == Hex2BinIsOpen::Input); } TEST(Hex2BinTest, TestOpenFiles) { - h2b::Hex2Bin hex2bin{}; + Hex2Bin hex2bin{}; const auto fileIn = SAMPLE1; const auto fileOut = SAMPLE_TEMP; const auto oretBoth = hex2bin.isFilesOpen(); @@ -43,19 +55,18 @@ TEST(Hex2BinTest, TestOpenFiles) const auto oretOutput = hex2bin.isFilesOpen(); const auto retOut = hex2bin.openOutput(fileOut); const auto oretSuccess = hex2bin.isFilesOpen(); + cleanup(hex2bin, fileOut); - std::remove(fileOut); - - EXPECT_EQ(1, retIn == h2b::Hex2BinOpenResult::Success); - EXPECT_EQ(1, retOut == h2b::Hex2BinOpenResult::Success); - EXPECT_EQ(1, oretBoth == h2b::Hex2BinIsOpen::Both); - EXPECT_EQ(1, oretOutput == h2b::Hex2BinIsOpen::Output); - EXPECT_EQ(1, oretSuccess == h2b::Hex2BinIsOpen::Success); + EXPECT_EQ(1, retIn == Hex2BinOpenResult::Success); + EXPECT_EQ(1, retOut == Hex2BinOpenResult::Success); + EXPECT_EQ(1, oretBoth == Hex2BinIsOpen::Both); + EXPECT_EQ(1, oretOutput == Hex2BinIsOpen::Output); + EXPECT_EQ(1, oretSuccess == Hex2BinIsOpen::Success); } TEST(Hex2BinTest, TestStart) { - h2b::Hex2Bin hex2bin{}; + Hex2Bin hex2bin{}; const auto svalue = "32"; const auto value = 32U; std::string what{}; @@ -68,7 +79,7 @@ TEST(Hex2BinTest, TestStart) TEST(Hex2BinTest, TestLimit) { - h2b::Hex2Bin hex2bin{}; + Hex2Bin hex2bin{}; const auto svalue = "16"; const auto value = 16U; std::string what{}; @@ -79,7 +90,7 @@ TEST(Hex2BinTest, TestLimit) EXPECT_EQ(1, hex2bin.getLimit() == value); } -static auto testExtractNoPrintWithStart(h2b::Hex2Bin& hex2bin, const std::string& sstart, const std::string& fileIn, const std::string& fileOut) -> void +static auto testExtractNoPrintWithStart(Hex2Bin& hex2bin, std::string_view sstart, std::string_view fileIn, std::string_view fileOut) -> void { std::string what{}; @@ -90,18 +101,18 @@ static auto testExtractNoPrintWithStart(h2b::Hex2Bin& hex2bin, const std::string const auto retIn = hex2bin.openInput(fileIn); const auto retOut = hex2bin.openOutput(fileOut); const auto oret = hex2bin.isFilesOpen(); + const auto eret = hex2bin.extractNoPrint(); + cleanup(hex2bin, fileOut); - EXPECT_EQ(1, retIn == h2b::Hex2BinOpenResult::Success); - EXPECT_EQ(1, retOut == h2b::Hex2BinOpenResult::Success); - EXPECT_EQ(1, oret == h2b::Hex2BinIsOpen::Success); - EXPECT_EQ(1, hex2bin.extractNoPrint()); - - std::remove(fileOut.c_str()); + EXPECT_EQ(1, retIn == Hex2BinOpenResult::Success); + EXPECT_EQ(1, retOut == Hex2BinOpenResult::Success); + EXPECT_EQ(1, oret == Hex2BinIsOpen::Success); + EXPECT_EQ(1, eret); } TEST(Hex2BinTest, TestExtractNoPrint1) { - h2b::Hex2Bin hex2bin{}; + Hex2Bin hex2bin{}; std::string what{}; EXPECT_EQ(1, hex2bin.setLimit("47", what) == true); @@ -112,7 +123,7 @@ TEST(Hex2BinTest, TestExtractNoPrint1) TEST(Hex2BinTest, TestExtractNoPrint2) { - h2b::Hex2Bin hex2bin{}; + Hex2Bin hex2bin{}; const auto slimit = "47"; const auto fileIn = SAMPLE2; const auto fileOut = SAMPLE_TEMP; @@ -125,18 +136,18 @@ TEST(Hex2BinTest, TestExtractNoPrint2) const auto retIn = hex2bin.openInput(fileIn); const auto retOut = hex2bin.openOutput(fileOut); const auto oret = hex2bin.isFilesOpen(); + const auto eret = hex2bin.extractNoPrint(); + cleanup(hex2bin, fileOut); - EXPECT_EQ(1, retIn == h2b::Hex2BinOpenResult::Success); - EXPECT_EQ(1, retOut == h2b::Hex2BinOpenResult::Success); - EXPECT_EQ(1, oret == h2b::Hex2BinIsOpen::Success); - EXPECT_EQ(1, hex2bin.extractNoPrint()); - - std::remove(fileOut); + EXPECT_EQ(1, retIn == Hex2BinOpenResult::Success); + EXPECT_EQ(1, retOut == Hex2BinOpenResult::Success); + EXPECT_EQ(1, oret == Hex2BinIsOpen::Success); + EXPECT_EQ(1, eret); } TEST(Hex2BinTest, TestExtractNoPrint3) { - h2b::Hex2Bin hex2bin{}; + Hex2Bin hex2bin{}; testExtractNoPrintWithStart(hex2bin, "1", SAMPLE3, SAMPLE_TEMP); }