values_test_util.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 path in
  14. // the given dictionary equals the given expected value.
  15. void ExpectDictBooleanValue(bool expected_value,
  16. const Value::Dict& dict,
  17. StringPiece path);
  18. void ExpectDictIntegerValue(int expected_value,
  19. const Value::Dict& dict,
  20. StringPiece path);
  21. void ExpectDictStringValue(StringPiece expected_value,
  22. const Value::Dict& dict,
  23. StringPiece path);
  24. void ExpectDictValue(const Value& expected_value,
  25. const Value::Dict& dict,
  26. StringPiece path);
  27. void ExpectStringValue(const std::string& expected_str, const Value& actual);
  28. namespace test {
  29. // A custom GMock matcher which matches if a base::Value is a dictionary which
  30. // has a key |key| that is equal to |value|.
  31. testing::Matcher<const base::Value&> DictionaryHasValue(
  32. const std::string& key,
  33. const base::Value& expected_value);
  34. // A custom GMock matcher which matches if a base::Value is a dictionary which
  35. // contains all key/value pairs from |template_value|.
  36. testing::Matcher<const base::Value&> DictionaryHasValues(
  37. const base::Value& template_value);
  38. // A custom GMock matcher. For details, see
  39. // https://github.com/google/googletest/blob/644319b9f06f6ca9bf69fe791be399061044bc3d/googlemock/docs/CookBook.md#writing-new-polymorphic-matchers
  40. class IsJsonMatcher {
  41. public:
  42. explicit IsJsonMatcher(base::StringPiece json);
  43. explicit IsJsonMatcher(const base::Value& value);
  44. IsJsonMatcher(const IsJsonMatcher& other);
  45. ~IsJsonMatcher();
  46. bool MatchAndExplain(base::StringPiece json,
  47. testing::MatchResultListener* listener) const;
  48. bool MatchAndExplain(const base::Value& value,
  49. testing::MatchResultListener* listener) const;
  50. bool MatchAndExplain(const base::Value::Dict& dict,
  51. testing::MatchResultListener* listener) const;
  52. bool MatchAndExplain(const base::Value::List& list,
  53. testing::MatchResultListener* listener) const;
  54. void DescribeTo(std::ostream* os) const;
  55. void DescribeNegationTo(std::ostream* os) const;
  56. private:
  57. IsJsonMatcher& operator=(const IsJsonMatcher& other) = delete;
  58. base::Value expected_value_;
  59. };
  60. // Creates a GMock matcher for testing equivalence of JSON values represented as
  61. // either JSON strings or base::Value objects. Parsing of the expected value
  62. // uses ParseJson(), which allows trailing commas for convenience. Parsing of
  63. // the actual value follows the JSON spec strictly.
  64. //
  65. // Although it possible to use this matcher when the actual and expected values
  66. // are both base::Value objects, there is no advantage in that case to using
  67. // this matcher in place of GMock's normal equality semantics.
  68. template <typename T>
  69. inline testing::PolymorphicMatcher<IsJsonMatcher> IsJson(const T& value) {
  70. return testing::MakePolymorphicMatcher(IsJsonMatcher(value));
  71. }
  72. // Parses `json` as JSON, allowing trailing commas, and returns the resulting
  73. // value. If `json` fails to parse, causes an EXPECT failure and returns the
  74. // Null Value.
  75. Value ParseJson(StringPiece json);
  76. // Just like ParseJson(), except returns Dicts/Lists. If `json` fails to parse
  77. // or is not of the expected type, causes an EXPECT failure and returns an empty
  78. // container.
  79. Value::Dict ParseJsonDict(StringPiece json);
  80. Value::List ParseJsonList(StringPiece json);
  81. // DEPRECATED.
  82. // Parses |json| as JSON, allowing trailing commas, and returns the
  83. // resulting value. If the json fails to parse, causes an EXPECT
  84. // failure and returns the Null Value (but never a NULL pointer).
  85. std::unique_ptr<Value> ParseJsonDeprecated(StringPiece json);
  86. } // namespace test
  87. } // namespace base
  88. #endif // BASE_TEST_VALUES_TEST_UTIL_H_