Skip to content

Commit

Permalink
The test doesn't systematically delete the temporary file (+ use of s…
Browse files Browse the repository at this point in the history
…tring_view)
  • Loading branch information
Keidan committed Jan 3, 2024
1 parent 376583b commit 94f4fdb
Showing 1 changed file with 43 additions and 32 deletions.
75 changes: 43 additions & 32 deletions src/main_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,56 +6,67 @@
/* Includes -----------------------------------------------------------------*/
#include "Hex2Bin.hpp"
#include "gtest/gtest.h"
#include <filesystem>

/* Private defines ----------------------------------------------------------*/
static constexpr auto* SAMPLE1 = "samples/sample1.txt";
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();
const auto retIn = hex2bin.openInput(fileIn);
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{};
Expand All @@ -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{};
Expand All @@ -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{};

Expand All @@ -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);
Expand All @@ -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;
Expand All @@ -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);
}

Expand Down

0 comments on commit 94f4fdb

Please sign in to comment.