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 <gflags/gflags.h>
23 #include <gtest/gtest.h>
24
25 #include "test/cpp/util/test_config.h"
26
27 // In some distros, gflags is in the namespace google, and in some others,
28 // in gflags. This hack is enabling us to find both.
29 namespace google {}
30 namespace gflags {}
31 using namespace google;
32 using namespace gflags;
33
34 DEFINE_string(
35 generated_file_path, "",
36 "path to the directory containing generated files compiler_test.grpc.pb.h "
37 "and compiler_test_mock.grpc.pb.h");
38
39 const char kGoldenFilePath[] = "test/cpp/codegen/compiler_test_golden";
40 const char kMockGoldenFilePath[] = "test/cpp/codegen/compiler_test_mock_golden";
41
run_test(const std::basic_string<char> & generated_file,const std::basic_string<char> & golden_file)42 void run_test(const std::basic_string<char>& generated_file,
43 const std::basic_string<char>& golden_file) {
44 std::ifstream generated(generated_file);
45 std::ifstream golden(golden_file);
46
47 ASSERT_TRUE(generated.good());
48 ASSERT_TRUE(golden.good());
49
50 std::ostringstream gen_oss;
51 std::ostringstream gold_oss;
52 gen_oss << generated.rdbuf();
53 gold_oss << golden.rdbuf();
54 EXPECT_EQ(gold_oss.str(), gen_oss.str());
55
56 generated.close();
57 golden.close();
58 }
59
TEST(GoldenFileTest,TestGeneratedFile)60 TEST(GoldenFileTest, TestGeneratedFile) {
61 run_test(FLAGS_generated_file_path + "compiler_test.grpc.pb.h",
62 kGoldenFilePath);
63 }
64
TEST(GoldenMockFileTest,TestGeneratedMockFile)65 TEST(GoldenMockFileTest, TestGeneratedMockFile) {
66 run_test(FLAGS_generated_file_path + "compiler_test_mock.grpc.pb.h",
67 kMockGoldenFilePath);
68 }
69
main(int argc,char ** argv)70 int main(int argc, char** argv) {
71 ::testing::InitGoogleTest(&argc, argv);
72 grpc::testing::InitTest(&argc, &argv, true);
73 if (FLAGS_generated_file_path.empty()) {
74 FLAGS_generated_file_path = "gens/src/proto/grpc/testing/";
75 }
76 if (FLAGS_generated_file_path.back() != '/')
77 FLAGS_generated_file_path.append("/");
78 return RUN_ALL_TESTS();
79 }
80