proto_to_carbon_test.cpp 1.8 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 "testing/fuzzing/proto_to_carbon.h"
  5. #include <gmock/gmock.h>
  6. #include <gtest/gtest.h>
  7. #include "common/error.h"
  8. #include "testing/fuzzing/carbon.pb.h"
  9. namespace Carbon::Testing {
  10. namespace {
  11. TEST(FuzzerUtilTest, ProtoToCarbon) {
  12. const ErrorOr<Fuzzing::Carbon> carbon_proto = ParseCarbonTextProto(R"(
  13. compilation_unit {
  14. package_statement { package_name: "P" }
  15. is_api: true
  16. declarations {
  17. function {
  18. name {
  19. name: "Main"
  20. }
  21. param_pattern {}
  22. return_term {
  23. kind: Expression
  24. type { int_type_literal {} }
  25. }
  26. body {
  27. statements {
  28. return_expression_statement {
  29. expression { int_literal { value: 0 } }
  30. }
  31. }
  32. }
  33. }
  34. }
  35. })");
  36. ASSERT_TRUE(carbon_proto.ok());
  37. static constexpr char SourceCode[] = R"(// Generated by proto_to_carbon.
  38. package P api;
  39. fn Main() -> i32
  40. {
  41. return 0;
  42. }
  43. )";
  44. EXPECT_THAT(ProtoToCarbon(*carbon_proto, /*maybe_add_main=*/false),
  45. testing::Eq(SourceCode));
  46. EXPECT_THAT(ProtoToCarbon(*carbon_proto, /*maybe_add_main=*/true),
  47. testing::Eq(SourceCode));
  48. }
  49. TEST(FuzzerUtilTest, ParseCarbonTextProtoWithUnknownField) {
  50. const ErrorOr<Fuzzing::Carbon> carbon_proto = ParseCarbonTextProto(R"(
  51. compilation_unit {
  52. garbage: "value"
  53. declarations {
  54. choice {
  55. name {
  56. name: "Ch"
  57. }
  58. }
  59. }
  60. })");
  61. ASSERT_FALSE(carbon_proto.ok());
  62. }
  63. } // namespace
  64. } // namespace Carbon::Testing