fuzzer_util_test.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. namespace Carbon::Testing {
  11. namespace {
  12. TEST(FuzzerUtilTest, ParseAndExecute) {
  13. const ErrorOr<Fuzzing::Carbon> carbon_proto = ParseCarbonTextProto(R"(
  14. compilation_unit {
  15. package_statement { package_name: "P" }
  16. is_api: true
  17. declarations {
  18. function {
  19. name: "Main"
  20. param_pattern {}
  21. return_term {
  22. kind: Expression
  23. type { int_type_literal {} }
  24. }
  25. body {
  26. statements {
  27. return_expression_statement {
  28. expression { int_literal { value: 0 } }
  29. }
  30. }
  31. }
  32. }
  33. }
  34. })");
  35. ASSERT_TRUE(carbon_proto.ok());
  36. const ErrorOr<int> result = ParseAndExecute(carbon_proto->compilation_unit());
  37. ASSERT_TRUE(result.ok()) << "Execution failed: " << result.error();
  38. EXPECT_EQ(*result, 0);
  39. }
  40. TEST(FuzzerUtilTest, GetRunfilesFile) {
  41. EXPECT_THAT(*Internal::GetRunfilesFile("carbon/explorer/data/prelude.carbon"),
  42. testing::EndsWith("/prelude.carbon"));
  43. EXPECT_THAT(Internal::GetRunfilesFile("nonexistent-file").error().message(),
  44. testing::EndsWith("doesn't exist"));
  45. }
  46. TEST(FuzzerUtilTest, ParseCarbonTextProtoWithUnknownField) {
  47. const ErrorOr<Fuzzing::Carbon> carbon_proto =
  48. ParseCarbonTextProto(R"(
  49. compilation_unit {
  50. garbage: "value"
  51. declarations {
  52. choice {
  53. name: "Ch"
  54. }
  55. }
  56. })",
  57. /*allow_unknown=*/true);
  58. ASSERT_TRUE(carbon_proto.ok());
  59. // No EqualsProto in gmock - https://github.com/google/googletest/issues/1761.
  60. EXPECT_EQ(carbon_proto->compilation_unit().declarations(0).choice().name(),
  61. "Ch");
  62. }
  63. } // namespace
  64. } // namespace Carbon::Testing