-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_find.cpp
More file actions
91 lines (82 loc) · 3.41 KB
/
Copy pathtest_find.cpp
File metadata and controls
91 lines (82 loc) · 3.41 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
84
85
86
87
88
89
90
91
#include <cfbox/applets.hpp>
#include <gtest/gtest.h>
#include "test_capture.hpp"
#include <cfbox/applet_config.hpp>
#if CFBOX_ENABLE_FIND
using namespace cfbox::test;
TEST(FindTest, FindAllFiles) {
TempDir tmp;
tmp.write_file("a.txt", "");
tmp.write_file("b.cpp", "");
std::filesystem::create_directory(tmp.path / "sub");
tmp.write_file("sub/c.txt", "");
char a0[] = "find", a1[256];
std::snprintf(a1, sizeof(a1), "%s", tmp.path.string().c_str());
char* argv[] = {a0, a1};
auto out = capture_stdout([&]{ return find_main(2, argv); });
EXPECT_NE(out.find("a.txt"), std::string::npos);
EXPECT_NE(out.find("b.cpp"), std::string::npos);
EXPECT_NE(out.find("sub"), std::string::npos);
EXPECT_NE(out.find("c.txt"), std::string::npos);
}
TEST(FindTest, FindByName) {
TempDir tmp;
tmp.write_file("match.txt", "");
tmp.write_file("nomatch.cpp", "");
tmp.write_file("also.txt", "");
char a0[] = "find", a1[256], a2[] = "-name", a3[] = "*.txt";
std::snprintf(a1, sizeof(a1), "%s", tmp.path.string().c_str());
char* argv[] = {a0, a1, a2, a3};
auto out = capture_stdout([&]{ return find_main(4, argv); });
EXPECT_NE(out.find("match.txt"), std::string::npos);
EXPECT_NE(out.find("also.txt"), std::string::npos);
EXPECT_EQ(out.find("nomatch.cpp"), std::string::npos);
}
TEST(FindTest, FindByTypeDir) {
TempDir tmp;
tmp.write_file("file.txt", "");
std::filesystem::create_directory(tmp.path / "mydir");
char a0[] = "find", a1[256], a2[] = "-type", a3[] = "d";
std::snprintf(a1, sizeof(a1), "%s", tmp.path.string().c_str());
char* argv[] = {a0, a1, a2, a3};
auto out = capture_stdout([&]{ return find_main(4, argv); });
EXPECT_NE(out.find("mydir"), std::string::npos);
EXPECT_EQ(out.find("file.txt"), std::string::npos);
}
TEST(FindTest, FindByTypeFile) {
TempDir tmp;
tmp.write_file("file.txt", "");
std::filesystem::create_directory(tmp.path / "mydir");
char a0[] = "find", a1[256], a2[] = "-type", a3[] = "f";
std::snprintf(a1, sizeof(a1), "%s", tmp.path.string().c_str());
char* argv[] = {a0, a1, a2, a3};
auto out = capture_stdout([&]{ return find_main(4, argv); });
EXPECT_NE(out.find("file.txt"), std::string::npos);
EXPECT_EQ(out.find("mydir"), std::string::npos);
}
TEST(FindTest, MaxDepth) {
TempDir tmp;
std::filesystem::create_directory(tmp.path / "sub");
tmp.write_file("file1.txt", "");
tmp.write_file("sub/file2.txt", "");
char a0[] = "find", a1[256], a2[] = "-maxdepth", a3[] = "1";
std::snprintf(a1, sizeof(a1), "%s", tmp.path.string().c_str());
char* argv[] = {a0, a1, a2, a3};
auto out = capture_stdout([&]{ return find_main(4, argv); });
EXPECT_NE(out.find("file1.txt"), std::string::npos);
EXPECT_NE(out.find("sub"), std::string::npos);
EXPECT_EQ(out.find("file2.txt"), std::string::npos);
}
TEST(FindTest, NameAndTypeCombined) {
TempDir tmp;
tmp.write_file("data.txt", "");
std::filesystem::create_directory(tmp.path / "docs.txt");
char a0[] = "find", a1[256], a2[] = "-name", a3[] = "*.txt",
a4[] = "-type", a5[] = "f";
std::snprintf(a1, sizeof(a1), "%s", tmp.path.string().c_str());
char* argv[] = {a0, a1, a2, a3, a4, a5};
auto out = capture_stdout([&]{ return find_main(6, argv); });
EXPECT_NE(out.find("data.txt"), std::string::npos);
EXPECT_EQ(out.find("docs.txt"), std::string::npos);
}
#endif // CFBOX_ENABLE_FIND