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

allow simple test

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