set_file_context_raii_test.cpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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 <gmock/gmock.h>
  5. #include <gtest/gtest.h>
  6. #include "explorer/common/trace_stream.h"
  7. namespace Carbon::Testing {
  8. namespace {
  9. // TODO: write test cases to distinguish between file context of main file and
  10. // an import once imports are supported.
  11. TEST(SetFileContextRaiiTest, Simple) {
  12. TraceStream trace_stream;
  13. {
  14. SetFileContext set_file_ctx(trace_stream,
  15. SourceLocation("example/main.carbon", 9));
  16. EXPECT_TRUE(trace_stream.file_context() == FileContext::Main);
  17. }
  18. // Considering the file context for a trace stream is FileContext::Unknown by
  19. // default, as the default value of source location in a trace stream is
  20. // std::nullopt.
  21. EXPECT_TRUE(trace_stream.file_context() == FileContext::Unknown);
  22. }
  23. TEST(SetFileContextRaiiTest, UpdateFileContext) {
  24. TraceStream trace_stream;
  25. {
  26. SetFileContext set_file_ctx(trace_stream,
  27. SourceLocation("example/prelude.carbon", 9));
  28. EXPECT_TRUE(trace_stream.file_context() == FileContext::Prelude);
  29. set_file_ctx.update_source_loc(SourceLocation("example/main.carbon", 9));
  30. EXPECT_TRUE(trace_stream.file_context() == FileContext::Main);
  31. }
  32. // Considering the file context for a trace stream is FileContext::Unknown by
  33. // default, as the default value of source location in a trace stream is
  34. // std::nullopt.
  35. EXPECT_TRUE(trace_stream.file_context() == FileContext::Unknown);
  36. }
  37. } // namespace
  38. } // namespace Carbon::Testing