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, 6);
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).
248 TEST(Parser
, Condition
) {
249 DoParserPrintTest("if(1) { a = 2 }",
258 DoParserPrintTest("if(1) { a = 2 } else if (0) { a = 3 } else { a = 4 }",
278 TEST(Parser
, OnlyCallAndAssignInBody
) {
279 DoParserErrorTest("[]", 1, 2);
280 DoParserErrorTest("3 + 4", 1, 5);
281 DoParserErrorTest("6 - 7", 1, 5);
282 DoParserErrorTest("if (1) { 5 } else { print(4) }", 1, 12);
285 TEST(Parser
, NoAssignmentInCondition
) {
286 DoParserErrorTest("if (a=2) {}", 1, 5);
289 TEST(Parser
, CompleteFunction
) {
291 "cc_test(\"foo\") {\n"
296 " dependencies = [\n"
300 const char* expected
=
302 " FUNCTION(cc_test)\n"
304 " LITERAL(\"foo\")\n"
307 " IDENTIFIER(sources)\n"
309 " LITERAL(\"foo.cc\")\n"
310 " LITERAL(\"foo.h\")\n"
312 " IDENTIFIER(dependencies)\n"
314 " LITERAL(\"base\")\n";
315 DoParserPrintTest(input
, expected
);
318 TEST(Parser
, FunctionWithConditional
) {
320 "cc_test(\"foo\") {\n"
321 " sources = [\"foo.cc\"]\n"
322 " if (OS == \"mac\") {\n"
323 " sources += \"bar.cc\"\n"
324 " } else if (OS == \"win\") {\n"
325 " sources -= [\"asd.cc\", \"foo.cc\"]\n"
327 " dependencies += [\"bar.cc\"]\n"
330 const char* expected
=
332 " FUNCTION(cc_test)\n"
334 " LITERAL(\"foo\")\n"
337 " IDENTIFIER(sources)\n"
339 " LITERAL(\"foo.cc\")\n"
343 " LITERAL(\"mac\")\n"
346 " IDENTIFIER(sources)\n"
347 " LITERAL(\"bar.cc\")\n"
351 " LITERAL(\"win\")\n"
354 " IDENTIFIER(sources)\n"
356 " LITERAL(\"asd.cc\")\n"
357 " LITERAL(\"foo.cc\")\n"
360 " IDENTIFIER(dependencies)\n"
362 " LITERAL(\"bar.cc\")\n";
363 DoParserPrintTest(input
, expected
);
366 TEST(Parser
, UnterminatedBlock
) {
367 DoParserErrorTest("stuff() {", 1, 9);
370 TEST(Parser
, BadlyTerminatedNumber
) {
371 DoParserErrorTest("1234z", 1, 5);
374 TEST(Parser
, NewlinesInUnusualPlaces
) {
388 TEST(Parser
, NewlinesInUnusualPlaces2
) {
396 "x =\ny if\n(1\n) {}",
415 TEST(Parser
, NewlineBeforeSubscript
) {
416 const char* input
= "a = b[1]";
417 const char* input_with_newline
= "a = b\n[1]";
418 const char* expected
=
433 TEST(Parser
, SequenceOfExpressions
) {
445 TEST(Parser
, BlockAfterFunction
) {
446 const char* input
= "func(\"stuff\") {\n}";
447 // TODO(scottmg): Do we really want these to mean different things?
448 const char* input_with_newline
= "func(\"stuff\")\n{\n}";
449 const char* expected
=
453 " LITERAL(\"stuff\")\n"
455 DoParserPrintTest(input
, expected
);
456 DoParserPrintTest(input_with_newline
, expected
);
459 TEST(Parser
, LongExpression
) {
460 const char* input
= "a = b + c && d || e";
461 const char* expected
=
472 DoParserPrintTest(input
, expected
);
475 TEST(Parser
, CommentsStandalone
) {
477 "# Toplevel comment.\n"
479 "executable(\"wee\") {}\n";
480 const char* expected
=
482 " BLOCK_COMMENT(# Toplevel comment.)\n"
483 " FUNCTION(executable)\n"
485 " LITERAL(\"wee\")\n"
487 DoParserPrintTest(input
, expected
);
490 TEST(Parser
, CommentsStandaloneEof
) {
492 "executable(\"wee\") {}\n"
494 const char* expected
=
496 " +AFTER_COMMENT(\"# EOF comment.\")\n"
497 " FUNCTION(executable)\n"
499 " LITERAL(\"wee\")\n"
501 DoParserPrintTest(input
, expected
);
504 TEST(Parser
, CommentsLineAttached
) {
506 "executable(\"wee\") {\n"
511 " # This file is special or something.\n"
515 const char* expected
=
517 " FUNCTION(executable)\n"
519 " LITERAL(\"wee\")\n"
522 " +BEFORE_COMMENT(\"# Some sources.\")\n"
523 " IDENTIFIER(sources)\n"
525 " LITERAL(\"stuff.cc\")\n"
526 " LITERAL(\"things.cc\")\n"
527 " LITERAL(\"another.cc\")\n"
528 " +BEFORE_COMMENT(\"# This file is special or something.\")\n";
529 DoParserPrintTest(input
, expected
);
532 TEST(Parser
, CommentsSuffix
) {
534 "executable(\"wee\") { # This is some stuff.\n"
535 "sources = [ \"a.cc\" # And another comment here.\n"
537 const char* expected
=
539 " FUNCTION(executable)\n"
541 " LITERAL(\"wee\")\n"
543 " +SUFFIX_COMMENT(\"# This is some stuff.\")\n"
546 " IDENTIFIER(sources)\n"
548 " LITERAL(\"a.cc\")\n"
549 " +SUFFIX_COMMENT(\"# And another comment here.\")\n";
550 DoParserPrintTest(input
, expected
);
553 TEST(Parser
, CommentsSuffixDifferentLine
) {
555 "executable(\"wee\") {\n"
556 " sources = [ \"a\",\n"
557 " \"b\" ] # Comment\n"
559 const char* expected
=
561 " FUNCTION(executable)\n"
563 " LITERAL(\"wee\")\n"
566 " IDENTIFIER(sources)\n"
571 " +SUFFIX_COMMENT(\"# Comment\")\n";
572 DoParserPrintTest(input
, expected
);
575 TEST(Parser
, CommentsSuffixMultiple
) {
577 "executable(\"wee\") {\n"
579 " \"a\", # This is a comment,\n"
580 " # and some more,\n" // Note that this is aligned with above.
584 const char* expected
=
586 " FUNCTION(executable)\n"
588 " LITERAL(\"wee\")\n"
591 " IDENTIFIER(sources)\n"
594 " +SUFFIX_COMMENT(\"# This is a comment,\")\n"
595 " +SUFFIX_COMMENT(\"# and some more,\")\n"
596 " +SUFFIX_COMMENT(\"# then the end.\")\n";
597 DoParserPrintTest(input
, expected
);
600 TEST(Parser
, CommentsConnectedInList
) {
604 " # Connected comment.\n"
608 const char* expected
=
611 " IDENTIFIER(defines)\n"
613 " LITERAL(\"WEE\")\n"
614 " +BEFORE_COMMENT(\"# Connected comment.\")\n"
615 " LITERAL(\"BLORPY\")\n";
616 DoParserPrintTest(input
, expected
);
619 TEST(Parser
, CommentsAtEndOfBlock
) {
622 " sources = [\"a.cc\"]\n"
623 " # Some comment at end.\n"
625 const char* expected
=
628 " IDENTIFIER(is_win)\n"
631 " IDENTIFIER(sources)\n"
633 " LITERAL(\"a.cc\")\n"
635 " +BEFORE_COMMENT(\"# Some comment at end.\")\n";
636 DoParserPrintTest(input
, expected
);
639 // TODO(scottmg): I could be convinced this is incorrect. It's not clear to me
640 // which thing this comment is intended to be attached to.
641 TEST(Parser
, CommentsEndOfBlockSingleLine
) {
643 "defines = [ # EOL defines.\n"
645 const char* expected
=
648 " IDENTIFIER(defines)\n"
649 " +SUFFIX_COMMENT(\"# EOL defines.\")\n"
651 DoParserPrintTest(input
, expected
);
654 TEST(Parser
, HangingIf
) {
655 DoParserErrorTest("if", 1, 1);
658 TEST(Parser
, NegatingList
) {
659 DoParserErrorTest("executable(\"wee\") { sources =- [ \"foo.cc\" ] }", 1, 30);
662 TEST(Parser
, ConditionNoBracesIf
) {
665 " foreach(foo, []) {}\n"
667 " foreach(bar, []) {}\n"
672 TEST(Parser
, ConditionNoBracesElse
) {
675 " foreach(foo, []) {}\n"
677 " foreach(bar, []) {}\n",
681 TEST(Parser
, ConditionNoBracesElseIf
) {
684 " foreach(foo, []) {}\n"
686 " foreach(bar, []) {}\n",
690 // Disallow standalone {} for introducing new scopes. These are ambiguous with
691 // target declarations (e.g. is:
693 // a function with an associated block, or a standalone function with a
694 // freestanding block.
695 TEST(Parser
, StandaloneBlock
) {