Commit 1cd1ef2c authored by Patrick Chen's avatar Patrick Chen

allow simple test

parent b0be3542
......@@ -2,13 +2,13 @@
"mockData": [
{
"command": "GetName",
"response": "Patrick"
"response": "Master"
}
],
"script": [
{
"command": "GetGreetingMessage",
"expect": "Hello, Patrick!"
"expect": "Hello, Master!"
}
]
}
......@@ -26,7 +26,7 @@ namespace xs { namespace test
explicit Tester(const boost::filesystem::path& file);
void wait() noexcept;
void run() noexcept;
private:
std::string onCommand(const std::string& name, const std::string& params) noexcept;
......
#include "test/Service.h"
#include "test/Tester.h"
#include <boost/format.hpp>
#include <boost/range/adaptors.hpp>
#include <nlohmann/json.hpp>
#include <chrono>
#include <functional>
......@@ -10,6 +12,14 @@
namespace xs { namespace test {
namespace detail {
std::string toString(const nlohmann::json& object)
{
return object.is_string() ? object.get<std::string>()
: object.dump();
}
}
Tester::Tester(const boost::filesystem::path& file)
: config_(file)
, server_(
......@@ -49,9 +59,7 @@ std::string Tester::onCommand(const std::string& name, const std::string& params
const auto& response = service->response_;
/// TODO: keep track the status
return response.is_string() ? response.get<std::string>()
: response.dump();
return detail::toString(response);
}
void Tester::onEvent(const std::string& name, const std::string& params) noexcept
......@@ -63,11 +71,30 @@ void Tester::onEvent(const std::string& name, const std::string& params) noexcep
/// TODO: keep track the status
}
void Tester::wait() noexcept
void Tester::run() noexcept
{
using namespace std::chrono_literals;
while (true) {
std::this_thread::sleep_for(1s);
for (const auto& e : config_.getExpectations()) {
const auto& command = e.name_;
const auto& expect = e.value_;
const auto response = server_.runCmd(command, "");
bool asExpect = false;
if (expect.is_string()) {
asExpect = (response == expect.get<std::string>());
} else {
try {
asExpect = (nlohmann::json::parse(response) == expect);
} catch (...) {
}
}
if (asExpect) {
std::cout << "[PASS] run command: " << command << std::endl;
} else {
std::cout << "[FAILED] run command: " << command << " with unexpected response: "
<< "\"" << response << "\"(real) vs " << detail::toString(expect) << "(expect)" << std::endl;
}
}
}
......
......@@ -22,5 +22,5 @@ int main(int argc, char* argv[]) {
}
xs::test::Tester tester{configFile};
tester.wait();
tester.run();
}
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