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

Unified Diff: tools/gn/function_get_target_gen_path_unittest.cc

Issue 264773014: Add get_target_gen_path function (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 8 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « tools/gn/function_get_target_gen_path.cc ('k') | tools/gn/functions.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: tools/gn/function_get_target_gen_path_unittest.cc
diff --git a/tools/gn/function_get_target_gen_path_unittest.cc b/tools/gn/function_get_target_gen_path_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..56272d51a6d88d7e75e6f78154eb534960e83b73
--- /dev/null
+++ b/tools/gn/function_get_target_gen_path_unittest.cc
@@ -0,0 +1,156 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "build/build_config.h"
+#include "testing/gtest/include/gtest/gtest.h"
+#include "tools/gn/filesystem_utils.h"
+#include "tools/gn/functions.h"
+#include "tools/gn/parse_tree.h"
+#include "tools/gn/test_with_scope.h"
+
+namespace {
+
+std::string CallGetTargetGenPath(Scope* scope,
+ const char* target_name,
+ const char* suffix) {
+ std::vector<Value> args;
+ args.push_back(Value(NULL, target_name));
+ args.push_back(Value(NULL, suffix));
+
+ Err err;
+ FunctionCallNode function;
+ Value result = functions::RunGetTargetGenPath(scope, &function, args, &err);
+ EXPECT_EQ(Value::STRING, result.type());
+ EXPECT_FALSE(err.has_error());
+ return result.string_value();
+}
+
+std::string GetExpectedTargetGenPath(const Target& target, const char* suffix) {
+ std::string target_name = target.label().name();
+ SourceDir target_gen_dir = GetTargetGenDir(&target);
+ return target_gen_dir.value() + target_name + std::string(suffix);
+}
+
+} // namespace
+
+TEST(GetTargetGenPath, Strings) {
+ TestWithScope setup;
+ setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/"));
+ Scope* tools_gn = setup.scope();
+ tools_gn->set_source_dir(SourceDir("//tools/gn/"));
+
+ SourceDir tools_gn_util_dir("//tools/gn/util");
+
+ Err err;
+ Target foo_target(setup.settings(), Label(tools_gn->GetSourceDir(), "foo"));
+ Target bar_target(setup.settings(), Label(tools_gn_util_dir, "bar"));
+
+ // Target in same file.
+ EXPECT_EQ(
+ GetExpectedTargetGenPath(foo_target, ".ext"),
+ CallGetTargetGenPath(tools_gn, ":foo", ".ext"));
+
+ EXPECT_EQ(
+ GetExpectedTargetGenPath(foo_target, ".ext"),
+ CallGetTargetGenPath(tools_gn, "//tools/gn:foo", ".ext"));
+
+ EXPECT_EQ(
+ GetExpectedTargetGenPath(foo_target, ".ext"),
+ CallGetTargetGenPath(tools_gn, "../gn:foo", ".ext"));
+
+ // Target in different dir.
+ EXPECT_EQ(
+ GetExpectedTargetGenPath(bar_target, ".ext"),
+ CallGetTargetGenPath(tools_gn, "util:bar", ".ext"));
+
+ EXPECT_EQ(
+ GetExpectedTargetGenPath(bar_target, ".ext"),
+ CallGetTargetGenPath(tools_gn, "//tools/gn/util:bar", ".ext"));
+
+ EXPECT_EQ(
+ GetExpectedTargetGenPath(bar_target, ".ext"),
+ CallGetTargetGenPath(tools_gn, "../gn/util:bar", ".ext"));
+
+}
+
+// Test list input.
+TEST(GetTargetGenPath, List) {
+ TestWithScope setup;
+ setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/"));
+ Scope* tools_gn = setup.scope();
+ tools_gn->set_source_dir(SourceDir("//tools/gn/"));
+
+ SourceDir tools_gn_util_dir("//tools/gn/util");
+
+ Target foo_target(setup.settings(), Label(tools_gn->GetSourceDir(), "foo"));
+ Target bar_target(setup.settings(), Label(tools_gn_util_dir, "bar"));
+
+ Value inputs_list(NULL, Value::LIST);
+ inputs_list.list_value().push_back(Value(NULL, ":foo"));
+ inputs_list.list_value().push_back(Value(NULL, "util:bar"));
+
+ std::vector<Value> args;
+ args.push_back(inputs_list);
+ args.push_back(Value(NULL, ".ext"));
+
+ Err err;
+ FunctionCallNode function;
+ Value result =
+ functions::RunGetTargetGenPath(tools_gn, &function, args, &err);
+ ASSERT_EQ(Value::LIST, result.type());
+ EXPECT_FALSE(err.has_error());
+
+ ASSERT_EQ((size_t)2, result.list_value().size());
+ ASSERT_EQ(Value::STRING, result.list_value()[0].type());
+ ASSERT_EQ(Value::STRING, result.list_value()[1].type());
+
+ EXPECT_EQ(
+ GetExpectedTargetGenPath(foo_target, ".ext"),
+ result.list_value()[0].string_value());
+ EXPECT_EQ(
+ GetExpectedTargetGenPath(bar_target, ".ext"),
+ result.list_value()[1].string_value());
+}
+
+bool DoErrorCheck(const std::string& gn_input) {
+ TestWithScope setup;
+ setup.scope()->set_source_dir(SourceDir("//"));
+ TestParseInput input(gn_input);
+ if (input.has_error()) {
+ ADD_FAILURE() << input.parse_err().message();
+ return true;
+ }
+
+ Err err;
+ input.parsed()->Execute(setup.scope(), &err);
+ return err.has_error();
+}
+
+TEST(GetTargetGenPath, Errors) {
+ // Valid uses.
+ EXPECT_FALSE(DoErrorCheck(
+ "get_target_gen_path(\":foo\", \".ext\")"
+ "get_target_gen_path([\":foo\", \":bar\"], \".ext\")"));
+
+ EXPECT_FALSE(
+ DoErrorCheck("get_target_gen_path(\":foo\", \"/subdir/file\")\n"));
+
+ // Missing suffix.
+ EXPECT_TRUE(DoErrorCheck("get_target_gen_path(\":foo\")"));
+ EXPECT_TRUE(DoErrorCheck("get_target_gen_path([\":foo\"])"));
+
+ // Suffix type wrong.
+ EXPECT_TRUE(DoErrorCheck("get_target_gen_path(\":foo\", [\".ext\"])"));
+
+ // Too many arguments.
+ EXPECT_TRUE(
+ DoErrorCheck("get_target_gen_path(\":foo\", \".ext\", \"extra\")"));
+
+ // Invalid target name.
+ EXPECT_TRUE(
+ DoErrorCheck("get_target_gen_path(\"\\\\..\\:foo\", \".ext\")"));
+
+ // Final path should be a file (not a directory).
+ EXPECT_TRUE(DoErrorCheck("get_target_gen_path(\":foo\", \"/subdir/\")\n"));
+}
« no previous file with comments | « tools/gn/function_get_target_gen_path.cc ('k') | tools/gn/functions.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698