Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2014 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 <sstream> | |
| 6 | |
| 7 #include "testing/gtest/include/gtest/gtest.h" | |
| 8 #include "tools/gn/err.h" | |
| 9 #include "tools/gn/escape.h" | |
| 10 #include "tools/gn/substitution_pattern.h" | |
| 11 #include "tools/gn/substitution_writer.h" | |
| 12 #include "tools/gn/test_with_scope.h" | |
| 13 | |
| 14 TEST(SubstitutionWriter, ApplyPatternToSource) { | |
| 15 TestWithScope setup; | |
| 16 | |
| 17 SubstitutionPattern pattern; | |
| 18 Err err; | |
| 19 ASSERT_TRUE(pattern.Parse("{{source_gen_dir}}/{{source_name_part}}.tmp", | |
| 20 NULL, &err)); | |
| 21 | |
| 22 SourceFile result = SubstitutionWriter::ApplyPatternToSource( | |
| 23 setup.settings(), pattern, SourceFile("//foo/bar/myfile.txt")); | |
| 24 ASSERT_EQ("//out/Debug/gen/foo/bar/myfile.tmp", result.value()); | |
| 25 } | |
| 26 | |
| 27 TEST(SubstitutionWriter, ApplyPatternToSourceAsOutputFile) { | |
| 28 TestWithScope setup; | |
| 29 | |
| 30 SubstitutionPattern pattern; | |
| 31 Err err; | |
| 32 ASSERT_TRUE(pattern.Parse("{{source_gen_dir}}/{{source_name_part}}.tmp", | |
| 33 NULL, &err)); | |
| 34 | |
| 35 OutputFile result = SubstitutionWriter::ApplyPatternToSourceAsOutputFile( | |
| 36 setup.settings(), pattern, SourceFile("//foo/bar/myfile.txt")); | |
| 37 ASSERT_EQ("gen/foo/bar/myfile.tmp", result.value()); | |
| 38 } | |
| 39 | |
| 40 TEST(SubstutitionWriter, WriteNinjaVariablesForSource) { | |
| 41 TestWithScope setup; | |
| 42 | |
| 43 std::vector<SubstitutionType> types; | |
| 44 types.push_back(SUBSTITUTION_SOURCE); | |
| 45 types.push_back(SUBSTITUTION_SOURCE_NAME_PART); | |
| 46 types.push_back(SUBSTITUTION_SOURCE_DIR); | |
| 47 | |
| 48 EscapeOptions options; | |
| 49 options.mode = ESCAPE_NONE; | |
| 50 | |
| 51 std::ostringstream out; | |
| 52 SubstitutionWriter::WriteNinjaVariablesForSource( | |
| 53 setup.settings(), SourceFile("//foo/bar/baz.txt"), types, options, out); | |
| 54 | |
| 55 // The "source" should be skipped since that will expand to $in which is | |
| 56 // implicit. | |
| 57 EXPECT_EQ( | |
| 58 " source_name_part = baz\n" | |
| 59 " source_dir = ../../foo/bar\n", | |
| 60 out.str()); | |
| 61 } | |
| 62 | |
| 63 TEST(SubstitutionWriter, WriteWithNinjaVariables) { | |
| 64 Err err; | |
| 65 SubstitutionPattern pattern; | |
| 66 ASSERT_TRUE(pattern.Parse( | |
| 67 "-i {{source}} --out=bar\"{{source_name_part}}\".o", | |
| 68 NULL, &err)); | |
| 69 EXPECT_FALSE(err.has_error()); | |
| 70 | |
| 71 EscapeOptions options; | |
| 72 options.mode = ESCAPE_NONE; | |
| 73 | |
| 74 std::ostringstream out; | |
| 75 SubstitutionWriter::WriteWithNinjaVariables(pattern, options, out); | |
| 76 | |
| 77 EXPECT_EQ( | |
| 78 "-i ${in} --out=bar\"${source_name_part}\".o", | |
| 79 out.str()); | |
| 80 } | |
| 81 | |
| 82 // Tests in isolation different types of substitutions and that the right | |
| 83 // things are generated. | |
| 84 TEST(SubstutitionWriter, Substitutions) { | |
| 85 TestWithScope setup; | |
| 86 | |
| 87 // Call to get substitutions relative to the build dir. | |
| 88 #define GetRelSubst(str, what) \ | |
| 89 SubstitutionWriter::GetSourceSubstitution( \ | |
| 90 setup.settings(), \ | |
| 91 SourceFile(str), \ | |
| 92 what, \ | |
| 93 SubstitutionWriter::OUTPUT_RELATIVE, \ | |
| 94 setup.settings()->build_settings()->build_dir()) | |
| 95 | |
| 96 // Call to get absolute directory substitutions. | |
| 97 #define GetAbsSubst(str, what) \ | |
| 98 SubstitutionWriter::GetSourceSubstitution( \ | |
| 99 setup.settings(), \ | |
| 100 SourceFile(str), \ | |
| 101 what, \ | |
| 102 SubstitutionWriter::OUTPUT_ABSOLUTE, \ | |
| 103 SourceDir()) | |
| 104 | |
| 105 // Try all possible templates with a normal looking string. | |
| 106 EXPECT_EQ("../../foo/bar/baz.txt", | |
| 107 GetRelSubst("//foo/bar/baz.txt", SUBSTITUTION_SOURCE)); | |
| 108 EXPECT_EQ("//foo/bar/baz.txt", | |
| 109 GetAbsSubst("//foo/bar/baz.txt", SUBSTITUTION_SOURCE)); | |
| 110 | |
| 111 EXPECT_EQ("baz", | |
| 112 GetRelSubst("//foo/bar/baz.txt", SUBSTITUTION_SOURCE_NAME_PART)); | |
| 113 EXPECT_EQ("baz", | |
| 114 GetAbsSubst("//foo/bar/baz.txt", SUBSTITUTION_SOURCE_NAME_PART)); | |
| 115 | |
| 116 EXPECT_EQ("baz.txt", | |
| 117 GetRelSubst("//foo/bar/baz.txt", SUBSTITUTION_SOURCE_FILE_PART)); | |
| 118 EXPECT_EQ("baz.txt", | |
| 119 GetAbsSubst("//foo/bar/baz.txt", SUBSTITUTION_SOURCE_FILE_PART)); | |
| 120 | |
| 121 EXPECT_EQ("../../foo/bar", | |
| 122 GetRelSubst("//foo/bar/baz.txt", SUBSTITUTION_SOURCE_DIR)); | |
| 123 EXPECT_EQ("//foo/bar", | |
| 124 GetAbsSubst("//foo/bar/baz.txt", SUBSTITUTION_SOURCE_DIR)); | |
| 125 | |
| 126 EXPECT_EQ("foo/bar", GetRelSubst("//foo/bar/baz.txt", | |
| 127 SUBSTITUTION_SOURCE_ROOT_RELATIVE_DIR)); | |
| 128 EXPECT_EQ("foo/bar", GetAbsSubst("//foo/bar/baz.txt", | |
| 129 SUBSTITUTION_SOURCE_ROOT_RELATIVE_DIR)); | |
| 130 | |
| 131 EXPECT_EQ("gen/foo/bar", | |
| 132 GetRelSubst("//foo/bar/baz.txt", SUBSTITUTION_SOURCE_GEN_DIR)); | |
| 133 EXPECT_EQ("//out/Debug/gen/foo/bar", | |
| 134 GetAbsSubst("//foo/bar/baz.txt", SUBSTITUTION_SOURCE_GEN_DIR)); | |
| 135 | |
| 136 EXPECT_EQ("obj/foo/bar", | |
| 137 GetRelSubst("//foo/bar/baz.txt", SUBSTITUTION_SOURCE_OUT_DIR)); | |
| 138 EXPECT_EQ("//out/Debug/obj/foo/bar", | |
| 139 GetAbsSubst("//foo/bar/baz.txt", SUBSTITUTION_SOURCE_OUT_DIR)); | |
| 140 | |
| 141 // Operations on an absolute path. | |
| 142 EXPECT_EQ("/baz.txt", GetRelSubst("/baz.txt", SUBSTITUTION_SOURCE)); | |
| 143 EXPECT_EQ("/.", GetRelSubst("/baz.txt", SUBSTITUTION_SOURCE_DIR)); | |
| 144 EXPECT_EQ("gen", GetRelSubst("/baz.txt", SUBSTITUTION_SOURCE_GEN_DIR)); | |
| 145 EXPECT_EQ("obj", GetRelSubst("/baz.txt", SUBSTITUTION_SOURCE_OUT_DIR)); | |
| 146 | |
| 147 EXPECT_EQ(".", | |
| 148 GetRelSubst("//baz.txt", SUBSTITUTION_SOURCE_ROOT_RELATIVE_DIR)); | |
| 149 | |
|
scottmg
2014/08/05 22:30:32
The other SUBSTITUTION_ types will be exercised on
brettw
2014/08/06 18:25:01
Yes
| |
| 150 #undef GetAbsSubst | |
| 151 #undef GetRelSubst | |
| 152 } | |
| OLD | NEW |