Files
carbon-lang/executable_semantics/ast/pattern_test.cpp
T
Jon Meow 70797e8bf8 Mass rename SourceLoc and Tag (#860)
This does a mass rename of:

-   `SourceLoc()` -> `source_loc()` for property naming
    - `loc` -> `source_loc_` for underscore+consistency
    - Generally changing function args to `source_loc` for consistency
-   `Tag()` -> `kind()` for property naming and `Kind` parity
    - `tag` -> `kind_` for underscore

Also renames `Pos` and `Results` on `Action`. These are a bit of an exception in that most base classes only have `Tag` and maybe `SourceLoc`, whereas `Action` has a little more. I felt okay having `source_loc()` and `kind()` on the base class where children do `Exp()` and the like, but it felt weird to me to mix it on the same class.

The reason for doing this cross-class in one PR is so that I can do it efficiently with a global replace in the codebase, rather than e.g. changing `Expression` but having to read through compiler errors to determine where it's calling `Expression`'s `Tag` versus a different `Tag`. The end result should be equivalent.
2021-09-29 16:52:12 -07:00

145 lines
5.0 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 "executable_semantics/ast/pattern.h"
#include "executable_semantics/ast/expression.h"
#include "executable_semantics/ast/paren_contents.h"
#include "executable_semantics/common/arena.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "llvm/Support/Casting.h"
namespace Carbon {
namespace {
using llvm::cast;
using llvm::isa;
using testing::ElementsAre;
using testing::IsEmpty;
// Matches a TuplePattern::Field named `name` whose `pattern` is an
// `AutoPattern`.
MATCHER_P(AutoFieldNamed, name, "") {
return arg.name == std::string(name) && isa<AutoPattern>(*arg.pattern);
}
static auto FakeSourceLoc(int line_num) -> SourceLocation {
return SourceLocation("<test>", line_num);
}
class PatternTest : public ::testing::Test {
protected:
Arena arena;
};
TEST_F(PatternTest, EmptyAsPattern) {
ParenContents<Pattern> contents = {.elements = {},
.has_trailing_comma = false};
Nonnull<const Pattern*> pattern =
PatternFromParenContents(&arena, FakeSourceLoc(1), contents);
EXPECT_EQ(pattern->source_loc(), FakeSourceLoc(1));
ASSERT_TRUE(isa<TuplePattern>(*pattern));
EXPECT_THAT(cast<TuplePattern>(*pattern).Fields(), IsEmpty());
}
TEST_F(PatternTest, EmptyAsTuplePattern) {
ParenContents<Pattern> contents = {.elements = {},
.has_trailing_comma = false};
Nonnull<const TuplePattern*> tuple =
TuplePatternFromParenContents(&arena, FakeSourceLoc(1), contents);
EXPECT_EQ(tuple->source_loc(), FakeSourceLoc(1));
EXPECT_THAT(tuple->Fields(), IsEmpty());
}
TEST_F(PatternTest, UnaryNoCommaAsPattern) {
// Equivalent to a code fragment like
// ```
// (
// auto
// )
// ```
ParenContents<Pattern> contents = {
.elements = {{.name = std::nullopt,
.term = arena.New<AutoPattern>(FakeSourceLoc(2))}},
.has_trailing_comma = false};
Nonnull<const Pattern*> pattern =
PatternFromParenContents(&arena, FakeSourceLoc(1), contents);
EXPECT_EQ(pattern->source_loc(), FakeSourceLoc(2));
ASSERT_TRUE(isa<AutoPattern>(*pattern));
}
TEST_F(PatternTest, UnaryNoCommaAsTuplePattern) {
ParenContents<Pattern> contents = {
.elements = {{.name = std::nullopt,
.term = arena.New<AutoPattern>(FakeSourceLoc(2))}},
.has_trailing_comma = false};
Nonnull<const TuplePattern*> tuple =
TuplePatternFromParenContents(&arena, FakeSourceLoc(1), contents);
EXPECT_EQ(tuple->source_loc(), FakeSourceLoc(1));
EXPECT_THAT(tuple->Fields(), ElementsAre(AutoFieldNamed("0")));
}
TEST_F(PatternTest, UnaryWithCommaAsPattern) {
ParenContents<Pattern> contents = {
.elements = {{.name = std::nullopt,
.term = arena.New<AutoPattern>(FakeSourceLoc(2))}},
.has_trailing_comma = true};
Nonnull<const Pattern*> pattern =
PatternFromParenContents(&arena, FakeSourceLoc(1), contents);
EXPECT_EQ(pattern->source_loc(), FakeSourceLoc(1));
ASSERT_TRUE(isa<TuplePattern>(*pattern));
EXPECT_THAT(cast<TuplePattern>(*pattern).Fields(),
ElementsAre(AutoFieldNamed("0")));
}
TEST_F(PatternTest, UnaryWithCommaAsTuplePattern) {
ParenContents<Pattern> contents = {
.elements = {{.name = std::nullopt,
.term = arena.New<AutoPattern>(FakeSourceLoc(2))}},
.has_trailing_comma = true};
Nonnull<const TuplePattern*> tuple =
TuplePatternFromParenContents(&arena, FakeSourceLoc(1), contents);
EXPECT_EQ(tuple->source_loc(), FakeSourceLoc(1));
EXPECT_THAT(tuple->Fields(), ElementsAre(AutoFieldNamed("0")));
}
TEST_F(PatternTest, BinaryAsPattern) {
ParenContents<Pattern> contents = {
.elements = {{.name = std::nullopt,
.term = arena.New<AutoPattern>(FakeSourceLoc(2))},
{.name = std::nullopt,
.term = arena.New<AutoPattern>(FakeSourceLoc(2))}},
.has_trailing_comma = true};
Nonnull<const Pattern*> pattern =
PatternFromParenContents(&arena, FakeSourceLoc(1), contents);
EXPECT_EQ(pattern->source_loc(), FakeSourceLoc(1));
ASSERT_TRUE(isa<TuplePattern>(*pattern));
EXPECT_THAT(cast<TuplePattern>(*pattern).Fields(),
ElementsAre(AutoFieldNamed("0"), AutoFieldNamed("1")));
}
TEST_F(PatternTest, BinaryAsTuplePattern) {
ParenContents<Pattern> contents = {
.elements = {{.name = std::nullopt,
.term = arena.New<AutoPattern>(FakeSourceLoc(2))},
{.name = std::nullopt,
.term = arena.New<AutoPattern>(FakeSourceLoc(2))}},
.has_trailing_comma = true};
Nonnull<const TuplePattern*> tuple =
TuplePatternFromParenContents(&arena, FakeSourceLoc(1), contents);
EXPECT_EQ(tuple->source_loc(), FakeSourceLoc(1));
EXPECT_THAT(tuple->Fields(),
ElementsAre(AutoFieldNamed("0"), AutoFieldNamed("1")));
}
} // namespace
} // namespace Carbon