From 1d39e9f0929360003a356c0959a7789ee6baed30 Mon Sep 17 00:00:00 2001 From: Henry Fredrick Schreiner Date: Mon, 13 Feb 2017 09:25:33 -0500 Subject: [PATCH] Adding ofstream method --- tests/HelpersTest.cpp | 18 ++++++++++++++++++ tests/app_helper.hpp | 5 ++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/tests/HelpersTest.cpp b/tests/HelpersTest.cpp index 0502ef89..4b274956 100644 --- a/tests/HelpersTest.cpp +++ b/tests/HelpersTest.cpp @@ -56,6 +56,7 @@ TEST(AppHelper, TempfileCreated) { bool ok = static_cast(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 results {"a", "long", "--lone", "-q"}; diff --git a/tests/app_helper.hpp b/tests/app_helper.hpp index ec435838..85b02aa6 100644 --- a/tests/app_helper.hpp +++ b/tests/app_helper.hpp @@ -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();} };