Commit af54a7e8 authored by Patrick Chen's avatar Patrick Chen

wrap expected info as object in config

parent 337ed1f3
...@@ -11,9 +11,11 @@ ...@@ -11,9 +11,11 @@
"script": [ "script": [
{ {
"command": "GetMessage", "command": "GetMessage",
"expect": "Hello, Master!", "expect": {
"response": "Hello, Master!",
"runs": ["GetName"], "runs": ["GetName"],
"posts": ["NotifyGetMessage"] "posts": ["NotifyGetMessage"]
} }
}
] ]
} }
...@@ -23,9 +23,6 @@ namespace xs { namespace test ...@@ -23,9 +23,6 @@ namespace xs { namespace test
explicit Expectation(const nlohmann::json& object); explicit Expectation(const nlohmann::json& object);
static bool verify(const nlohmann::json& object) noexcept; static bool verify(const nlohmann::json& object) noexcept;
private:
Expectation(std::string name, nlohmann::json value) noexcept;
public: public:
std::string name_; std::string name_;
nlohmann::json value_; nlohmann::json value_;
......
...@@ -7,10 +7,13 @@ ...@@ -7,10 +7,13 @@
namespace xs { namespace test namespace xs { namespace test
{ {
Expectation::Expectation(const nlohmann::json& object) Expectation::Expectation(const nlohmann::json& object)
: Expectation(object["command"].get<std::string>(), object["expect"]) : name_(object["command"].get<std::string>())
{ {
if (object.count("runs")) { const auto& expect = object["expect"];
for (const auto& element : object["runs"]) { value_ = expect["response"];
if (expect.count("runs")) {
for (const auto& element : expect["runs"]) {
if (!element.is_string()) { if (!element.is_string()) {
throw std::runtime_error("element in \"runs\" is not string"); throw std::runtime_error("element in \"runs\" is not string");
} }
...@@ -18,8 +21,8 @@ namespace xs { namespace test ...@@ -18,8 +21,8 @@ namespace xs { namespace test
} }
} }
if (object.count("posts")) { if (expect.count("posts")) {
for (const auto& element : object["posts"]) { for (const auto& element : expect["posts"]) {
if (!element.is_string()) { if (!element.is_string()) {
throw std::runtime_error("element in \"posts\" is not string"); throw std::runtime_error("element in \"posts\" is not string");
} }
...@@ -30,30 +33,35 @@ namespace xs { namespace test ...@@ -30,30 +33,35 @@ namespace xs { namespace test
bool Expectation::verify(const nlohmann::json& object) noexcept bool Expectation::verify(const nlohmann::json& object) noexcept
{ {
if (!(object.is_object() if (!object.is_object()) {
&& object.count("command") return false;
&& object.count("expect"))) { }
if (!(object.count("command") && object["command"].is_string())) {
return false;
}
if (!(object.count("expect") && object["expect"].is_object())) {
return false;
}
const auto& expect = object["expect"];
if (!expect.count("response")) {
return false; return false;
} }
if (object.count("runs") && !object["runs"].is_array()) { if (!(expect["response"].is_string() || expect["response"].is_object())) {
return false; return false;
} }
if (object.count("posts") && !object["posts"].is_array()) { if (expect.count("runs") && !expect["runs"].is_array()) {
return false; return false;
} }
return ( if (expect.count("posts") && !expect["posts"].is_array()) {
object["command"].is_string() return false;
&& (
object["expect"].is_string() || object["expect"].is_object()
)
);
} }
Expectation::Expectation(std::string name, nlohmann::json value) noexcept return true;
: name_(std::move(name)) }
, value_(std::move(value))
{ }
} } } }
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