Commit af54a7e8 authored by Patrick Chen's avatar Patrick Chen

wrap expected info as object in config

parent 337ed1f3
......@@ -11,9 +11,11 @@
"script": [
{
"command": "GetMessage",
"expect": "Hello, Master!",
"runs": ["GetName"],
"posts": ["NotifyGetMessage"]
"expect": {
"response": "Hello, Master!",
"runs": ["GetName"],
"posts": ["NotifyGetMessage"]
}
}
]
}
......@@ -23,9 +23,6 @@ namespace xs { namespace test
explicit Expectation(const nlohmann::json& object);
static bool verify(const nlohmann::json& object) noexcept;
private:
Expectation(std::string name, nlohmann::json value) noexcept;
public:
std::string name_;
nlohmann::json value_;
......
......@@ -7,10 +7,13 @@
namespace xs { namespace test
{
Expectation::Expectation(const nlohmann::json& object)
: Expectation(object["command"].get<std::string>(), object["expect"])
: name_(object["command"].get<std::string>())
{
if (object.count("runs")) {
for (const auto& element : object["runs"]) {
const auto& expect = object["expect"];
value_ = expect["response"];
if (expect.count("runs")) {
for (const auto& element : expect["runs"]) {
if (!element.is_string()) {
throw std::runtime_error("element in \"runs\" is not string");
}
......@@ -18,8 +21,8 @@ namespace xs { namespace test
}
}
if (object.count("posts")) {
for (const auto& element : object["posts"]) {
if (expect.count("posts")) {
for (const auto& element : expect["posts"]) {
if (!element.is_string()) {
throw std::runtime_error("element in \"posts\" is not string");
}
......@@ -30,30 +33,35 @@ namespace xs { namespace test
bool Expectation::verify(const nlohmann::json& object) noexcept
{
if (!(object.is_object()
&& object.count("command")
&& object.count("expect"))) {
if (!object.is_object()) {
return false;
}
if (object.count("runs") && !object["runs"].is_array()) {
if (!(object.count("command") && object["command"].is_string())) {
return false;
}
if (object.count("posts") && !object["posts"].is_array()) {
if (!(object.count("expect") && object["expect"].is_object())) {
return false;
}
return (
object["command"].is_string()
&& (
object["expect"].is_string() || object["expect"].is_object()
)
);
}
const auto& expect = object["expect"];
if (!expect.count("response")) {
return false;
}
if (!(expect["response"].is_string() || expect["response"].is_object())) {
return false;
}
Expectation::Expectation(std::string name, nlohmann::json value) noexcept
: name_(std::move(name))
, value_(std::move(value))
{ }
if (expect.count("runs") && !expect["runs"].is_array()) {
return false;
}
if (expect.count("posts") && !expect["posts"].is_array()) {
return false;
}
return true;
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment