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