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

Adding ofstream method

This commit is contained in:
Henry Fredrick Schreiner 2017-02-13 09:25:33 -05:00
parent 77071fdb95
commit 1d39e9f092
2 changed files with 22 additions and 1 deletions

View File

@ -56,6 +56,7 @@ TEST(AppHelper, TempfileCreated) {
bool ok = static_cast<bool>(std::ofstream(myfile.c_str()).put('a')); // create file
EXPECT_TRUE(ok);
EXPECT_TRUE(CLI::ExistingFile(name));
EXPECT_THROW({TempFile otherfile(name);}, std::runtime_error);
}
EXPECT_FALSE(CLI::ExistingFile(name));
}
@ -70,6 +71,23 @@ TEST(AppHelper, TempfileNotCreated) {
EXPECT_FALSE(CLI::ExistingFile(name));
}
TEST(AppHelper, Ofstream) {
std::string name = "TestFileNotUsed.txt";
{
TempFile myfile(name);
{
std::ofstream out = myfile.ofstream();
out << "this is output" << std::endl;
}
EXPECT_TRUE(CLI::ExistingFile(myfile));
}
EXPECT_FALSE(CLI::ExistingFile(name));
}
TEST(Split, StringList) {
std::vector<std::string> results {"a", "long", "--lone", "-q"};

View File

@ -39,6 +39,9 @@ public:
std::remove(_name.c_str()); // Doesn't matter if returns 0 or not
}
/// Returns by move in C++11
std::ofstream ofstream() const {return std::ofstream(_name);}
operator const std::string& () const {return _name;}
const char * c_str() const {return _name.c_str();}
const char* c_str() const {return _name.c_str();}
};