Commit 59377c67 authored by Patrick Chen's avatar Patrick Chen

allow run command without expectation

parent a587a988
...@@ -16,9 +16,6 @@ ...@@ -16,9 +16,6 @@
"param": { "param": {
"uuid": "A667E644-7F1B-4F8C-A939-24C596E4847C", "uuid": "A667E644-7F1B-4F8C-A939-24C596E4847C",
"active": true "active": true
},
"expect": {
"response": {}
} }
}, },
{ {
......
#ifndef XS_TEST_EXPECTATION_H_INCLUDED #ifndef XS_TEST_EXPECTATION_H_INCLUDED
#define XS_TEST_EXPECTATION_H_INCLUDED #define XS_TEST_EXPECTATION_H_INCLUDED
#include <boost/optional.hpp>
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
#include <string> #include <string>
...@@ -26,7 +27,7 @@ namespace xs { namespace test ...@@ -26,7 +27,7 @@ namespace xs { namespace test
public: public:
std::string name_; std::string name_;
nlohmann::json request_; nlohmann::json request_;
nlohmann::json response_; boost::optional<nlohmann::json> response_;
std::unordered_set<std::string> runs_; std::unordered_set<std::string> runs_;
std::unordered_set<std::string> posts_; std::unordered_set<std::string> posts_;
}; };
......
...@@ -11,6 +11,10 @@ namespace xs { namespace test ...@@ -11,6 +11,10 @@ namespace xs { namespace test
{ {
request_ = object["param"]; request_ = object["param"];
if (!object.count("expect")) {
return;
}
const auto& expect = object["expect"]; const auto& expect = object["expect"];
response_ = expect["response"]; response_ = expect["response"];
...@@ -51,7 +55,11 @@ namespace xs { namespace test ...@@ -51,7 +55,11 @@ namespace xs { namespace test
return false; return false;
} }
if (!(object.count("expect") && object["expect"].is_object())) { if (!object.count("expect")) {
return true;
}
if (!object["expect"].is_object()) {
return false; return false;
} }
......
...@@ -56,12 +56,17 @@ void Tester::resetCounters() noexcept ...@@ -56,12 +56,17 @@ void Tester::resetCounters() noexcept
Tester::Report Tester::createReport(const std::string& response, const Expectation& expectation) noexcept Tester::Report Tester::createReport(const std::string& response, const Expectation& expectation) noexcept
{ {
Report report; Report report{ true };
if (expectation.response_.is_string()) { // execute withou expected response
report.pass = (response == expectation.response_.get<std::string>()); if (!expectation.response_) {
return report;
}
if (expectation.response_->is_string()) {
report.pass = (response == expectation.response_->get<std::string>());
} else { } else {
try { try {
report.pass = (nlohmann::json::parse(response) == expectation.response_); report.pass = (nlohmann::json::parse(response) == *expectation.response_);
} catch (...) { } catch (...) {
} }
...@@ -150,7 +155,7 @@ void Tester::run() noexcept ...@@ -150,7 +155,7 @@ void Tester::run() noexcept
std::cout << "[FAILED] run command: " << expectation.name_ << std::endl; std::cout << "[FAILED] run command: " << expectation.name_ << std::endl;
if (report.missedCommands.empty() && report.missedEvents.empty()) { if (report.missedCommands.empty() && report.missedEvents.empty()) {
std::cout << boost::format(R"([REASON] unexpected response: "%1%"(real) vs %2%(expect))") std::cout << boost::format(R"([REASON] unexpected response: "%1%"(real) vs %2%(expect))")
% response % detail::toString(expectation.response_) % response % detail::toString(*expectation.response_)
<< std::endl; << std::endl;
} else if (!report.missedCommands.empty()) { } else if (!report.missedCommands.empty()) {
std::cout << boost::format("[REASON] missed commands: [%1%]") std::cout << boost::format("[REASON] missed commands: [%1%]")
......
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