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