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