-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_wc.cpp
More file actions
83 lines (74 loc) · 2.71 KB
/
Copy pathtest_wc.cpp
File metadata and controls
83 lines (74 loc) · 2.71 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <cfbox/applets.hpp>
#include <gtest/gtest.h>
#include "test_capture.hpp"
#include <cfbox/applet_config.hpp>
#if CFBOX_ENABLE_WC
using namespace cfbox::test;
TEST(WcTest, DefaultAll) {
TempDir tmp;
tmp.write_file("test.txt", "hello world\nfoo bar\n");
auto f = (tmp.path / "test.txt").string();
char a0[] = "wc", a1[256];
std::snprintf(a1, sizeof(a1), "%s", f.c_str());
char* argv[] = {a0, a1};
auto out = capture_stdout([&]{ return wc_main(2, argv); });
// 2 lines, 4 words, 20 bytes
EXPECT_NE(out.find("2"), std::string::npos);
EXPECT_NE(out.find("4"), std::string::npos);
EXPECT_NE(out.find("20"), std::string::npos);
EXPECT_NE(out.find("test.txt"), std::string::npos);
}
TEST(WcTest, LinesOnly) {
TempDir tmp;
tmp.write_file("test.txt", "a\nb\nc\n");
auto f = (tmp.path / "test.txt").string();
char a0[] = "wc", a1[] = "-l", a2[256];
std::snprintf(a2, sizeof(a2), "%s", f.c_str());
char* argv[] = {a0, a1, a2};
auto out = capture_stdout([&]{ return wc_main(3, argv); });
EXPECT_NE(out.find("3"), std::string::npos);
}
TEST(WcTest, WordsOnly) {
TempDir tmp;
tmp.write_file("test.txt", "one two three\n");
auto f = (tmp.path / "test.txt").string();
char a0[] = "wc", a1[] = "-w", a2[256];
std::snprintf(a2, sizeof(a2), "%s", f.c_str());
char* argv[] = {a0, a1, a2};
auto out = capture_stdout([&]{ return wc_main(3, argv); });
EXPECT_NE(out.find("3"), std::string::npos);
}
TEST(WcTest, BytesOnly) {
TempDir tmp;
tmp.write_file("test.txt", "abc");
auto f = (tmp.path / "test.txt").string();
char a0[] = "wc", a1[] = "-c", a2[256];
std::snprintf(a2, sizeof(a2), "%s", f.c_str());
char* argv[] = {a0, a1, a2};
auto out = capture_stdout([&]{ return wc_main(3, argv); });
EXPECT_NE(out.find("3"), std::string::npos);
}
TEST(WcTest, EmptyFile) {
TempDir tmp;
tmp.write_file("empty.txt", "");
auto f = (tmp.path / "empty.txt").string();
char a0[] = "wc", a1[256];
std::snprintf(a1, sizeof(a1), "%s", f.c_str());
char* argv[] = {a0, a1};
auto out = capture_stdout([&]{ return wc_main(2, argv); });
EXPECT_NE(out.find("0"), std::string::npos);
}
TEST(WcTest, MultipleFilesShowsTotal) {
TempDir tmp;
tmp.write_file("a.txt", "aaa\n");
tmp.write_file("b.txt", "bbb\n");
auto fa = (tmp.path / "a.txt").string();
auto fb = (tmp.path / "b.txt").string();
char a0[] = "wc", a1[256], a2[256];
std::snprintf(a1, sizeof(a1), "%s", fa.c_str());
std::snprintf(a2, sizeof(a2), "%s", fb.c_str());
char* argv[] = {a0, a1, a2};
auto out = capture_stdout([&]{ return wc_main(3, argv); });
EXPECT_NE(out.find("total"), std::string::npos);
}
#endif // CFBOX_ENABLE_WC