xref: /aosp_15_r20/external/grpc-grpc/test/cpp/codegen/golden_file_test.cc (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
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 
19 #include <fstream>
20 #include <sstream>
21 
22 #include <gtest/gtest.h>
23 
24 #include "absl/flags/flag.h"
25 
26 #include "test/core/util/test_config.h"
27 #include "test/cpp/util/test_config.h"
28 
29 ABSL_FLAG(
30     std::string, generated_file_path, "",
31     "path to the directory containing generated files compiler_test.grpc.pb.h "
32     "and compiler_test_mock.grpc.pb.h");
33 
34 const char kGoldenFilePath[] = "test/cpp/codegen/compiler_test_golden";
35 const char kMockGoldenFilePath[] = "test/cpp/codegen/compiler_test_mock_golden";
36 
run_test(const std::basic_string<char> & generated_file,const std::basic_string<char> & golden_file)37 void run_test(const std::basic_string<char>& generated_file,
38               const std::basic_string<char>& golden_file) {
39   std::ifstream generated(generated_file);
40   std::ifstream golden(golden_file);
41 
42   ASSERT_TRUE(generated.good());
43   ASSERT_TRUE(golden.good());
44 
45   std::ostringstream gen_oss;
46   std::ostringstream gold_oss;
47   gen_oss << generated.rdbuf();
48   gold_oss << golden.rdbuf();
49   EXPECT_EQ(gold_oss.str(), gen_oss.str());
50 
51   generated.close();
52   golden.close();
53 }
54 
TEST(GoldenFileTest,TestGeneratedFile)55 TEST(GoldenFileTest, TestGeneratedFile) {
56   run_test(absl::GetFlag(FLAGS_generated_file_path) + "compiler_test.grpc.pb.h",
57            kGoldenFilePath);
58 }
59 
TEST(GoldenMockFileTest,TestGeneratedMockFile)60 TEST(GoldenMockFileTest, TestGeneratedMockFile) {
61   run_test(
62       absl::GetFlag(FLAGS_generated_file_path) + "compiler_test_mock.grpc.pb.h",
63       kMockGoldenFilePath);
64 }
65 
main(int argc,char ** argv)66 int main(int argc, char** argv) {
67   grpc::testing::TestEnvironment env(&argc, argv);
68   ::testing::InitGoogleTest(&argc, argv);
69   grpc::testing::InitTest(&argc, &argv, true);
70   if (absl::GetFlag(FLAGS_generated_file_path).empty()) {
71     absl::SetFlag(&FLAGS_generated_file_path, "gens/src/proto/grpc/testing/");
72   }
73   if (absl::GetFlag(FLAGS_generated_file_path).back() != '/') {
74     absl::SetFlag(&FLAGS_generated_file_path,
75                   absl::GetFlag(FLAGS_generated_file_path).append("/"));
76   }
77   return RUN_ALL_TESTS();
78 }
79