OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 // Tests v8::internal::Scanner. Note that presently most unit tests for the |
| 6 // Scanner are in cctest/test-parsing.cc, rather than here. |
| 7 |
| 8 #include "src/handles-inl.h" |
| 9 #include "src/parsing/scanner-character-streams.h" |
| 10 #include "src/parsing/scanner.h" |
| 11 #include "src/unicode-cache.h" |
| 12 #include "test/cctest/cctest.h" |
| 13 |
| 14 using namespace v8::internal; |
| 15 |
| 16 namespace { |
| 17 |
| 18 const char src_simple[] = "function foo() { var x = 2 * a() + b; }"; |
| 19 |
| 20 static UnicodeCache* unicode_cache = new UnicodeCache(); |
| 21 |
| 22 std::unique_ptr<Scanner> make_scanner(const char* src) { |
| 23 std::unique_ptr<Scanner> scanner(new Scanner(new UnicodeCache())); |
| 24 scanner->Initialize(ScannerStream::ForTesting(src).release()); |
| 25 return scanner; |
| 26 } |
| 27 |
| 28 } // anonymous namespace |
| 29 |
| 30 TEST(Bookmarks) { |
| 31 // Scan through the given source and record the tokens for use as reference |
| 32 // below. |
| 33 std::vector<Token::Value> tokens; |
| 34 { |
| 35 auto scanner = make_scanner(src_simple); |
| 36 do { |
| 37 tokens.push_back(scanner->Next()); |
| 38 } while (scanner->current_token() != Token::EOS); |
| 39 } |
| 40 |
| 41 // For each position: |
| 42 // - Scan through file, |
| 43 // - set a bookmark once the position is reached, |
| 44 // - scan a bit more, |
| 45 // - reset to the bookmark, and |
| 46 // - scan until the end. |
| 47 // At each step, compare to the reference token sequence generated above. |
| 48 for (size_t bookmark_pos = 0; bookmark_pos < tokens.size(); bookmark_pos++) { |
| 49 auto scanner = make_scanner(src_simple); |
| 50 Scanner::BookmarkScope bookmark(scanner.get()); |
| 51 |
| 52 for (size_t i = 0; i < std::min(bookmark_pos + 10, tokens.size()); i++) { |
| 53 if (i == bookmark_pos) { |
| 54 bookmark.Set(); |
| 55 } |
| 56 DCHECK_EQ(tokens[i], scanner->Next()); |
| 57 } |
| 58 |
| 59 bookmark.Reset(); |
| 60 for (size_t i = bookmark_pos; i < tokens.size(); i++) { |
| 61 DCHECK_EQ(tokens[i], scanner->Next()); |
| 62 } |
| 63 } |
| 64 } |
OLD | NEW |