| 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 <iostream> | 5 #include <iostream> |
| 6 #include <sstream> | 6 #include <sstream> |
| 7 | 7 |
| 8 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
| 9 #include "tools/gn/input_file.h" | 9 #include "tools/gn/input_file.h" |
| 10 #include "tools/gn/parser.h" | 10 #include "tools/gn/parser.h" |
| (...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 239 // Member accessors. | 239 // Member accessors. |
| 240 DoParserPrintTest("a=b.c+2", | 240 DoParserPrintTest("a=b.c+2", |
| 241 "BLOCK\n" | 241 "BLOCK\n" |
| 242 " BINARY(=)\n" | 242 " BINARY(=)\n" |
| 243 " IDENTIFIER(a)\n" | 243 " IDENTIFIER(a)\n" |
| 244 " BINARY(+)\n" | 244 " BINARY(+)\n" |
| 245 " ACCESSOR\n" | 245 " ACCESSOR\n" |
| 246 " b\n" | 246 " b\n" |
| 247 " IDENTIFIER(c)\n" | 247 " IDENTIFIER(c)\n" |
| 248 " LITERAL(2)\n"); | 248 " LITERAL(2)\n"); |
| 249 DoParserPrintTest("a.b = 5", |
| 250 "BLOCK\n" |
| 251 " BINARY(=)\n" |
| 252 " ACCESSOR\n" |
| 253 " a\n" |
| 254 " IDENTIFIER(b)\n" |
| 255 " LITERAL(5)\n"); |
| 249 DoParserErrorTest("a = b.c.d", 1, 6); // Can't nest accessors (currently). | 256 DoParserErrorTest("a = b.c.d", 1, 6); // Can't nest accessors (currently). |
| 250 DoParserErrorTest("a.b = 5", 1, 1); // Can't assign to accessors (currently). | |
| 251 | 257 |
| 252 // Error at the bad dot in the RHS, not the + operator (crbug.com/472038). | 258 // Error at the bad dot in the RHS, not the + operator (crbug.com/472038). |
| 253 DoParserErrorTest("foo(a + b.c.d)", 1, 10); | 259 DoParserErrorTest("foo(a + b.c.d)", 1, 10); |
| 254 } | 260 } |
| 255 | 261 |
| 256 TEST(Parser, Condition) { | 262 TEST(Parser, Condition) { |
| 257 DoParserPrintTest("if(1) { a = 2 }", | 263 DoParserPrintTest("if(1) { a = 2 }", |
| 258 "BLOCK\n" | 264 "BLOCK\n" |
| 259 " CONDITION\n" | 265 " CONDITION\n" |
| 260 " LITERAL(1)\n" | 266 " LITERAL(1)\n" |
| (...skipping 433 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 694 " foreach(bar, []) {}\n", | 700 " foreach(bar, []) {}\n", |
| 695 4, 3); | 701 4, 3); |
| 696 } | 702 } |
| 697 | 703 |
| 698 // Disallow standalone {} for introducing new scopes. These are ambiguous with | 704 // Disallow standalone {} for introducing new scopes. These are ambiguous with |
| 699 // target declarations (e.g. is: | 705 // target declarations (e.g. is: |
| 700 // foo("bar") {} | 706 // foo("bar") {} |
| 701 // a function with an associated block, or a standalone function with a | 707 // a function with an associated block, or a standalone function with a |
| 702 // freestanding block. | 708 // freestanding block. |
| 703 TEST(Parser, StandaloneBlock) { | 709 TEST(Parser, StandaloneBlock) { |
| 710 // The error is reported at the end of the block when nothing is done |
| 711 // with it. If we had said "a = { ..." then it would have been OK. |
| 704 DoParserErrorTest( | 712 DoParserErrorTest( |
| 705 "if (true) {\n" | 713 "if (true) {\n" |
| 706 "}\n" | 714 "}\n" |
| 707 "{\n" | 715 "{\n" |
| 708 " assert(false)\n" | 716 " assert(false)\n" |
| 709 "}\n", | 717 "}\n", |
| 710 3, 1); | 718 5, 1); |
| 711 } | 719 } |
| 720 |
| 721 TEST(Parser, BlockValues) { |
| 722 const char* input = |
| 723 "print({a = 1 b = 2}, 3)\n" |
| 724 "a = { b = \"asd\" }"; |
| 725 const char* expected = |
| 726 "BLOCK\n" |
| 727 " FUNCTION(print)\n" |
| 728 " LIST\n" |
| 729 " BLOCK\n" |
| 730 " BINARY(=)\n" |
| 731 " IDENTIFIER(a)\n" |
| 732 " LITERAL(1)\n" |
| 733 " BINARY(=)\n" |
| 734 " IDENTIFIER(b)\n" |
| 735 " LITERAL(2)\n" |
| 736 " LITERAL(3)\n" |
| 737 " BINARY(=)\n" |
| 738 " IDENTIFIER(a)\n" |
| 739 " BLOCK\n" |
| 740 " BINARY(=)\n" |
| 741 " IDENTIFIER(b)\n" |
| 742 " LITERAL(\"asd\")\n"; |
| 743 DoParserPrintTest(input, expected); |
| 744 } |
| OLD | NEW |