mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-25 19:20:10 +01:00
The small size is for the 1m vs 5m time limit -- all these tests _should_ be fast so a lower limit seems consistent, and the 5m timeout was getting in my way when trying to debug *actual* timeouts. The Carbon::Testing bit is for convenience -- test libraries are generally using it, it seems like the tests should too. Note this reduces the need for `using`. This does push NodeMatchers into Carbon::Testing -- I don't think this was benefiting from having its own namespace; `using namespace` is discouraged [under style](https://google.github.io/styleguide/cppguide.html#Namespaces), we wouldn't support an equivalent in Carbon, and it feels like it's not helping to avoid name collisions. (also tidy was bugging about it, and while I could NOLINT that, this felt like the better approach)
158 lines
6.9 KiB
C++
158 lines
6.9 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/parser/precedence.h"
|
|
|
|
#include <gmock/gmock.h>
|
|
#include <gtest/gtest.h>
|
|
|
|
#include "toolchain/lexer/token_kind.h"
|
|
|
|
namespace Carbon::Testing {
|
|
namespace {
|
|
|
|
using ::testing::Eq;
|
|
|
|
TEST(PrecedenceTest, OperatorsAreRecognized) {
|
|
EXPECT_TRUE(PrecedenceGroup::ForLeading(TokenKind::Minus()).hasValue());
|
|
EXPECT_TRUE(PrecedenceGroup::ForLeading(TokenKind::Tilde()).hasValue());
|
|
EXPECT_FALSE(PrecedenceGroup::ForLeading(TokenKind::Slash()).hasValue());
|
|
EXPECT_FALSE(PrecedenceGroup::ForLeading(TokenKind::Identifier()).hasValue());
|
|
|
|
EXPECT_TRUE(
|
|
PrecedenceGroup::ForTrailing(TokenKind::Minus(), false).hasValue());
|
|
EXPECT_FALSE(
|
|
PrecedenceGroup::ForTrailing(TokenKind::Tilde(), false).hasValue());
|
|
EXPECT_TRUE(
|
|
PrecedenceGroup::ForTrailing(TokenKind::Slash(), true).hasValue());
|
|
EXPECT_FALSE(
|
|
PrecedenceGroup::ForTrailing(TokenKind::Identifier(), false).hasValue());
|
|
|
|
EXPECT_TRUE(
|
|
PrecedenceGroup::ForTrailing(TokenKind::Minus(), true)->is_binary);
|
|
EXPECT_FALSE(
|
|
PrecedenceGroup::ForTrailing(TokenKind::MinusMinus(), false)->is_binary);
|
|
}
|
|
|
|
TEST(PrecedenceTest, InfixVsPostfix) {
|
|
// A trailing `-` is always infix; a trailing `--` is always postfix.
|
|
EXPECT_TRUE(
|
|
PrecedenceGroup::ForTrailing(TokenKind::Minus(), false)->is_binary);
|
|
EXPECT_FALSE(
|
|
PrecedenceGroup::ForTrailing(TokenKind::MinusMinus(), true)->is_binary);
|
|
|
|
// A trailing `*` is interpreted based on context.
|
|
EXPECT_TRUE(PrecedenceGroup::ForTrailing(TokenKind::Star(), true)->is_binary);
|
|
EXPECT_FALSE(
|
|
PrecedenceGroup::ForTrailing(TokenKind::Star(), false)->is_binary);
|
|
|
|
// Infix `*` can appear in type contexts; binary `*` cannot.
|
|
EXPECT_THAT(PrecedenceGroup::GetPriority(
|
|
PrecedenceGroup::ForTrailing(TokenKind::Star(), true)->level,
|
|
PrecedenceGroup::ForType()),
|
|
Eq(OperatorPriority::Ambiguous));
|
|
EXPECT_THAT(PrecedenceGroup::GetPriority(
|
|
PrecedenceGroup::ForTrailing(TokenKind::Star(), false)->level,
|
|
PrecedenceGroup::ForType()),
|
|
Eq(OperatorPriority::LeftFirst));
|
|
|
|
// Binary `*` can appear in `+` contexts; binary `*` cannot.
|
|
EXPECT_THAT(PrecedenceGroup::GetPriority(
|
|
PrecedenceGroup::ForTrailing(TokenKind::Star(), true)->level,
|
|
PrecedenceGroup::ForTrailing(TokenKind::Plus(), true)->level),
|
|
Eq(OperatorPriority::LeftFirst));
|
|
EXPECT_THAT(PrecedenceGroup::GetPriority(
|
|
PrecedenceGroup::ForTrailing(TokenKind::Star(), false)->level,
|
|
PrecedenceGroup::ForTrailing(TokenKind::Plus(), true)->level),
|
|
Eq(OperatorPriority::Ambiguous));
|
|
}
|
|
|
|
TEST(PrecedenceTest, Associativity) {
|
|
EXPECT_THAT(
|
|
PrecedenceGroup::ForLeading(TokenKind::Minus())->GetAssociativity(),
|
|
Eq(Associativity::RightToLeft));
|
|
EXPECT_THAT(PrecedenceGroup::ForTrailing(TokenKind::PlusPlus(), false)
|
|
->level.GetAssociativity(),
|
|
Eq(Associativity::LeftToRight));
|
|
EXPECT_THAT(PrecedenceGroup::ForTrailing(TokenKind::Plus(), true)
|
|
->level.GetAssociativity(),
|
|
Eq(Associativity::LeftToRight));
|
|
EXPECT_THAT(PrecedenceGroup::ForTrailing(TokenKind::Equal(), true)
|
|
->level.GetAssociativity(),
|
|
Eq(Associativity::RightToLeft));
|
|
EXPECT_THAT(PrecedenceGroup::ForTrailing(TokenKind::PlusEqual(), true)
|
|
->level.GetAssociativity(),
|
|
Eq(Associativity::None));
|
|
}
|
|
|
|
TEST(PrecedenceTest, DirectRelations) {
|
|
EXPECT_THAT(PrecedenceGroup::GetPriority(
|
|
PrecedenceGroup::ForTrailing(TokenKind::Star(), true)->level,
|
|
PrecedenceGroup::ForTrailing(TokenKind::Plus(), true)->level),
|
|
Eq(OperatorPriority::LeftFirst));
|
|
EXPECT_THAT(PrecedenceGroup::GetPriority(
|
|
PrecedenceGroup::ForTrailing(TokenKind::Plus(), true)->level,
|
|
PrecedenceGroup::ForTrailing(TokenKind::Star(), true)->level),
|
|
Eq(OperatorPriority::RightFirst));
|
|
|
|
EXPECT_THAT(PrecedenceGroup::GetPriority(
|
|
PrecedenceGroup::ForTrailing(TokenKind::Amp(), true)->level,
|
|
PrecedenceGroup::ForTrailing(TokenKind::Less(), true)->level),
|
|
Eq(OperatorPriority::LeftFirst));
|
|
EXPECT_THAT(PrecedenceGroup::GetPriority(
|
|
PrecedenceGroup::ForTrailing(TokenKind::Less(), true)->level,
|
|
PrecedenceGroup::ForTrailing(TokenKind::Amp(), true)->level),
|
|
Eq(OperatorPriority::RightFirst));
|
|
}
|
|
|
|
TEST(PrecedenceTest, IndirectRelations) {
|
|
EXPECT_THAT(
|
|
PrecedenceGroup::GetPriority(
|
|
PrecedenceGroup::ForTrailing(TokenKind::Star(), true)->level,
|
|
PrecedenceGroup::ForTrailing(TokenKind::OrKeyword(), true)->level),
|
|
Eq(OperatorPriority::LeftFirst));
|
|
EXPECT_THAT(
|
|
PrecedenceGroup::GetPriority(
|
|
PrecedenceGroup::ForTrailing(TokenKind::OrKeyword(), true)->level,
|
|
PrecedenceGroup::ForTrailing(TokenKind::Star(), true)->level),
|
|
Eq(OperatorPriority::RightFirst));
|
|
|
|
EXPECT_THAT(
|
|
PrecedenceGroup::GetPriority(
|
|
*PrecedenceGroup::ForLeading(TokenKind::Tilde()),
|
|
PrecedenceGroup::ForTrailing(TokenKind::Equal(), true)->level),
|
|
Eq(OperatorPriority::LeftFirst));
|
|
EXPECT_THAT(PrecedenceGroup::GetPriority(
|
|
PrecedenceGroup::ForTrailing(TokenKind::Equal(), true)->level,
|
|
*PrecedenceGroup::ForLeading(TokenKind::Tilde())),
|
|
Eq(OperatorPriority::RightFirst));
|
|
}
|
|
|
|
TEST(PrecedenceTest, IncomparableOperators) {
|
|
EXPECT_THAT(PrecedenceGroup::GetPriority(
|
|
*PrecedenceGroup::ForLeading(TokenKind::Tilde()),
|
|
*PrecedenceGroup::ForLeading(TokenKind::NotKeyword())),
|
|
Eq(OperatorPriority::Ambiguous));
|
|
EXPECT_THAT(PrecedenceGroup::GetPriority(
|
|
*PrecedenceGroup::ForLeading(TokenKind::Tilde()),
|
|
*PrecedenceGroup::ForLeading(TokenKind::Minus())),
|
|
Eq(OperatorPriority::Ambiguous));
|
|
EXPECT_THAT(PrecedenceGroup::GetPriority(
|
|
*PrecedenceGroup::ForLeading(TokenKind::Minus()),
|
|
PrecedenceGroup::ForTrailing(TokenKind::Amp(), true)->level),
|
|
Eq(OperatorPriority::Ambiguous));
|
|
EXPECT_THAT(
|
|
PrecedenceGroup::GetPriority(
|
|
PrecedenceGroup::ForTrailing(TokenKind::Equal(), true)->level,
|
|
PrecedenceGroup::ForTrailing(TokenKind::PipeEqual(), true)->level),
|
|
Eq(OperatorPriority::Ambiguous));
|
|
EXPECT_THAT(PrecedenceGroup::GetPriority(
|
|
PrecedenceGroup::ForTrailing(TokenKind::Plus(), true)->level,
|
|
PrecedenceGroup::ForTrailing(TokenKind::Amp(), true)->level),
|
|
Eq(OperatorPriority::Ambiguous));
|
|
}
|
|
|
|
} // namespace
|
|
} // namespace Carbon::Testing
|