golden_file_test.cc 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. *
  3. * Copyright 2016 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. #include <fstream>
  19. #include <sstream>
  20. #include <gtest/gtest.h>
  21. #include "absl/flags/flag.h"
  22. #include "test/core/util/test_config.h"
  23. #include "test/cpp/util/test_config.h"
  24. ABSL_FLAG(
  25. std::string, generated_file_path, "",
  26. "path to the directory containing generated files compiler_test.grpc.pb.h "
  27. "and compiler_test_mock.grpc.pb.h");
  28. const char kGoldenFilePath[] = "test/cpp/codegen/compiler_test_golden";
  29. const char kMockGoldenFilePath[] = "test/cpp/codegen/compiler_test_mock_golden";
  30. void run_test(const std::basic_string<char>& generated_file,
  31. const std::basic_string<char>& golden_file) {
  32. std::ifstream generated(generated_file);
  33. std::ifstream golden(golden_file);
  34. ASSERT_TRUE(generated.good());
  35. ASSERT_TRUE(golden.good());
  36. std::ostringstream gen_oss;
  37. std::ostringstream gold_oss;
  38. gen_oss << generated.rdbuf();
  39. gold_oss << golden.rdbuf();
  40. EXPECT_EQ(gold_oss.str(), gen_oss.str());
  41. generated.close();
  42. golden.close();
  43. }
  44. TEST(GoldenFileTest, TestGeneratedFile) {
  45. run_test(absl::GetFlag(FLAGS_generated_file_path) + "compiler_test.grpc.pb.h",
  46. kGoldenFilePath);
  47. }
  48. TEST(GoldenMockFileTest, TestGeneratedMockFile) {
  49. run_test(
  50. absl::GetFlag(FLAGS_generated_file_path) + "compiler_test_mock.grpc.pb.h",
  51. kMockGoldenFilePath);
  52. }
  53. int main(int argc, char** argv) {
  54. grpc::testing::TestEnvironment env(argc, argv);
  55. ::testing::InitGoogleTest(&argc, argv);
  56. grpc::testing::InitTest(&argc, &argv, true);
  57. if (absl::GetFlag(FLAGS_generated_file_path).empty()) {
  58. absl::SetFlag(&FLAGS_generated_file_path, "gens/src/proto/grpc/testing/");
  59. }
  60. if (absl::GetFlag(FLAGS_generated_file_path).back() != '/') {
  61. absl::SetFlag(&FLAGS_generated_file_path,
  62. absl::GetFlag(FLAGS_generated_file_path).append("/"));
  63. }
  64. return RUN_ALL_TESTS();
  65. }