buffet: Raw string literals for some JSON constants in tests
Added EXPECT_JSON_EQ() macro to help compare JSON values without being
influenced by string formatting and whitespace. This allows us to use
raw literal strings in conditions which improves readability of the
tests.
BUG=None
TEST=FEATURE=test emerge-gizmo buffet
Change-Id: Ibc6642a053dd3e6f3b667de31ceefa21f03e85bf
Reviewed-on: https://chromium-review.googlesource.com/268961
Reviewed-by: Alex Vakulenko <avakulenko@chromium.org>
Commit-Queue: Vitaly Buka <vitalybuka@chromium.org>
Tested-by: Vitaly Buka <vitalybuka@chromium.org>
diff --git a/buffet/commands/unittest_utils.cc b/buffet/commands/unittest_utils.cc
index db8aa4a..be4d4e7 100644
--- a/buffet/commands/unittest_utils.cc
+++ b/buffet/commands/unittest_utils.cc
@@ -14,25 +14,22 @@
std::string json2(json);
// Convert apostrophes to double-quotes so JSONReader can parse the string.
std::replace(json2.begin(), json2.end(), '\'', '"');
- return std::unique_ptr<base::Value>(base::JSONReader::Read(json2));
+ int error = 0;
+ std::string message;
+ std::unique_ptr<base::Value> value{base::JSONReader::ReadAndReturnError(
+ json2, base::JSON_PARSE_RFC, &error, &message)};
+ CHECK(value) << "Failed to load JSON: " << message << ", " << json;
+ return value;
}
std::unique_ptr<base::DictionaryValue> CreateDictionaryValue(const char* json) {
- std::string json2(json);
- std::replace(json2.begin(), json2.end(), '\'', '"');
- base::Value* value = base::JSONReader::Read(json2);
- CHECK(value) << "Failed to load JSON: " << json2;
+ std::unique_ptr<base::Value> value = CreateValue(json);
base::DictionaryValue* dict = nullptr;
value->GetAsDictionary(&dict);
+ CHECK(dict) << "Value is not dictionary: " << json;
+ value.release();
return std::unique_ptr<base::DictionaryValue>(dict);
}
-std::string ValueToString(const base::Value* value) {
- std::string json;
- base::JSONWriter::Write(value, &json);
- std::replace(json.begin(), json.end(), '"', '\'');
- return json;
-}
-
} // namespace unittests
} // namespace buffet