OLD | NEW |
1 // Protocol Buffers - Google's data interchange format | 1 // Protocol Buffers - Google's data interchange format |
2 // Copyright 2014 Google Inc. All rights reserved. | 2 // Copyright 2014 Google Inc. All rights reserved. |
3 // https://developers.google.com/protocol-buffers/ | 3 // https://developers.google.com/protocol-buffers/ |
4 // | 4 // |
5 // Redistribution and use in source and binary forms, with or without | 5 // Redistribution and use in source and binary forms, with or without |
6 // modification, are permitted provided that the following conditions are | 6 // modification, are permitted provided that the following conditions are |
7 // met: | 7 // met: |
8 // | 8 // |
9 // * Redistributions of source code must retain the above copyright | 9 // * Redistributions of source code must retain the above copyright |
10 // notice, this list of conditions and the following disclaimer. | 10 // notice, this list of conditions and the following disclaimer. |
(...skipping 27 matching lines...) Expand all Loading... |
38 #include <google/protobuf/testing/googletest.h> | 38 #include <google/protobuf/testing/googletest.h> |
39 #include <gtest/gtest.h> | 39 #include <gtest/gtest.h> |
40 #include <google/protobuf/testing/file.h> | 40 #include <google/protobuf/testing/file.h> |
41 | 41 |
42 namespace google { | 42 namespace google { |
43 namespace protobuf { | 43 namespace protobuf { |
44 namespace compiler { | 44 namespace compiler { |
45 namespace ruby { | 45 namespace ruby { |
46 namespace { | 46 namespace { |
47 | 47 |
48 string FindRubyTestDir() { | 48 string FindRubyTestDir(const string& file) { |
49 return TestSourceDir() + "/google/protobuf/compiler/ruby"; | 49 // Inspired by TestSourceDir() in src/google/protobuf/testing/googletest.cc. |
| 50 #ifndef GOOGLE_THIRD_PARTY_PROTOBUF |
| 51 string prefix = "."; |
| 52 while (!File::Exists(prefix + "/src/google/protobuf/compiler/ruby" + file)) { |
| 53 if (!File::Exists(prefix)) { |
| 54 GOOGLE_LOG(FATAL) |
| 55 << "Could not find Ruby test directory. Please run tests from " |
| 56 "somewhere within the protobuf source package."; |
| 57 } |
| 58 prefix += "/.."; |
| 59 } |
| 60 return prefix + "/src/google/protobuf/compiler/ruby"; |
| 61 #else |
| 62 return "third_party/protobuf/src/google/protobuf/compiler/ruby"; |
| 63 #endif // GOOGLE_THIRD_PARTY_PROTOBUF |
50 } | 64 } |
51 | 65 |
52 // This test is a simple golden-file test over the output of the Ruby code | 66 // This test is a simple golden-file test over the output of the Ruby code |
53 // generator. When we make changes to the Ruby extension and alter the Ruby code | 67 // generator. When we make changes to the Ruby extension and alter the Ruby code |
54 // generator to use those changes, we should (i) manually test the output of the | 68 // generator to use those changes, we should (i) manually test the output of the |
55 // code generator with the extension, and (ii) update the golden output above. | 69 // code generator with the extension, and (ii) update the golden output above. |
56 // Some day, we may integrate build systems between protoc and the language | 70 // Some day, we may integrate build systems between protoc and the language |
57 // extensions to the point where we can do this test in a more automated way. | 71 // extensions to the point where we can do this test in a more automated way. |
58 | 72 |
59 TEST(RubyGeneratorTest, GeneratorTest) { | 73 TEST(RubyGeneratorTest, GeneratorTest) { |
60 string ruby_tests = FindRubyTestDir(); | 74 string ruby_tests = FindRubyTestDir("/ruby_generated_code.proto"); |
61 | 75 |
62 google::protobuf::compiler::CommandLineInterface cli; | 76 google::protobuf::compiler::CommandLineInterface cli; |
63 cli.SetInputsAreProtoPathRelative(true); | 77 cli.SetInputsAreProtoPathRelative(true); |
64 | 78 |
65 ruby::Generator ruby_generator; | 79 ruby::Generator ruby_generator; |
66 cli.RegisterGenerator("--ruby_out", &ruby_generator, ""); | 80 cli.RegisterGenerator("--ruby_out", &ruby_generator, ""); |
67 | 81 |
68 // Copy generated_code.proto to the temporary test directory. | 82 // Copy generated_code.proto to the temporary test directory. |
69 string test_input; | 83 string test_input; |
70 GOOGLE_CHECK_OK(File::GetContents( | 84 GOOGLE_CHECK_OK(File::GetContents( |
(...skipping 13 matching lines...) Expand all Loading... |
84 ruby_out.c_str(), | 98 ruby_out.c_str(), |
85 proto_path.c_str(), | 99 proto_path.c_str(), |
86 "ruby_generated_code.proto", | 100 "ruby_generated_code.proto", |
87 }; | 101 }; |
88 | 102 |
89 EXPECT_EQ(0, cli.Run(4, argv)); | 103 EXPECT_EQ(0, cli.Run(4, argv)); |
90 | 104 |
91 // Load the generated output and compare to the expected result. | 105 // Load the generated output and compare to the expected result. |
92 string output; | 106 string output; |
93 GOOGLE_CHECK_OK(File::GetContents( | 107 GOOGLE_CHECK_OK(File::GetContents( |
94 TestTempDir() + "/ruby_generated_code_pb.rb", | 108 TestTempDir() + "/ruby_generated_code.rb", |
95 &output, | 109 &output, |
96 true)); | 110 true)); |
97 string expected_output; | 111 string expected_output; |
98 GOOGLE_CHECK_OK(File::GetContents( | 112 GOOGLE_CHECK_OK(File::GetContents( |
99 ruby_tests + "/ruby_generated_code_pb.rb", | 113 ruby_tests + "/ruby_generated_code.rb", |
100 &expected_output, | 114 &expected_output, |
101 true)); | 115 true)); |
102 EXPECT_EQ(expected_output, output); | 116 EXPECT_EQ(expected_output, output); |
103 } | 117 } |
104 | 118 |
105 } // namespace | 119 } // namespace |
106 } // namespace ruby | 120 } // namespace ruby |
107 } // namespace compiler | 121 } // namespace compiler |
108 } // namespace protobuf | 122 } // namespace protobuf |
109 } // namespace google | 123 } // namespace google |
OLD | NEW |