Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(93)

Side by Side Diff: tools/gn/function_rebase_path_unittest.cc

Issue 161783002: Remove default value checking in GN, adds getenv function, reorders parameters to rebase_path. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "build/build_config.h" 5 #include "build/build_config.h"
6 #include "testing/gtest/include/gtest/gtest.h" 6 #include "testing/gtest/include/gtest/gtest.h"
7 #include "tools/gn/functions.h" 7 #include "tools/gn/functions.h"
8 #include "tools/gn/parse_tree.h" 8 #include "tools/gn/parse_tree.h"
9 #include "tools/gn/test_with_scope.h" 9 #include "tools/gn/test_with_scope.h"
10 10
11 namespace { 11 namespace {
12 12
13 std::string RebaseOne(Scope* scope, 13 std::string RebaseOne(Scope* scope,
14 const char* input, 14 const char* input,
15 const char* to_dir,
15 const char* from_dir, 16 const char* from_dir,
16 const char* to_dir,
17 const char* sep = NULL) { 17 const char* sep = NULL) {
18 std::vector<Value> args; 18 std::vector<Value> args;
19 args.push_back(Value(NULL, input)); 19 args.push_back(Value(NULL, input));
20 args.push_back(Value(NULL, to_dir));
20 args.push_back(Value(NULL, from_dir)); 21 args.push_back(Value(NULL, from_dir));
21 args.push_back(Value(NULL, to_dir));
22 if (sep) 22 if (sep)
23 args.push_back(Value(NULL, sep)); 23 args.push_back(Value(NULL, sep));
24 24
25 Err err; 25 Err err;
26 FunctionCallNode function; 26 FunctionCallNode function;
27 Value result = functions::RunRebasePath(scope, &function, args, &err); 27 Value result = functions::RunRebasePath(scope, &function, args, &err);
28 bool is_string = result.type() == Value::STRING; 28 bool is_string = result.type() == Value::STRING;
29 EXPECT_TRUE(is_string); 29 EXPECT_TRUE(is_string);
30 30
31 return result.string_value(); 31 return result.string_value();
32 } 32 }
33 33
34 } // namespace 34 } // namespace
35 35
36 TEST(RebasePath, Strings) { 36 TEST(RebasePath, Strings) {
37 TestWithScope setup; 37 TestWithScope setup;
38 setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/")); 38 setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/"));
39 Scope* scope = setup.scope(); 39 Scope* scope = setup.scope();
40 scope->set_source_dir(SourceDir("//tools/gn/")); 40 scope->set_source_dir(SourceDir("//tools/gn/"));
41 41
42 // Build-file relative paths. 42 // Build-file relative paths.
43 EXPECT_EQ("../../tools/gn", RebaseOne(scope, ".", ".", "//out/Debug")); 43 EXPECT_EQ("../../tools/gn", RebaseOne(scope, ".", "//out/Debug", "."));
44 EXPECT_EQ("../../tools/gn/", RebaseOne(scope, "./", ".", "//out/Debug")); 44 EXPECT_EQ("../../tools/gn/", RebaseOne(scope, "./", "//out/Debug", "."));
45 EXPECT_EQ("../../tools/gn/foo", RebaseOne(scope, "foo", ".", "//out/Debug")); 45 EXPECT_EQ("../../tools/gn/foo", RebaseOne(scope, "foo", "//out/Debug", "."));
46 EXPECT_EQ("../..", RebaseOne(scope, "../..", ".", "//out/Debug")); 46 EXPECT_EQ("../..", RebaseOne(scope, "../..", "//out/Debug", "."));
47 EXPECT_EQ("../../", RebaseOne(scope, "../../", ".", "//out/Debug")); 47 EXPECT_EQ("../../", RebaseOne(scope, "../../", "//out/Debug", "."));
48 48
49 // We don't allow going above the root source dir. 49 // We don't allow going above the root source dir.
50 EXPECT_EQ("../..", RebaseOne(scope, "../../..", ".", "//out/Debug")); 50 EXPECT_EQ("../..", RebaseOne(scope, "../../..", "//out/Debug", "."));
51 51
52 // Source-absolute input paths. 52 // Source-absolute input paths.
53 EXPECT_EQ("./", RebaseOne(scope, "//", "//", "//")); 53 EXPECT_EQ("./", RebaseOne(scope, "//", "//", "//"));
54 EXPECT_EQ("foo", RebaseOne(scope, "//foo", "//", "//")); 54 EXPECT_EQ("foo", RebaseOne(scope, "//foo", "//", "//"));
55 EXPECT_EQ("foo/", RebaseOne(scope, "//foo/", "//", "//")); 55 EXPECT_EQ("foo/", RebaseOne(scope, "//foo/", "//", "//"));
56 EXPECT_EQ("../../foo/bar", RebaseOne(scope, "//foo/bar", ".", "//out/Debug")); 56 EXPECT_EQ("../../foo/bar", RebaseOne(scope, "//foo/bar", "//out/Debug", "."));
57 EXPECT_EQ("./", RebaseOne(scope, "//foo/", "//", "//foo/")); 57 EXPECT_EQ("./", RebaseOne(scope, "//foo/", "//foo/", "//"));
58 // Thie one is technically correct but could be simplified to "." if 58 // Thie one is technically correct but could be simplified to "." if
59 // necessary. 59 // necessary.
60 EXPECT_EQ("../foo", RebaseOne(scope, "//foo", "//", "//foo")); 60 EXPECT_EQ("../foo", RebaseOne(scope, "//foo", "//foo", "//"));
61 61
62 // Test slash conversion. 62 // Test slash conversion.
63 #if defined(OS_WIN) 63 #if defined(OS_WIN)
64 EXPECT_EQ("foo/bar", RebaseOne(scope, "foo/bar", ".", ".", "none")); 64 EXPECT_EQ("foo/bar", RebaseOne(scope, "foo/bar", ".", ".", "none"));
65 EXPECT_EQ("foo\\bar", RebaseOne(scope, "foo/bar", ".", ".", "to_system")); 65 EXPECT_EQ("foo\\bar", RebaseOne(scope, "foo/bar", ".", ".", "to_system"));
66 EXPECT_EQ("foo/bar", RebaseOne(scope, "foo/bar", ".", ".", "from_system")); 66 EXPECT_EQ("foo/bar", RebaseOne(scope, "foo/bar", ".", ".", "from_system"));
67 67
68 EXPECT_EQ("foo\\bar", RebaseOne(scope, "foo\\bar", ".", ".", "none")); 68 EXPECT_EQ("foo\\bar", RebaseOne(scope, "foo\\bar", ".", ".", "none"));
69 EXPECT_EQ("foo\\bar", RebaseOne(scope, "foo\\bar", ".", ".", "to_system")); 69 EXPECT_EQ("foo\\bar", RebaseOne(scope, "foo\\bar", ".", ".", "to_system"));
70 EXPECT_EQ("foo/bar", RebaseOne(scope, "foo\\bar", ".", ".", "from_system")); 70 EXPECT_EQ("foo/bar", RebaseOne(scope, "foo\\bar", ".", ".", "from_system"));
71 #else // No transformations on Posix. 71 #else // No transformations on Posix.
72 EXPECT_EQ("foo/bar", RebaseOne(scope, "foo/bar", ".", ".", "none")); 72 EXPECT_EQ("foo/bar", RebaseOne(scope, "foo/bar", ".", ".", "none"));
73 EXPECT_EQ("foo/bar", RebaseOne(scope, "foo/bar", ".", ".", "to_system")); 73 EXPECT_EQ("foo/bar", RebaseOne(scope, "foo/bar", ".", ".", "to_system"));
74 EXPECT_EQ("foo/bar", RebaseOne(scope, "foo/bar", ".", ".", "from_system")); 74 EXPECT_EQ("foo/bar", RebaseOne(scope, "foo/bar", ".", ".", "from_system"));
75 #endif 75 #endif
76 76
77 // Test system path output. 77 // Test system path output.
78 #if defined(OS_WIN) 78 #if defined(OS_WIN)
79 setup.build_settings()->SetRootPath(base::FilePath(L"C:\\source")); 79 setup.build_settings()->SetRootPath(base::FilePath(L"C:\\source"));
80 EXPECT_EQ("C:\\source", RebaseOne(scope, ".", "//", "")); 80 EXPECT_EQ("C:\\source", RebaseOne(scope, ".", "", "//"));
81 EXPECT_EQ("C:\\source\\", RebaseOne(scope, "//", "//", "")); 81 EXPECT_EQ("C:\\source\\", RebaseOne(scope, "//", "", "//"));
82 EXPECT_EQ("C:\\source\\foo", RebaseOne(scope, "foo", "//", "")); 82 EXPECT_EQ("C:\\source\\foo", RebaseOne(scope, "foo", "", "//"));
83 EXPECT_EQ("C:\\source\\foo\\", RebaseOne(scope, "foo/", "//", "")); 83 EXPECT_EQ("C:\\source\\foo\\", RebaseOne(scope, "foo/", "", "//"));
84 EXPECT_EQ("C:\\source\\tools\\gn\\foo", RebaseOne(scope, "foo", ".", "")); 84 EXPECT_EQ("C:\\source\\tools\\gn\\foo", RebaseOne(scope, "foo", "", "."));
85 #else 85 #else
86 setup.build_settings()->SetRootPath(base::FilePath("/source")); 86 setup.build_settings()->SetRootPath(base::FilePath("/source"));
87 EXPECT_EQ("/source", RebaseOne(scope, ".", "//", "")); 87 EXPECT_EQ("/source", RebaseOne(scope, ".", "//", ""));
88 EXPECT_EQ("/source/", RebaseOne(scope, "//", "//", "")); 88 EXPECT_EQ("/source/", RebaseOne(scope, "//", "//", ""));
89 EXPECT_EQ("/source/foo", RebaseOne(scope, "foo", "//", "")); 89 EXPECT_EQ("/source/foo", RebaseOne(scope, "foo", "//", ""));
90 EXPECT_EQ("/source/foo/", RebaseOne(scope, "foo/", "//", "")); 90 EXPECT_EQ("/source/foo/", RebaseOne(scope, "foo/", "//", ""));
91 EXPECT_EQ("/source/tools/gn/foo", RebaseOne(scope, "foo", ".", "")); 91 EXPECT_EQ("/source/tools/gn/foo", RebaseOne(scope, "foo", ".", ""));
92 #endif 92 #endif
93 } 93 }
94 94
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
135 EXPECT_TRUE(err.has_error()); 135 EXPECT_TRUE(err.has_error());
136 136
137 // Two arg string input. 137 // Two arg string input.
138 args.clear(); 138 args.clear();
139 args.push_back(Value(NULL, "hello")); 139 args.push_back(Value(NULL, "hello"));
140 args.push_back(Value(NULL, "world")); 140 args.push_back(Value(NULL, "world"));
141 err = Err(); 141 err = Err();
142 ret = functions::RunRebasePath(setup.scope(), &function, args, &err); 142 ret = functions::RunRebasePath(setup.scope(), &function, args, &err);
143 EXPECT_TRUE(err.has_error()); 143 EXPECT_TRUE(err.has_error());
144 } 144 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698