- add sources.
[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, false);
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, false);
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, false);
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, ShellEscaping) {
88   SourceDir build_dir("//out/Debug/");
89   PathOutput writer(build_dir, ESCAPE_SHELL, false);
90   {
91     // Spaces in filenames should get quoted.
92     std::ostringstream out;
93     writer.WriteFile(out, SourceFile("//foo/foo bar.cc"));
94     EXPECT_EQ("\"../../foo/foo bar.cc\"", out.str());
95   }
96   {
97     // Quotes should get blackslash-escaped.
98     std::ostringstream out;
99     writer.WriteFile(out, SourceFile("//foo/\"foobar\".cc"));
100     EXPECT_EQ("../../foo/\\\"foobar\\\".cc", out.str());
101   }
102   {
103     // Backslashes should get escaped on non-Windows and preserved on Windows.
104     std::ostringstream out;
105     writer.WriteFile(out, SourceFile("//foo\\bar.cc"));
106 #if defined(OS_WIN)
107     EXPECT_EQ("../../foo\\bar.cc", out.str());
108 #else
109     EXPECT_EQ("../../foo\\\\bar.cc", out.str());
110 #endif
111   }
112 }
113
114 TEST(PathOutput, SlashConversion) {
115   SourceDir build_dir("//out/Debug/");
116   PathOutput writer(build_dir, ESCAPE_NINJA, true);
117   {
118     std::ostringstream out;
119     writer.WriteFile(out, SourceFile("//foo/bar.cc"));
120 #if defined(OS_WIN)
121     EXPECT_EQ("..\\..\\foo\\bar.cc", out.str());
122 #else
123     EXPECT_EQ("../../foo/bar.cc", out.str());
124 #endif
125   }
126 }
127
128 TEST(PathOutput, InhibitQuoting) {
129   SourceDir build_dir("//out/Debug/");
130   PathOutput writer(build_dir, ESCAPE_SHELL, false);
131   writer.set_inhibit_quoting(true);
132   {
133     // We should get unescaped spaces in the output with no quotes.
134     std::ostringstream out;
135     writer.WriteFile(out, SourceFile("//foo/foo bar.cc"));
136     EXPECT_EQ("../../foo/foo bar.cc", out.str());
137   }
138 }
139
140 TEST(PathOutput, WriteDir) {
141   {
142     SourceDir build_dir("//out/Debug/");
143     PathOutput writer(build_dir, ESCAPE_NINJA, false);
144     {
145       std::ostringstream out;
146       writer.WriteDir(out, SourceDir("//foo/bar/"),
147                       PathOutput::DIR_INCLUDE_LAST_SLASH);
148       EXPECT_EQ("../../foo/bar/", out.str());
149     }
150     {
151       std::ostringstream out;
152       writer.WriteDir(out, SourceDir("//foo/bar/"),
153                       PathOutput::DIR_NO_LAST_SLASH);
154       EXPECT_EQ("../../foo/bar", out.str());
155     }
156
157     // Output source root dir.
158     {
159       std::ostringstream out;
160       writer.WriteDir(out, SourceDir("//"),
161                       PathOutput::DIR_INCLUDE_LAST_SLASH);
162       EXPECT_EQ("../../", out.str());
163     }
164     {
165       std::ostringstream out;
166       writer.WriteDir(out, SourceDir("//"),
167                       PathOutput::DIR_NO_LAST_SLASH);
168       EXPECT_EQ("../..", out.str());
169     }
170
171     // Output system root dir.
172     {
173       std::ostringstream out;
174       writer.WriteDir(out, SourceDir("/"),
175                       PathOutput::DIR_INCLUDE_LAST_SLASH);
176       EXPECT_EQ("/", out.str());
177     }
178     {
179       std::ostringstream out;
180       writer.WriteDir(out, SourceDir("/"),
181                       PathOutput::DIR_INCLUDE_LAST_SLASH);
182       EXPECT_EQ("/", out.str());
183     }
184     {
185       std::ostringstream out;
186       writer.WriteDir(out, SourceDir("/"),
187                       PathOutput::DIR_NO_LAST_SLASH);
188       EXPECT_EQ("/.", out.str());
189     }
190
191     // Output inside current dir.
192     {
193       std::ostringstream out;
194       writer.WriteDir(out, SourceDir("//out/Debug/"),
195                       PathOutput::DIR_INCLUDE_LAST_SLASH);
196       EXPECT_EQ("./", out.str());
197     }
198     {
199       std::ostringstream out;
200       writer.WriteDir(out, SourceDir("//out/Debug/"),
201                       PathOutput::DIR_NO_LAST_SLASH);
202       EXPECT_EQ(".", out.str());
203     }
204     {
205       std::ostringstream out;
206       writer.WriteDir(out, SourceDir("//out/Debug/foo/"),
207                       PathOutput::DIR_INCLUDE_LAST_SLASH);
208       EXPECT_EQ("foo/", out.str());
209     }
210     {
211       std::ostringstream out;
212       writer.WriteDir(out, SourceDir("//out/Debug/foo/"),
213                       PathOutput::DIR_NO_LAST_SLASH);
214       EXPECT_EQ("foo", out.str());
215     }
216   }
217   {
218     // Empty build dir writer.
219     PathOutput root_writer(SourceDir("//"), ESCAPE_NINJA, false);
220     {
221       std::ostringstream out;
222       root_writer.WriteDir(out, SourceDir("//"),
223                            PathOutput::DIR_INCLUDE_LAST_SLASH);
224       EXPECT_EQ("./", out.str());
225     }
226     {
227       std::ostringstream out;
228       root_writer.WriteDir(out, SourceDir("//"),
229                            PathOutput::DIR_NO_LAST_SLASH);
230       EXPECT_EQ(".", out.str());
231     }
232   }
233 }