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

allow run command without expectation

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