error_builders_test.cpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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/common/error_builders.h"
  5. #include <gtest/gtest.h>
  6. #include "explorer/common/source_location.h"
  7. namespace Carbon::Testing {
  8. namespace {
  9. auto ToString(const Error& err) -> std::string {
  10. std::string result;
  11. llvm::raw_string_ostream out(result);
  12. err.Print(out);
  13. return result;
  14. }
  15. TEST(ErrorBuildersTest, CompilationError) {
  16. Error err = CompilationError(SourceLocation("x", 1)) << "test";
  17. EXPECT_EQ(err.prefix(), "COMPILATION ERROR");
  18. EXPECT_EQ(err.location(), "x:1");
  19. EXPECT_EQ(err.message(), "test");
  20. EXPECT_EQ(ToString(err), "COMPILATION ERROR: x:1: test");
  21. }
  22. TEST(ErrorBuildersTest, ProgramError) {
  23. Error err = ProgramError(SourceLocation("x", 1)) << "test";
  24. EXPECT_EQ(err.prefix(), "PROGRAM ERROR");
  25. EXPECT_EQ(err.location(), "x:1");
  26. EXPECT_EQ(err.message(), "test");
  27. EXPECT_EQ(ToString(err), "PROGRAM ERROR: x:1: test");
  28. }
  29. TEST(ErrorBuildersTest, RuntimeError) {
  30. Error err = RuntimeError(SourceLocation("x", 1)) << "test";
  31. EXPECT_EQ(err.prefix(), "RUNTIME ERROR");
  32. EXPECT_EQ(err.location(), "x:1");
  33. EXPECT_EQ(err.message(), "test");
  34. EXPECT_EQ(ToString(err), "RUNTIME ERROR: x:1: test");
  35. }
  36. } // namespace
  37. } // namespace Carbon::Testing