Commit 5d7ea9c4 authored by Patrick Chen's avatar Patrick Chen

add params in expectation

parent af54a7e8
......@@ -32,7 +32,7 @@ XSTEST_OBJS=$(subst $(REPO_DIR),$(BUILD_DIR),$(XSTEST_SRCS:.cc=.o))
GREETING_SRCS=$(REPO_DIR)/example/greeting.cc
GREETING_OBJS=$(subst $(REPO_DIR),$(BUILD_DIR),$(GREETING_SRCS:.cc=.o))
all: $(BUILD_DIR)/xs-test
all: $(BUILD_DIR)/xs-test example
$(BUILD_DIR)/xs-test: $(XSTEST_OBJS)
$(CXX) -o $@ $(XSTEST_OBJS) $(LD_FLAGS)
......
......@@ -11,6 +11,7 @@
"script": [
{
"command": "GetMessage",
"params": {},
"expect": {
"response": "Hello, Master!",
"runs": ["GetName"],
......
......@@ -9,7 +9,7 @@
namespace xs { namespace test
{
class Expectation
class Expectation final
{
public:
Expectation() = delete;
......@@ -25,7 +25,8 @@ namespace xs { namespace test
public:
std::string name_;
nlohmann::json value_;
nlohmann::json params_;
nlohmann::json response_;
std::unordered_set<std::string> runs_;
std::unordered_set<std::string> posts_;
};
......
......@@ -9,8 +9,10 @@ namespace xs { namespace test
Expectation::Expectation(const nlohmann::json& object)
: name_(object["command"].get<std::string>())
{
params_ = object["params"];
const auto& expect = object["expect"];
value_ = expect["response"];
response_ = expect["response"];
if (expect.count("runs")) {
for (const auto& element : expect["runs"]) {
......@@ -41,6 +43,14 @@ namespace xs { namespace test
return false;
}
if (!object.count("params")) {
return false;
}
if (!(object["params"].is_string() || object["params"].is_object())) {
return false;
}
if (!(object.count("expect") && object["expect"].is_object())) {
return false;
}
......
......@@ -62,11 +62,11 @@ void Tester::resetCounters() noexcept
Tester::Report Tester::createReport(const std::string& response, const Expectation& expectation) noexcept
{
Report report;
if (expectation.value_.is_string()) {
report.pass = (response == expectation.value_.get<std::string>());
if (expectation.response_.is_string()) {
report.pass = (response == expectation.response_.get<std::string>());
} else {
try {
report.pass = (nlohmann::json::parse(response) == expectation.value_);
report.pass = (nlohmann::json::parse(response) == expectation.response_);
} catch (...) {
}
......@@ -114,7 +114,7 @@ void Tester::run() noexcept
for (const auto& expectation : config_.getExpectations()) {
resetCounters();
const auto response = server_.runCmd(expectation.name_, "");
const auto response = server_.runCmd(expectation.name_, expectation.params_.dump());
// wait 100ms for upcoming events
using namespace std::chrono_literals;
......@@ -130,7 +130,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.value_)
% 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