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
3 // found in the LICENSE file.
8 #include "testing/gtest/include/gtest/gtest.h"
9 #include "tools/gn/input_file.h"
10 #include "tools/gn/parser.h"
11 #include "tools/gn/tokenizer.h"
15 bool GetTokens(const InputFile
* input
, std::vector
<Token
>* result
) {
18 *result
= Tokenizer::Tokenize(input
, &err
);
19 return !err
.has_error();
22 void DoParserPrintTest(const char* input
, const char* expected
) {
23 std::vector
<Token
> tokens
;
24 InputFile
input_file(SourceFile("/test"));
25 input_file
.SetContents(input
);
26 ASSERT_TRUE(GetTokens(&input_file
, &tokens
));
29 scoped_ptr
<ParseNode
> result
= Parser::Parse(tokens
, &err
);
34 std::ostringstream collector
;
35 result
->Print(collector
, 0);
37 EXPECT_EQ(expected
, collector
.str());
40 void DoExpressionPrintTest(const char* input
, const char* expected
) {
41 std::vector
<Token
> tokens
;
42 InputFile
input_file(SourceFile("/test"));
43 input_file
.SetContents(input
);
44 ASSERT_TRUE(GetTokens(&input_file
, &tokens
));
47 scoped_ptr
<ParseNode
> result
= Parser::ParseExpression(tokens
, &err
);
50 std::ostringstream collector
;
51 result
->Print(collector
, 0);
53 EXPECT_EQ(expected
, collector
.str());
56 // Expects the tokenizer or parser to identify an error at the given line and
58 void DoParserErrorTest(const char* input
, int err_line
, int err_char
) {
59 InputFile
input_file(SourceFile("/test"));
60 input_file
.SetContents(input
);
63 std::vector
<Token
> tokens
= Tokenizer::Tokenize(&input_file
, &err
);
64 if (!err
.has_error()) {
65 scoped_ptr
<ParseNode
> result
= Parser::Parse(tokens
, &err
);
67 ASSERT_TRUE(err
.has_error());
70 EXPECT_EQ(err_line
, err
.location().line_number());
71 EXPECT_EQ(err_char
, err
.location().char_offset());
74 // Expects the tokenizer or parser to identify an error at the given line and
76 void DoExpressionErrorTest(const char* input
, int err_line
, int err_char
) {
77 InputFile
input_file(SourceFile("/test"));
78 input_file
.SetContents(input
);
81 std::vector
<Token
> tokens
= Tokenizer::Tokenize(&input_file
, &err
);
82 if (!err
.has_error()) {
83 scoped_ptr
<ParseNode
> result
= Parser::ParseExpression(tokens
, &err
);
85 ASSERT_TRUE(err
.has_error());
88 EXPECT_EQ(err_line
, err
.location().line_number());
89 EXPECT_EQ(err_char
, err
.location().char_offset());
94 TEST(Parser
, Literal
) {
95 DoExpressionPrintTest("5", "LITERAL(5)\n");
96 DoExpressionPrintTest("\"stuff\"", "LITERAL(\"stuff\")\n");
99 TEST(Parser
, BinaryOp
) {
100 // TODO(scottmg): The tokenizer is dumb, and treats "5-1" as two integers,
101 // not a binary operator between two positive integers.
102 DoExpressionPrintTest("5 - 1",
106 DoExpressionPrintTest("5+1",
110 DoExpressionPrintTest("5 - 1 - 2",
118 TEST(Parser
, FunctionCall
) {
119 DoExpressionPrintTest("foo()",
122 DoExpressionPrintTest("blah(1, 2)",
127 DoExpressionErrorTest("foo(1, 2,)", 1, 10);
128 DoExpressionErrorTest("foo(1 2)", 1, 7);
131 TEST(Parser
, ParenExpression
) {
132 const char* input
= "(foo(1)) + (a + (b - c) + d)";
133 const char* expected
=
145 DoExpressionPrintTest(input
, expected
);
146 DoExpressionErrorTest("(a +", 1, 4);
149 TEST(Parser
, OrderOfOperationsLeftAssociative
) {
150 const char* input
= "5 - 1 - 2\n";
151 const char* expected
=
157 DoExpressionPrintTest(input
, expected
);
160 TEST(Parser
, OrderOfOperationsEqualityBoolean
) {
162 "if (a == \"b\" && is_stuff) {\n"
165 const char* expected
=
172 " IDENTIFIER(is_stuff)\n"
176 " LITERAL(\"hai\")\n";
177 DoParserPrintTest(input
, expected
);
180 TEST(Parser
, UnaryOp
) {
181 DoExpressionPrintTest("!foo",
183 " IDENTIFIER(foo)\n");
187 DoExpressionPrintTest("[]", "LIST\n");
188 DoExpressionPrintTest("[1,asd,]",
191 " IDENTIFIER(asd)\n");
192 DoExpressionPrintTest("[1, 2+3 - foo]",
199 " IDENTIFIER(foo)\n");
200 DoExpressionPrintTest("[1,\n2,\n 3,\n 4]",
207 DoExpressionErrorTest("[a, 2+,]", 1, 7);
208 DoExpressionErrorTest("[,]", 1, 2);
209 DoExpressionErrorTest("[a,,]", 1, 4);
212 TEST(Parser
, Assignment
) {
213 DoParserPrintTest("a=2",
220 TEST(Parser
, Accessor
) {
221 // Accessor indexing.
222 DoParserPrintTest("a=b[c+2]",
227 " b\n" // AccessorNode is a bit weird in that it holds
228 // a Token, not a ParseNode for the base.
232 DoParserErrorTest("a = b[1][0]", 1, 5);
235 DoParserPrintTest("a=b.c+2",
244 DoParserErrorTest("a = b.c.d", 1, 6); // Can't nest accessors (currently).
245 DoParserErrorTest("a.b = 5", 1, 1); // Can't assign to accessors (currently).
247 // Error at the bad dot in the RHS, not the + operator (crbug.com/472038).
248 DoParserErrorTest("foo(a + b.c.d)", 1, 10);
251 TEST(Parser
, Condition
) {
252 DoParserPrintTest("if(1) { a = 2 }",
261 DoParserPrintTest("if(1) { a = 2 } else if (0) { a = 3 } else { a = 4 }",
281 TEST(Parser
, OnlyCallAndAssignInBody
) {
282 DoParserErrorTest("[]", 1, 2);
283 DoParserErrorTest("3 + 4", 1, 5);
284 DoParserErrorTest("6 - 7", 1, 5);
285 DoParserErrorTest("if (1) { 5 } else { print(4) }", 1, 12);
288 TEST(Parser
, NoAssignmentInCondition
) {
289 DoParserErrorTest("if (a=2) {}", 1, 5);
292 TEST(Parser
, CompleteFunction
) {
294 "cc_test(\"foo\") {\n"
299 " dependencies = [\n"
303 const char* expected
=
305 " FUNCTION(cc_test)\n"
307 " LITERAL(\"foo\")\n"
310 " IDENTIFIER(sources)\n"
312 " LITERAL(\"foo.cc\")\n"
313 " LITERAL(\"foo.h\")\n"
315 " IDENTIFIER(dependencies)\n"
317 " LITERAL(\"base\")\n";
318 DoParserPrintTest(input
, expected
);
321 TEST(Parser
, FunctionWithConditional
) {
323 "cc_test(\"foo\") {\n"
324 " sources = [\"foo.cc\"]\n"
325 " if (OS == \"mac\") {\n"
326 " sources += \"bar.cc\"\n"
327 " } else if (OS == \"win\") {\n"
328 " sources -= [\"asd.cc\", \"foo.cc\"]\n"
330 " dependencies += [\"bar.cc\"]\n"
333 const char* expected
=
335 " FUNCTION(cc_test)\n"
337 " LITERAL(\"foo\")\n"
340 " IDENTIFIER(sources)\n"
342 " LITERAL(\"foo.cc\")\n"
346 " LITERAL(\"mac\")\n"
349 " IDENTIFIER(sources)\n"
350 " LITERAL(\"bar.cc\")\n"
354 " LITERAL(\"win\")\n"
357 " IDENTIFIER(sources)\n"
359 " LITERAL(\"asd.cc\")\n"
360 " LITERAL(\"foo.cc\")\n"
363 " IDENTIFIER(dependencies)\n"
365 " LITERAL(\"bar.cc\")\n";
366 DoParserPrintTest(input
, expected
);
369 TEST(Parser
, UnterminatedBlock
) {
370 DoParserErrorTest("stuff() {", 1, 9);
373 TEST(Parser
, BadlyTerminatedNumber
) {
374 DoParserErrorTest("1234z", 1, 5);
377 TEST(Parser
, NewlinesInUnusualPlaces
) {
391 TEST(Parser
, NewlinesInUnusualPlaces2
) {
399 "x =\ny if\n(1\n) {}",
418 TEST(Parser
, NewlineBeforeSubscript
) {
419 const char* input
= "a = b[1]";
420 const char* input_with_newline
= "a = b\n[1]";
421 const char* expected
=
436 TEST(Parser
, SequenceOfExpressions
) {
448 TEST(Parser
, BlockAfterFunction
) {
449 const char* input
= "func(\"stuff\") {\n}";
450 // TODO(scottmg): Do we really want these to mean different things?
451 const char* input_with_newline
= "func(\"stuff\")\n{\n}";
452 const char* expected
=
456 " LITERAL(\"stuff\")\n"
458 DoParserPrintTest(input
, expected
);
459 DoParserPrintTest(input_with_newline
, expected
);
462 TEST(Parser
, LongExpression
) {
463 const char* input
= "a = b + c && d || e";
464 const char* expected
=
475 DoParserPrintTest(input
, expected
);
478 TEST(Parser
, CommentsStandalone
) {
480 "# Toplevel comment.\n"
482 "executable(\"wee\") {}\n";
483 const char* expected
=
485 " BLOCK_COMMENT(# Toplevel comment.)\n"
486 " FUNCTION(executable)\n"
488 " LITERAL(\"wee\")\n"
490 DoParserPrintTest(input
, expected
);
493 TEST(Parser
, CommentsStandaloneEof
) {
495 "executable(\"wee\") {}\n"
497 const char* expected
=
499 " +AFTER_COMMENT(\"# EOF comment.\")\n"
500 " FUNCTION(executable)\n"
502 " LITERAL(\"wee\")\n"
504 DoParserPrintTest(input
, expected
);
507 TEST(Parser
, CommentsLineAttached
) {
509 "executable(\"wee\") {\n"
514 " # This file is special or something.\n"
518 const char* expected
=
520 " FUNCTION(executable)\n"
522 " LITERAL(\"wee\")\n"
525 " +BEFORE_COMMENT(\"# Some sources.\")\n"
526 " IDENTIFIER(sources)\n"
528 " LITERAL(\"stuff.cc\")\n"
529 " LITERAL(\"things.cc\")\n"
530 " LITERAL(\"another.cc\")\n"
531 " +BEFORE_COMMENT(\"# This file is special or something.\")\n";
532 DoParserPrintTest(input
, expected
);
535 TEST(Parser
, CommentsSuffix
) {
537 "executable(\"wee\") { # This is some stuff.\n"
538 "sources = [ \"a.cc\" # And another comment here.\n"
540 const char* expected
=
542 " FUNCTION(executable)\n"
544 " LITERAL(\"wee\")\n"
546 " +SUFFIX_COMMENT(\"# This is some stuff.\")\n"
549 " IDENTIFIER(sources)\n"
551 " LITERAL(\"a.cc\")\n"
552 " +SUFFIX_COMMENT(\"# And another comment here.\")\n";
553 DoParserPrintTest(input
, expected
);
556 TEST(Parser
, CommentsSuffixDifferentLine
) {
558 "executable(\"wee\") {\n"
559 " sources = [ \"a\",\n"
560 " \"b\" ] # Comment\n"
562 const char* expected
=
564 " FUNCTION(executable)\n"
566 " LITERAL(\"wee\")\n"
569 " IDENTIFIER(sources)\n"
574 " +SUFFIX_COMMENT(\"# Comment\")\n";
575 DoParserPrintTest(input
, expected
);
578 TEST(Parser
, CommentsSuffixMultiple
) {
580 "executable(\"wee\") {\n"
582 " \"a\", # This is a comment,\n"
583 " # and some more,\n" // Note that this is aligned with above.
587 const char* expected
=
589 " FUNCTION(executable)\n"
591 " LITERAL(\"wee\")\n"
594 " IDENTIFIER(sources)\n"
597 " +SUFFIX_COMMENT(\"# This is a comment,\")\n"
598 " +SUFFIX_COMMENT(\"# and some more,\")\n"
599 " +SUFFIX_COMMENT(\"# then the end.\")\n";
600 DoParserPrintTest(input
, expected
);
603 TEST(Parser
, CommentsConnectedInList
) {
607 " # Connected comment.\n"
611 const char* expected
=
614 " IDENTIFIER(defines)\n"
616 " LITERAL(\"WEE\")\n"
617 " +BEFORE_COMMENT(\"# Connected comment.\")\n"
618 " LITERAL(\"BLORPY\")\n";
619 DoParserPrintTest(input
, expected
);
622 TEST(Parser
, CommentsAtEndOfBlock
) {
625 " sources = [\"a.cc\"]\n"
626 " # Some comment at end.\n"
628 const char* expected
=
631 " IDENTIFIER(is_win)\n"
634 " IDENTIFIER(sources)\n"
636 " LITERAL(\"a.cc\")\n"
638 " +BEFORE_COMMENT(\"# Some comment at end.\")\n";
639 DoParserPrintTest(input
, expected
);
642 // TODO(scottmg): I could be convinced this is incorrect. It's not clear to me
643 // which thing this comment is intended to be attached to.
644 TEST(Parser
, CommentsEndOfBlockSingleLine
) {
646 "defines = [ # EOL defines.\n"
648 const char* expected
=
651 " IDENTIFIER(defines)\n"
652 " +SUFFIX_COMMENT(\"# EOL defines.\")\n"
654 DoParserPrintTest(input
, expected
);
657 TEST(Parser
, HangingIf
) {
658 DoParserErrorTest("if", 1, 1);
661 TEST(Parser
, NegatingList
) {
662 DoParserErrorTest("executable(\"wee\") { sources =- [ \"foo.cc\" ] }", 1, 30);
665 TEST(Parser
, ConditionNoBracesIf
) {
668 " foreach(foo, []) {}\n"
670 " foreach(bar, []) {}\n"
675 TEST(Parser
, ConditionNoBracesElse
) {
678 " foreach(foo, []) {}\n"
680 " foreach(bar, []) {}\n",
684 TEST(Parser
, ConditionNoBracesElseIf
) {
687 " foreach(foo, []) {}\n"
689 " foreach(bar, []) {}\n",
693 // Disallow standalone {} for introducing new scopes. These are ambiguous with
694 // target declarations (e.g. is:
696 // a function with an associated block, or a standalone function with a
697 // freestanding block.
698 TEST(Parser
, StandaloneBlock
) {