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

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

Issue 547063004: Allow GN template collisions if they're the same. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 3 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
« no previous file with comments | « tools/gn/scope.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 "testing/gtest/include/gtest/gtest.h" 5 #include "testing/gtest/include/gtest/gtest.h"
6 #include "tools/gn/input_file.h" 6 #include "tools/gn/input_file.h"
7 #include "tools/gn/parse_tree.h" 7 #include "tools/gn/parse_tree.h"
8 #include "tools/gn/scope.h" 8 #include "tools/gn/scope.h"
9 #include "tools/gn/template.h"
9 #include "tools/gn/test_with_scope.h" 10 #include "tools/gn/test_with_scope.h"
10 11
11 namespace { 12 namespace {
12 13
13 bool HasStringValueEqualTo(const Scope* scope, 14 bool HasStringValueEqualTo(const Scope* scope,
14 const char* name, 15 const char* name,
15 const char* expected_value) { 16 const char* expected_value) {
16 const Value* value = scope->GetValue(name); 17 const Value* value = scope->GetValue(name);
17 if (!value) 18 if (!value)
18 return false; 19 return false;
19 if (value->type() != Value::STRING) 20 if (value->type() != Value::STRING)
20 return false; 21 return false;
21 return value->string_value() == expected_value; 22 return value->string_value() == expected_value;
22 } 23 }
23 24
24 } // namespace 25 } // namespace
25 26
26 TEST(Scope, NonRecursiveMergeTo) { 27 TEST(Scope, NonRecursiveMergeTo) {
27 TestWithScope setup; 28 TestWithScope setup;
28 29
29 // Make a pretend parse node with proper tracking that we can blame for the 30 // Make a pretend parse node with proper tracking that we can blame for the
30 // given value. 31 // given value.
31 InputFile input_file(SourceFile("//foo")); 32 InputFile input_file(SourceFile("//foo"));
32 Token assignment_token(Location(&input_file, 1, 1), Token::STRING, 33 Token assignment_token(Location(&input_file, 1, 1), Token::STRING,
33 "\"hello\""); 34 "\"hello\"");
34 LiteralNode assignment; 35 LiteralNode assignment;
35 assignment.set_value(assignment_token); 36 assignment.set_value(assignment_token);
36 37
38 // Add some values to the scope.
37 Value old_value(&assignment, "hello"); 39 Value old_value(&assignment, "hello");
38 setup.scope()->SetValue("v", old_value, &assignment); 40 setup.scope()->SetValue("v", old_value, &assignment);
39 base::StringPiece private_var_name("_private"); 41 base::StringPiece private_var_name("_private");
40 setup.scope()->SetValue(private_var_name, old_value, &assignment); 42 setup.scope()->SetValue(private_var_name, old_value, &assignment);
41 43
44 // Add some templates to the scope.
45 FunctionCallNode templ_definition;
46 scoped_refptr<Template> templ(new Template(setup.scope(), &templ_definition));
47 setup.scope()->AddTemplate("templ", templ.get());
48 scoped_refptr<Template> private_templ(
49 new Template(setup.scope(), &templ_definition));
50 setup.scope()->AddTemplate("_templ", private_templ.get());
51
42 // Detect collisions of values' values. 52 // Detect collisions of values' values.
43 { 53 {
44 Scope new_scope(setup.settings()); 54 Scope new_scope(setup.settings());
45 Value new_value(&assignment, "goodbye"); 55 Value new_value(&assignment, "goodbye");
46 new_scope.SetValue("v", new_value, &assignment); 56 new_scope.SetValue("v", new_value, &assignment);
47 57
48 Err err; 58 Err err;
49 EXPECT_FALSE(setup.scope()->NonRecursiveMergeTo( 59 EXPECT_FALSE(setup.scope()->NonRecursiveMergeTo(
50 &new_scope, Scope::MergeOptions(), 60 &new_scope, Scope::MergeOptions(),
51 &assignment, "error", &err)); 61 &assignment, "error", &err));
52 EXPECT_TRUE(err.has_error()); 62 EXPECT_TRUE(err.has_error());
53 } 63 }
54 64
65 // Template name collisions.
66 {
67 Scope new_scope(setup.settings());
68
69 scoped_refptr<Template> new_templ(
70 new Template(&new_scope, &templ_definition));
71 new_scope.AddTemplate("templ", new_templ.get());
72
73 Err err;
74 EXPECT_FALSE(setup.scope()->NonRecursiveMergeTo(
75 &new_scope, Scope::MergeOptions(), &assignment, "error", &err));
76 EXPECT_TRUE(err.has_error());
77 }
78
55 // The clobber flag should just overwrite colliding values. 79 // The clobber flag should just overwrite colliding values.
56 { 80 {
57 Scope new_scope(setup.settings()); 81 Scope new_scope(setup.settings());
58 Value new_value(&assignment, "goodbye"); 82 Value new_value(&assignment, "goodbye");
59 new_scope.SetValue("v", new_value, &assignment); 83 new_scope.SetValue("v", new_value, &assignment);
60 84
61 Err err; 85 Err err;
62 Scope::MergeOptions options; 86 Scope::MergeOptions options;
63 options.clobber_existing = true; 87 options.clobber_existing = true;
64 EXPECT_TRUE(setup.scope()->NonRecursiveMergeTo( 88 EXPECT_TRUE(setup.scope()->NonRecursiveMergeTo(
65 &new_scope, options, &assignment, "error", &err)); 89 &new_scope, options, &assignment, "error", &err));
66 EXPECT_FALSE(err.has_error()); 90 EXPECT_FALSE(err.has_error());
67 91
68 const Value* found_value = new_scope.GetValue("v"); 92 const Value* found_value = new_scope.GetValue("v");
69 ASSERT_TRUE(found_value); 93 ASSERT_TRUE(found_value);
70 EXPECT_TRUE(old_value == *found_value); 94 EXPECT_TRUE(old_value == *found_value);
71 } 95 }
72 96
97 // Clobber flag for templates.
98 {
99 Scope new_scope(setup.settings());
100
101 scoped_refptr<Template> new_templ(
102 new Template(&new_scope, &templ_definition));
103 new_scope.AddTemplate("templ", new_templ.get());
104 Scope::MergeOptions options;
105 options.clobber_existing = true;
106
107 Err err;
108 EXPECT_TRUE(setup.scope()->NonRecursiveMergeTo(
109 &new_scope, options, &assignment, "error", &err));
110 EXPECT_FALSE(err.has_error());
111
112 const Template* found_value = new_scope.GetTemplate("templ");
113 ASSERT_TRUE(found_value);
114 EXPECT_TRUE(templ.get() == found_value);
115 }
116
73 // Don't flag values that technically collide but have the same value. 117 // Don't flag values that technically collide but have the same value.
74 { 118 {
75 Scope new_scope(setup.settings()); 119 Scope new_scope(setup.settings());
76 Value new_value(&assignment, "hello"); 120 Value new_value(&assignment, "hello");
77 new_scope.SetValue("v", new_value, &assignment); 121 new_scope.SetValue("v", new_value, &assignment);
78 122
79 Err err; 123 Err err;
80 Scope::MergeOptions options;
81 options.clobber_existing = true;
82 EXPECT_TRUE(setup.scope()->NonRecursiveMergeTo( 124 EXPECT_TRUE(setup.scope()->NonRecursiveMergeTo(
83 &new_scope, options, &assignment, "error", &err)); 125 &new_scope, Scope::MergeOptions(), &assignment, "error", &err));
84 EXPECT_FALSE(err.has_error()); 126 EXPECT_FALSE(err.has_error());
85 } 127 }
86 128
87 // Copy private values. 129 // Templates that technically collide but are the same.
88 { 130 {
89 Scope new_scope(setup.settings()); 131 Scope new_scope(setup.settings());
90 132
133 scoped_refptr<Template> new_templ(
134 new Template(&new_scope, &templ_definition));
135 new_scope.AddTemplate("templ", templ.get());
136
137 Err err;
138 EXPECT_TRUE(setup.scope()->NonRecursiveMergeTo(
139 &new_scope, Scope::MergeOptions(), &assignment, "error", &err));
140 EXPECT_FALSE(err.has_error());
141 }
142
143 // Copy private values and templates.
144 {
145 Scope new_scope(setup.settings());
146
91 Err err; 147 Err err;
92 EXPECT_TRUE(setup.scope()->NonRecursiveMergeTo( 148 EXPECT_TRUE(setup.scope()->NonRecursiveMergeTo(
93 &new_scope, Scope::MergeOptions(), &assignment, "error", &err)); 149 &new_scope, Scope::MergeOptions(), &assignment, "error", &err));
94 EXPECT_FALSE(err.has_error()); 150 EXPECT_FALSE(err.has_error());
95 EXPECT_TRUE(new_scope.GetValue(private_var_name)); 151 EXPECT_TRUE(new_scope.GetValue(private_var_name));
152 EXPECT_TRUE(new_scope.GetTemplate("_templ"));
96 } 153 }
97 154
98 // Skip private values. 155 // Skip private values and templates.
99 { 156 {
100 Scope new_scope(setup.settings()); 157 Scope new_scope(setup.settings());
101 158
102 Err err; 159 Err err;
103 Scope::MergeOptions options; 160 Scope::MergeOptions options;
104 options.skip_private_vars = true; 161 options.skip_private_vars = true;
105 EXPECT_TRUE(setup.scope()->NonRecursiveMergeTo( 162 EXPECT_TRUE(setup.scope()->NonRecursiveMergeTo(
106 &new_scope, options, &assignment, "error", &err)); 163 &new_scope, options, &assignment, "error", &err));
107 EXPECT_FALSE(err.has_error()); 164 EXPECT_FALSE(err.has_error());
108 EXPECT_FALSE(new_scope.GetValue(private_var_name)); 165 EXPECT_FALSE(new_scope.GetValue(private_var_name));
166 EXPECT_FALSE(new_scope.GetTemplate("_templ"));
109 } 167 }
110 168
111 // Don't mark used. 169 // Don't mark used.
112 { 170 {
113 Scope new_scope(setup.settings()); 171 Scope new_scope(setup.settings());
114 172
115 Err err; 173 Err err;
116 Scope::MergeOptions options; 174 Scope::MergeOptions options;
117 EXPECT_TRUE(setup.scope()->NonRecursiveMergeTo( 175 EXPECT_TRUE(setup.scope()->NonRecursiveMergeTo(
118 &new_scope, options, &assignment, "error", &err)); 176 &new_scope, options, &assignment, "error", &err));
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after
228 286
229 TEST(Scope, RemovePrivateIdentifiers) { 287 TEST(Scope, RemovePrivateIdentifiers) {
230 TestWithScope setup; 288 TestWithScope setup;
231 setup.scope()->SetValue("a", Value(NULL, true), NULL); 289 setup.scope()->SetValue("a", Value(NULL, true), NULL);
232 setup.scope()->SetValue("_b", Value(NULL, true), NULL); 290 setup.scope()->SetValue("_b", Value(NULL, true), NULL);
233 291
234 setup.scope()->RemovePrivateIdentifiers(); 292 setup.scope()->RemovePrivateIdentifiers();
235 EXPECT_TRUE(setup.scope()->GetValue("a")); 293 EXPECT_TRUE(setup.scope()->GetValue("a"));
236 EXPECT_FALSE(setup.scope()->GetValue("_b")); 294 EXPECT_FALSE(setup.scope()->GetValue("_b"));
237 } 295 }
OLDNEW
« no previous file with comments | « tools/gn/scope.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698