OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
97 | 97 |
98 Err err; | 98 Err err; |
99 input.parsed()->Execute(setup.scope(), &err); | 99 input.parsed()->Execute(setup.scope(), &err); |
100 EXPECT_TRUE(err.has_error()); | 100 EXPECT_TRUE(err.has_error()); |
101 | 101 |
102 // The origin for the "not a string" error message should be where the value | 102 // The origin for the "not a string" error message should be where the value |
103 // was dereferenced (the "a" on the second line). | 103 // was dereferenced (the "a" on the second line). |
104 EXPECT_EQ(2, err.location().line_number()); | 104 EXPECT_EQ(2, err.location().line_number()); |
105 EXPECT_EQ(20, err.location().char_offset()); | 105 EXPECT_EQ(20, err.location().char_offset()); |
106 } | 106 } |
| 107 |
| 108 TEST(ParseTree, SortRangeExtraction) { |
| 109 TestWithScope setup; |
| 110 |
| 111 // Ranges are [begin, end). |
| 112 |
| 113 { |
| 114 TestParseInput input( |
| 115 "sources = [\n" |
| 116 " \"a\",\n" |
| 117 " \"b\",\n" |
| 118 " \n" |
| 119 " #\n" |
| 120 " # Block\n" |
| 121 " #\n" |
| 122 " \n" |
| 123 " \"c\"," |
| 124 " \"d\"," |
| 125 "]\n"); |
| 126 EXPECT_FALSE(input.has_error()); |
| 127 ASSERT_TRUE(input.parsed()->AsBlock()); |
| 128 ASSERT_TRUE(input.parsed()->AsBlock()->statements()[0]->AsBinaryOp()); |
| 129 const BinaryOpNode* binop = |
| 130 input.parsed()->AsBlock()->statements()[0]->AsBinaryOp(); |
| 131 ASSERT_TRUE(binop->right()->AsList()); |
| 132 const ListNode* list = binop->right()->AsList(); |
| 133 EXPECT_EQ(5u, list->contents().size()); |
| 134 auto ranges = list->GetSortRanges(); |
| 135 ASSERT_EQ(2u, ranges.size()); |
| 136 EXPECT_EQ(0u, ranges[0].begin); |
| 137 EXPECT_EQ(2u, ranges[0].end); |
| 138 EXPECT_EQ(3u, ranges[1].begin); |
| 139 EXPECT_EQ(5u, ranges[1].end); |
| 140 } |
| 141 |
| 142 { |
| 143 TestParseInput input( |
| 144 "sources = [\n" |
| 145 " \"a\",\n" |
| 146 " \"b\",\n" |
| 147 " \n" |
| 148 " # Attached comment.\n" |
| 149 " \"c\"," |
| 150 " \"d\"," |
| 151 "]\n"); |
| 152 EXPECT_FALSE(input.has_error()); |
| 153 ASSERT_TRUE(input.parsed()->AsBlock()); |
| 154 ASSERT_TRUE(input.parsed()->AsBlock()->statements()[0]->AsBinaryOp()); |
| 155 const BinaryOpNode* binop = |
| 156 input.parsed()->AsBlock()->statements()[0]->AsBinaryOp(); |
| 157 ASSERT_TRUE(binop->right()->AsList()); |
| 158 const ListNode* list = binop->right()->AsList(); |
| 159 EXPECT_EQ(4u, list->contents().size()); |
| 160 auto ranges = list->GetSortRanges(); |
| 161 ASSERT_EQ(2u, ranges.size()); |
| 162 EXPECT_EQ(0u, ranges[0].begin); |
| 163 EXPECT_EQ(2u, ranges[0].end); |
| 164 EXPECT_EQ(2u, ranges[1].begin); |
| 165 EXPECT_EQ(4u, ranges[1].end); |
| 166 } |
| 167 |
| 168 { |
| 169 TestParseInput input( |
| 170 "sources = [\n" |
| 171 " # At end of list.\n" |
| 172 " \"zzzzzzzzzzz.cc\"," |
| 173 "]\n"); |
| 174 EXPECT_FALSE(input.has_error()); |
| 175 ASSERT_TRUE(input.parsed()->AsBlock()); |
| 176 ASSERT_TRUE(input.parsed()->AsBlock()->statements()[0]->AsBinaryOp()); |
| 177 const BinaryOpNode* binop = |
| 178 input.parsed()->AsBlock()->statements()[0]->AsBinaryOp(); |
| 179 ASSERT_TRUE(binop->right()->AsList()); |
| 180 const ListNode* list = binop->right()->AsList(); |
| 181 EXPECT_EQ(1u, list->contents().size()); |
| 182 auto ranges = list->GetSortRanges(); |
| 183 ASSERT_EQ(1u, ranges.size()); |
| 184 EXPECT_EQ(0u, ranges[0].begin); |
| 185 EXPECT_EQ(1u, ranges[0].end); |
| 186 } |
| 187 |
| 188 { |
| 189 TestParseInput input( |
| 190 "sources = [\n" |
| 191 " # Block at start.\n" |
| 192 " \n" |
| 193 " \"z.cc\"," |
| 194 " \"y.cc\"," |
| 195 "]\n"); |
| 196 EXPECT_FALSE(input.has_error()); |
| 197 ASSERT_TRUE(input.parsed()->AsBlock()); |
| 198 ASSERT_TRUE(input.parsed()->AsBlock()->statements()[0]->AsBinaryOp()); |
| 199 const BinaryOpNode* binop = |
| 200 input.parsed()->AsBlock()->statements()[0]->AsBinaryOp(); |
| 201 ASSERT_TRUE(binop->right()->AsList()); |
| 202 const ListNode* list = binop->right()->AsList(); |
| 203 EXPECT_EQ(3u, list->contents().size()); |
| 204 auto ranges = list->GetSortRanges(); |
| 205 ASSERT_EQ(1u, ranges.size()); |
| 206 EXPECT_EQ(1u, ranges[0].begin); |
| 207 EXPECT_EQ(3u, ranges[0].end); |
| 208 } |
| 209 } |
OLD | NEW |