| 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(const string& file) { | 48 string FindRubyTestDir() { |
| 49 // Inspired by TestSourceDir() in src/google/protobuf/testing/googletest.cc. | 49 return TestSourceDir() + "/google/protobuf/compiler/ruby"; |
| 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 | |
| 64 } | 50 } |
| 65 | 51 |
| 66 // This test is a simple golden-file test over the output of the Ruby code | 52 // This test is a simple golden-file test over the output of the Ruby code |
| 67 // generator. When we make changes to the Ruby extension and alter the Ruby code | 53 // generator. When we make changes to the Ruby extension and alter the Ruby code |
| 68 // generator to use those changes, we should (i) manually test the output of the | 54 // generator to use those changes, we should (i) manually test the output of the |
| 69 // code generator with the extension, and (ii) update the golden output above. | 55 // code generator with the extension, and (ii) update the golden output above. |
| 70 // Some day, we may integrate build systems between protoc and the language | 56 // Some day, we may integrate build systems between protoc and the language |
| 71 // extensions to the point where we can do this test in a more automated way. | 57 // extensions to the point where we can do this test in a more automated way. |
| 72 | 58 |
| 73 TEST(RubyGeneratorTest, GeneratorTest) { | 59 TEST(RubyGeneratorTest, GeneratorTest) { |
| 74 string ruby_tests = FindRubyTestDir("/ruby_generated_code.proto"); | 60 string ruby_tests = FindRubyTestDir(); |
| 75 | 61 |
| 76 google::protobuf::compiler::CommandLineInterface cli; | 62 google::protobuf::compiler::CommandLineInterface cli; |
| 77 cli.SetInputsAreProtoPathRelative(true); | 63 cli.SetInputsAreProtoPathRelative(true); |
| 78 | 64 |
| 79 ruby::Generator ruby_generator; | 65 ruby::Generator ruby_generator; |
| 80 cli.RegisterGenerator("--ruby_out", &ruby_generator, ""); | 66 cli.RegisterGenerator("--ruby_out", &ruby_generator, ""); |
| 81 | 67 |
| 82 // Copy generated_code.proto to the temporary test directory. | 68 // Copy generated_code.proto to the temporary test directory. |
| 83 string test_input; | 69 string test_input; |
| 84 GOOGLE_CHECK_OK(File::GetContents( | 70 GOOGLE_CHECK_OK(File::GetContents( |
| (...skipping 13 matching lines...) Expand all Loading... |
| 98 ruby_out.c_str(), | 84 ruby_out.c_str(), |
| 99 proto_path.c_str(), | 85 proto_path.c_str(), |
| 100 "ruby_generated_code.proto", | 86 "ruby_generated_code.proto", |
| 101 }; | 87 }; |
| 102 | 88 |
| 103 EXPECT_EQ(0, cli.Run(4, argv)); | 89 EXPECT_EQ(0, cli.Run(4, argv)); |
| 104 | 90 |
| 105 // Load the generated output and compare to the expected result. | 91 // Load the generated output and compare to the expected result. |
| 106 string output; | 92 string output; |
| 107 GOOGLE_CHECK_OK(File::GetContents( | 93 GOOGLE_CHECK_OK(File::GetContents( |
| 108 TestTempDir() + "/ruby_generated_code.rb", | 94 TestTempDir() + "/ruby_generated_code_pb.rb", |
| 109 &output, | 95 &output, |
| 110 true)); | 96 true)); |
| 111 string expected_output; | 97 string expected_output; |
| 112 GOOGLE_CHECK_OK(File::GetContents( | 98 GOOGLE_CHECK_OK(File::GetContents( |
| 113 ruby_tests + "/ruby_generated_code.rb", | 99 ruby_tests + "/ruby_generated_code_pb.rb", |
| 114 &expected_output, | 100 &expected_output, |
| 115 true)); | 101 true)); |
| 116 EXPECT_EQ(expected_output, output); | 102 EXPECT_EQ(expected_output, output); |
| 117 } | 103 } |
| 118 | 104 |
| 119 } // namespace | 105 } // namespace |
| 120 } // namespace ruby | 106 } // namespace ruby |
| 121 } // namespace compiler | 107 } // namespace compiler |
| 122 } // namespace protobuf | 108 } // namespace protobuf |
| 123 } // namespace google | 109 } // namespace google |
| OLD | NEW |