Roll src/third_party/WebKit f36d5e0:68b67cd (svn 193299:193303)
[chromium-blink-merge.git] / tools / gn / source_dir_unittest.cc
blob5034335795bedac83dd94d7eba6cad35e3b76521
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.
5 #include "testing/gtest/include/gtest/gtest.h"
6 #include "tools/gn/source_dir.h"
7 #include "tools/gn/source_file.h"
9 TEST(SourceDir, ResolveRelativeFile) {
10 SourceDir base("//base/");
11 #if defined(OS_WIN)
12 base::StringPiece source_root("C:/source/root");
13 #else
14 base::StringPiece source_root("/source/root");
15 #endif
17 // Empty input is an error.
18 EXPECT_TRUE(base.ResolveRelativeFile("", source_root) == SourceFile());
20 // These things are directories, so should be an error.
21 EXPECT_TRUE(base.ResolveRelativeFile("//foo/bar/", source_root) ==
22 SourceFile());
23 EXPECT_TRUE(base.ResolveRelativeFile("bar/", source_root) ==
24 SourceFile());
26 // Absolute paths should be passed unchanged.
27 EXPECT_TRUE(base.ResolveRelativeFile("//foo",source_root) ==
28 SourceFile("//foo"));
29 EXPECT_TRUE(base.ResolveRelativeFile("/foo", source_root) ==
30 SourceFile("/foo"));
32 // Basic relative stuff.
33 EXPECT_TRUE(base.ResolveRelativeFile("foo", source_root) ==
34 SourceFile("//base/foo"));
35 EXPECT_TRUE(base.ResolveRelativeFile("./foo", source_root) ==
36 SourceFile("//base/foo"));
37 EXPECT_TRUE(base.ResolveRelativeFile("../foo", source_root) ==
38 SourceFile("//foo"));
40 // If the given relative path points outside the source root, we
41 // expect an absolute path.
42 #if defined(OS_WIN)
43 EXPECT_TRUE(base.ResolveRelativeFile("../../foo", source_root) ==
44 SourceFile("/C:/source/foo"));
45 #else
46 EXPECT_TRUE(base.ResolveRelativeFile("../../foo", source_root) ==
47 SourceFile("/source/foo"));
48 #endif
50 #if defined(OS_WIN)
51 // Note that we don't canonicalize the backslashes to forward slashes.
52 // This could potentially be changed in the future which would mean we should
53 // just change the expected result.
54 EXPECT_TRUE(base.ResolveRelativeFile("C:\\foo\\bar.txt", source_root) ==
55 SourceFile("/C:/foo/bar.txt"));
56 #endif
59 TEST(SourceDir, ResolveRelativeDir) {
60 SourceDir base("//base/");
61 #if defined(OS_WIN)
62 base::StringPiece source_root("C:/source/root");
63 #else
64 base::StringPiece source_root("/source/root");
65 #endif
67 // Empty input is an error.
68 EXPECT_TRUE(base.ResolveRelativeDir("", source_root) == SourceDir());
70 // Absolute paths should be passed unchanged.
71 EXPECT_TRUE(base.ResolveRelativeDir("//foo", source_root) ==
72 SourceDir("//foo/"));
73 EXPECT_TRUE(base.ResolveRelativeDir("/foo", source_root) ==
74 SourceDir("/foo/"));
76 // Basic relative stuff.
77 EXPECT_TRUE(base.ResolveRelativeDir("foo", source_root) ==
78 SourceDir("//base/foo/"));
79 EXPECT_TRUE(base.ResolveRelativeDir("./foo", source_root) ==
80 SourceDir("//base/foo/"));
81 EXPECT_TRUE(base.ResolveRelativeDir("../foo", source_root) ==
82 SourceDir("//foo/"));
84 // If the given relative path points outside the source root, we
85 // expect an absolute path.
86 #if defined(OS_WIN)
87 EXPECT_TRUE(base.ResolveRelativeDir("../../foo", source_root) ==
88 SourceDir("/C:/source/foo/"));
89 #else
90 EXPECT_TRUE(base.ResolveRelativeDir("../../foo", source_root) ==
91 SourceDir("/source/foo/"));
92 #endif
94 #if defined(OS_WIN)
95 // Canonicalize the existing backslashes to forward slashes and add a
96 // leading slash if necessary.
97 EXPECT_TRUE(base.ResolveRelativeDir("\\C:\\foo") == SourceDir("/C:/foo/"));
98 EXPECT_TRUE(base.ResolveRelativeDir("C:\\foo") == SourceDir("/C:/foo/"));
99 #endif