conformance_test.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 <functional>
  38. #include <string>
  39. #include <google/protobuf/stubs/common.h>
  40. #include <google/protobuf/util/type_resolver.h>
  41. #include <google/protobuf/wire_format_lite.h>
  42. #include "third_party/jsoncpp/value.h"
  43. #include "third_party/jsoncpp/reader.h"
  44. namespace conformance {
  45. class ConformanceRequest;
  46. class ConformanceResponse;
  47. class TestAllTypes;
  48. } // namespace conformance
  49. namespace google {
  50. namespace protobuf {
  51. class ConformanceTestRunner {
  52. public:
  53. virtual ~ConformanceTestRunner() {}
  54. // Call to run a single conformance test.
  55. //
  56. // "input" is a serialized conformance.ConformanceRequest.
  57. // "output" should be set to a serialized conformance.ConformanceResponse.
  58. //
  59. // If there is any error in running the test itself, set "runtime_error" in
  60. // the response.
  61. virtual void RunTest(const std::string& test_name,
  62. const std::string& input,
  63. std::string* output) = 0;
  64. };
  65. // Class representing the test suite itself. To run it, implement your own
  66. // class derived from ConformanceTestRunner and then write code like:
  67. //
  68. // class MyConformanceTestRunner : public ConformanceTestRunner {
  69. // public:
  70. // virtual void RunTest(...) {
  71. // // INSERT YOUR FRAMEWORK-SPECIFIC CODE HERE.
  72. // }
  73. // };
  74. //
  75. // int main() {
  76. // MyConformanceTestRunner runner;
  77. // google::protobuf::ConformanceTestSuite suite;
  78. //
  79. // std::string output;
  80. // suite.RunSuite(&runner, &output);
  81. // }
  82. //
  83. class ConformanceTestSuite {
  84. public:
  85. ConformanceTestSuite() : verbose_(false) {}
  86. void SetVerbose(bool verbose) { verbose_ = verbose; }
  87. // Sets the list of tests that are expected to fail when RunSuite() is called.
  88. // RunSuite() will fail unless the set of failing tests is exactly the same
  89. // as this list.
  90. void SetFailureList(const std::vector<std::string>& failure_list);
  91. // Run all the conformance tests against the given test runner.
  92. // Test output will be stored in "output".
  93. //
  94. // Returns true if the set of failing tests was exactly the same as the
  95. // failure list. If SetFailureList() was not called, returns true if all
  96. // tests passed.
  97. bool RunSuite(ConformanceTestRunner* runner, std::string* output);
  98. private:
  99. void ReportSuccess(const std::string& test_name);
  100. void ReportFailure(const string& test_name,
  101. const conformance::ConformanceRequest& request,
  102. const conformance::ConformanceResponse& response,
  103. const char* fmt, ...);
  104. void ReportSkip(const string& test_name,
  105. const conformance::ConformanceRequest& request,
  106. const conformance::ConformanceResponse& response);
  107. void RunTest(const std::string& test_name,
  108. const conformance::ConformanceRequest& request,
  109. conformance::ConformanceResponse* response);
  110. void RunValidInputTest(const string& test_name, const string& input,
  111. conformance::WireFormat input_format,
  112. const string& equivalent_text_format,
  113. conformance::WireFormat requested_output);
  114. void RunValidJsonTest(const string& test_name, const string& input_json,
  115. const string& equivalent_text_format);
  116. void RunValidJsonTestWithProtobufInput(const string& test_name,
  117. const conformance::TestAllTypes& input,
  118. const string& equivalent_text_format);
  119. typedef std::function<bool(const Json::Value&)> Validator;
  120. void RunValidJsonTestWithValidator(const string& test_name,
  121. const string& input_json,
  122. const Validator& validator);
  123. void ExpectParseFailureForJson(const string& test_name,
  124. const string& input_json);
  125. void ExpectSerializeFailureForJson(const string& test_name,
  126. const string& text_format);
  127. void ExpectParseFailureForProto(const std::string& proto,
  128. const std::string& test_name);
  129. void ExpectHardParseFailureForProto(const std::string& proto,
  130. const std::string& test_name);
  131. void TestPrematureEOFForType(google::protobuf::FieldDescriptor::Type type);
  132. bool CheckSetEmpty(const set<string>& set_to_check, const char* msg);
  133. ConformanceTestRunner* runner_;
  134. int successes_;
  135. int expected_failures_;
  136. bool verbose_;
  137. std::string output_;
  138. // The set of test names that are expected to fail in this run, but haven't
  139. // failed yet.
  140. std::set<std::string> expected_to_fail_;
  141. // The set of test names that have been run. Used to ensure that there are no
  142. // duplicate names in the suite.
  143. std::set<std::string> test_names_;
  144. // The set of tests that failed, but weren't expected to.
  145. std::set<std::string> unexpected_failing_tests_;
  146. // The set of tests that succeeded, but weren't expected to.
  147. std::set<std::string> unexpected_succeeding_tests_;
  148. // The set of tests that the testee opted out of;
  149. std::set<std::string> skipped_;
  150. google::protobuf::internal::scoped_ptr<google::protobuf::util::TypeResolver>
  151. type_resolver_;
  152. std::string type_url_;
  153. };
  154. } // namespace protobuf
  155. } // namespace google
  156. #endif // CONFORMANCE_CONFORMANCE_TEST_H