Commit 1d39e9f0929360003a356c0959a7789ee6baed30
1 parent
77071fdb
Adding ofstream method
Showing
2 changed files
with
22 additions
and
1 deletions
tests/HelpersTest.cpp
| ... | ... | @@ -56,6 +56,7 @@ TEST(AppHelper, TempfileCreated) { |
| 56 | 56 | bool ok = static_cast<bool>(std::ofstream(myfile.c_str()).put('a')); // create file |
| 57 | 57 | EXPECT_TRUE(ok); |
| 58 | 58 | EXPECT_TRUE(CLI::ExistingFile(name)); |
| 59 | + EXPECT_THROW({TempFile otherfile(name);}, std::runtime_error); | |
| 59 | 60 | } |
| 60 | 61 | EXPECT_FALSE(CLI::ExistingFile(name)); |
| 61 | 62 | } |
| ... | ... | @@ -70,6 +71,23 @@ TEST(AppHelper, TempfileNotCreated) { |
| 70 | 71 | EXPECT_FALSE(CLI::ExistingFile(name)); |
| 71 | 72 | } |
| 72 | 73 | |
| 74 | +TEST(AppHelper, Ofstream) { | |
| 75 | + | |
| 76 | + std::string name = "TestFileNotUsed.txt"; | |
| 77 | + { | |
| 78 | + TempFile myfile(name); | |
| 79 | + | |
| 80 | + { | |
| 81 | + std::ofstream out = myfile.ofstream(); | |
| 82 | + out << "this is output" << std::endl; | |
| 83 | + } | |
| 84 | + | |
| 85 | + EXPECT_TRUE(CLI::ExistingFile(myfile)); | |
| 86 | + } | |
| 87 | + EXPECT_FALSE(CLI::ExistingFile(name)); | |
| 88 | + | |
| 89 | +} | |
| 90 | + | |
| 73 | 91 | TEST(Split, StringList) { |
| 74 | 92 | |
| 75 | 93 | std::vector<std::string> results {"a", "long", "--lone", "-q"}; | ... | ... |
tests/app_helper.hpp
| ... | ... | @@ -39,6 +39,9 @@ public: |
| 39 | 39 | std::remove(_name.c_str()); // Doesn't matter if returns 0 or not |
| 40 | 40 | } |
| 41 | 41 | |
| 42 | + /// Returns by move in C++11 | |
| 43 | + std::ofstream ofstream() const {return std::ofstream(_name);} | |
| 44 | + | |
| 42 | 45 | operator const std::string& () const {return _name;} |
| 43 | - const char * c_str() const {return _name.c_str();} | |
| 46 | + const char* c_str() const {return _name.c_str();} | |
| 44 | 47 | }; | ... | ... |