element_test.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #include "explorer/ast/element.h"
  5. #include <gmock/gmock.h>
  6. #include <gtest/gtest.h>
  7. #include <optional>
  8. #include "explorer/ast/bindings.h"
  9. #include "explorer/ast/declaration.h"
  10. #include "explorer/ast/expression.h"
  11. #include "explorer/ast/value.h"
  12. #include "explorer/base/arena.h"
  13. #include "llvm/Support/Casting.h"
  14. namespace Carbon {
  15. namespace {
  16. static auto FakeSourceLoc(int line_num) -> SourceLocation {
  17. return SourceLocation("<test>", line_num, FileKind::Main);
  18. }
  19. class ElementTest : public ::testing::Test {
  20. protected:
  21. Arena arena;
  22. };
  23. TEST_F(ElementTest, NamedElementType) {
  24. const auto src_loc = FakeSourceLoc(1);
  25. VariableDeclaration decl{
  26. src_loc,
  27. arena.New<BindingPattern>(src_loc, "valuename",
  28. arena.New<AutoPattern>(src_loc),
  29. ExpressionCategory::Reference),
  30. std::nullopt, ExpressionCategory::Reference};
  31. const auto* static_type = arena.New<IntValue>(1);
  32. decl.set_static_type(static_type);
  33. NamedElement element_decl(&decl);
  34. EXPECT_EQ(&element_decl.type(), static_type);
  35. NamedElement named_val(
  36. arena.New<NamedValue>(NamedValue{"valuename", static_type}));
  37. EXPECT_EQ(&named_val.type(), static_type);
  38. }
  39. TEST_F(ElementTest, NamedElementDeclaration) {
  40. const auto src_loc = FakeSourceLoc(1);
  41. VariableDeclaration decl{
  42. src_loc,
  43. arena.New<BindingPattern>(src_loc, "valuename",
  44. arena.New<AutoPattern>(src_loc),
  45. ExpressionCategory::Reference),
  46. std::nullopt, ExpressionCategory::Reference};
  47. const auto* static_type = arena.New<IntValue>(1);
  48. NamedElement element_decl(&decl);
  49. EXPECT_TRUE(element_decl.declaration());
  50. NamedElement named_val(
  51. arena.New<NamedValue>(NamedValue{"valuename", static_type}));
  52. EXPECT_FALSE(named_val.declaration());
  53. }
  54. TEST_F(ElementTest, NamedElementIsNamed) {
  55. const auto src_loc = FakeSourceLoc(1);
  56. VariableDeclaration decl{
  57. src_loc,
  58. arena.New<BindingPattern>(src_loc, "valuename",
  59. arena.New<AutoPattern>(src_loc),
  60. ExpressionCategory::Reference),
  61. std::nullopt, ExpressionCategory::Reference};
  62. NamedElement member_decl(&decl);
  63. EXPECT_TRUE(member_decl.IsNamed("valuename"));
  64. EXPECT_FALSE(member_decl.IsNamed("anything"));
  65. NamedElement named_val(
  66. arena.New<NamedValue>(NamedValue{"valuename", arena.New<IntValue>(1)}));
  67. EXPECT_TRUE(named_val.IsNamed("valuename"));
  68. EXPECT_FALSE(named_val.IsNamed("anything"));
  69. }
  70. TEST_F(ElementTest, PositionalElementIsNamed) {
  71. PositionalElement element(1, arena.New<IntValue>(1));
  72. EXPECT_FALSE(element.IsNamed("anything"));
  73. }
  74. TEST_F(ElementTest, BaseElementIsNamed) {
  75. BaseElement element(arena.New<IntValue>(1));
  76. EXPECT_FALSE(element.IsNamed("anything"));
  77. }
  78. } // namespace
  79. } // namespace Carbon