30 using namespace folly;
43 return coerce ?
k.asString() :
k;
48 p.
required(good_k, [&]() {
throw std::runtime_error(
"failsauce"); });
51 auto parse_error = errors.
at(
"nested").at(coerce_fn(good_k));
55 auto key_error = errors.at(
"key_errors").at(coerce_fn(missing_k));
72 TEST(TestDynamicParser, CoercedAndUncoercedKeys) {
80 TEST(TestDynamicParser, OnErrorThrowSuccess) {
90 TEST(TestDynamicParser, OnErrorThrowError) {
96 FAIL() <<
"Should have thrown";
100 error.at(
"nested").at(
"0").at(
"nested").at(
"int").at(
"error");
103 "DynamicParserParseError: .*Invalid leading.*", ex.what());
113 <<
"THROW releases the first error eagerly, and throws";
119 TEST(TestDynamicParser, AllParserFeaturesSuccess) {
128 std::map<std::string, double> doubles;
130 std::vector<std::string>
strings;
135 std::vector<bool> bools;
145 doubles.emplace(k, v2);
161 strings.emplace_back(v2);
191 p.
optional(
"not set", [&](
bool) {
FAIL() <<
"No key 'not set'"; });
211 EXPECT_EQ((decltype(doubles){{
"a", 7.}, {
"b", 9.}, {
"c", 13.3}}), doubles);
213 EXPECT_EQ((decltype(strings){
"x",
"y",
"1",
"z"}), strings);
219 EXPECT_EQ((decltype(bools){
false,
true,
false,
true}), bools);
223 template <
typename Fn>
232 auto x_key_msg = errors.
at(
"key_errors").at(
"x");
234 auto y_key_msg = errors.at(
"key_errors").at(
"y");
236 auto parse_msg = errors.at(
"error");
242 (
"value", d)), errors);
248 TEST(TestDynamicParser, TestKeyAndParseErrors) {
256 "Couldn't find key (x|y) .*",
271 TEST(TestDynamicParser, TestRequiredOptionalParseErrors) {
279 auto error = errors.at(
"nested").at(k);
286 (
"x", get_expected_error_fn(
"x",
"TypeError: .* but had type `array'"))
287 (
"y", get_expected_error_fn(
"y",
".*Invalid leading character.*"))
288 (
"z", get_expected_error_fn(
"z",
"CUSTOM")))), errors);
292 template <
typename Fn>
303 auto error = errors.at(
"nested").at(err_k);
316 TEST(TestDynamicParser, TestItemParseErrors) {
324 "TypeError: .* but had type `array'");
330 ".*Non-whitespace.*");
334 TEST(TestDynamicParser, TestErrorNesting) {
352 throw std::runtime_error(
"custom parse error");
355 p.
required(
"also not a key", []() {});
356 throw std::runtime_error(
"another parse error");
363 auto& base = errors.
at(
"nested").at(
"x").at(
"nested").at(
"0");
365 base.at(
"nested").at(
"y").at(
"key_errors").at(
"not a key");
366 auto innermost_key_err = base.at(
"nested")
372 auto outer_key_err = base.at(
"key_errors").at(
"also not a key");
373 auto root_key_err = errors.at(
"key_errors").at(
"non-key");
374 auto k_parse_err = errors.at(
"nested").at(
"k").at(
"error");
383 (
"value",
"non-object")
386 (
"error",
"custom parse error")
390 (
"error",
"another parse error")
400 TEST(TestDynamicParser, TestRecordThrowOnDoubleParseErrors) {
406 FAIL() <<
"Should throw on double-parsing a value with an error";
412 TEST(TestDynamicParser, TestRecordThrowOnChangingValue) {
419 FAIL() <<
"Should throw on second error with a changing value";
423 ".*Overwriting value: (0|null) with 5 for error TypeError: .*",
428 TEST(TestDynamicParser, TestThrowOnReleaseWhileParsing) {
435 TEST(TestDynamicParser, TestThrowOnReleaseTwice) {
442 TEST(TestDynamicParser, TestThrowOnNullValue) {
449 TEST(TestDynamicParser, TestThrowOnKeyOutsideCallback) {
static ObjectMaker object()
#define EXPECT_THROW(statement, expected_exception)
void required(const folly::dynamic &key, Fn)
#define EXPECT_PCRE_MATCH(pattern_stringpiece, target_stringpiece)
#define EXPECT_EQ(val1, val2)
void checkCoercedAndUncoercedKeys(dynamic good_k, dynamic missing_k)
const std::string & getString() const &
—— Concurrent Priority Queue Implementation ——
requires And< SemiMovable< VN >... > &&SemiMovable< E > auto error(E e)
DynamicParser & setAllowNonStringKeyErrors(bool b)
void checkMaybeCoercedKeys(bool coerce, dynamic good_k, dynamic missing_k)
void optional(const folly::dynamic &key, Fn)
const folly::dynamic & value() const
std::string asString() const
const folly::dynamic & error() const
#define EXPECT_TRUE(condition)
void checkItemParseError(dynamic d, Fn fn, dynamic real_k, dynamic err_k, std::string re)
static void array(EmptyArrayTag)
FOLLY_CPP14_CONSTEXPR const Value & value() const &
IfIsNonStringDynamicConvertible< K, dynamic const & > at(K &&) const &
#define EXPECT_FALSE(condition)
static vector< fbstring > strings
const folly::dynamic & key() const
folly::dynamic releaseErrors()
TEST(SequencedExecutor, CPUThreadPoolExecutor)
void checkXYKeyErrorsAndParseError(const dynamic &d, Fn fn, std::string key_re, std::string parse_re)