Extract JSON type check. (#8677)

- Reuse it in `GetMissing`.
- Add test.
This commit is contained in:
Jiaming Yuan
2023-01-17 03:11:07 +08:00
committed by GitHub
parent 9f598efc3e
commit 43152657d4
3 changed files with 76 additions and 12 deletions

View File

@@ -10,6 +10,7 @@
#include "../../../src/common/io.h"
#include "../filesystem.h" // dmlc::TemporaryDirectory
#include "../helpers.h"
#include "dmlc/logging.h"
#include "xgboost/json.h"
#include "xgboost/json_io.h"
#include "xgboost/logging.h"
@@ -675,4 +676,19 @@ TEST(UBJson, Basic) {
ASSERT_FLOAT_EQ(2.71, get<Number>(get<Array>(ret["test"])[0]));
}
}
TEST(Json, TypeCheck) {
Json config{Object{}};
config["foo"] = String{"bar"};
auto test = [&]() { TypeCheck<Number, Integer, Array, I32Array>(config["foo"], "foo"); };
ASSERT_THROW({ test(); }, dmlc::Error);
try {
test();
} catch (dmlc::Error const& e) {
auto err = std::string{e.what()};
ASSERT_NE(err.find("Number"), std::string::npos);
ASSERT_NE(err.find("I32Array"), std::string::npos);
ASSERT_NE(err.find("foo"), std::string::npos);
}
}
} // namespace xgboost