test-scanner.cc 3.44 KB
Newer Older
1 2 3 4 5 6 7
// Copyright 2016 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Tests v8::internal::Scanner. Note that presently most unit tests for the
// Scanner are in cctest/test-parsing.cc, rather than here.

8
#include "src/handles/handles-inl.h"
9
#include "src/objects/objects-inl.h"
10
#include "src/parsing/parse-info.h"
11 12 13 14
#include "src/parsing/scanner-character-streams.h"
#include "src/parsing/scanner.h"
#include "test/cctest/cctest.h"

15 16
namespace v8 {
namespace internal {
17 18 19 20 21

namespace {

const char src_simple[] = "function foo() { var x = 2 * a() + b; }";

22 23
struct ScannerTestHelper {
  ScannerTestHelper() = default;
24
  ScannerTestHelper(ScannerTestHelper&& other) V8_NOEXCEPT
25
      : stream(std::move(other.stream)),
26 27
        scanner(std::move(other.scanner)) {}

28
  std::unique_ptr<Utf16CharacterStream> stream;
29 30 31 32 33 34 35 36 37
  std::unique_ptr<Scanner> scanner;

  Scanner* operator->() const { return scanner.get(); }
  Scanner* get() const { return scanner.get(); }
};

ScannerTestHelper make_scanner(const char* src) {
  ScannerTestHelper helper;
  helper.stream = ScannerStream::ForTesting(src);
38 39 40
  helper.scanner = std::unique_ptr<Scanner>(
      new Scanner(helper.stream.get(),
                  UnoptimizedCompileFlags::ForTest(CcTest::i_isolate())));
41
  helper.scanner->Initialize();
42
  return helper;
43 44 45 46
}

}  // anonymous namespace

47
// CHECK_TOK checks token equality, but by checking for equality of the token
48
// names. That should have the same result, but has much nicer error messaages.
49
#define CHECK_TOK(a, b) CHECK_EQ(Token::Name(a), Token::Name(b))
50

51 52 53 54 55
TEST(Bookmarks) {
  // Scan through the given source and record the tokens for use as reference
  // below.
  std::vector<Token::Value> tokens;
  {
56
    auto scanner = make_scanner(src_simple);
57 58 59 60 61 62 63 64 65 66 67 68 69
    do {
      tokens.push_back(scanner->Next());
    } while (scanner->current_token() != Token::EOS);
  }

  // For each position:
  // - Scan through file,
  // - set a bookmark once the position is reached,
  // - scan a bit more,
  // - reset to the bookmark, and
  // - scan until the end.
  // At each step, compare to the reference token sequence generated above.
  for (size_t bookmark_pos = 0; bookmark_pos < tokens.size(); bookmark_pos++) {
70
    auto scanner = make_scanner(src_simple);
71 72 73 74
    Scanner::BookmarkScope bookmark(scanner.get());

    for (size_t i = 0; i < std::min(bookmark_pos + 10, tokens.size()); i++) {
      if (i == bookmark_pos) {
75
        bookmark.Set(scanner->peek_location().beg_pos);
76
      }
77
      CHECK_TOK(tokens[i], scanner->Next());
78 79
    }

80
    bookmark.Apply();
81
    for (size_t i = bookmark_pos; i < tokens.size(); i++) {
82
      CHECK_TOK(tokens[i], scanner->Next());
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
    }
  }
}

TEST(AllThePushbacks) {
  const struct {
    const char* src;
    const Token::Value tokens[5];  // Large enough for any of the test cases.
  } test_cases[] = {
      {"<-x", {Token::LT, Token::SUB, Token::IDENTIFIER, Token::EOS}},
      {"<!x", {Token::LT, Token::NOT, Token::IDENTIFIER, Token::EOS}},
      {"<!-x",
       {Token::LT, Token::NOT, Token::SUB, Token::IDENTIFIER, Token::EOS}},
      {"<!-- xx -->\nx", {Token::IDENTIFIER, Token::EOS}},
  };

  for (const auto& test_case : test_cases) {
100
    auto scanner = make_scanner(test_case.src);
101
    for (size_t i = 0; test_case.tokens[i] != Token::EOS; i++) {
102
      CHECK_TOK(test_case.tokens[i], scanner->Next());
103
    }
104
    CHECK_TOK(Token::EOS, scanner->Next());
105 106
  }
}
107

108 109
}  // namespace internal
}  // namespace v8