OLD | NEW |
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 |
(...skipping 18 matching lines...) Expand all Loading... |
29 // 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 |
30 // given value. | 30 // given value. |
31 InputFile input_file(SourceFile("//foo")); | 31 InputFile input_file(SourceFile("//foo")); |
32 Token assignment_token(Location(&input_file, 1, 1), Token::STRING, | 32 Token assignment_token(Location(&input_file, 1, 1), Token::STRING, |
33 "\"hello\""); | 33 "\"hello\""); |
34 LiteralNode assignment; | 34 LiteralNode assignment; |
35 assignment.set_value(assignment_token); | 35 assignment.set_value(assignment_token); |
36 | 36 |
37 Value old_value(&assignment, "hello"); | 37 Value old_value(&assignment, "hello"); |
38 setup.scope()->SetValue("v", old_value, &assignment); | 38 setup.scope()->SetValue("v", old_value, &assignment); |
| 39 base::StringPiece private_var_name("_private"); |
| 40 setup.scope()->SetValue(private_var_name, old_value, &assignment); |
39 | 41 |
40 // Detect collisions of values' values. | 42 // Detect collisions of values' values. |
41 { | 43 { |
42 Scope new_scope(setup.settings()); | 44 Scope new_scope(setup.settings()); |
43 Value new_value(&assignment, "goodbye"); | 45 Value new_value(&assignment, "goodbye"); |
44 new_scope.SetValue("v", new_value, &assignment); | 46 new_scope.SetValue("v", new_value, &assignment); |
45 | 47 |
46 Err err; | 48 Err err; |
47 EXPECT_FALSE(setup.scope()->NonRecursiveMergeTo( | 49 EXPECT_FALSE(setup.scope()->NonRecursiveMergeTo( |
48 &new_scope, false, &assignment, "error", &err)); | 50 &new_scope, Scope::MergeOptions(), |
| 51 &assignment, "error", &err)); |
49 EXPECT_TRUE(err.has_error()); | 52 EXPECT_TRUE(err.has_error()); |
50 } | 53 } |
51 | 54 |
52 // The clobber flag should just overwrite colliding values. | 55 // The clobber flag should just overwrite colliding values. |
53 { | 56 { |
54 Scope new_scope(setup.settings()); | 57 Scope new_scope(setup.settings()); |
55 Value new_value(&assignment, "goodbye"); | 58 Value new_value(&assignment, "goodbye"); |
56 new_scope.SetValue("v", new_value, &assignment); | 59 new_scope.SetValue("v", new_value, &assignment); |
57 | 60 |
58 Err err; | 61 Err err; |
| 62 Scope::MergeOptions options; |
| 63 options.clobber_existing = true; |
59 EXPECT_TRUE(setup.scope()->NonRecursiveMergeTo( | 64 EXPECT_TRUE(setup.scope()->NonRecursiveMergeTo( |
60 &new_scope, true, &assignment, "error", &err)); | 65 &new_scope, options, &assignment, "error", &err)); |
61 EXPECT_FALSE(err.has_error()); | 66 EXPECT_FALSE(err.has_error()); |
62 | 67 |
63 const Value* found_value = new_scope.GetValue("v"); | 68 const Value* found_value = new_scope.GetValue("v"); |
64 ASSERT_TRUE(found_value); | 69 ASSERT_TRUE(found_value); |
65 EXPECT_TRUE(old_value == *found_value); | 70 EXPECT_TRUE(old_value == *found_value); |
66 } | 71 } |
67 | 72 |
68 // Don't flag values that technically collide but have the same value. | 73 // Don't flag values that technically collide but have the same value. |
69 { | 74 { |
70 Scope new_scope(setup.settings()); | 75 Scope new_scope(setup.settings()); |
71 Value new_value(&assignment, "hello"); | 76 Value new_value(&assignment, "hello"); |
72 new_scope.SetValue("v", new_value, &assignment); | 77 new_scope.SetValue("v", new_value, &assignment); |
73 | 78 |
74 Err err; | 79 Err err; |
| 80 Scope::MergeOptions options; |
| 81 options.clobber_existing = true; |
75 EXPECT_TRUE(setup.scope()->NonRecursiveMergeTo( | 82 EXPECT_TRUE(setup.scope()->NonRecursiveMergeTo( |
76 &new_scope, false, &assignment, "error", &err)); | 83 &new_scope, options, &assignment, "error", &err)); |
77 EXPECT_FALSE(err.has_error()); | 84 EXPECT_FALSE(err.has_error()); |
78 } | 85 } |
| 86 |
| 87 // Copy private values. |
| 88 { |
| 89 Scope new_scope(setup.settings()); |
| 90 |
| 91 Err err; |
| 92 EXPECT_TRUE(setup.scope()->NonRecursiveMergeTo( |
| 93 &new_scope, Scope::MergeOptions(), &assignment, "error", &err)); |
| 94 EXPECT_FALSE(err.has_error()); |
| 95 EXPECT_TRUE(new_scope.GetValue(private_var_name)); |
| 96 } |
| 97 |
| 98 // Skip private values. |
| 99 { |
| 100 Scope new_scope(setup.settings()); |
| 101 |
| 102 Err err; |
| 103 Scope::MergeOptions options; |
| 104 options.copy_private_vars = false; |
| 105 EXPECT_TRUE(setup.scope()->NonRecursiveMergeTo( |
| 106 &new_scope, options, &assignment, "error", &err)); |
| 107 EXPECT_FALSE(err.has_error()); |
| 108 EXPECT_FALSE(new_scope.GetValue(private_var_name)); |
| 109 } |
79 } | 110 } |
80 | 111 |
81 TEST(Scope, MakeClosure) { | 112 TEST(Scope, MakeClosure) { |
82 // Create 3 nested scopes [const root from setup] <- nested1 <- nested2. | 113 // Create 3 nested scopes [const root from setup] <- nested1 <- nested2. |
83 TestWithScope setup; | 114 TestWithScope setup; |
84 | 115 |
85 // Make a pretend parse node with proper tracking that we can blame for the | 116 // Make a pretend parse node with proper tracking that we can blame for the |
86 // given value. | 117 // given value. |
87 InputFile input_file(SourceFile("//foo")); | 118 InputFile input_file(SourceFile("//foo")); |
88 Token assignment_token(Location(&input_file, 1, 1), Token::STRING, | 119 Token assignment_token(Location(&input_file, 1, 1), Token::STRING, |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
160 mutable1_result = mutable_scope2.GetMutableValue(kOnMutable1, true); | 191 mutable1_result = mutable_scope2.GetMutableValue(kOnMutable1, true); |
161 EXPECT_TRUE(mutable1_result); | 192 EXPECT_TRUE(mutable1_result); |
162 err = Err(); | 193 err = Err(); |
163 EXPECT_TRUE(mutable_scope1.CheckForUnusedVars(&err)); | 194 EXPECT_TRUE(mutable_scope1.CheckForUnusedVars(&err)); |
164 | 195 |
165 // Test reading a value from scope 2. | 196 // Test reading a value from scope 2. |
166 Value* mutable2_result = mutable_scope2.GetMutableValue(kOnMutable2, true); | 197 Value* mutable2_result = mutable_scope2.GetMutableValue(kOnMutable2, true); |
167 ASSERT_TRUE(mutable2_result); | 198 ASSERT_TRUE(mutable2_result); |
168 EXPECT_TRUE(*mutable2_result == value); | 199 EXPECT_TRUE(*mutable2_result == value); |
169 } | 200 } |
OLD | NEW |