// 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(*arg.pattern); } static auto FakeSourceLoc(int line_num) -> SourceLocation { return SourceLocation("", line_num); } class PatternTest : public ::testing::Test { protected: Arena arena; }; TEST_F(PatternTest, EmptyAsPattern) { ParenContents contents = {.elements = {}, .has_trailing_comma = false}; Nonnull pattern = PatternFromParenContents(&arena, FakeSourceLoc(1), contents); EXPECT_EQ(pattern->source_loc(), FakeSourceLoc(1)); ASSERT_TRUE(isa(*pattern)); EXPECT_THAT(cast(*pattern).Fields(), IsEmpty()); } TEST_F(PatternTest, EmptyAsTuplePattern) { ParenContents contents = {.elements = {}, .has_trailing_comma = false}; Nonnull 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 contents = { .elements = {{.name = std::nullopt, .term = arena.New(FakeSourceLoc(2))}}, .has_trailing_comma = false}; Nonnull pattern = PatternFromParenContents(&arena, FakeSourceLoc(1), contents); EXPECT_EQ(pattern->source_loc(), FakeSourceLoc(2)); ASSERT_TRUE(isa(*pattern)); } TEST_F(PatternTest, UnaryNoCommaAsTuplePattern) { ParenContents contents = { .elements = {{.name = std::nullopt, .term = arena.New(FakeSourceLoc(2))}}, .has_trailing_comma = false}; Nonnull 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 contents = { .elements = {{.name = std::nullopt, .term = arena.New(FakeSourceLoc(2))}}, .has_trailing_comma = true}; Nonnull pattern = PatternFromParenContents(&arena, FakeSourceLoc(1), contents); EXPECT_EQ(pattern->source_loc(), FakeSourceLoc(1)); ASSERT_TRUE(isa(*pattern)); EXPECT_THAT(cast(*pattern).Fields(), ElementsAre(AutoFieldNamed("0"))); } TEST_F(PatternTest, UnaryWithCommaAsTuplePattern) { ParenContents contents = { .elements = {{.name = std::nullopt, .term = arena.New(FakeSourceLoc(2))}}, .has_trailing_comma = true}; Nonnull 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 contents = { .elements = {{.name = std::nullopt, .term = arena.New(FakeSourceLoc(2))}, {.name = std::nullopt, .term = arena.New(FakeSourceLoc(2))}}, .has_trailing_comma = true}; Nonnull pattern = PatternFromParenContents(&arena, FakeSourceLoc(1), contents); EXPECT_EQ(pattern->source_loc(), FakeSourceLoc(1)); ASSERT_TRUE(isa(*pattern)); EXPECT_THAT(cast(*pattern).Fields(), ElementsAre(AutoFieldNamed("0"), AutoFieldNamed("1"))); } TEST_F(PatternTest, BinaryAsTuplePattern) { ParenContents contents = { .elements = {{.name = std::nullopt, .term = arena.New(FakeSourceLoc(2))}, {.name = std::nullopt, .term = arena.New(FakeSourceLoc(2))}}, .has_trailing_comma = true}; Nonnull 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