OLD | NEW |
| (Empty) |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include <sstream> | |
6 | |
7 #include "testing/gtest/include/gtest/gtest.h" | |
8 #include "tools/gn/escape.h" | |
9 #include "tools/gn/file_template.h" | |
10 #include "tools/gn/test_with_scope.h" | |
11 | |
12 TEST(FileTemplate, Static) { | |
13 TestWithScope setup; | |
14 | |
15 std::vector<std::string> templates; | |
16 templates.push_back("something_static"); | |
17 FileTemplate t(setup.settings(), templates, | |
18 FileTemplate::OUTPUT_ABSOLUTE, SourceDir("//")); | |
19 EXPECT_FALSE(t.has_substitutions()); | |
20 | |
21 std::vector<std::string> result; | |
22 t.Apply(SourceFile("//foo/bar"), &result); | |
23 ASSERT_EQ(1u, result.size()); | |
24 EXPECT_EQ("something_static", result[0]); | |
25 } | |
26 | |
27 TEST(FileTemplate, Typical) { | |
28 TestWithScope setup; | |
29 | |
30 std::vector<std::string> templates; | |
31 templates.push_back("foo/{{source_name_part}}.cc"); | |
32 templates.push_back("foo/{{source_name_part}}.h"); | |
33 FileTemplate t(setup.settings(), templates, | |
34 FileTemplate::OUTPUT_ABSOLUTE, SourceDir("//")); | |
35 EXPECT_TRUE(t.has_substitutions()); | |
36 | |
37 std::vector<std::string> result; | |
38 t.Apply(SourceFile("//sources/ha.idl"), &result); | |
39 ASSERT_EQ(2u, result.size()); | |
40 EXPECT_EQ("foo/ha.cc", result[0]); | |
41 EXPECT_EQ("foo/ha.h", result[1]); | |
42 } | |
43 | |
44 TEST(FileTemplate, Weird) { | |
45 TestWithScope setup; | |
46 | |
47 std::vector<std::string> templates; | |
48 templates.push_back("{{{source}}{{source}}{{"); | |
49 FileTemplate t(setup.settings(), templates, | |
50 FileTemplate::OUTPUT_RELATIVE, | |
51 setup.settings()->build_settings()->build_dir()); | |
52 EXPECT_TRUE(t.has_substitutions()); | |
53 | |
54 std::vector<std::string> result; | |
55 t.Apply(SourceFile("//foo/lalala.c"), &result); | |
56 ASSERT_EQ(1u, result.size()); | |
57 EXPECT_EQ("{../../foo/lalala.c../../foo/lalala.c{{", result[0]); | |
58 } | |
59 | |
60 TEST(FileTemplate, NinjaExpansions) { | |
61 TestWithScope setup; | |
62 | |
63 std::vector<std::string> templates; | |
64 templates.push_back("-i"); | |
65 templates.push_back("{{source}}"); | |
66 templates.push_back("--out=foo bar\"{{source_name_part}}\".o"); | |
67 templates.push_back(""); // Test empty string. | |
68 | |
69 FileTemplate t(setup.settings(), templates, | |
70 FileTemplate::OUTPUT_RELATIVE, | |
71 setup.settings()->build_settings()->build_dir()); | |
72 | |
73 std::ostringstream out; | |
74 t.WriteWithNinjaExpansions(out); | |
75 | |
76 #if defined(OS_WIN) | |
77 // The third argument should get quoted since it contains a space, and the | |
78 // embedded quotes should get shell escaped. | |
79 EXPECT_EQ( | |
80 " -i ${source} \"--out=foo$ bar\\\"${source_name_part}\\\".o\" \"\"", | |
81 out.str()); | |
82 #else | |
83 // On Posix we don't need to quote the whole thing and can escape all | |
84 // individual bad chars. | |
85 EXPECT_EQ( | |
86 " -i ${source} --out=foo\\$ bar\\\"${source_name_part}\\\".o \"\"", | |
87 out.str()); | |
88 #endif | |
89 } | |
90 | |
91 TEST(FileTemplate, NinjaVariables) { | |
92 TestWithScope setup; | |
93 | |
94 std::vector<std::string> templates; | |
95 templates.push_back("-i"); | |
96 templates.push_back("{{source}}"); | |
97 templates.push_back("--out=foo bar\"{{source_name_part}}\".o"); | |
98 templates.push_back("{{source_file_part}}"); | |
99 templates.push_back("{{source_dir}}"); | |
100 templates.push_back("{{source_root_relative_dir}}"); | |
101 templates.push_back("{{source_gen_dir}}"); | |
102 templates.push_back("{{source_out_dir}}"); | |
103 | |
104 FileTemplate t(setup.settings(), templates, | |
105 FileTemplate::OUTPUT_RELATIVE, | |
106 setup.settings()->build_settings()->build_dir()); | |
107 | |
108 std::ostringstream out; | |
109 EscapeOptions options; | |
110 options.mode = ESCAPE_NINJA_COMMAND; | |
111 t.WriteNinjaVariablesForSubstitution(out, SourceFile("//foo/bar.txt"), | |
112 options); | |
113 | |
114 // Just the variables used above should be written. | |
115 EXPECT_EQ( | |
116 " source = ../../foo/bar.txt\n" | |
117 " source_name_part = bar\n" | |
118 " source_file_part = bar.txt\n" | |
119 " source_dir = ../../foo\n" | |
120 " source_root_rel_dir = foo\n" | |
121 " source_gen_dir = gen/foo\n" | |
122 " source_out_dir = obj/foo\n", | |
123 out.str()); | |
124 } | |
125 | |
126 // Tests in isolation different types of substitutions and that the right | |
127 // things are generated. | |
128 TEST(FileTemplate, Substitutions) { | |
129 TestWithScope setup; | |
130 | |
131 // Call to get substitutions relative to the build dir. | |
132 #define GetRelSubst(str, what) \ | |
133 FileTemplate::GetSubstitution( \ | |
134 setup.settings(), \ | |
135 SourceFile(str), \ | |
136 FileTemplate::Subrange::what, \ | |
137 FileTemplate::OUTPUT_RELATIVE, \ | |
138 setup.settings()->build_settings()->build_dir()) | |
139 | |
140 // Call to get absolute directory substitutions. | |
141 #define GetAbsSubst(str, what) \ | |
142 FileTemplate::GetSubstitution( \ | |
143 setup.settings(), \ | |
144 SourceFile(str), \ | |
145 FileTemplate::Subrange::what, \ | |
146 FileTemplate::OUTPUT_ABSOLUTE, \ | |
147 SourceDir()) | |
148 | |
149 // Try all possible templates with a normal looking string. | |
150 EXPECT_EQ("../../foo/bar/baz.txt", GetRelSubst("//foo/bar/baz.txt", SOURCE)); | |
151 EXPECT_EQ("//foo/bar/baz.txt", GetAbsSubst("//foo/bar/baz.txt", SOURCE)); | |
152 | |
153 EXPECT_EQ("baz", GetRelSubst("//foo/bar/baz.txt", NAME_PART)); | |
154 EXPECT_EQ("baz", GetAbsSubst("//foo/bar/baz.txt", NAME_PART)); | |
155 | |
156 EXPECT_EQ("baz.txt", GetRelSubst("//foo/bar/baz.txt", FILE_PART)); | |
157 EXPECT_EQ("baz.txt", GetAbsSubst("//foo/bar/baz.txt", FILE_PART)); | |
158 | |
159 EXPECT_EQ("../../foo/bar", GetRelSubst("//foo/bar/baz.txt", SOURCE_DIR)); | |
160 EXPECT_EQ("//foo/bar", GetAbsSubst("//foo/bar/baz.txt", SOURCE_DIR)); | |
161 | |
162 EXPECT_EQ("foo/bar", GetRelSubst("//foo/bar/baz.txt", ROOT_RELATIVE_DIR)); | |
163 EXPECT_EQ("foo/bar", GetAbsSubst("//foo/bar/baz.txt", ROOT_RELATIVE_DIR)); | |
164 | |
165 EXPECT_EQ("gen/foo/bar", GetRelSubst("//foo/bar/baz.txt", SOURCE_GEN_DIR)); | |
166 EXPECT_EQ("//out/Debug/gen/foo/bar", | |
167 GetAbsSubst("//foo/bar/baz.txt", SOURCE_GEN_DIR)); | |
168 | |
169 EXPECT_EQ("obj/foo/bar", GetRelSubst("//foo/bar/baz.txt", SOURCE_OUT_DIR)); | |
170 EXPECT_EQ("//out/Debug/obj/foo/bar", | |
171 GetAbsSubst("//foo/bar/baz.txt", SOURCE_OUT_DIR)); | |
172 | |
173 // Operations on an absolute path. | |
174 EXPECT_EQ("/baz.txt", GetRelSubst("/baz.txt", SOURCE)); | |
175 EXPECT_EQ("/.", GetRelSubst("/baz.txt", SOURCE_DIR)); | |
176 EXPECT_EQ("gen", GetRelSubst("/baz.txt", SOURCE_GEN_DIR)); | |
177 EXPECT_EQ("obj", GetRelSubst("/baz.txt", SOURCE_OUT_DIR)); | |
178 | |
179 EXPECT_EQ(".", GetRelSubst("//baz.txt", ROOT_RELATIVE_DIR)); | |
180 | |
181 #undef GetAbsSubst | |
182 #undef GetRelSubst | |
183 } | |
OLD | NEW |