OLD | NEW |
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 | 11 |
11 TEST(FileTemplate, Static) { | 12 TEST(FileTemplate, Static) { |
| 13 TestWithScope setup; |
| 14 |
12 std::vector<std::string> templates; | 15 std::vector<std::string> templates; |
13 templates.push_back("something_static"); | 16 templates.push_back("something_static"); |
14 FileTemplate t(templates); | 17 FileTemplate t(setup.settings(), templates); |
15 EXPECT_FALSE(t.has_substitutions()); | 18 EXPECT_FALSE(t.has_substitutions()); |
16 | 19 |
17 std::vector<std::string> result; | 20 std::vector<std::string> result; |
18 t.ApplyString("", &result); | 21 t.Apply(SourceFile("//foo/bar"), &result); |
19 ASSERT_EQ(1u, result.size()); | |
20 EXPECT_EQ("something_static", result[0]); | |
21 | |
22 result.clear(); | |
23 t.ApplyString("lalala", &result); | |
24 ASSERT_EQ(1u, result.size()); | 22 ASSERT_EQ(1u, result.size()); |
25 EXPECT_EQ("something_static", result[0]); | 23 EXPECT_EQ("something_static", result[0]); |
26 } | 24 } |
27 | 25 |
28 TEST(FileTemplate, Typical) { | 26 TEST(FileTemplate, Typical) { |
| 27 TestWithScope setup; |
| 28 |
29 std::vector<std::string> templates; | 29 std::vector<std::string> templates; |
30 templates.push_back("foo/{{source_name_part}}.cc"); | 30 templates.push_back("foo/{{source_name_part}}.cc"); |
31 templates.push_back("foo/{{source_name_part}}.h"); | 31 templates.push_back("foo/{{source_name_part}}.h"); |
32 FileTemplate t(templates); | 32 FileTemplate t(setup.settings(), templates); |
33 EXPECT_TRUE(t.has_substitutions()); | 33 EXPECT_TRUE(t.has_substitutions()); |
34 | 34 |
35 std::vector<std::string> result; | 35 std::vector<std::string> result; |
36 t.ApplyString("sources/ha.idl", &result); | 36 t.Apply(SourceFile("//sources/ha.idl"), &result); |
37 ASSERT_EQ(2u, result.size()); | 37 ASSERT_EQ(2u, result.size()); |
38 EXPECT_EQ("foo/ha.cc", result[0]); | 38 EXPECT_EQ("foo/ha.cc", result[0]); |
39 EXPECT_EQ("foo/ha.h", result[1]); | 39 EXPECT_EQ("foo/ha.h", result[1]); |
40 } | 40 } |
41 | 41 |
42 TEST(FileTemplate, Weird) { | 42 TEST(FileTemplate, Weird) { |
| 43 TestWithScope setup; |
| 44 |
43 std::vector<std::string> templates; | 45 std::vector<std::string> templates; |
44 templates.push_back("{{{source}}{{source}}{{"); | 46 templates.push_back("{{{source}}{{source}}{{"); |
45 FileTemplate t(templates); | 47 FileTemplate t(setup.settings(), templates); |
46 EXPECT_TRUE(t.has_substitutions()); | 48 EXPECT_TRUE(t.has_substitutions()); |
47 | 49 |
48 std::vector<std::string> result; | 50 std::vector<std::string> result; |
49 t.ApplyString("foo/lalala.c", &result); | 51 t.Apply(SourceFile("//foo/lalala.c"), &result); |
50 ASSERT_EQ(1u, result.size()); | 52 ASSERT_EQ(1u, result.size()); |
51 EXPECT_EQ("{foo/lalala.cfoo/lalala.c{{", result[0]); | 53 EXPECT_EQ("{../../foo/lalala.c../../foo/lalala.c{{", result[0]); |
52 } | 54 } |
53 | 55 |
54 TEST(FileTemplate, NinjaExpansions) { | 56 TEST(FileTemplate, NinjaExpansions) { |
| 57 TestWithScope setup; |
| 58 |
55 std::vector<std::string> templates; | 59 std::vector<std::string> templates; |
56 templates.push_back("-i"); | 60 templates.push_back("-i"); |
57 templates.push_back("{{source}}"); | 61 templates.push_back("{{source}}"); |
58 templates.push_back("--out=foo bar\"{{source_name_part}}\".o"); | 62 templates.push_back("--out=foo bar\"{{source_name_part}}\".o"); |
59 templates.push_back(""); // Test empty string. | 63 templates.push_back(""); // Test empty string. |
60 | 64 |
61 FileTemplate t(templates); | 65 FileTemplate t(setup.settings(), templates); |
62 | 66 |
63 std::ostringstream out; | 67 std::ostringstream out; |
64 t.WriteWithNinjaExpansions(out); | 68 t.WriteWithNinjaExpansions(out); |
65 | 69 |
66 #if defined(OS_WIN) | 70 #if defined(OS_WIN) |
67 // The third argument should get quoted since it contains a space, and the | 71 // The third argument should get quoted since it contains a space, and the |
68 // embedded quotes should get shell escaped. | 72 // embedded quotes should get shell escaped. |
69 EXPECT_EQ( | 73 EXPECT_EQ( |
70 " -i ${source} \"--out=foo$ bar\\\"${source_name_part}\\\".o\" \"\"", | 74 " -i ${source} \"--out=foo$ bar\\\"${source_name_part}\\\".o\" \"\"", |
71 out.str()); | 75 out.str()); |
72 #else | 76 #else |
73 // On Posix we don't need to quote the whole thing and can escape all | 77 // On Posix we don't need to quote the whole thing and can escape all |
74 // individual bad chars. | 78 // individual bad chars. |
75 EXPECT_EQ( | 79 EXPECT_EQ( |
76 " -i ${source} --out=foo\\$ bar\\\"${source_name_part}\\\".o \"\"", | 80 " -i ${source} --out=foo\\$ bar\\\"${source_name_part}\\\".o \"\"", |
77 out.str()); | 81 out.str()); |
78 #endif | 82 #endif |
79 } | 83 } |
80 | 84 |
81 TEST(FileTemplate, NinjaVariables) { | 85 TEST(FileTemplate, NinjaVariables) { |
| 86 TestWithScope setup; |
| 87 |
82 std::vector<std::string> templates; | 88 std::vector<std::string> templates; |
83 templates.push_back("-i"); | 89 templates.push_back("-i"); |
84 templates.push_back("{{source}}"); | 90 templates.push_back("{{source}}"); |
85 templates.push_back("--out=foo bar\"{{source_name_part}}\".o"); | 91 templates.push_back("--out=foo bar\"{{source_name_part}}\".o"); |
| 92 templates.push_back("{{source_file_part}}"); |
| 93 templates.push_back("{{source_dir}}"); |
| 94 templates.push_back("{{source_root_relative_dir}}"); |
| 95 templates.push_back("{{source_gen_dir}}"); |
| 96 templates.push_back("{{source_out_dir}}"); |
86 | 97 |
87 FileTemplate t(templates); | 98 FileTemplate t(setup.settings(), templates); |
88 | 99 |
89 std::ostringstream out; | 100 std::ostringstream out; |
90 EscapeOptions options; | 101 EscapeOptions options; |
91 options.mode = ESCAPE_NINJA_COMMAND; | 102 options.mode = ESCAPE_NINJA_COMMAND; |
92 t.WriteNinjaVariablesForSubstitution(out, "../../foo/bar.txt", options); | 103 t.WriteNinjaVariablesForSubstitution(out, setup.settings(), |
| 104 SourceFile("//foo/bar.txt"), options); |
93 | 105 |
94 // Just the variables used above should be written. | 106 // Just the variables used above should be written. |
95 EXPECT_EQ( | 107 EXPECT_EQ( |
96 " source = ../../foo/bar.txt\n" | 108 " source = ../../foo/bar.txt\n" |
97 " source_name_part = bar\n", | 109 " source_name_part = bar\n" |
| 110 " source_file_part = bar.txt\n" |
| 111 " source_dir = ../../foo\n" |
| 112 " source_root_rel_dir = foo\n" |
| 113 " source_gen_dir = gen/foo\n" |
| 114 " source_out_dir = obj/foo\n", |
98 out.str()); | 115 out.str()); |
99 } | 116 } |
| 117 |
| 118 // Tests in isolation different types of substitutions and that the right |
| 119 // things are generated. |
| 120 TEST(FileTemplate, Substitutions) { |
| 121 TestWithScope setup; |
| 122 |
| 123 #define GetSubst(str, what) \ |
| 124 FileTemplate::GetSubstitution(setup.settings(), \ |
| 125 SourceFile(str), \ |
| 126 FileTemplate::Subrange::what) |
| 127 |
| 128 // Try all possible templates with a normal looking string. |
| 129 EXPECT_EQ("../../foo/bar/baz.txt", GetSubst("//foo/bar/baz.txt", SOURCE)); |
| 130 EXPECT_EQ("baz", GetSubst("//foo/bar/baz.txt", NAME_PART)); |
| 131 EXPECT_EQ("baz.txt", GetSubst("//foo/bar/baz.txt", FILE_PART)); |
| 132 EXPECT_EQ("../../foo/bar", GetSubst("//foo/bar/baz.txt", SOURCE_DIR)); |
| 133 EXPECT_EQ("foo/bar", GetSubst("//foo/bar/baz.txt", ROOT_RELATIVE_DIR)); |
| 134 EXPECT_EQ("gen/foo/bar", GetSubst("//foo/bar/baz.txt", SOURCE_GEN_DIR)); |
| 135 EXPECT_EQ("obj/foo/bar", GetSubst("//foo/bar/baz.txt", SOURCE_OUT_DIR)); |
| 136 |
| 137 // Operations on an absolute path. |
| 138 EXPECT_EQ("/baz.txt", GetSubst("/baz.txt", SOURCE)); |
| 139 EXPECT_EQ("/.", GetSubst("/baz.txt", SOURCE_DIR)); |
| 140 EXPECT_EQ("gen", GetSubst("/baz.txt", SOURCE_GEN_DIR)); |
| 141 EXPECT_EQ("obj", GetSubst("/baz.txt", SOURCE_OUT_DIR)); |
| 142 |
| 143 EXPECT_EQ(".", GetSubst("//baz.txt", ROOT_RELATIVE_DIR)); |
| 144 |
| 145 #undef GetSubst |
| 146 } |
OLD | NEW |