Add ICU message format support
[chromium-blink-merge.git] / tools / gn / function_rebase_path_unittest.cc
blobbed5663917b66df3fb6c78e08c6777441f220824
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 "build/build_config.h"
6 #include "testing/gtest/include/gtest/gtest.h"
7 #include "tools/gn/functions.h"
8 #include "tools/gn/parse_tree.h"
9 #include "tools/gn/test_with_scope.h"
11 namespace {
13 std::string RebaseOne(Scope* scope,
14 const char* input,
15 const char* to_dir,
16 const char* from_dir) {
17 std::vector<Value> args;
18 args.push_back(Value(nullptr, input));
19 args.push_back(Value(nullptr, to_dir));
20 args.push_back(Value(nullptr, from_dir));
22 Err err;
23 FunctionCallNode function;
24 Value result = functions::RunRebasePath(scope, &function, args, &err);
25 bool is_string = result.type() == Value::STRING;
26 EXPECT_TRUE(is_string);
28 return result.string_value();
31 } // namespace
33 TEST(RebasePath, Strings) {
34 TestWithScope setup;
35 setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/"));
36 Scope* scope = setup.scope();
37 scope->set_source_dir(SourceDir("//tools/gn/"));
39 // Build-file relative paths.
40 EXPECT_EQ("../../tools/gn", RebaseOne(scope, ".", "//out/Debug", "."));
41 EXPECT_EQ("../../tools/gn/", RebaseOne(scope, "./", "//out/Debug", "."));
42 EXPECT_EQ("../../tools/gn/foo", RebaseOne(scope, "foo", "//out/Debug", "."));
43 EXPECT_EQ("../..", RebaseOne(scope, "../..", "//out/Debug", "."));
44 EXPECT_EQ("../../", RebaseOne(scope, "../../", "//out/Debug", "."));
46 // We don't allow going above the root source dir.
47 EXPECT_EQ("../..", RebaseOne(scope, "../../..", "//out/Debug", "."));
49 // Source-absolute input paths.
50 EXPECT_EQ("./", RebaseOne(scope, "//", "//", "//"));
51 EXPECT_EQ("foo", RebaseOne(scope, "//foo", "//", "//"));
52 EXPECT_EQ("foo/", RebaseOne(scope, "//foo/", "//", "//"));
53 EXPECT_EQ("../../foo/bar", RebaseOne(scope, "//foo/bar", "//out/Debug", "."));
54 EXPECT_EQ("./", RebaseOne(scope, "//foo/", "//foo/", "//"));
55 // Thie one is technically correct but could be simplified to "." if
56 // necessary.
57 EXPECT_EQ("../foo", RebaseOne(scope, "//foo", "//foo", "//"));
59 // Test slash conversion.
60 EXPECT_EQ("foo/bar", RebaseOne(scope, "foo/bar", ".", "."));
61 EXPECT_EQ("foo/bar", RebaseOne(scope, "foo\\bar", ".", "."));
63 // Test system path output.
64 #if defined(OS_WIN)
65 setup.build_settings()->SetRootPath(base::FilePath(L"C:/source"));
66 EXPECT_EQ("C:/source", RebaseOne(scope, ".", "", "//"));
67 EXPECT_EQ("C:/source/", RebaseOne(scope, "//", "", "//"));
68 EXPECT_EQ("C:/source/foo", RebaseOne(scope, "foo", "", "//"));
69 EXPECT_EQ("C:/source/foo/", RebaseOne(scope, "foo/", "", "//"));
70 EXPECT_EQ("C:/source/tools/gn/foo", RebaseOne(scope, "foo", "", "."));
71 #else
72 setup.build_settings()->SetRootPath(base::FilePath("/source"));
73 EXPECT_EQ("/source", RebaseOne(scope, ".", "", "//"));
74 EXPECT_EQ("/source/", RebaseOne(scope, "//", "", "//"));
75 EXPECT_EQ("/source/foo", RebaseOne(scope, "foo", "", "//"));
76 EXPECT_EQ("/source/foo/", RebaseOne(scope, "foo/", "", "//"));
77 EXPECT_EQ("/source/tools/gn/foo", RebaseOne(scope, "foo", "", "."));
78 #endif
81 TEST(RebasePath, StringsSystemPaths) {
82 TestWithScope setup;
83 Scope* scope = setup.scope();
85 #if defined(OS_WIN)
86 setup.build_settings()->SetBuildDir(SourceDir("C:/ssd/out/Debug"));
87 setup.build_settings()->SetRootPath(base::FilePath(L"C:/hdd/src"));
89 // Test system absolute to-dir.
90 EXPECT_EQ("../../ssd/out/Debug",
91 RebaseOne(scope, ".", "//", "C:/ssd/out/Debug"));
92 EXPECT_EQ("../../ssd/out/Debug/",
93 RebaseOne(scope, "./", "//", "C:/ssd/out/Debug"));
94 EXPECT_EQ("../../ssd/out/Debug/foo",
95 RebaseOne(scope, "foo", "//", "C:/ssd/out/Debug"));
96 EXPECT_EQ("../../ssd/out/Debug/foo/",
97 RebaseOne(scope, "foo/", "//", "C:/ssd/out/Debug"));
99 // Test system absolute from-dir.
100 EXPECT_EQ("../../../hdd/src",
101 RebaseOne(scope, ".", "C:/ssd/out/Debug", "//"));
102 EXPECT_EQ("../../../hdd/src/",
103 RebaseOne(scope, "./", "C:/ssd/out/Debug", "//"));
104 EXPECT_EQ("../../../hdd/src/foo",
105 RebaseOne(scope, "foo", "C:/ssd/out/Debug", "//"));
106 EXPECT_EQ("../../../hdd/src/foo/",
107 RebaseOne(scope, "foo/", "C:/ssd/out/Debug", "//"));
108 #else
109 setup.build_settings()->SetBuildDir(SourceDir("/ssd/out/Debug"));
110 setup.build_settings()->SetRootPath(base::FilePath("/hdd/src"));
112 // Test system absolute to-dir.
113 EXPECT_EQ("../../ssd/out/Debug",
114 RebaseOne(scope, ".", "//", "/ssd/out/Debug"));
115 EXPECT_EQ("../../ssd/out/Debug/",
116 RebaseOne(scope, "./", "//", "/ssd/out/Debug"));
117 EXPECT_EQ("../../ssd/out/Debug/foo",
118 RebaseOne(scope, "foo", "//", "/ssd/out/Debug"));
119 EXPECT_EQ("../../ssd/out/Debug/foo/",
120 RebaseOne(scope, "foo/", "//", "/ssd/out/Debug"));
122 // Test system absolute from-dir.
123 EXPECT_EQ("../../../hdd/src",
124 RebaseOne(scope, ".", "/ssd/out/Debug", "//"));
125 EXPECT_EQ("../../../hdd/src/",
126 RebaseOne(scope, "./", "/ssd/out/Debug", "//"));
127 EXPECT_EQ("../../../hdd/src/foo",
128 RebaseOne(scope, "foo", "/ssd/out/Debug", "//"));
129 EXPECT_EQ("../../../hdd/src/foo/",
130 RebaseOne(scope, "foo/", "/ssd/out/Debug", "//"));
131 #endif
134 // Test list input.
135 TEST(RebasePath, List) {
136 TestWithScope setup;
137 setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/"));
138 setup.scope()->set_source_dir(SourceDir("//tools/gn/"));
140 std::vector<Value> args;
141 args.push_back(Value(nullptr, Value::LIST));
142 args[0].list_value().push_back(Value(nullptr, "foo.txt"));
143 args[0].list_value().push_back(Value(nullptr, "bar.txt"));
144 args.push_back(Value(nullptr, "//out/Debug/"));
145 args.push_back(Value(nullptr, "."));
147 Err err;
148 FunctionCallNode function;
149 Value ret = functions::RunRebasePath(setup.scope(), &function, args, &err);
150 EXPECT_FALSE(err.has_error());
152 ASSERT_EQ(Value::LIST, ret.type());
153 ASSERT_EQ(2u, ret.list_value().size());
155 EXPECT_EQ("../../tools/gn/foo.txt", ret.list_value()[0].string_value());
156 EXPECT_EQ("../../tools/gn/bar.txt", ret.list_value()[1].string_value());
159 TEST(RebasePath, Errors) {
160 TestWithScope setup;
161 setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/"));
163 // No arg input should issue an error.
164 Err err;
165 std::vector<Value> args;
166 FunctionCallNode function;
167 Value ret = functions::RunRebasePath(setup.scope(), &function, args, &err);
168 EXPECT_TRUE(err.has_error());