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/");
12 base::StringPiece
source_root("C:/source/root");
14 base::StringPiece
source_root("/source/root");
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
) ==
23 EXPECT_TRUE(base
.ResolveRelativeFile("bar/", source_root
) ==
26 // Absolute paths should be passed unchanged.
27 EXPECT_TRUE(base
.ResolveRelativeFile("//foo",source_root
) ==
29 EXPECT_TRUE(base
.ResolveRelativeFile("/foo", source_root
) ==
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
) ==
40 // If the given relative path points outside the source root, we
41 // expect an absolute path.
43 EXPECT_TRUE(base
.ResolveRelativeFile("../../foo", source_root
) ==
44 SourceFile("/C:/source/foo"));
46 EXPECT_TRUE(base
.ResolveRelativeFile("../../foo", source_root
) ==
47 SourceFile("/source/foo"));
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"));
59 TEST(SourceDir
, ResolveRelativeDir
) {
60 SourceDir
base("//base/");
62 base::StringPiece
source_root("C:/source/root");
64 base::StringPiece
source_root("/source/root");
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
) ==
73 EXPECT_TRUE(base
.ResolveRelativeDir("/foo", source_root
) ==
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
) ==
84 // If the given relative path points outside the source root, we
85 // expect an absolute path.
87 EXPECT_TRUE(base
.ResolveRelativeDir("../../foo", source_root
) ==
88 SourceDir("/C:/source/foo/"));
90 EXPECT_TRUE(base
.ResolveRelativeDir("../../foo", source_root
) ==
91 SourceDir("/source/foo/"));
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/"));