| 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)); |
| 84 EXPECT_FALSE(err.has_error()); |
| 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.skip_private_vars = true; |
| 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 } |
| 110 |
| 111 // Don't mark used. |
| 112 { |
| 113 Scope new_scope(setup.settings()); |
| 114 |
| 115 Err err; |
| 116 Scope::MergeOptions options; |
| 117 EXPECT_TRUE(setup.scope()->NonRecursiveMergeTo( |
| 118 &new_scope, options, &assignment, "error", &err)); |
| 119 EXPECT_FALSE(err.has_error()); |
| 120 EXPECT_FALSE(new_scope.CheckForUnusedVars(&err)); |
| 121 EXPECT_TRUE(err.has_error()); |
| 122 } |
| 123 |
| 124 // Mark used. |
| 125 { |
| 126 Scope new_scope(setup.settings()); |
| 127 |
| 128 Err err; |
| 129 Scope::MergeOptions options; |
| 130 options.mark_used = true; |
| 131 EXPECT_TRUE(setup.scope()->NonRecursiveMergeTo( |
| 132 &new_scope, options, &assignment, "error", &err)); |
| 133 EXPECT_FALSE(err.has_error()); |
| 134 EXPECT_TRUE(new_scope.CheckForUnusedVars(&err)); |
| 77 EXPECT_FALSE(err.has_error()); | 135 EXPECT_FALSE(err.has_error()); |
| 78 } | 136 } |
| 79 } | 137 } |
| 80 | 138 |
| 81 TEST(Scope, MakeClosure) { | 139 TEST(Scope, MakeClosure) { |
| 82 // Create 3 nested scopes [const root from setup] <- nested1 <- nested2. | 140 // Create 3 nested scopes [const root from setup] <- nested1 <- nested2. |
| 83 TestWithScope setup; | 141 TestWithScope setup; |
| 84 | 142 |
| 85 // Make a pretend parse node with proper tracking that we can blame for the | 143 // Make a pretend parse node with proper tracking that we can blame for the |
| 86 // given value. | 144 // given value. |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 160 mutable1_result = mutable_scope2.GetMutableValue(kOnMutable1, true); | 218 mutable1_result = mutable_scope2.GetMutableValue(kOnMutable1, true); |
| 161 EXPECT_TRUE(mutable1_result); | 219 EXPECT_TRUE(mutable1_result); |
| 162 err = Err(); | 220 err = Err(); |
| 163 EXPECT_TRUE(mutable_scope1.CheckForUnusedVars(&err)); | 221 EXPECT_TRUE(mutable_scope1.CheckForUnusedVars(&err)); |
| 164 | 222 |
| 165 // Test reading a value from scope 2. | 223 // Test reading a value from scope 2. |
| 166 Value* mutable2_result = mutable_scope2.GetMutableValue(kOnMutable2, true); | 224 Value* mutable2_result = mutable_scope2.GetMutableValue(kOnMutable2, true); |
| 167 ASSERT_TRUE(mutable2_result); | 225 ASSERT_TRUE(mutable2_result); |
| 168 EXPECT_TRUE(*mutable2_result == value); | 226 EXPECT_TRUE(*mutable2_result == value); |
| 169 } | 227 } |
| 228 |
| 229 TEST(Scope, RemovePrivateIdentifiers) { |
| 230 TestWithScope setup; |
| 231 setup.scope()->SetValue("a", Value(NULL, true), NULL); |
| 232 setup.scope()->SetValue("_b", Value(NULL, true), NULL); |
| 233 |
| 234 setup.scope()->RemovePrivateIdentifiers(); |
| 235 EXPECT_TRUE(setup.scope()->GetValue("a")); |
| 236 EXPECT_FALSE(setup.scope()->GetValue("_b")); |
| 237 } |
| OLD | NEW |