Commit 77071fdb95dc2753bddaeea8a516b0aa255976bd

Authored by Henry Fredrick Schreiner
1 parent 0f476207

Adding tempfile helper

tests/HelpersTest.cpp
1 -#ifdef CLI_SINGLE_FILE  
2 -#include "CLI11.hpp"  
3 -#else  
4 -#include "CLI/CLI.hpp"  
5 -#endif 1 +#include "app_helper.hpp"
6 2
7 -#include "gtest/gtest.h"  
8 #include <cstdio> 3 #include <cstdio>
9 #include <fstream> 4 #include <fstream>
10 5
@@ -50,6 +45,31 @@ TEST(Validators, FileNotExists) { @@ -50,6 +45,31 @@ TEST(Validators, FileNotExists) {
50 EXPECT_TRUE(CLI::NonexistentPath(myfile)); 45 EXPECT_TRUE(CLI::NonexistentPath(myfile));
51 } 46 }
52 47
  48 +// Yes, this is testing an app_helper :)
  49 +TEST(AppHelper, TempfileCreated) {
  50 + std::string name = "TestFileNotUsed.txt";
  51 + {
  52 + TempFile myfile{name};
  53 +
  54 + EXPECT_FALSE(CLI::ExistingFile(myfile));
  55 +
  56 + bool ok = static_cast<bool>(std::ofstream(myfile.c_str()).put('a')); // create file
  57 + EXPECT_TRUE(ok);
  58 + EXPECT_TRUE(CLI::ExistingFile(name));
  59 + }
  60 + EXPECT_FALSE(CLI::ExistingFile(name));
  61 +}
  62 +
  63 +TEST(AppHelper, TempfileNotCreated) {
  64 + std::string name = "TestFileNotUsed.txt";
  65 + {
  66 + TempFile myfile{name};
  67 +
  68 + EXPECT_FALSE(CLI::ExistingFile(myfile));
  69 + }
  70 + EXPECT_FALSE(CLI::ExistingFile(name));
  71 +}
  72 +
53 TEST(Split, StringList) { 73 TEST(Split, StringList) {
54 74
55 std::vector<std::string> results {"a", "long", "--lone", "-q"}; 75 std::vector<std::string> results {"a", "long", "--lone", "-q"};
tests/IniTest.cpp
1 -#ifdef CLI_SINGLE_FILE  
2 -#include "CLI11.hpp"  
3 -#else  
4 -#include "CLI/CLI.hpp"  
5 -#endif  
6 -  
7 -#include "gtest/gtest.h" 1 +#include "app_helper.hpp"
8 2
9 #include <cstdio> 3 #include <cstdio>
10 #include <sstream> 4 #include <sstream>
tests/app_helper.hpp
@@ -24,4 +24,21 @@ struct TApp : public ::testing::Test { @@ -24,4 +24,21 @@ struct TApp : public ::testing::Test {
24 }; 24 };
25 25
26 26
  27 +class TempFile {
  28 + std::string _name;
27 29
  30 +public:
  31 +
  32 + TempFile(std::string name) : _name(name) {
  33 + if(!CLI::NonexistentPath(_name))
  34 + throw std::runtime_error(_name);
  35 +
  36 + }
  37 +
  38 + ~TempFile() {
  39 + std::remove(_name.c_str()); // Doesn't matter if returns 0 or not
  40 + }
  41 +
  42 + operator const std::string& () const {return _name;}
  43 + const char * c_str() const {return _name.c_str();}
  44 +};