1
0
mirror of https://github.com/CLIUtils/CLI11.git synced 2025-05-02 13:43:52 +00:00

Adding tempfile helper

This commit is contained in:
Henry Fredrick Schreiner 2017-02-13 08:59:53 -05:00
parent 0f47620704
commit 77071fdb95
3 changed files with 44 additions and 13 deletions

View File

@ -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 <cstdio>
#include <fstream>
@ -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<bool>(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<std::string> results {"a", "long", "--lone", "-q"};

View File

@ -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 <cstdio>
#include <sstream>

View File

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