| 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" |
| 11 #include "tools/gn/tokenizer.h" | 11 #include "tools/gn/tokenizer.h" |
| 12 | 12 |
| 13 namespace { | 13 namespace { |
| 14 | 14 |
| 15 bool GetTokens(const InputFile* input, std::vector<Token>* result) { | 15 bool GetTokens(const InputFile* input, std::vector<Token>* result) { |
| 16 result->clear(); | 16 result->clear(); |
| 17 Err err; | 17 Err err; |
| 18 *result = Tokenizer::Tokenize(input, &err); | 18 *result = Tokenizer::Tokenize(input, &err); |
| 19 return !err.has_error(); | 19 return !err.has_error(); |
| 20 } | 20 } |
| 21 | 21 |
| 22 void DoParserPrintTest(const char* input, const char* expected) { | 22 void DoParserPrintTest(const char* input, const char* expected) { |
| 23 std::vector<Token> tokens; | 23 std::vector<Token> tokens; |
| 24 InputFile input_file(SourceFile("/test")); | 24 InputFile input_file(SourceFile("/test")); |
| 25 input_file.SetContents(input); | 25 input_file.SetContents(input); |
| 26 ASSERT_TRUE(GetTokens(&input_file, &tokens)); | 26 ASSERT_TRUE(GetTokens(&input_file, &tokens)); |
| 27 | 27 |
| 28 Err err; | 28 Err err; |
| 29 scoped_ptr<ParseNode> result = Parser::Parse(tokens, &err); | 29 std::unique_ptr<ParseNode> result = Parser::Parse(tokens, &err); |
| 30 if (!result) | 30 if (!result) |
| 31 err.PrintToStdout(); | 31 err.PrintToStdout(); |
| 32 ASSERT_TRUE(result); | 32 ASSERT_TRUE(result); |
| 33 | 33 |
| 34 std::ostringstream collector; | 34 std::ostringstream collector; |
| 35 result->Print(collector, 0); | 35 result->Print(collector, 0); |
| 36 | 36 |
| 37 EXPECT_EQ(expected, collector.str()); | 37 EXPECT_EQ(expected, collector.str()); |
| 38 } | 38 } |
| 39 | 39 |
| 40 void DoExpressionPrintTest(const char* input, const char* expected) { | 40 void DoExpressionPrintTest(const char* input, const char* expected) { |
| 41 std::vector<Token> tokens; | 41 std::vector<Token> tokens; |
| 42 InputFile input_file(SourceFile("/test")); | 42 InputFile input_file(SourceFile("/test")); |
| 43 input_file.SetContents(input); | 43 input_file.SetContents(input); |
| 44 ASSERT_TRUE(GetTokens(&input_file, &tokens)); | 44 ASSERT_TRUE(GetTokens(&input_file, &tokens)); |
| 45 | 45 |
| 46 Err err; | 46 Err err; |
| 47 scoped_ptr<ParseNode> result = Parser::ParseExpression(tokens, &err); | 47 std::unique_ptr<ParseNode> result = Parser::ParseExpression(tokens, &err); |
| 48 ASSERT_TRUE(result); | 48 ASSERT_TRUE(result); |
| 49 | 49 |
| 50 std::ostringstream collector; | 50 std::ostringstream collector; |
| 51 result->Print(collector, 0); | 51 result->Print(collector, 0); |
| 52 | 52 |
| 53 EXPECT_EQ(expected, collector.str()); | 53 EXPECT_EQ(expected, collector.str()); |
| 54 } | 54 } |
| 55 | 55 |
| 56 // Expects the tokenizer or parser to identify an error at the given line and | 56 // Expects the tokenizer or parser to identify an error at the given line and |
| 57 // character. | 57 // character. |
| 58 void DoParserErrorTest(const char* input, int err_line, int err_char) { | 58 void DoParserErrorTest(const char* input, int err_line, int err_char) { |
| 59 InputFile input_file(SourceFile("/test")); | 59 InputFile input_file(SourceFile("/test")); |
| 60 input_file.SetContents(input); | 60 input_file.SetContents(input); |
| 61 | 61 |
| 62 Err err; | 62 Err err; |
| 63 std::vector<Token> tokens = Tokenizer::Tokenize(&input_file, &err); | 63 std::vector<Token> tokens = Tokenizer::Tokenize(&input_file, &err); |
| 64 if (!err.has_error()) { | 64 if (!err.has_error()) { |
| 65 scoped_ptr<ParseNode> result = Parser::Parse(tokens, &err); | 65 std::unique_ptr<ParseNode> result = Parser::Parse(tokens, &err); |
| 66 ASSERT_FALSE(result); | 66 ASSERT_FALSE(result); |
| 67 ASSERT_TRUE(err.has_error()); | 67 ASSERT_TRUE(err.has_error()); |
| 68 } | 68 } |
| 69 | 69 |
| 70 EXPECT_EQ(err_line, err.location().line_number()); | 70 EXPECT_EQ(err_line, err.location().line_number()); |
| 71 EXPECT_EQ(err_char, err.location().column_number()); | 71 EXPECT_EQ(err_char, err.location().column_number()); |
| 72 } | 72 } |
| 73 | 73 |
| 74 // Expects the tokenizer or parser to identify an error at the given line and | 74 // Expects the tokenizer or parser to identify an error at the given line and |
| 75 // character. | 75 // character. |
| 76 void DoExpressionErrorTest(const char* input, int err_line, int err_char) { | 76 void DoExpressionErrorTest(const char* input, int err_line, int err_char) { |
| 77 InputFile input_file(SourceFile("/test")); | 77 InputFile input_file(SourceFile("/test")); |
| 78 input_file.SetContents(input); | 78 input_file.SetContents(input); |
| 79 | 79 |
| 80 Err err; | 80 Err err; |
| 81 std::vector<Token> tokens = Tokenizer::Tokenize(&input_file, &err); | 81 std::vector<Token> tokens = Tokenizer::Tokenize(&input_file, &err); |
| 82 if (!err.has_error()) { | 82 if (!err.has_error()) { |
| 83 scoped_ptr<ParseNode> result = Parser::ParseExpression(tokens, &err); | 83 std::unique_ptr<ParseNode> result = Parser::ParseExpression(tokens, &err); |
| 84 ASSERT_FALSE(result); | 84 ASSERT_FALSE(result); |
| 85 ASSERT_TRUE(err.has_error()); | 85 ASSERT_TRUE(err.has_error()); |
| 86 } | 86 } |
| 87 | 87 |
| 88 EXPECT_EQ(err_line, err.location().line_number()); | 88 EXPECT_EQ(err_line, err.location().line_number()); |
| 89 EXPECT_EQ(err_char, err.location().column_number()); | 89 EXPECT_EQ(err_char, err.location().column_number()); |
| 90 } | 90 } |
| 91 | 91 |
| 92 } // namespace | 92 } // namespace |
| 93 | 93 |
| (...skipping 608 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 702 // freestanding block. | 702 // freestanding block. |
| 703 TEST(Parser, StandaloneBlock) { | 703 TEST(Parser, StandaloneBlock) { |
| 704 DoParserErrorTest( | 704 DoParserErrorTest( |
| 705 "if (true) {\n" | 705 "if (true) {\n" |
| 706 "}\n" | 706 "}\n" |
| 707 "{\n" | 707 "{\n" |
| 708 " assert(false)\n" | 708 " assert(false)\n" |
| 709 "}\n", | 709 "}\n", |
| 710 3, 1); | 710 3, 1); |
| 711 } | 711 } |
| OLD | NEW |