include_test.cc
Go to the documentation of this file.
1 #include "cetlib/include.h"
2 #include <cstdlib>
3 #include <fstream>
4 #include <regex>
5 
6 using cet::include;
7 
8 namespace {
9 
10  void
11  ensure(int which, bool claim)
12  {
13  if (not claim)
14  std::exit(which);
15  }
16 
17  inline void
18  ensure(int which, std::istream& os)
19  {
20  // Force boolean context.
21  ensure(which, !!os);
22  }
23 
24  std::string const file_a = "./a.txt";
25  std::string const file_b = "./b.txt";
26  std::string const file_c = "./c.txt";
27  std::string const file_i = "./i.txt";
28  std::string const file_j = "./j.txt";
29  std::string const file_k = "./k.txt";
30 
31  std::string const contents_a = "abcde\n"
32  "vwxyz\n";
33 
34  std::string const contents_b = "12345\n"
35  "67890\n";
36 
37  std::string const contents_c = "a1b2c3\r"
38  "d4e5f6\r";
39 
40  std::string const contents_i = "begin\n"
41  "#include \"./a.txt\"\n"
42  "end\n";
43 
44  std::string const contents_j = "begin\n"
45  "#include \"./a.txt\"\n"
46  "#include \"./b.txt\"\r\n"
47  "end\n";
48 
49  std::string const contents_k = "begin\n"
50  "#include \"./a.txt\"\n"
51  "#include \"./b.txt\"\r\n"
52  "#include \"./c.txt\"\r"
53  "end\n";
54 
55  inline std::string
56  expected_string(std::string const& str)
57  {
58  return std::regex_replace(str, std::regex("\r"), "\n");
59  }
60 
61  void
62  write_files()
63  {
64  std::ofstream a(file_a);
65  a << contents_a;
66  std::ofstream b(file_b);
67  b << contents_b;
68  std::ofstream c(file_c);
69  c << contents_c;
70  std::ofstream i(file_i);
71  i << contents_i;
72  std::ofstream j(file_j);
73  j << contents_j;
74  std::ofstream k(file_k);
75  k << contents_k;
76  }
77 }
78 
79 int
81 {
82  write_files();
83 
84  {
86 
87  std::ifstream a(file_a);
88  ensure(1, a);
89  result.clear();
90  include(a, result);
91  ensure(2, result == expected_string(contents_a));
92 
93  std::ifstream b(file_b);
94  ensure(3, b);
95  result.clear();
96  include(b, result);
97  ensure(4, result == expected_string(contents_b));
98 
99  std::ifstream c(file_c);
100  ensure(5, c);
101  result.clear();
102  include(c, result);
103  ensure(6, result == expected_string(contents_c));
104  }
105 
106  {
108  std::ifstream i(file_i);
109  ensure(11, i);
110  result.clear();
111  include(i, result);
112  ensure(12,
113  result == expected_string("begin\n") + expected_string(contents_a) +
114  expected_string("end\n"));
115  }
116 
117  {
119  std::ifstream j(file_j);
120  ensure(21, j);
121  result.clear();
122  include(j, result);
123  ensure(22,
124  result == expected_string("begin\n") + expected_string(contents_a) +
125  expected_string(contents_b) + expected_string("end\n"));
126  }
127 
128  {
130  std::ifstream k(file_k);
131  ensure(31, k);
132  result.clear();
133  include(k, result);
134  ensure(32,
135  result == expected_string("begin\n") + expected_string(contents_a) +
136  expected_string(contents_b) +
137  expected_string(contents_c) + expected_string("end\n"));
138  }
139 
140 } // main()
static QCString result
std::string string
Definition: nybbler.cc:12
int main()
Definition: include_test.cc:80
void ensure(int which, bool claim)
const double a
void include(std::istream &in, std::string &result)
Definition: include.cc:63
static bool * b
Definition: config.cpp:1043
static QCString str