Files
carbon-lang/toolchain/lexer/token_kind_test.cpp
T
Chandler Carruth 5f67029479 Use upstream GoogleTest and add related test utils. (#876)
This moves over to the vanilla upstream GoogleTest pulled in the more
expected manner with Bazel. It also adds Abseil and Google Benchmark
libraries in the same fashion (there are cross dependencies here).

As part of this, also introduce a dependency check test that can enforce
basic layering of dependencies. For example, this lets us ensure that
non-test Carbon code only depends on LLVM and Clang despite having other
libraries available. There remains some cleanup to improve the way these
dependency tests work, but this at least ensures we don't regress.

I've also provided workarounds to allow both Carbon code and LLVM code
to freely be used with GoogleTest (and other `std::ostream` based
output code). This is done by extending the code in
`//common/ostream.h`. One downside is that it requires opening the
`llvm` namespace and adding an ADL_found overload there. I think on
balance this is still a win and doesn't make me too nervous.

The new version of GoogleTest requires printing more often from matchers
and so I've also added several printing routines to types that
previously didn't require them. Otherwise, most of the updates are just
using the more conventional upstream style of including the headers and
adding `ostream.h` where it is needed.

I did consider moving code over to use `std::ostream` instead of LLVM's
`raw_ostream`, but the advantages of not doing virtual dispatch still
seem significant, and it also seems good to retain access to LLVM's
formatting utilities built around `raw_ostream` given that we can't pull
arbitrary dependencies into Carbon code outside of test code.

All of this was slightly motivated by requests for newer features in
GoogleTest, but much more-so by my desire to have access to Google
Benchmark and Abseil when writing benchmarks. For example, using
Abseil's random number generator seems extremely helpful when generating
inputs for benchmarks. The growing dependencies between these packages
further motivated me to just pull them all in and ensure they worked
well.
2021-11-02 20:14:12 -07:00

95 lines
4.8 KiB
C++

// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#include "toolchain/lexer/token_kind.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <cstring>
#include "llvm/ADT/StringRef.h"
namespace Carbon {
namespace {
using ::testing::MatchesRegex;
// We restrict symbols to punctuation characters that are expected to be widely
// available on modern keyboards used for programming.
constexpr llvm::StringLiteral SymbolRegex = "[][{}!@#%^&*()/?\\|;:.,<>=+~-]+";
// We restrict keywords to be lowercase ASCII letters and underscores.
constexpr llvm::StringLiteral KeywordRegex = "[a-z_]+";
#define CARBON_TOKEN(TokenName) \
TEST(TokenKindTest, TokenName) { \
EXPECT_EQ(#TokenName, TokenKind::TokenName().Name()); \
EXPECT_FALSE(TokenKind::TokenName().IsSymbol()); \
EXPECT_FALSE(TokenKind::TokenName().IsKeyword()); \
EXPECT_EQ("", TokenKind::TokenName().GetFixedSpelling()); \
}
#define CARBON_SYMBOL_TOKEN(TokenName, Spelling) \
TEST(TokenKindTest, TokenName) { \
EXPECT_EQ(#TokenName, TokenKind::TokenName().Name()); \
EXPECT_TRUE(TokenKind::TokenName().IsSymbol()); \
EXPECT_FALSE(TokenKind::TokenName().IsGroupingSymbol()); \
EXPECT_FALSE(TokenKind::TokenName().IsOpeningSymbol()); \
EXPECT_FALSE(TokenKind::TokenName().IsClosingSymbol()); \
EXPECT_FALSE(TokenKind::TokenName().IsKeyword()); \
EXPECT_EQ(Spelling, TokenKind::TokenName().GetFixedSpelling()); \
EXPECT_THAT(Spelling, MatchesRegex(SymbolRegex.str())); \
}
#define CARBON_OPENING_GROUP_SYMBOL_TOKEN(TokenName, Spelling, ClosingName) \
TEST(TokenKindTest, TokenName) { \
EXPECT_EQ(#TokenName, TokenKind::TokenName().Name()); \
EXPECT_TRUE(TokenKind::TokenName().IsSymbol()); \
EXPECT_TRUE(TokenKind::TokenName().IsGroupingSymbol()); \
EXPECT_TRUE(TokenKind::TokenName().IsOpeningSymbol()); \
EXPECT_EQ(TokenKind::ClosingName(), \
TokenKind::TokenName().GetClosingSymbol()); \
EXPECT_FALSE(TokenKind::TokenName().IsClosingSymbol()); \
EXPECT_FALSE(TokenKind::TokenName().IsKeyword()); \
EXPECT_EQ(Spelling, TokenKind::TokenName().GetFixedSpelling()); \
EXPECT_THAT(Spelling, MatchesRegex(SymbolRegex.str())); \
}
#define CARBON_CLOSING_GROUP_SYMBOL_TOKEN(TokenName, Spelling, OpeningName) \
TEST(TokenKindTest, TokenName) { \
EXPECT_EQ(#TokenName, TokenKind::TokenName().Name()); \
EXPECT_TRUE(TokenKind::TokenName().IsSymbol()); \
EXPECT_TRUE(TokenKind::TokenName().IsGroupingSymbol()); \
EXPECT_FALSE(TokenKind::TokenName().IsOpeningSymbol()); \
EXPECT_TRUE(TokenKind::TokenName().IsClosingSymbol()); \
EXPECT_EQ(TokenKind::OpeningName(), \
TokenKind::TokenName().GetOpeningSymbol()); \
EXPECT_FALSE(TokenKind::TokenName().IsKeyword()); \
EXPECT_EQ(Spelling, TokenKind::TokenName().GetFixedSpelling()); \
EXPECT_THAT(Spelling, MatchesRegex(SymbolRegex.str())); \
}
#define CARBON_KEYWORD_TOKEN(TokenName, Spelling) \
TEST(TokenKindTest, TokenName) { \
EXPECT_EQ(#TokenName, TokenKind::TokenName().Name()); \
EXPECT_FALSE(TokenKind::TokenName().IsSymbol()); \
EXPECT_TRUE(TokenKind::TokenName().IsKeyword()); \
EXPECT_EQ(Spelling, TokenKind::TokenName().GetFixedSpelling()); \
EXPECT_THAT(Spelling, MatchesRegex(KeywordRegex.str())); \
}
#include "toolchain/lexer/token_registry.def"
// Verify that the symbol tokens are sorted from longest to shortest. This is
// important to ensure that simply in-order testing will identify tokens
// following the max-munch rule.
TEST(TokenKindTest, SymbolsInDescendingLength) {
int previous_length = INT_MAX;
#define CARBON_SYMBOL_TOKEN(TokenName, Spelling) \
EXPECT_LE(llvm::StringRef(Spelling).size(), previous_length) \
<< "Symbol token not in descending length order: " << #TokenName; \
previous_length = llvm::StringRef(Spelling).size();
#include "toolchain/lexer/token_registry.def"
EXPECT_GT(previous_length, 0);
}
} // namespace
} // namespace Carbon