conformance_test.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // https://developers.google.com/protocol-buffers/
  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. // This file defines a protocol for running the conformance test suite
  31. // in-process. In other words, the suite itself will run in the same process as
  32. // the code under test.
  33. //
  34. // For pros and cons of this approach, please see conformance.proto.
  35. #ifndef CONFORMANCE_CONFORMANCE_TEST_H
  36. #define CONFORMANCE_CONFORMANCE_TEST_H
  37. #include <string>
  38. #include <google/protobuf/wire_format_lite.h>
  39. namespace conformance {
  40. class ConformanceRequest;
  41. class ConformanceResponse;
  42. } // namespace conformance
  43. namespace google {
  44. namespace protobuf {
  45. class ConformanceTestRunner {
  46. public:
  47. // Call to run a single conformance test.
  48. //
  49. // "input" is a serialized conformance.ConformanceRequest.
  50. // "output" should be set to a serialized conformance.ConformanceResponse.
  51. //
  52. // If there is any error in running the test itself, set "runtime_error" in
  53. // the response.
  54. virtual void RunTest(const std::string& input, std::string* output) = 0;
  55. };
  56. // Class representing the test suite itself. To run it, implement your own
  57. // class derived from ConformanceTestRunner and then write code like:
  58. //
  59. // class MyConformanceTestRunner : public ConformanceTestRunner {
  60. // public:
  61. // virtual void RunTest(...) {
  62. // // INSERT YOUR FRAMEWORK-SPECIFIC CODE HERE.
  63. // }
  64. // };
  65. //
  66. // int main() {
  67. // MyConformanceTestRunner runner;
  68. // google::protobuf::ConformanceTestSuite suite;
  69. //
  70. // std::string output;
  71. // suite.RunSuite(&runner, &output);
  72. // }
  73. //
  74. class ConformanceTestSuite {
  75. public:
  76. ConformanceTestSuite() : verbose_(false) {}
  77. // Run all the conformance tests against the given test runner.
  78. // Test output will be stored in "output".
  79. void RunSuite(ConformanceTestRunner* runner, std::string* output);
  80. private:
  81. void ReportSuccess();
  82. void ReportFailure(const char* fmt, ...);
  83. void RunTest(const conformance::ConformanceRequest& request,
  84. conformance::ConformanceResponse* response);
  85. void DoExpectParseFailureForProto(const std::string& proto, int line);
  86. void TestPrematureEOFForType(
  87. google::protobuf::internal::WireFormatLite::FieldType type);
  88. ConformanceTestRunner* runner_;
  89. int successes_;
  90. int failures_;
  91. bool verbose_;
  92. std::string output_;
  93. };
  94. } // namespace protobuf
  95. } // namespace google
  96. #endif // CONFORMANCE_CONFORMANCE_TEST_H