OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/err.h" | 8 #include "tools/gn/err.h" |
9 #include "tools/gn/escape.h" | 9 #include "tools/gn/escape.h" |
| 10 #include "tools/gn/substitution_list.h" |
10 #include "tools/gn/substitution_pattern.h" | 11 #include "tools/gn/substitution_pattern.h" |
11 #include "tools/gn/substitution_writer.h" | 12 #include "tools/gn/substitution_writer.h" |
| 13 #include "tools/gn/target.h" |
12 #include "tools/gn/test_with_scope.h" | 14 #include "tools/gn/test_with_scope.h" |
13 | 15 |
| 16 TEST(SubstitutionWriter, GetListAs) { |
| 17 TestWithScope setup; |
| 18 |
| 19 SubstitutionList list = SubstitutionList::MakeForTest( |
| 20 "//foo/bar/a.cc", |
| 21 "//foo/bar/b.cc"); |
| 22 |
| 23 std::vector<SourceFile> sources; |
| 24 SubstitutionWriter::GetListAsSourceFiles(list, &sources); |
| 25 ASSERT_EQ(2u, sources.size()); |
| 26 EXPECT_EQ("//foo/bar/a.cc", sources[0].value()); |
| 27 EXPECT_EQ("//foo/bar/b.cc", sources[1].value()); |
| 28 |
| 29 std::vector<OutputFile> outputs; |
| 30 SubstitutionWriter::GetListAsOutputFiles(setup.settings(), list, &outputs); |
| 31 ASSERT_EQ(2u, outputs.size()); |
| 32 EXPECT_EQ("../../foo/bar/a.cc", outputs[0].value()); |
| 33 EXPECT_EQ("../../foo/bar/b.cc", outputs[1].value()); |
| 34 } |
| 35 |
14 TEST(SubstitutionWriter, ApplyPatternToSource) { | 36 TEST(SubstitutionWriter, ApplyPatternToSource) { |
15 TestWithScope setup; | 37 TestWithScope setup; |
16 | 38 |
17 SubstitutionPattern pattern; | 39 SubstitutionPattern pattern; |
18 Err err; | 40 Err err; |
19 ASSERT_TRUE(pattern.Parse("{{source_gen_dir}}/{{source_name_part}}.tmp", | 41 ASSERT_TRUE(pattern.Parse("{{source_gen_dir}}/{{source_name_part}}.tmp", |
20 NULL, &err)); | 42 NULL, &err)); |
21 | 43 |
22 SourceFile result = SubstitutionWriter::ApplyPatternToSource( | 44 SourceFile result = SubstitutionWriter::ApplyPatternToSource( |
23 setup.settings(), pattern, SourceFile("//foo/bar/myfile.txt")); | 45 setup.settings(), pattern, SourceFile("//foo/bar/myfile.txt")); |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
72 options.mode = ESCAPE_NONE; | 94 options.mode = ESCAPE_NONE; |
73 | 95 |
74 std::ostringstream out; | 96 std::ostringstream out; |
75 SubstitutionWriter::WriteWithNinjaVariables(pattern, options, out); | 97 SubstitutionWriter::WriteWithNinjaVariables(pattern, options, out); |
76 | 98 |
77 EXPECT_EQ( | 99 EXPECT_EQ( |
78 "-i ${in} --out=bar\"${source_name_part}\".o", | 100 "-i ${in} --out=bar\"${source_name_part}\".o", |
79 out.str()); | 101 out.str()); |
80 } | 102 } |
81 | 103 |
82 // Tests in isolation different types of substitutions and that the right | 104 TEST(SubstutitionWriter, SourceSubstitutions) { |
83 // things are generated. | |
84 TEST(SubstutitionWriter, Substitutions) { | |
85 TestWithScope setup; | 105 TestWithScope setup; |
86 | 106 |
87 // Call to get substitutions relative to the build dir. | 107 // Call to get substitutions relative to the build dir. |
88 #define GetRelSubst(str, what) \ | 108 #define GetRelSubst(str, what) \ |
89 SubstitutionWriter::GetSourceSubstitution( \ | 109 SubstitutionWriter::GetSourceSubstitution( \ |
90 setup.settings(), \ | 110 setup.settings(), \ |
91 SourceFile(str), \ | 111 SourceFile(str), \ |
92 what, \ | 112 what, \ |
93 SubstitutionWriter::OUTPUT_RELATIVE, \ | 113 SubstitutionWriter::OUTPUT_RELATIVE, \ |
94 setup.settings()->build_settings()->build_dir()) | 114 setup.settings()->build_settings()->build_dir()) |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
143 EXPECT_EQ("/.", GetRelSubst("/baz.txt", SUBSTITUTION_SOURCE_DIR)); | 163 EXPECT_EQ("/.", GetRelSubst("/baz.txt", SUBSTITUTION_SOURCE_DIR)); |
144 EXPECT_EQ("gen", GetRelSubst("/baz.txt", SUBSTITUTION_SOURCE_GEN_DIR)); | 164 EXPECT_EQ("gen", GetRelSubst("/baz.txt", SUBSTITUTION_SOURCE_GEN_DIR)); |
145 EXPECT_EQ("obj", GetRelSubst("/baz.txt", SUBSTITUTION_SOURCE_OUT_DIR)); | 165 EXPECT_EQ("obj", GetRelSubst("/baz.txt", SUBSTITUTION_SOURCE_OUT_DIR)); |
146 | 166 |
147 EXPECT_EQ(".", | 167 EXPECT_EQ(".", |
148 GetRelSubst("//baz.txt", SUBSTITUTION_SOURCE_ROOT_RELATIVE_DIR)); | 168 GetRelSubst("//baz.txt", SUBSTITUTION_SOURCE_ROOT_RELATIVE_DIR)); |
149 | 169 |
150 #undef GetAbsSubst | 170 #undef GetAbsSubst |
151 #undef GetRelSubst | 171 #undef GetRelSubst |
152 } | 172 } |
| 173 |
| 174 TEST(SubstitutionWriter, TargetSubstitutions) { |
| 175 TestWithScope setup; |
| 176 |
| 177 Target target(setup.settings(), Label(SourceDir("//foo/bar/"), "baz")); |
| 178 target.set_output_type(Target::STATIC_LIBRARY); |
| 179 target.SetToolchain(setup.toolchain()); |
| 180 target.OnResolved(); |
| 181 |
| 182 std::string result; |
| 183 EXPECT_TRUE(SubstitutionWriter::GetTargetSubstitution( |
| 184 &target, SUBSTITUTION_LABEL, &result)); |
| 185 EXPECT_EQ("//foo/bar:baz", result); |
| 186 |
| 187 EXPECT_TRUE(SubstitutionWriter::GetTargetSubstitution( |
| 188 &target, SUBSTITUTION_ROOT_GEN_DIR, &result)); |
| 189 EXPECT_EQ("gen", result); |
| 190 |
| 191 EXPECT_TRUE(SubstitutionWriter::GetTargetSubstitution( |
| 192 &target, SUBSTITUTION_ROOT_OUT_DIR, &result)); |
| 193 EXPECT_EQ("", result); |
| 194 |
| 195 EXPECT_TRUE(SubstitutionWriter::GetTargetSubstitution( |
| 196 &target, SUBSTITUTION_TARGET_GEN_DIR, &result)); |
| 197 EXPECT_EQ("gen/foo/bar", result); |
| 198 |
| 199 EXPECT_TRUE(SubstitutionWriter::GetTargetSubstitution( |
| 200 &target, SUBSTITUTION_TARGET_OUT_DIR, &result)); |
| 201 EXPECT_EQ("obj/foo/bar", result); |
| 202 |
| 203 EXPECT_TRUE(SubstitutionWriter::GetTargetSubstitution( |
| 204 &target, SUBSTITUTION_TARGET_OUTPUT_NAME, &result)); |
| 205 EXPECT_EQ("libbaz", result); |
| 206 } |
| 207 |
| 208 TEST(SubstitutionWriter, CompilerSubstitutions) { |
| 209 TestWithScope setup; |
| 210 |
| 211 Target target(setup.settings(), Label(SourceDir("//foo/bar/"), "baz")); |
| 212 target.set_output_type(Target::STATIC_LIBRARY); |
| 213 target.SetToolchain(setup.toolchain()); |
| 214 target.OnResolved(); |
| 215 |
| 216 // The compiler substitution is just source + target combined. So test one |
| 217 // of each of those classes of things to make sure this is hooked up. |
| 218 EXPECT_EQ("file", |
| 219 SubstitutionWriter::GetCompilerSubstitution( |
| 220 &target, SourceFile("//foo/bar/file.txt"), |
| 221 SUBSTITUTION_SOURCE_NAME_PART)); |
| 222 EXPECT_EQ("gen/foo/bar", |
| 223 SubstitutionWriter::GetCompilerSubstitution( |
| 224 &target, SourceFile("//foo/bar/file.txt"), |
| 225 SUBSTITUTION_TARGET_GEN_DIR)); |
| 226 } |
| 227 |
| 228 TEST(SubstitutionWriter, LinkerSubstitutions) { |
| 229 TestWithScope setup; |
| 230 |
| 231 Target target(setup.settings(), Label(SourceDir("//foo/bar/"), "baz")); |
| 232 target.set_output_type(Target::SHARED_LIBRARY); |
| 233 target.SetToolchain(setup.toolchain()); |
| 234 target.OnResolved(); |
| 235 |
| 236 const Tool* tool = setup.toolchain()->GetToolForTargetFinalOutput(&target); |
| 237 |
| 238 // The compiler substitution is just target + OUTPUT_EXTENSION combined. So |
| 239 // test one target one plus the output extension. |
| 240 EXPECT_EQ(".so", |
| 241 SubstitutionWriter::GetLinkerSubstitution( |
| 242 &target, tool, SUBSTITUTION_OUTPUT_EXTENSION)); |
| 243 EXPECT_EQ("gen/foo/bar", |
| 244 SubstitutionWriter::GetLinkerSubstitution( |
| 245 &target, tool, SUBSTITUTION_TARGET_GEN_DIR)); |
| 246 |
| 247 // Test that we handle paths that end up in the root build dir properly |
| 248 // (no leading "./" or "/"). |
| 249 Err err; |
| 250 SubstitutionPattern pattern; |
| 251 ASSERT_TRUE( |
| 252 pattern.Parse("{{root_out_dir}}/{{target_output_name}}.so", NULL, &err)); |
| 253 |
| 254 OutputFile output = SubstitutionWriter::ApplyPatternToLinkerAsOutputFile( |
| 255 &target, tool, pattern); |
| 256 EXPECT_EQ("libbaz.so", output.value()); |
| 257 } |
OLD | NEW |