-
Notifications
You must be signed in to change notification settings - Fork 0
/
file-util-test.cc
54 lines (44 loc) · 1.27 KB
/
file-util-test.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// file-util-test.cc
// Tests for file-util.
#include "file-util.h" // module under test
#include <cstdlib> // std::getenv, std::exit
#include <iostream> // std::cout, etc.
#include <sstream> // std::ostringstream
#include <string> // std::string
#include <assert.h> // assert
using std::cerr;
using std::cout;
using std::string;
void file_util_unit_tests()
{
string fname = "Makefile";
// If set, this unit test acts like a crude version of 'cat'. This
// is mainly useful to let me exercise the error reporting manually.
char const *altFname = std::getenv("FILE_UTIL_TEST_FNAME");
if (altFname) {
fname = altFname;
}
string contents;
string err = readFile(contents /*OUT*/, fname);
if (!err.empty()) {
cerr << "error: " << err << "\n";
std::exit(2);
}
if (!altFname) {
// Get the first line.
std::ostringstream os;
for (char c : contents) {
if (c == '\n') {
break;
}
os << c;
}
assert(os.str() == "# print-clang-ast/Makefile");
}
else {
// Print the entire thing and exit.
cout << contents;
std::exit(0);
}
}
// EOF