Upstream version 8.37.180.0
[platform/framework/web/crosswalk.git] / src / tools / gn / path_output_unittest.cc
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/path_output.h"
9 #include "tools/gn/source_dir.h"
10 #include "tools/gn/source_file.h"
11
12 TEST(PathOutput, Basic) {
13   SourceDir build_dir("//out/Debug/");
14   PathOutput writer(build_dir, ESCAPE_NONE);
15   {
16     // Normal source-root path.
17     std::ostringstream out;
18     writer.WriteFile(out, SourceFile("//foo/bar.cc"));
19     EXPECT_EQ("../../foo/bar.cc", out.str());
20   }
21   {
22     // File in the root dir.
23     std::ostringstream out;
24     writer.WriteFile(out, SourceFile("//foo.cc"));
25     EXPECT_EQ("../../foo.cc", out.str());
26   }
27   {
28     // Files in the output dir.
29     std::ostringstream out;
30     writer.WriteFile(out, SourceFile("//out/Debug/foo.cc"));
31     out << " ";
32     writer.WriteFile(out, SourceFile("//out/Debug/bar/baz.cc"));
33     EXPECT_EQ("foo.cc bar/baz.cc", out.str());
34   }
35 #if defined(OS_WIN)
36   {
37     // System-absolute path.
38     std::ostringstream out;
39     writer.WriteFile(out, SourceFile("/C:/foo/bar.cc"));
40     EXPECT_EQ("C:/foo/bar.cc", out.str());
41   }
42 #else
43   {
44     // System-absolute path.
45     std::ostringstream out;
46     writer.WriteFile(out, SourceFile("/foo/bar.cc"));
47     EXPECT_EQ("/foo/bar.cc", out.str());
48   }
49 #endif
50 }
51
52 // Same as basic but the output dir is the root.
53 TEST(PathOutput, BasicInRoot) {
54   SourceDir build_dir("//");
55   PathOutput writer(build_dir, ESCAPE_NONE);
56   {
57     // Normal source-root path.
58     std::ostringstream out;
59     writer.WriteFile(out, SourceFile("//foo/bar.cc"));
60     EXPECT_EQ("foo/bar.cc", out.str());
61   }
62   {
63     // File in the root dir.
64     std::ostringstream out;
65     writer.WriteFile(out, SourceFile("//foo.cc"));
66     EXPECT_EQ("foo.cc", out.str());
67   }
68 }
69
70 TEST(PathOutput, NinjaEscaping) {
71   SourceDir build_dir("//out/Debug/");
72   PathOutput writer(build_dir, ESCAPE_NINJA);
73   {
74     // Spaces and $ in filenames.
75     std::ostringstream out;
76     writer.WriteFile(out, SourceFile("//foo/foo bar$.cc"));
77     EXPECT_EQ("../../foo/foo$ bar$$.cc", out.str());
78   }
79   {
80     // Not other weird stuff
81     std::ostringstream out;
82     writer.WriteFile(out, SourceFile("//foo/\"foo\\bar\".cc"));
83     EXPECT_EQ("../../foo/\"foo\\bar\".cc", out.str());
84   }
85 }
86
87 TEST(PathOutput, NinjaForkEscaping) {
88   SourceDir build_dir("//out/Debug/");
89   PathOutput writer(build_dir, ESCAPE_NINJA_COMMAND);
90
91   // Spaces in filenames should get quoted on Windows.
92   writer.set_escape_platform(ESCAPE_PLATFORM_WIN);
93   {
94     std::ostringstream out;
95     writer.WriteFile(out, SourceFile("//foo/foo bar.cc"));
96     EXPECT_EQ("\"../../foo/foo$ bar.cc\"", out.str());
97   }
98
99   // Spaces in filenames should get escaped on Posix.
100   writer.set_escape_platform(ESCAPE_PLATFORM_POSIX);
101   {
102     std::ostringstream out;
103     writer.WriteFile(out, SourceFile("//foo/foo bar.cc"));
104     EXPECT_EQ("../../foo/foo\\$ bar.cc", out.str());
105   }
106
107   // Quotes should get blackslash-escaped on Windows and Posix.
108   writer.set_escape_platform(ESCAPE_PLATFORM_WIN);
109   {
110     std::ostringstream out;
111     writer.WriteFile(out, SourceFile("//foo/\"foobar\".cc"));
112     // Our Windows code currently quotes the whole thing in this case for
113     // code simplicity, even though it's strictly unnecessary. This might
114     // change in the future.
115     EXPECT_EQ("\"../../foo/\\\"foobar\\\".cc\"", out.str());
116   }
117   writer.set_escape_platform(ESCAPE_PLATFORM_POSIX);
118   {
119     std::ostringstream out;
120     writer.WriteFile(out, SourceFile("//foo/\"foobar\".cc"));
121     EXPECT_EQ("../../foo/\\\"foobar\\\".cc", out.str());
122   }
123
124
125   // Backslashes should get escaped on non-Windows and preserved on Windows.
126   writer.set_escape_platform(ESCAPE_PLATFORM_WIN);
127   {
128     std::ostringstream out;
129     writer.WriteFile(out, SourceFile("//foo\\bar.cc"));
130     EXPECT_EQ("../../foo\\bar.cc", out.str());
131   }
132   writer.set_escape_platform(ESCAPE_PLATFORM_POSIX);
133   {
134     std::ostringstream out;
135     writer.WriteFile(out, SourceFile("//foo\\bar.cc"));
136     EXPECT_EQ("../../foo\\\\bar.cc", out.str());
137   }
138 }
139
140 TEST(PathOutput, InhibitQuoting) {
141   SourceDir build_dir("//out/Debug/");
142   PathOutput writer(build_dir, ESCAPE_NINJA_COMMAND);
143   writer.set_inhibit_quoting(true);
144
145   writer.set_escape_platform(ESCAPE_PLATFORM_WIN);
146   {
147     // We should get unescaped spaces in the output with no quotes.
148     std::ostringstream out;
149     writer.WriteFile(out, SourceFile("//foo/foo bar.cc"));
150     EXPECT_EQ("../../foo/foo$ bar.cc", out.str());
151   }
152
153   writer.set_escape_platform(ESCAPE_PLATFORM_POSIX);
154   {
155     // Escapes the space.
156     std::ostringstream out;
157     writer.WriteFile(out, SourceFile("//foo/foo bar.cc"));
158     EXPECT_EQ("../../foo/foo\\$ bar.cc", out.str());
159   }
160 }
161
162 TEST(PathOutput, WriteDir) {
163   {
164     SourceDir build_dir("//out/Debug/");
165     PathOutput writer(build_dir, ESCAPE_NINJA);
166     {
167       std::ostringstream out;
168       writer.WriteDir(out, SourceDir("//foo/bar/"),
169                       PathOutput::DIR_INCLUDE_LAST_SLASH);
170       EXPECT_EQ("../../foo/bar/", out.str());
171     }
172     {
173       std::ostringstream out;
174       writer.WriteDir(out, SourceDir("//foo/bar/"),
175                       PathOutput::DIR_NO_LAST_SLASH);
176       EXPECT_EQ("../../foo/bar", out.str());
177     }
178
179     // Output source root dir.
180     {
181       std::ostringstream out;
182       writer.WriteDir(out, SourceDir("//"),
183                       PathOutput::DIR_INCLUDE_LAST_SLASH);
184       EXPECT_EQ("../../", out.str());
185     }
186     {
187       std::ostringstream out;
188       writer.WriteDir(out, SourceDir("//"),
189                       PathOutput::DIR_NO_LAST_SLASH);
190       EXPECT_EQ("../..", out.str());
191     }
192
193     // Output system root dir.
194     {
195       std::ostringstream out;
196       writer.WriteDir(out, SourceDir("/"),
197                       PathOutput::DIR_INCLUDE_LAST_SLASH);
198       EXPECT_EQ("/", out.str());
199     }
200     {
201       std::ostringstream out;
202       writer.WriteDir(out, SourceDir("/"),
203                       PathOutput::DIR_INCLUDE_LAST_SLASH);
204       EXPECT_EQ("/", out.str());
205     }
206     {
207       std::ostringstream out;
208       writer.WriteDir(out, SourceDir("/"),
209                       PathOutput::DIR_NO_LAST_SLASH);
210       EXPECT_EQ("/.", out.str());
211     }
212
213     // Output inside current dir.
214     {
215       std::ostringstream out;
216
217
218       writer.WriteDir(out, SourceDir("//out/Debug/"),
219                       PathOutput::DIR_INCLUDE_LAST_SLASH);
220       EXPECT_EQ("./", out.str());
221     }
222     {
223       std::ostringstream out;
224       writer.WriteDir(out, SourceDir("//out/Debug/"),
225                       PathOutput::DIR_NO_LAST_SLASH);
226       EXPECT_EQ(".", out.str());
227     }
228     {
229       std::ostringstream out;
230       writer.WriteDir(out, SourceDir("//out/Debug/foo/"),
231                       PathOutput::DIR_INCLUDE_LAST_SLASH);
232       EXPECT_EQ("foo/", out.str());
233     }
234     {
235       std::ostringstream out;
236       writer.WriteDir(out, SourceDir("//out/Debug/foo/"),
237                       PathOutput::DIR_NO_LAST_SLASH);
238       EXPECT_EQ("foo", out.str());
239     }
240   }
241   {
242     // Empty build dir writer.
243     PathOutput root_writer(SourceDir("//"), ESCAPE_NINJA);
244     {
245       std::ostringstream out;
246       root_writer.WriteDir(out, SourceDir("//"),
247                            PathOutput::DIR_INCLUDE_LAST_SLASH);
248       EXPECT_EQ("./", out.str());
249     }
250     {
251       std::ostringstream out;
252       root_writer.WriteDir(out, SourceDir("//"),
253                            PathOutput::DIR_NO_LAST_SLASH);
254       EXPECT_EQ(".", out.str());
255     }
256   }
257 }