From 77071fdb95dc2753bddaeea8a516b0aa255976bd Mon Sep 17 00:00:00 2001 From: Henry Fredrick Schreiner Date: Mon, 13 Feb 2017 08:59:53 -0500 Subject: [PATCH] Adding tempfile helper --- tests/HelpersTest.cpp | 32 ++++++++++++++++++++++++++------ tests/IniTest.cpp | 8 +------- tests/app_helper.hpp | 17 +++++++++++++++++ 3 files changed, 44 insertions(+), 13 deletions(-) diff --git a/tests/HelpersTest.cpp b/tests/HelpersTest.cpp index fe8b4b3f..0502ef89 100644 --- a/tests/HelpersTest.cpp +++ b/tests/HelpersTest.cpp @@ -1,10 +1,5 @@ -#ifdef CLI_SINGLE_FILE -#include "CLI11.hpp" -#else -#include "CLI/CLI.hpp" -#endif +#include "app_helper.hpp" -#include "gtest/gtest.h" #include #include @@ -50,6 +45,31 @@ TEST(Validators, FileNotExists) { EXPECT_TRUE(CLI::NonexistentPath(myfile)); } +// Yes, this is testing an app_helper :) +TEST(AppHelper, TempfileCreated) { + std::string name = "TestFileNotUsed.txt"; + { + TempFile myfile{name}; + + EXPECT_FALSE(CLI::ExistingFile(myfile)); + + bool ok = static_cast(std::ofstream(myfile.c_str()).put('a')); // create file + EXPECT_TRUE(ok); + EXPECT_TRUE(CLI::ExistingFile(name)); + } + EXPECT_FALSE(CLI::ExistingFile(name)); +} + +TEST(AppHelper, TempfileNotCreated) { + std::string name = "TestFileNotUsed.txt"; + { + TempFile myfile{name}; + + EXPECT_FALSE(CLI::ExistingFile(myfile)); + } + EXPECT_FALSE(CLI::ExistingFile(name)); +} + TEST(Split, StringList) { std::vector results {"a", "long", "--lone", "-q"}; diff --git a/tests/IniTest.cpp b/tests/IniTest.cpp index 1805845c..25cbcf38 100644 --- a/tests/IniTest.cpp +++ b/tests/IniTest.cpp @@ -1,10 +1,4 @@ -#ifdef CLI_SINGLE_FILE -#include "CLI11.hpp" -#else -#include "CLI/CLI.hpp" -#endif - -#include "gtest/gtest.h" +#include "app_helper.hpp" #include #include diff --git a/tests/app_helper.hpp b/tests/app_helper.hpp index cf60d95b..ec435838 100644 --- a/tests/app_helper.hpp +++ b/tests/app_helper.hpp @@ -24,4 +24,21 @@ struct TApp : public ::testing::Test { }; +class TempFile { + std::string _name; +public: + + TempFile(std::string name) : _name(name) { + if(!CLI::NonexistentPath(_name)) + throw std::runtime_error(_name); + + } + + ~TempFile() { + std::remove(_name.c_str()); // Doesn't matter if returns 0 or not + } + + operator const std::string& () const {return _name;} + const char * c_str() const {return _name.c_str();} +};