OLD | NEW |
1 // Copyright 2016 the V8 project authors. All rights reserved. | 1 // Copyright 2016 the V8 project 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 // Tests v8::internal::Scanner. Note that presently most unit tests for the | 5 // Tests v8::internal::Scanner. Note that presently most unit tests for the |
6 // Scanner are in cctest/test-parsing.cc, rather than here. | 6 // Scanner are in cctest/test-parsing.cc, rather than here. |
7 | 7 |
8 #include "src/handles-inl.h" | 8 #include "src/handles-inl.h" |
9 #include "src/parsing/scanner-character-streams.h" | 9 #include "src/parsing/scanner-character-streams.h" |
10 #include "src/parsing/scanner.h" | 10 #include "src/parsing/scanner.h" |
11 #include "src/unicode-cache.h" | 11 #include "src/unicode-cache.h" |
12 #include "test/cctest/cctest.h" | 12 #include "test/cctest/cctest.h" |
13 | 13 |
14 using namespace v8::internal; | 14 using namespace v8::internal; |
15 | 15 |
16 namespace { | 16 namespace { |
17 | 17 |
18 const char src_simple[] = "function foo() { var x = 2 * a() + b; }"; | 18 const char src_simple[] = "function foo() { var x = 2 * a() + b; }"; |
19 | 19 |
20 std::unique_ptr<Scanner> make_scanner(const char* src) { | 20 std::unique_ptr<Scanner> make_scanner(const char* src) { |
21 std::unique_ptr<Scanner> scanner(new Scanner(new UnicodeCache())); | 21 std::unique_ptr<Scanner> scanner(new Scanner(new UnicodeCache())); |
22 scanner->Initialize(ScannerStream::ForTesting(src).release()); | 22 scanner->Initialize(ScannerStream::ForTesting(src).release()); |
23 return scanner; | 23 return scanner; |
24 } | 24 } |
25 | 25 |
26 } // anonymous namespace | 26 } // anonymous namespace |
27 | 27 |
| 28 // DCHECK_TOK checks token equality, but by checking for equality of the token |
| 29 // names. That should have the same result, but has much nicer error messaages. |
| 30 #define DCHECK_TOK(a, b) DCHECK_EQ(Token::Name(a), Token::Name(b)) |
| 31 |
28 TEST(Bookmarks) { | 32 TEST(Bookmarks) { |
29 // Scan through the given source and record the tokens for use as reference | 33 // Scan through the given source and record the tokens for use as reference |
30 // below. | 34 // below. |
31 std::vector<Token::Value> tokens; | 35 std::vector<Token::Value> tokens; |
32 { | 36 { |
33 auto scanner = make_scanner(src_simple); | 37 auto scanner = make_scanner(src_simple); |
34 do { | 38 do { |
35 tokens.push_back(scanner->Next()); | 39 tokens.push_back(scanner->Next()); |
36 } while (scanner->current_token() != Token::EOS); | 40 } while (scanner->current_token() != Token::EOS); |
37 } | 41 } |
38 | 42 |
39 // For each position: | 43 // For each position: |
40 // - Scan through file, | 44 // - Scan through file, |
41 // - set a bookmark once the position is reached, | 45 // - set a bookmark once the position is reached, |
42 // - scan a bit more, | 46 // - scan a bit more, |
43 // - reset to the bookmark, and | 47 // - reset to the bookmark, and |
44 // - scan until the end. | 48 // - scan until the end. |
45 // At each step, compare to the reference token sequence generated above. | 49 // At each step, compare to the reference token sequence generated above. |
46 for (size_t bookmark_pos = 0; bookmark_pos < tokens.size(); bookmark_pos++) { | 50 for (size_t bookmark_pos = 0; bookmark_pos < tokens.size(); bookmark_pos++) { |
47 auto scanner = make_scanner(src_simple); | 51 auto scanner = make_scanner(src_simple); |
48 Scanner::BookmarkScope bookmark(scanner.get()); | 52 Scanner::BookmarkScope bookmark(scanner.get()); |
49 | 53 |
50 for (size_t i = 0; i < std::min(bookmark_pos + 10, tokens.size()); i++) { | 54 for (size_t i = 0; i < std::min(bookmark_pos + 10, tokens.size()); i++) { |
51 if (i == bookmark_pos) { | 55 if (i == bookmark_pos) { |
52 bookmark.Set(); | 56 bookmark.Set(); |
53 } | 57 } |
54 DCHECK_EQ(tokens[i], scanner->Next()); | 58 DCHECK_TOK(tokens[i], scanner->Next()); |
55 } | 59 } |
56 | 60 |
57 bookmark.Apply(); | 61 bookmark.Apply(); |
58 for (size_t i = bookmark_pos; i < tokens.size(); i++) { | 62 for (size_t i = bookmark_pos; i < tokens.size(); i++) { |
59 DCHECK_EQ(tokens[i], scanner->Next()); | 63 DCHECK_TOK(tokens[i], scanner->Next()); |
60 } | 64 } |
61 } | 65 } |
62 } | 66 } |
| 67 |
| 68 TEST(AllThePushbacks) { |
| 69 const struct { |
| 70 const char* src; |
| 71 const Token::Value tokens[5]; // Large enough for any of the test cases. |
| 72 } test_cases[] = { |
| 73 {"<-x", {Token::LT, Token::SUB, Token::IDENTIFIER, Token::EOS}}, |
| 74 {"<!x", {Token::LT, Token::NOT, Token::IDENTIFIER, Token::EOS}}, |
| 75 {"<!-x", |
| 76 {Token::LT, Token::NOT, Token::SUB, Token::IDENTIFIER, Token::EOS}}, |
| 77 {"<!-- xx -->\nx", {Token::IDENTIFIER, Token::EOS}}, |
| 78 }; |
| 79 |
| 80 for (const auto& test_case : test_cases) { |
| 81 auto scanner = make_scanner(test_case.src); |
| 82 for (size_t i = 0; test_case.tokens[i] != Token::EOS; i++) { |
| 83 DCHECK_TOK(test_case.tokens[i], scanner->Next()); |
| 84 } |
| 85 DCHECK_TOK(Token::EOS, scanner->Next()); |
| 86 } |
| 87 } |
OLD | NEW |