-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtest_mangle.cpp
More file actions
127 lines (114 loc) · 4.48 KB
/
test_mangle.cpp
File metadata and controls
127 lines (114 loc) · 4.48 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include <gtest/gtest.h>
import std;
import mcpp.pm.mangle;
using namespace mcpp::pm;
TEST(Mangle, NameFormat) {
EXPECT_EQ(mangle_name("json", "1.2.3"), "json__v1_2_3__mcpp");
EXPECT_EQ(mangle_name("mcpplibs.cmdline","2.0"),"mcpplibs.cmdline__v2_0__mcpp");
EXPECT_EQ(mangle_name("a", "0"), "a__v0__mcpp");
}
TEST(Mangle, RewriteEmpty) {
std::map<std::string, std::string> table;
EXPECT_EQ(rewrite_module_decls("", table), "");
EXPECT_EQ(rewrite_module_decls("export module foo;\n", table),
"export module foo;\n")
<< "empty rename table → no change";
}
TEST(Mangle, RewriteModuleDecl) {
std::map<std::string, std::string> table {
{"json", "json__v2_0_0__mcpp"},
};
EXPECT_EQ(rewrite_module_decls("export module json;\n", table),
"export module json__v2_0_0__mcpp;\n");
EXPECT_EQ(rewrite_module_decls("module json;\n", table),
"module json__v2_0_0__mcpp;\n");
}
TEST(Mangle, RewritePartitionDecl) {
std::map<std::string, std::string> table {
{"json", "json__v2_0_0__mcpp"},
};
EXPECT_EQ(rewrite_module_decls("export module json:utils;\n", table),
"export module json__v2_0_0__mcpp:utils;\n");
EXPECT_EQ(rewrite_module_decls("module json:impl;\n", table),
"module json__v2_0_0__mcpp:impl;\n");
}
TEST(Mangle, RewriteImports) {
std::map<std::string, std::string> table {
{"json", "json__v2_0_0__mcpp"},
};
EXPECT_EQ(rewrite_module_decls("import json;\n", table),
"import json__v2_0_0__mcpp;\n");
EXPECT_EQ(rewrite_module_decls("export import json;\n", table),
"export import json__v2_0_0__mcpp;\n");
EXPECT_EQ(rewrite_module_decls("import json:utils;\n", table),
"import json__v2_0_0__mcpp:utils;\n");
}
TEST(Mangle, KeepBarePartitionImport) {
std::map<std::string, std::string> table {
{"json", "json__v2_0_0__mcpp"},
};
// `import :P;` refers to the enclosing module's partition — not a
// named module; must NOT be rewritten.
EXPECT_EQ(rewrite_module_decls("import :utils;\n", table),
"import :utils;\n");
EXPECT_EQ(rewrite_module_decls("module ;\n", table),
"module ;\n")
<< "global module fragment opener has no name";
}
TEST(Mangle, KeepNonMatching) {
std::map<std::string, std::string> table {
{"json", "json__v2_0_0__mcpp"},
};
// Other modules in the same file shouldn't be touched.
EXPECT_EQ(rewrite_module_decls("import std;\n", table), "import std;\n");
EXPECT_EQ(rewrite_module_decls("import other;\n", table), "import other;\n");
EXPECT_EQ(rewrite_module_decls("export module yaml;\n", table),
"export module yaml;\n");
}
TEST(Mangle, MultipleLines) {
std::map<std::string, std::string> table {
{"json", "json__v2_0_0__mcpp"},
};
std::string in =
"// header comment\n"
"module;\n"
"#include <iostream>\n"
"export module json;\n"
"import std;\n"
"import json:utils;\n"
"export int answer() { return 42; }\n";
std::string expected =
"// header comment\n"
"module;\n"
"#include <iostream>\n"
"export module json__v2_0_0__mcpp;\n"
"import std;\n"
"import json__v2_0_0__mcpp:utils;\n"
"export int answer() { return 42; }\n";
EXPECT_EQ(rewrite_module_decls(in, table), expected);
}
TEST(Mangle, LeadingWhitespace) {
std::map<std::string, std::string> table {
{"json", "json__v2_0_0__mcpp"},
};
EXPECT_EQ(rewrite_module_decls(" export module json;\n", table),
" export module json__v2_0_0__mcpp;\n");
EXPECT_EQ(rewrite_module_decls("\timport json;\n", table),
"\timport json__v2_0_0__mcpp;\n");
}
TEST(Mangle, NoTrailingNewline) {
std::map<std::string, std::string> table {
{"json", "json__v2_0_0__mcpp"},
};
EXPECT_EQ(rewrite_module_decls("export module json;", table),
"export module json__v2_0_0__mcpp;");
}
TEST(Mangle, DottedNames) {
std::map<std::string, std::string> table {
{"mcpplibs.cmdline", "mcpplibs.cmdline__v2_0_0__mcpp"},
};
EXPECT_EQ(rewrite_module_decls("export module mcpplibs.cmdline;\n", table),
"export module mcpplibs.cmdline__v2_0_0__mcpp;\n");
EXPECT_EQ(rewrite_module_decls("import mcpplibs.cmdline;\n", table),
"import mcpplibs.cmdline__v2_0_0__mcpp;\n");
}