semantics_ir_factory_test.cpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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 "toolchain/semantics/semantics_ir_factory.h"
  5. #include <gmock/gmock.h>
  6. #include <gtest/gtest.h>
  7. #include <optional>
  8. #include "toolchain/diagnostics/mocks.h"
  9. #include "toolchain/lexer/tokenized_buffer.h"
  10. #include "toolchain/parser/parse_tree.h"
  11. #include "toolchain/source/source_buffer.h"
  12. namespace Carbon::Testing {
  13. namespace {
  14. using ::testing::_;
  15. class SemanticsIRFactoryTest : public ::testing::Test {
  16. protected:
  17. auto Analyze(llvm::Twine t) -> SemanticsIR {
  18. source_buffer.emplace(std::move(*SourceBuffer::CreateFromText(t.str())));
  19. tokenized_buffer = TokenizedBuffer::Lex(*source_buffer, consumer);
  20. EXPECT_FALSE(tokenized_buffer->has_errors());
  21. parse_tree = ParseTree::Parse(*tokenized_buffer, consumer);
  22. EXPECT_FALSE(parse_tree->has_errors());
  23. return SemanticsIRFactory::Build(*parse_tree);
  24. }
  25. std::optional<SourceBuffer> source_buffer;
  26. std::optional<TokenizedBuffer> tokenized_buffer;
  27. std::optional<ParseTree> parse_tree;
  28. MockDiagnosticConsumer consumer;
  29. };
  30. TEST_F(SemanticsIRFactoryTest, Empty) {
  31. EXPECT_CALL(consumer, HandleDiagnostic(_)).Times(0);
  32. Analyze("");
  33. }
  34. TEST_F(SemanticsIRFactoryTest, FunctionBasic) {
  35. EXPECT_CALL(consumer, HandleDiagnostic(_)).Times(0);
  36. Analyze("fn Foo() {}");
  37. }
  38. TEST_F(SemanticsIRFactoryTest, FunctionDuplicate) {
  39. Analyze(R"(fn Foo() {}
  40. fn Foo() {}
  41. )");
  42. }
  43. } // namespace
  44. } // namespace Carbon::Testing