fuzzer_util_test.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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/fuzzing/fuzzer_util.h"
  5. #include <gmock/gmock.h>
  6. #include <gtest/gtest.h>
  7. #include <fstream>
  8. #include "llvm/ADT/StringRef.h"
  9. #include "llvm/Support/raw_ostream.h"
  10. #include "testing/fuzzing/proto_to_carbon.h"
  11. namespace Carbon::Testing {
  12. namespace {
  13. TEST(FuzzerUtilTest, ParseAndExecuteProto) {
  14. const ErrorOr<Fuzzing::Carbon> carbon_proto = ParseCarbonTextProto(R"(
  15. compilation_unit {
  16. package_statement { package_name: "P" }
  17. is_api: true
  18. declarations {
  19. function {
  20. name {
  21. name: "Main"
  22. }
  23. param_pattern {}
  24. return_term {
  25. kind: Expression
  26. type { int_type_literal {} }
  27. }
  28. body {
  29. statements {
  30. return_expression_statement {
  31. expression { int_literal { value: 0 } }
  32. }
  33. }
  34. }
  35. }
  36. }
  37. })");
  38. ASSERT_TRUE(carbon_proto.ok());
  39. const ErrorOr<int> result = ParseAndExecuteProto(*carbon_proto);
  40. ASSERT_TRUE(result.ok()) << "Execution failed: " << result.error();
  41. EXPECT_EQ(*result, 0);
  42. }
  43. TEST(FuzzerUtilTest, GetRunfilesFile) {
  44. EXPECT_THAT(*GetRunfilesFile("carbon/explorer/data/prelude.carbon"),
  45. testing::EndsWith("/prelude.carbon"));
  46. EXPECT_THAT(GetRunfilesFile("nonexistent-file").error().message(),
  47. testing::EndsWith("doesn't exist"));
  48. }
  49. } // namespace
  50. } // namespace Carbon::Testing