values_test_util.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef BASE_TEST_VALUES_TEST_UTIL_H_
  5. #define BASE_TEST_VALUES_TEST_UTIL_H_
  6. #include <iosfwd>
  7. #include <memory>
  8. #include <string>
  9. #include "base/strings/string_piece.h"
  10. #include "base/values.h"
  11. #include "testing/gmock/include/gmock/gmock-matchers.h"
  12. namespace base {
  13. // All the functions below expect that the value for the given key in
  14. // the given dictionary equals the given expected value.
  15. void ExpectDictBooleanValue(bool expected_value,
  16. const DictionaryValue& value,
  17. const std::string& key);
  18. void ExpectDictDictionaryValue(const DictionaryValue& expected_value,
  19. const DictionaryValue& value,
  20. const std::string& key);
  21. void ExpectDictIntegerValue(int expected_value,
  22. const DictionaryValue& value,
  23. const std::string& key);
  24. void ExpectDictListValue(const ListValue& expected_value,
  25. const DictionaryValue& value,
  26. const std::string& key);
  27. void ExpectDictStringValue(const std::string& expected_value,
  28. const DictionaryValue& value,
  29. const std::string& key);
  30. void ExpectStringValue(const std::string& expected_str, const Value& actual);
  31. namespace test {
  32. // A custom GMock matcher which matches if a base::Value is a dictionary which
  33. // has a key |key| that is equal to |value|.
  34. testing::Matcher<const base::Value&> DictionaryHasValue(
  35. const std::string& key,
  36. const base::Value& expected_value);
  37. // A custom GMock matcher which matches if a base::Value is a dictionary which
  38. // contains all key/value pairs from |template_value|.
  39. testing::Matcher<const base::Value&> DictionaryHasValues(
  40. const base::Value& template_value);
  41. // A custom GMock matcher. For details, see
  42. // https://github.com/google/googletest/blob/644319b9f06f6ca9bf69fe791be399061044bc3d/googlemock/docs/CookBook.md#writing-new-polymorphic-matchers
  43. class IsJsonMatcher {
  44. public:
  45. explicit IsJsonMatcher(base::StringPiece json);
  46. explicit IsJsonMatcher(const base::Value& value);
  47. IsJsonMatcher(const IsJsonMatcher& other);
  48. ~IsJsonMatcher();
  49. bool MatchAndExplain(base::StringPiece json,
  50. testing::MatchResultListener* listener) const;
  51. bool MatchAndExplain(const base::Value& value,
  52. testing::MatchResultListener* listener) const;
  53. void DescribeTo(std::ostream* os) const;
  54. void DescribeNegationTo(std::ostream* os) const;
  55. private:
  56. IsJsonMatcher& operator=(const IsJsonMatcher& other) = delete;
  57. base::Value expected_value_;
  58. };
  59. // Creates a GMock matcher for testing equivalence of JSON values represented as
  60. // either JSON strings or base::Value objects. Parsing of the expected value
  61. // uses ParseJson(), which allows trailing commas for convenience. Parsing of
  62. // the actual value follows the JSON spec strictly.
  63. //
  64. // Although it possible to use this matcher when the actual and expected values
  65. // are both base::Value objects, there is no advantage in that case to using
  66. // this matcher in place of GMock's normal equality semantics.
  67. template <typename T>
  68. inline testing::PolymorphicMatcher<IsJsonMatcher> IsJson(const T& value) {
  69. return testing::MakePolymorphicMatcher(IsJsonMatcher(value));
  70. }
  71. // Parses |json| as JSON, allowing trailing commas, and returns the resulting
  72. // value. If |json| fails to parse, causes an EXPECT failure and returns the
  73. // Null Value.
  74. Value ParseJson(StringPiece json);
  75. // DEPRECATED.
  76. // Parses |json| as JSON, allowing trailing commas, and returns the
  77. // resulting value. If the json fails to parse, causes an EXPECT
  78. // failure and returns the Null Value (but never a NULL pointer).
  79. std::unique_ptr<Value> ParseJsonDeprecated(StringPiece json);
  80. } // namespace test
  81. } // namespace base
  82. #endif // BASE_TEST_VALUES_TEST_UTIL_H_