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

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

Issue 212933008: Don't dynamically scope GN templates. (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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « tools/gn/scope.cc ('k') | tools/gn/template.h » ('j') | 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/test_with_scope.h" 9 #include "tools/gn/test_with_scope.h"
10 10
11 namespace {
12
13 bool HasStringValueEqualTo(const Scope* scope,
14 const char* name,
15 const char* expected_value) {
16 const Value* value = scope->GetValue(name);
17 if (!value)
18 return false;
19 if (value->type() != Value::STRING)
20 return false;
21 return value->string_value() == expected_value;
22 }
23
24 } // namespace
25
11 TEST(Scope, NonRecursiveMergeTo) { 26 TEST(Scope, NonRecursiveMergeTo) {
12 TestWithScope setup; 27 TestWithScope setup;
13 28
14 // Make a pretend parse node with proper tracking that we can blame for the 29 // Make a pretend parse node with proper tracking that we can blame for the
15 // given value. 30 // given value.
16 InputFile input_file(SourceFile("//foo")); 31 InputFile input_file(SourceFile("//foo"));
17 Token assignment_token(Location(&input_file, 1, 1), Token::STRING, 32 Token assignment_token(Location(&input_file, 1, 1), Token::STRING,
18 "\"hello\""); 33 "\"hello\"");
19 LiteralNode assignment; 34 LiteralNode assignment;
20 assignment.set_value(assignment_token); 35 assignment.set_value(assignment_token);
21 36
22 Value old_value(&assignment, "hello"); 37 Value old_value(&assignment, "hello");
23 setup.scope()->SetValue("v", old_value, &assignment); 38 setup.scope()->SetValue("v", old_value, &assignment);
24 39
25 // Detect collisions of values' values. 40 // Detect collisions of values' values.
26 { 41 {
27 Scope new_scope(setup.settings()); 42 Scope new_scope(setup.settings());
28 Value new_value(&assignment, "goodbye"); 43 Value new_value(&assignment, "goodbye");
29 new_scope.SetValue("v", new_value, &assignment); 44 new_scope.SetValue("v", new_value, &assignment);
30 45
31 Err err; 46 Err err;
32 EXPECT_FALSE(new_scope.NonRecursiveMergeTo( 47 EXPECT_FALSE(setup.scope()->NonRecursiveMergeTo(
33 setup.scope(), &assignment, "error", &err)); 48 &new_scope, false, &assignment, "error", &err));
34 EXPECT_TRUE(err.has_error()); 49 EXPECT_TRUE(err.has_error());
35 } 50 }
36 51
52 // The clobber flag should just overwrite colliding values.
53 {
54 Scope new_scope(setup.settings());
55 Value new_value(&assignment, "goodbye");
56 new_scope.SetValue("v", new_value, &assignment);
57
58 Err err;
59 EXPECT_TRUE(setup.scope()->NonRecursiveMergeTo(
60 &new_scope, true, &assignment, "error", &err));
61 EXPECT_FALSE(err.has_error());
62
63 const Value* found_value = new_scope.GetValue("v");
64 ASSERT_TRUE(found_value);
65 EXPECT_TRUE(old_value == *found_value);
66 }
67
37 // Don't flag values that technically collide but have the same value. 68 // Don't flag values that technically collide but have the same value.
38 { 69 {
39 Scope new_scope(setup.settings()); 70 Scope new_scope(setup.settings());
40 Value new_value(&assignment, "hello"); 71 Value new_value(&assignment, "hello");
41 new_scope.SetValue("v", new_value, &assignment); 72 new_scope.SetValue("v", new_value, &assignment);
42 73
43 Err err; 74 Err err;
44 EXPECT_TRUE(new_scope.NonRecursiveMergeTo( 75 EXPECT_TRUE(setup.scope()->NonRecursiveMergeTo(
45 setup.scope(), &assignment, "error", &err)); 76 &new_scope, false, &assignment, "error", &err));
46 EXPECT_FALSE(err.has_error()); 77 EXPECT_FALSE(err.has_error());
47 } 78 }
48 } 79 }
49 80
81 TEST(Scope, MakeClosure) {
82 // Create 3 nested scopes [const root from setup] <- nested1 <- nested2.
83 TestWithScope setup;
84
85 // Make a pretend parse node with proper tracking that we can blame for the
86 // given value.
87 InputFile input_file(SourceFile("//foo"));
88 Token assignment_token(Location(&input_file, 1, 1), Token::STRING,
89 "\"hello\"");
90 LiteralNode assignment;
91 assignment.set_value(assignment_token);
92 setup.scope()->SetValue("on_root", Value(&assignment, "on_root"),
93 &assignment);
94
95 // Root scope should be const from the nested caller's perspective.
96 Scope nested1(static_cast<const Scope*>(setup.scope()));
97 nested1.SetValue("on_one", Value(&assignment, "on_one"), &assignment);
98
99 Scope nested2(&nested1);
100 nested2.SetValue("on_one", Value(&assignment, "on_two"), &assignment);
101 nested2.SetValue("on_two", Value(&assignment, "on_two2"), &assignment);
102
103 // Making a closure from the root scope.
104 scoped_ptr<Scope> result = setup.scope()->MakeClosure();
105 EXPECT_FALSE(result->containing()); // Should have no containing scope.
106 EXPECT_TRUE(result->GetValue("on_root")); // Value should be copied.
107
108 // Making a closure from the second nested scope.
109 result = nested2.MakeClosure();
110 EXPECT_EQ(setup.scope(),
111 result->containing()); // Containing scope should be the root.
112 EXPECT_TRUE(HasStringValueEqualTo(result.get(), "on_root", "on_root"));
113 EXPECT_TRUE(HasStringValueEqualTo(result.get(), "on_one", "on_two"));
114 EXPECT_TRUE(HasStringValueEqualTo(result.get(), "on_two", "on_two2"));
115 }
116
50 TEST(Scope, GetMutableValue) { 117 TEST(Scope, GetMutableValue) {
51 TestWithScope setup; 118 TestWithScope setup;
52 119
53 // Make a pretend parse node with proper tracking that we can blame for the 120 // Make a pretend parse node with proper tracking that we can blame for the
54 // given value. 121 // given value.
55 InputFile input_file(SourceFile("//foo")); 122 InputFile input_file(SourceFile("//foo"));
56 Token assignment_token(Location(&input_file, 1, 1), Token::STRING, 123 Token assignment_token(Location(&input_file, 1, 1), Token::STRING,
57 "\"hello\""); 124 "\"hello\"");
58 LiteralNode assignment; 125 LiteralNode assignment;
59 assignment.set_value(assignment_token); 126 assignment.set_value(assignment_token);
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
93 mutable1_result = mutable_scope2.GetMutableValue(kOnMutable1, true); 160 mutable1_result = mutable_scope2.GetMutableValue(kOnMutable1, true);
94 EXPECT_TRUE(mutable1_result); 161 EXPECT_TRUE(mutable1_result);
95 err = Err(); 162 err = Err();
96 EXPECT_TRUE(mutable_scope1.CheckForUnusedVars(&err)); 163 EXPECT_TRUE(mutable_scope1.CheckForUnusedVars(&err));
97 164
98 // Test reading a value from scope 2. 165 // Test reading a value from scope 2.
99 Value* mutable2_result = mutable_scope2.GetMutableValue(kOnMutable2, true); 166 Value* mutable2_result = mutable_scope2.GetMutableValue(kOnMutable2, true);
100 ASSERT_TRUE(mutable2_result); 167 ASSERT_TRUE(mutable2_result);
101 EXPECT_TRUE(*mutable2_result == value); 168 EXPECT_TRUE(*mutable2_result == value);
102 } 169 }
OLDNEW
« no previous file with comments | « tools/gn/scope.cc ('k') | tools/gn/template.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698