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 "tools/gn/operators.h" |
| 6 |
5 #include <stdint.h> | 7 #include <stdint.h> |
| 8 #include <utility> |
6 | 9 |
7 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
8 #include "tools/gn/operators.h" | |
9 #include "tools/gn/parse_tree.h" | 11 #include "tools/gn/parse_tree.h" |
10 #include "tools/gn/pattern.h" | 12 #include "tools/gn/pattern.h" |
11 #include "tools/gn/test_with_scope.h" | 13 #include "tools/gn/test_with_scope.h" |
12 | 14 |
13 namespace { | 15 namespace { |
14 | 16 |
15 bool IsValueIntegerEqualing(const Value& v, int64_t i) { | 17 bool IsValueIntegerEqualing(const Value& v, int64_t i) { |
16 if (v.type() != Value::INTEGER) | 18 if (v.type() != Value::INTEGER) |
17 return false; | 19 return false; |
18 return v.int_value() == i; | 20 return v.int_value() == i; |
19 } | 21 } |
20 | 22 |
21 bool IsValueStringEqualing(const Value& v, const char* s) { | 23 bool IsValueStringEqualing(const Value& v, const char* s) { |
22 if (v.type() != Value::STRING) | 24 if (v.type() != Value::STRING) |
23 return false; | 25 return false; |
24 return v.string_value() == s; | 26 return v.string_value() == s; |
25 } | 27 } |
26 | 28 |
27 // Returns a list populated with a single literal Value corresponding to the | 29 // Returns a list populated with a single literal Value corresponding to the |
28 // given token. The token must outlive the list (since the list will just | 30 // given token. The token must outlive the list (since the list will just |
29 // copy the reference). | 31 // copy the reference). |
30 scoped_ptr<ListNode> ListWithLiteral(const Token& token) { | 32 scoped_ptr<ListNode> ListWithLiteral(const Token& token) { |
31 scoped_ptr<ListNode> list(new ListNode); | 33 scoped_ptr<ListNode> list(new ListNode); |
32 list->append_item(scoped_ptr<ParseNode>(new LiteralNode(token))); | 34 list->append_item(scoped_ptr<ParseNode>(new LiteralNode(token))); |
33 return list.Pass(); | 35 return list; |
34 } | 36 } |
35 | 37 |
36 } // namespace | 38 } // namespace |
37 | 39 |
38 TEST(Operators, SourcesAppend) { | 40 TEST(Operators, SourcesAppend) { |
39 Err err; | 41 Err err; |
40 TestWithScope setup; | 42 TestWithScope setup; |
41 | 43 |
42 // Set up "sources" with an empty list. | 44 // Set up "sources" with an empty list. |
43 const char sources[] = "sources"; | 45 const char sources[] = "sources"; |
44 setup.scope()->SetValue(sources, Value(nullptr, Value::LIST), nullptr); | 46 setup.scope()->SetValue(sources, Value(nullptr, Value::LIST), nullptr); |
45 | 47 |
46 // Set up the operator. | 48 // Set up the operator. |
47 BinaryOpNode node; | 49 BinaryOpNode node; |
48 const char token_value[] = "+="; | 50 const char token_value[] = "+="; |
49 Token op(Location(), Token::PLUS_EQUALS, token_value); | 51 Token op(Location(), Token::PLUS_EQUALS, token_value); |
50 node.set_op(op); | 52 node.set_op(op); |
51 | 53 |
52 // Append to the sources variable. | 54 // Append to the sources variable. |
53 Token identifier_token(Location(), Token::IDENTIFIER, sources); | 55 Token identifier_token(Location(), Token::IDENTIFIER, sources); |
54 node.set_left(scoped_ptr<ParseNode>(new IdentifierNode(identifier_token))); | 56 node.set_left(scoped_ptr<ParseNode>(new IdentifierNode(identifier_token))); |
55 | 57 |
56 // Set up the filter on the scope to remove everything ending with "rm" | 58 // Set up the filter on the scope to remove everything ending with "rm" |
57 scoped_ptr<PatternList> pattern_list(new PatternList); | 59 scoped_ptr<PatternList> pattern_list(new PatternList); |
58 pattern_list->Append(Pattern("*rm")); | 60 pattern_list->Append(Pattern("*rm")); |
59 setup.scope()->set_sources_assignment_filter(pattern_list.Pass()); | 61 setup.scope()->set_sources_assignment_filter(std::move(pattern_list)); |
60 | 62 |
61 // Append an integer. | 63 // Append an integer. |
62 const char integer_value[] = "5"; | 64 const char integer_value[] = "5"; |
63 Token integer(Location(), Token::INTEGER, integer_value); | 65 Token integer(Location(), Token::INTEGER, integer_value); |
64 node.set_right(ListWithLiteral(integer)); | 66 node.set_right(ListWithLiteral(integer)); |
65 node.Execute(setup.scope(), &err); | 67 node.Execute(setup.scope(), &err); |
66 EXPECT_FALSE(err.has_error()); | 68 EXPECT_FALSE(err.has_error()); |
67 | 69 |
68 // Append a string that doesn't match the pattern, it should get appended. | 70 // Append a string that doesn't match the pattern, it should get appended. |
69 const char string_1_value[] = "\"good\""; | 71 const char string_1_value[] = "\"good\""; |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
114 | 116 |
115 // Append to the foo variable. | 117 // Append to the foo variable. |
116 Token identifier_token(Location(), Token::IDENTIFIER, foo); | 118 Token identifier_token(Location(), Token::IDENTIFIER, foo); |
117 node.set_left(scoped_ptr<ParseNode>(new IdentifierNode(identifier_token))); | 119 node.set_left(scoped_ptr<ParseNode>(new IdentifierNode(identifier_token))); |
118 | 120 |
119 // Append a list with a list, the result should be a nested list. | 121 // Append a list with a list, the result should be a nested list. |
120 scoped_ptr<ListNode> outer_list(new ListNode); | 122 scoped_ptr<ListNode> outer_list(new ListNode); |
121 const char twelve_str[] = "12"; | 123 const char twelve_str[] = "12"; |
122 Token twelve(Location(), Token::INTEGER, twelve_str); | 124 Token twelve(Location(), Token::INTEGER, twelve_str); |
123 outer_list->append_item(ListWithLiteral(twelve)); | 125 outer_list->append_item(ListWithLiteral(twelve)); |
124 node.set_right(outer_list.Pass()); | 126 node.set_right(std::move(outer_list)); |
125 | 127 |
126 Value ret = ExecuteBinaryOperator(setup.scope(), &node, node.left(), | 128 Value ret = ExecuteBinaryOperator(setup.scope(), &node, node.left(), |
127 node.right(), &err); | 129 node.right(), &err); |
128 EXPECT_FALSE(err.has_error()); | 130 EXPECT_FALSE(err.has_error()); |
129 | 131 |
130 // Return from the operator should always be "none", it should update the | 132 // Return from the operator should always be "none", it should update the |
131 // value only. | 133 // value only. |
132 EXPECT_EQ(Value::NONE, ret.type()); | 134 EXPECT_EQ(Value::NONE, ret.type()); |
133 | 135 |
134 // The value should be updated with "[ [ 12 ] ]" | 136 // The value should be updated with "[ [ 12 ] ]" |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
196 | 198 |
197 // Set right as foo, but don't define a value for it. | 199 // Set right as foo, but don't define a value for it. |
198 const char foo[] = "foo"; | 200 const char foo[] = "foo"; |
199 Token identifier_token(Location(), Token::IDENTIFIER, foo); | 201 Token identifier_token(Location(), Token::IDENTIFIER, foo); |
200 node.set_right(scoped_ptr<ParseNode>(new IdentifierNode(identifier_token))); | 202 node.set_right(scoped_ptr<ParseNode>(new IdentifierNode(identifier_token))); |
201 | 203 |
202 Value ret = ExecuteBinaryOperator(setup.scope(), &node, node.left(), | 204 Value ret = ExecuteBinaryOperator(setup.scope(), &node, node.left(), |
203 node.right(), &err); | 205 node.right(), &err); |
204 EXPECT_FALSE(err.has_error()); | 206 EXPECT_FALSE(err.has_error()); |
205 } | 207 } |
OLD | NEW |