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 "testing/gtest/include/gtest/gtest.h"
6#include "tools/gn/source_dir.h"
7#include "tools/gn/source_file.h"
8
9TEST(SourceDir, ResolveRelativeFile) {
10  SourceDir base("//base/");
11
12  // Empty input is an error.
13  EXPECT_TRUE(base.ResolveRelativeFile("") == SourceFile());
14
15  // These things are directories, so should be an error.
16  EXPECT_TRUE(base.ResolveRelativeFile("//foo/bar/") == SourceFile());
17  EXPECT_TRUE(base.ResolveRelativeFile("bar/") == SourceFile());
18
19  // Absolute paths should be passed unchanged.
20  EXPECT_TRUE(base.ResolveRelativeFile("//foo") == SourceFile("//foo"));
21  EXPECT_TRUE(base.ResolveRelativeFile("/foo") == SourceFile("/foo"));
22
23  // Basic relative stuff.
24  EXPECT_TRUE(base.ResolveRelativeFile("foo") == SourceFile("//base/foo"));
25  EXPECT_TRUE(base.ResolveRelativeFile("./foo") == SourceFile("//base/foo"));
26  EXPECT_TRUE(base.ResolveRelativeFile("../foo") == SourceFile("//foo"));
27  EXPECT_TRUE(base.ResolveRelativeFile("../../foo") == SourceFile("//foo"));
28
29#if defined(OS_WIN)
30  // Note that we don't canonicalize the backslashes to forward slashes.
31  // This could potentially be changed in the future which would mean we should
32  // just change the expected result.
33  EXPECT_TRUE(base.ResolveRelativeFile("C:\\foo\\bar.txt") ==
34              SourceFile("/C:\\foo\\bar.txt"));
35#endif
36}
37
38TEST(SourceDir, ResolveRelativeDir) {
39  SourceDir base("//base/");
40
41  // Empty input is an error.
42  EXPECT_TRUE(base.ResolveRelativeDir("") == SourceDir());
43
44  // Absolute paths should be passed unchanged.
45  EXPECT_TRUE(base.ResolveRelativeDir("//foo") == SourceDir("//foo/"));
46  EXPECT_TRUE(base.ResolveRelativeDir("/foo") == SourceDir("/foo/"));
47
48  // Basic relative stuff.
49  EXPECT_TRUE(base.ResolveRelativeDir("foo") == SourceDir("//base/foo/"));
50  EXPECT_TRUE(base.ResolveRelativeDir("./foo") == SourceDir("//base/foo/"));
51  EXPECT_TRUE(base.ResolveRelativeDir("../foo") == SourceDir("//foo/"));
52  EXPECT_TRUE(base.ResolveRelativeDir("../../foo/") == SourceDir("//foo/"));
53
54#if defined(OS_WIN)
55  // Note that we don't canonicalize the existing backslashes to forward
56  // slashes. This could potentially be changed in the future which would mean
57  // we should just change the expected result.
58  EXPECT_TRUE(base.ResolveRelativeDir("C:\\foo") == SourceDir("/C:\\foo/"));
59#endif
60}
61