summaryrefslogtreecommitdiffstats
path: root/tools/gn/file_template_unittest.cc
diff options
context:
space:
mode:
authorbrettw@chromium.org <brettw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2014-06-19 16:50:52 +0000
committerbrettw@chromium.org <brettw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2014-06-19 16:50:52 +0000
commite9bf4fcb470f7220306c39827fc3fd9be0a27741 (patch)
tree59d2c4de7ab1964d6c421dd014b629ce9bd961e1 /tools/gn/file_template_unittest.cc
parent252e6603f3012cdc8de4a212999ef62660769be7 (diff)
downloadchromium_src-e9bf4fcb470f7220306c39827fc3fd9be0a27741.zip
chromium_src-e9bf4fcb470f7220306c39827fc3fd9be0a27741.tar.gz
chromium_src-e9bf4fcb470f7220306c39827fc3fd9be0a27741.tar.bz2
Add directory extraction to GN path handling.
Some of the mojo templates want to put generated files in the directory corresponding to the source file, rather than the directory containing the BUILD file. Previously, this required putting the BUILD files in the same directory. This patch adds the ability to specify the generated file directory corresponding to the input file in the action_foreach file templates so this can be expressed naturally. For parity, it also adds qquerying for this information via get_path_info. BUG= R=cjhopman@chromium.org Review URL: https://codereview.chromium.org/334333005 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@278402 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'tools/gn/file_template_unittest.cc')
-rw-r--r--tools/gn/file_template_unittest.cc79
1 files changed, 63 insertions, 16 deletions
diff --git a/tools/gn/file_template_unittest.cc b/tools/gn/file_template_unittest.cc
index 8e3bf40..d10791e 100644
--- a/tools/gn/file_template_unittest.cc
+++ b/tools/gn/file_template_unittest.cc
@@ -7,58 +7,62 @@
#include "testing/gtest/include/gtest/gtest.h"
#include "tools/gn/escape.h"
#include "tools/gn/file_template.h"
+#include "tools/gn/test_with_scope.h"
TEST(FileTemplate, Static) {
+ TestWithScope setup;
+
std::vector<std::string> templates;
templates.push_back("something_static");
- FileTemplate t(templates);
+ FileTemplate t(setup.settings(), templates);
EXPECT_FALSE(t.has_substitutions());
std::vector<std::string> result;
- t.ApplyString("", &result);
- ASSERT_EQ(1u, result.size());
- EXPECT_EQ("something_static", result[0]);
-
- result.clear();
- t.ApplyString("lalala", &result);
+ t.Apply(SourceFile("//foo/bar"), &result);
ASSERT_EQ(1u, result.size());
EXPECT_EQ("something_static", result[0]);
}
TEST(FileTemplate, Typical) {
+ TestWithScope setup;
+
std::vector<std::string> templates;
templates.push_back("foo/{{source_name_part}}.cc");
templates.push_back("foo/{{source_name_part}}.h");
- FileTemplate t(templates);
+ FileTemplate t(setup.settings(), templates);
EXPECT_TRUE(t.has_substitutions());
std::vector<std::string> result;
- t.ApplyString("sources/ha.idl", &result);
+ t.Apply(SourceFile("//sources/ha.idl"), &result);
ASSERT_EQ(2u, result.size());
EXPECT_EQ("foo/ha.cc", result[0]);
EXPECT_EQ("foo/ha.h", result[1]);
}
TEST(FileTemplate, Weird) {
+ TestWithScope setup;
+
std::vector<std::string> templates;
templates.push_back("{{{source}}{{source}}{{");
- FileTemplate t(templates);
+ FileTemplate t(setup.settings(), templates);
EXPECT_TRUE(t.has_substitutions());
std::vector<std::string> result;
- t.ApplyString("foo/lalala.c", &result);
+ t.Apply(SourceFile("//foo/lalala.c"), &result);
ASSERT_EQ(1u, result.size());
- EXPECT_EQ("{foo/lalala.cfoo/lalala.c{{", result[0]);
+ EXPECT_EQ("{../../foo/lalala.c../../foo/lalala.c{{", result[0]);
}
TEST(FileTemplate, NinjaExpansions) {
+ TestWithScope setup;
+
std::vector<std::string> templates;
templates.push_back("-i");
templates.push_back("{{source}}");
templates.push_back("--out=foo bar\"{{source_name_part}}\".o");
templates.push_back(""); // Test empty string.
- FileTemplate t(templates);
+ FileTemplate t(setup.settings(), templates);
std::ostringstream out;
t.WriteWithNinjaExpansions(out);
@@ -79,21 +83,64 @@ TEST(FileTemplate, NinjaExpansions) {
}
TEST(FileTemplate, NinjaVariables) {
+ TestWithScope setup;
+
std::vector<std::string> templates;
templates.push_back("-i");
templates.push_back("{{source}}");
templates.push_back("--out=foo bar\"{{source_name_part}}\".o");
+ templates.push_back("{{source_file_part}}");
+ templates.push_back("{{source_dir}}");
+ templates.push_back("{{source_root_relative_dir}}");
+ templates.push_back("{{source_gen_dir}}");
+ templates.push_back("{{source_out_dir}}");
- FileTemplate t(templates);
+ FileTemplate t(setup.settings(), templates);
std::ostringstream out;
EscapeOptions options;
options.mode = ESCAPE_NINJA_COMMAND;
- t.WriteNinjaVariablesForSubstitution(out, "../../foo/bar.txt", options);
+ t.WriteNinjaVariablesForSubstitution(out, setup.settings(),
+ SourceFile("//foo/bar.txt"), options);
// Just the variables used above should be written.
EXPECT_EQ(
" source = ../../foo/bar.txt\n"
- " source_name_part = bar\n",
+ " source_name_part = bar\n"
+ " source_file_part = bar.txt\n"
+ " source_dir = ../../foo\n"
+ " source_root_rel_dir = foo\n"
+ " source_gen_dir = gen/foo\n"
+ " source_out_dir = obj/foo\n",
out.str());
}
+
+// Tests in isolation different types of substitutions and that the right
+// things are generated.
+TEST(FileTemplate, Substitutions) {
+ TestWithScope setup;
+
+ #define GetSubst(str, what) \
+ FileTemplate::GetSubstitution(setup.settings(), \
+ SourceFile(str), \
+ FileTemplate::Subrange::what)
+
+ // Try all possible templates with a normal looking string.
+ EXPECT_EQ("../../foo/bar/baz.txt", GetSubst("//foo/bar/baz.txt", SOURCE));
+ EXPECT_EQ("baz", GetSubst("//foo/bar/baz.txt", NAME_PART));
+ EXPECT_EQ("baz.txt", GetSubst("//foo/bar/baz.txt", FILE_PART));
+ EXPECT_EQ("../../foo/bar", GetSubst("//foo/bar/baz.txt", SOURCE_DIR));
+ EXPECT_EQ("foo/bar", GetSubst("//foo/bar/baz.txt", ROOT_RELATIVE_DIR));
+ EXPECT_EQ("gen/foo/bar", GetSubst("//foo/bar/baz.txt", SOURCE_GEN_DIR));
+ EXPECT_EQ("obj/foo/bar", GetSubst("//foo/bar/baz.txt", SOURCE_OUT_DIR));
+
+ // Operations on an absolute path.
+ EXPECT_EQ("/baz.txt", GetSubst("/baz.txt", SOURCE));
+ EXPECT_EQ("/.", GetSubst("/baz.txt", SOURCE_DIR));
+ EXPECT_EQ("gen", GetSubst("/baz.txt", SOURCE_GEN_DIR));
+ EXPECT_EQ("obj", GetSubst("/baz.txt", SOURCE_OUT_DIR));
+
+ EXPECT_EQ(".", GetSubst("//baz.txt", ROOT_RELATIVE_DIR));
+
+ #undef GetSubst
+}