Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(104)

Side by Side Diff: tools/gn/file_template_unittest.cc

Issue 387663003: Improve GN handling of directory templates. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « tools/gn/file_template.cc ('k') | tools/gn/filesystem_utils.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <sstream> 5 #include <sstream>
6 6
7 #include "testing/gtest/include/gtest/gtest.h" 7 #include "testing/gtest/include/gtest/gtest.h"
8 #include "tools/gn/escape.h" 8 #include "tools/gn/escape.h"
9 #include "tools/gn/file_template.h" 9 #include "tools/gn/file_template.h"
10 #include "tools/gn/test_with_scope.h" 10 #include "tools/gn/test_with_scope.h"
11 11
12 TEST(FileTemplate, Static) { 12 TEST(FileTemplate, Static) {
13 TestWithScope setup; 13 TestWithScope setup;
14 14
15 std::vector<std::string> templates; 15 std::vector<std::string> templates;
16 templates.push_back("something_static"); 16 templates.push_back("something_static");
17 FileTemplate t(setup.settings(), templates); 17 FileTemplate t(setup.settings(), templates,
18 FileTemplate::OUTPUT_ABSOLUTE, SourceDir("//"));
18 EXPECT_FALSE(t.has_substitutions()); 19 EXPECT_FALSE(t.has_substitutions());
19 20
20 std::vector<std::string> result; 21 std::vector<std::string> result;
21 t.Apply(SourceFile("//foo/bar"), &result); 22 t.Apply(SourceFile("//foo/bar"), &result);
22 ASSERT_EQ(1u, result.size()); 23 ASSERT_EQ(1u, result.size());
23 EXPECT_EQ("something_static", result[0]); 24 EXPECT_EQ("something_static", result[0]);
24 } 25 }
25 26
26 TEST(FileTemplate, Typical) { 27 TEST(FileTemplate, Typical) {
27 TestWithScope setup; 28 TestWithScope setup;
28 29
29 std::vector<std::string> templates; 30 std::vector<std::string> templates;
30 templates.push_back("foo/{{source_name_part}}.cc"); 31 templates.push_back("foo/{{source_name_part}}.cc");
31 templates.push_back("foo/{{source_name_part}}.h"); 32 templates.push_back("foo/{{source_name_part}}.h");
32 FileTemplate t(setup.settings(), templates); 33 FileTemplate t(setup.settings(), templates,
34 FileTemplate::OUTPUT_ABSOLUTE, SourceDir("//"));
33 EXPECT_TRUE(t.has_substitutions()); 35 EXPECT_TRUE(t.has_substitutions());
34 36
35 std::vector<std::string> result; 37 std::vector<std::string> result;
36 t.Apply(SourceFile("//sources/ha.idl"), &result); 38 t.Apply(SourceFile("//sources/ha.idl"), &result);
37 ASSERT_EQ(2u, result.size()); 39 ASSERT_EQ(2u, result.size());
38 EXPECT_EQ("foo/ha.cc", result[0]); 40 EXPECT_EQ("foo/ha.cc", result[0]);
39 EXPECT_EQ("foo/ha.h", result[1]); 41 EXPECT_EQ("foo/ha.h", result[1]);
40 } 42 }
41 43
42 TEST(FileTemplate, Weird) { 44 TEST(FileTemplate, Weird) {
43 TestWithScope setup; 45 TestWithScope setup;
44 46
45 std::vector<std::string> templates; 47 std::vector<std::string> templates;
46 templates.push_back("{{{source}}{{source}}{{"); 48 templates.push_back("{{{source}}{{source}}{{");
47 FileTemplate t(setup.settings(), templates); 49 FileTemplate t(setup.settings(), templates,
50 FileTemplate::OUTPUT_RELATIVE,
51 setup.settings()->build_settings()->build_dir());
48 EXPECT_TRUE(t.has_substitutions()); 52 EXPECT_TRUE(t.has_substitutions());
49 53
50 std::vector<std::string> result; 54 std::vector<std::string> result;
51 t.Apply(SourceFile("//foo/lalala.c"), &result); 55 t.Apply(SourceFile("//foo/lalala.c"), &result);
52 ASSERT_EQ(1u, result.size()); 56 ASSERT_EQ(1u, result.size());
53 EXPECT_EQ("{../../foo/lalala.c../../foo/lalala.c{{", result[0]); 57 EXPECT_EQ("{../../foo/lalala.c../../foo/lalala.c{{", result[0]);
54 } 58 }
55 59
56 TEST(FileTemplate, NinjaExpansions) { 60 TEST(FileTemplate, NinjaExpansions) {
57 TestWithScope setup; 61 TestWithScope setup;
58 62
59 std::vector<std::string> templates; 63 std::vector<std::string> templates;
60 templates.push_back("-i"); 64 templates.push_back("-i");
61 templates.push_back("{{source}}"); 65 templates.push_back("{{source}}");
62 templates.push_back("--out=foo bar\"{{source_name_part}}\".o"); 66 templates.push_back("--out=foo bar\"{{source_name_part}}\".o");
63 templates.push_back(""); // Test empty string. 67 templates.push_back(""); // Test empty string.
64 68
65 FileTemplate t(setup.settings(), templates); 69 FileTemplate t(setup.settings(), templates,
70 FileTemplate::OUTPUT_RELATIVE,
71 setup.settings()->build_settings()->build_dir());
66 72
67 std::ostringstream out; 73 std::ostringstream out;
68 t.WriteWithNinjaExpansions(out); 74 t.WriteWithNinjaExpansions(out);
69 75
70 #if defined(OS_WIN) 76 #if defined(OS_WIN)
71 // The third argument should get quoted since it contains a space, and the 77 // The third argument should get quoted since it contains a space, and the
72 // embedded quotes should get shell escaped. 78 // embedded quotes should get shell escaped.
73 EXPECT_EQ( 79 EXPECT_EQ(
74 " -i ${source} \"--out=foo$ bar\\\"${source_name_part}\\\".o\" \"\"", 80 " -i ${source} \"--out=foo$ bar\\\"${source_name_part}\\\".o\" \"\"",
75 out.str()); 81 out.str());
(...skipping 12 matching lines...) Expand all
88 std::vector<std::string> templates; 94 std::vector<std::string> templates;
89 templates.push_back("-i"); 95 templates.push_back("-i");
90 templates.push_back("{{source}}"); 96 templates.push_back("{{source}}");
91 templates.push_back("--out=foo bar\"{{source_name_part}}\".o"); 97 templates.push_back("--out=foo bar\"{{source_name_part}}\".o");
92 templates.push_back("{{source_file_part}}"); 98 templates.push_back("{{source_file_part}}");
93 templates.push_back("{{source_dir}}"); 99 templates.push_back("{{source_dir}}");
94 templates.push_back("{{source_root_relative_dir}}"); 100 templates.push_back("{{source_root_relative_dir}}");
95 templates.push_back("{{source_gen_dir}}"); 101 templates.push_back("{{source_gen_dir}}");
96 templates.push_back("{{source_out_dir}}"); 102 templates.push_back("{{source_out_dir}}");
97 103
98 FileTemplate t(setup.settings(), templates); 104 FileTemplate t(setup.settings(), templates,
105 FileTemplate::OUTPUT_RELATIVE,
106 setup.settings()->build_settings()->build_dir());
99 107
100 std::ostringstream out; 108 std::ostringstream out;
101 EscapeOptions options; 109 EscapeOptions options;
102 options.mode = ESCAPE_NINJA_COMMAND; 110 options.mode = ESCAPE_NINJA_COMMAND;
103 t.WriteNinjaVariablesForSubstitution(out, setup.settings(), 111 t.WriteNinjaVariablesForSubstitution(out, SourceFile("//foo/bar.txt"),
104 SourceFile("//foo/bar.txt"), options); 112 options);
105 113
106 // Just the variables used above should be written. 114 // Just the variables used above should be written.
107 EXPECT_EQ( 115 EXPECT_EQ(
108 " source = ../../foo/bar.txt\n" 116 " source = ../../foo/bar.txt\n"
109 " source_name_part = bar\n" 117 " source_name_part = bar\n"
110 " source_file_part = bar.txt\n" 118 " source_file_part = bar.txt\n"
111 " source_dir = ../../foo\n" 119 " source_dir = ../../foo\n"
112 " source_root_rel_dir = foo\n" 120 " source_root_rel_dir = foo\n"
113 " source_gen_dir = gen/foo\n" 121 " source_gen_dir = gen/foo\n"
114 " source_out_dir = obj/foo\n", 122 " source_out_dir = obj/foo\n",
115 out.str()); 123 out.str());
116 } 124 }
117 125
118 // Tests in isolation different types of substitutions and that the right 126 // Tests in isolation different types of substitutions and that the right
119 // things are generated. 127 // things are generated.
120 TEST(FileTemplate, Substitutions) { 128 TEST(FileTemplate, Substitutions) {
121 TestWithScope setup; 129 TestWithScope setup;
122 130
123 #define GetSubst(str, what) \ 131 // Call to get substitutions relative to the build dir.
124 FileTemplate::GetSubstitution(setup.settings(), \ 132 #define GetRelSubst(str, what) \
125 SourceFile(str), \ 133 FileTemplate::GetSubstitution( \
126 FileTemplate::Subrange::what) 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())
127 148
128 // Try all possible templates with a normal looking string. 149 // Try all possible templates with a normal looking string.
129 EXPECT_EQ("../../foo/bar/baz.txt", GetSubst("//foo/bar/baz.txt", SOURCE)); 150 EXPECT_EQ("../../foo/bar/baz.txt", GetRelSubst("//foo/bar/baz.txt", SOURCE));
130 EXPECT_EQ("baz", GetSubst("//foo/bar/baz.txt", NAME_PART)); 151 EXPECT_EQ("//foo/bar/baz.txt", GetAbsSubst("//foo/bar/baz.txt", SOURCE));
131 EXPECT_EQ("baz.txt", GetSubst("//foo/bar/baz.txt", FILE_PART)); 152
132 EXPECT_EQ("../../foo/bar", GetSubst("//foo/bar/baz.txt", SOURCE_DIR)); 153 EXPECT_EQ("baz", GetRelSubst("//foo/bar/baz.txt", NAME_PART));
133 EXPECT_EQ("foo/bar", GetSubst("//foo/bar/baz.txt", ROOT_RELATIVE_DIR)); 154 EXPECT_EQ("baz", GetAbsSubst("//foo/bar/baz.txt", NAME_PART));
134 EXPECT_EQ("gen/foo/bar", GetSubst("//foo/bar/baz.txt", SOURCE_GEN_DIR)); 155
135 EXPECT_EQ("obj/foo/bar", GetSubst("//foo/bar/baz.txt", SOURCE_OUT_DIR)); 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));
136 172
137 // Operations on an absolute path. 173 // Operations on an absolute path.
138 EXPECT_EQ("/baz.txt", GetSubst("/baz.txt", SOURCE)); 174 EXPECT_EQ("/baz.txt", GetRelSubst("/baz.txt", SOURCE));
139 EXPECT_EQ("/.", GetSubst("/baz.txt", SOURCE_DIR)); 175 EXPECT_EQ("/.", GetRelSubst("/baz.txt", SOURCE_DIR));
140 EXPECT_EQ("gen", GetSubst("/baz.txt", SOURCE_GEN_DIR)); 176 EXPECT_EQ("gen", GetRelSubst("/baz.txt", SOURCE_GEN_DIR));
141 EXPECT_EQ("obj", GetSubst("/baz.txt", SOURCE_OUT_DIR)); 177 EXPECT_EQ("obj", GetRelSubst("/baz.txt", SOURCE_OUT_DIR));
142 178
143 EXPECT_EQ(".", GetSubst("//baz.txt", ROOT_RELATIVE_DIR)); 179 EXPECT_EQ(".", GetRelSubst("//baz.txt", ROOT_RELATIVE_DIR));
144 180
145 #undef GetSubst 181 #undef GetAbsSubst
182 #undef GetRelSubst
146 } 183 }
OLDNEW
« no previous file with comments | « tools/gn/file_template.cc ('k') | tools/gn/filesystem_utils.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698