mirror of
https://github.com/CLIUtils/CLI11.git
synced 2025-05-02 21:53:51 +00:00
Adding tempfile helper
This commit is contained in:
parent
0f47620704
commit
77071fdb95
@ -1,10 +1,5 @@
|
|||||||
#ifdef CLI_SINGLE_FILE
|
#include "app_helper.hpp"
|
||||||
#include "CLI11.hpp"
|
|
||||||
#else
|
|
||||||
#include "CLI/CLI.hpp"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include "gtest/gtest.h"
|
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
|
||||||
@ -50,6 +45,31 @@ TEST(Validators, FileNotExists) {
|
|||||||
EXPECT_TRUE(CLI::NonexistentPath(myfile));
|
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) {
|
TEST(Split, StringList) {
|
||||||
|
|
||||||
std::vector<std::string> results {"a", "long", "--lone", "-q"};
|
std::vector<std::string> results {"a", "long", "--lone", "-q"};
|
||||||
|
@ -1,10 +1,4 @@
|
|||||||
#ifdef CLI_SINGLE_FILE
|
#include "app_helper.hpp"
|
||||||
#include "CLI11.hpp"
|
|
||||||
#else
|
|
||||||
#include "CLI/CLI.hpp"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include "gtest/gtest.h"
|
|
||||||
|
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
@ -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();}
|
||||||
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user