PageRenderTime 46ms CodeModel.GetById 39ms RepoModel.GetById 1ms app.codeStats 0ms

/thirdparty/breakpad/third_party/protobuf/protobuf/src/google/protobuf/compiler/python/python_plugin_unittest.cc

http://github.com/tomahawk-player/tomahawk
C++ | 116 lines | 65 code | 14 blank | 37 comment | 0 complexity | 603bdaeefba590a28c80cca1867e181d MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause, GPL-3.0, GPL-2.0
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // http://code.google.com/p/protobuf/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. // Author: kenton@google.com (Kenton Varda)
  31. //
  32. // TODO(kenton): Share code with the versions of this test in other languages?
  33. // It seemed like parameterizing it would add more complexity than it is
  34. // worth.
  35. #include <google/protobuf/compiler/python/python_generator.h>
  36. #include <google/protobuf/compiler/command_line_interface.h>
  37. #include <google/protobuf/io/zero_copy_stream.h>
  38. #include <google/protobuf/io/printer.h>
  39. #include <google/protobuf/testing/googletest.h>
  40. #include <gtest/gtest.h>
  41. #include <google/protobuf/testing/file.h>
  42. namespace google {
  43. namespace protobuf {
  44. namespace compiler {
  45. namespace python {
  46. namespace {
  47. class TestGenerator : public CodeGenerator {
  48. public:
  49. TestGenerator() {}
  50. ~TestGenerator() {}
  51. virtual bool Generate(const FileDescriptor* file,
  52. const string& parameter,
  53. GeneratorContext* context,
  54. string* error) const {
  55. TryInsert("test_pb2.py", "imports", context);
  56. TryInsert("test_pb2.py", "module_scope", context);
  57. TryInsert("test_pb2.py", "class_scope:foo.Bar", context);
  58. TryInsert("test_pb2.py", "class_scope:foo.Bar.Baz", context);
  59. return true;
  60. }
  61. void TryInsert(const string& filename, const string& insertion_point,
  62. GeneratorContext* context) const {
  63. scoped_ptr<io::ZeroCopyOutputStream> output(
  64. context->OpenForInsert(filename, insertion_point));
  65. io::Printer printer(output.get(), '$');
  66. printer.Print("// inserted $name$\n", "name", insertion_point);
  67. }
  68. };
  69. // This test verifies that all the expected insertion points exist. It does
  70. // not verify that they are correctly-placed; that would require actually
  71. // compiling the output which is a bit more than I care to do for this test.
  72. TEST(PythonPluginTest, PluginTest) {
  73. File::WriteStringToFileOrDie(
  74. "syntax = \"proto2\";\n"
  75. "package foo;\n"
  76. "message Bar {\n"
  77. " message Baz {}\n"
  78. "}\n",
  79. TestTempDir() + "/test.proto");
  80. google::protobuf::compiler::CommandLineInterface cli;
  81. cli.SetInputsAreProtoPathRelative(true);
  82. python::Generator python_generator;
  83. TestGenerator test_generator;
  84. cli.RegisterGenerator("--python_out", &python_generator, "");
  85. cli.RegisterGenerator("--test_out", &test_generator, "");
  86. string proto_path = "-I" + TestTempDir();
  87. string python_out = "--python_out=" + TestTempDir();
  88. string test_out = "--test_out=" + TestTempDir();
  89. const char* argv[] = {
  90. "protoc",
  91. proto_path.c_str(),
  92. python_out.c_str(),
  93. test_out.c_str(),
  94. "test.proto"
  95. };
  96. EXPECT_EQ(0, cli.Run(5, argv));
  97. }
  98. } // namespace
  99. } // namespace python
  100. } // namespace compiler
  101. } // namespace protobuf
  102. } // namespace google