Commit b0be3542 authored by Patrick Chen's avatar Patrick Chen

add new type 'Expectation' for scripting test

parent 574f0816
...@@ -25,8 +25,8 @@ LD_FLAGS=-L/usr/local/lib \ ...@@ -25,8 +25,8 @@ LD_FLAGS=-L/usr/local/lib \
-lpthread -lpthread
XSTEST_HDRS=$(INCLUDE_DIR)/test/Config.h $(INCLUDE_DIR)/test/Service.h $(INCLUDE_DIR)/test/Tester.h XSTEST_HDRS=$(INCLUDE_DIR)/test/Config.h $(INCLUDE_DIR)/test/Service.h $(INCLUDE_DIR)/test/Tester.h $(INCLUDE_DIR)/test/Expectation.h
XSTEST_SRCS=$(SRC_DIR)/xs-test.cc $(SRC_DIR)/test/Config.cc $(SRC_DIR)/test/Service.cc $(SRC_DIR)/test/Tester.cc XSTEST_SRCS=$(SRC_DIR)/xs-test.cc $(SRC_DIR)/test/Config.cc $(SRC_DIR)/test/Service.cc $(SRC_DIR)/test/Tester.cc $(SRC_DIR)/test/Expectation.cc
XSTEST_OBJS=$(subst $(REPO_DIR),$(BUILD_DIR),$(XSTEST_SRCS:.cc=.o)) XSTEST_OBJS=$(subst $(REPO_DIR),$(BUILD_DIR),$(XSTEST_SRCS:.cc=.o))
HELLO_WORLD_SRCS=$(REPO_DIR)/example/hello-world.cc HELLO_WORLD_SRCS=$(REPO_DIR)/example/hello-world.cc
......
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
#include <boost/range/iterator_range.hpp> #include <boost/range/iterator_range.hpp>
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
#include "test/Expectation.h"
#include "test/Service.h" #include "test/Service.h"
...@@ -17,6 +18,7 @@ namespace xs { namespace test ...@@ -17,6 +18,7 @@ namespace xs { namespace test
{ {
private: private:
using service_range_type = std::deque<Service>; using service_range_type = std::deque<Service>;
using expectation_range_type = std::deque<Expectation>;
public: public:
Config() = delete; Config() = delete;
...@@ -32,11 +34,16 @@ namespace xs { namespace test ...@@ -32,11 +34,16 @@ namespace xs { namespace test
service_range_type::const_iterator service_range_type::const_iterator
> getServices() const noexcept; > getServices() const noexcept;
boost::iterator_range<
expectation_range_type::const_iterator
> getExpectations() const noexcept;
private: private:
static bool verify(const nlohmann::json& object) noexcept; static bool verify(const nlohmann::json& object) noexcept;
private: private:
std::deque<Service> services_; service_range_type services_;
expectation_range_type expectations_;
}; };
} } } }
......
#ifndef XS_TEST_EXPECTATION_H_INCLUDED
#define XS_TEST_EXPECTATION_H_INCLUDED
#include <nlohmann/json.hpp>
#include <string>
namespace xs { namespace test
{
class Expectation
{
public:
Expectation() = delete;
Expectation(const Expectation&) = delete;
Expectation(Expectation&&) noexcept = default;
~Expectation() noexcept = default;
Expectation& operator=(const Expectation&) = delete;
Expectation& operator=(Expectation&&) = delete;
explicit Expectation(const nlohmann::json& object);
static bool verify(const nlohmann::json& object) noexcept;
private:
Expectation(std::string name, nlohmann::json value) noexcept;
public:
std::string name_;
nlohmann::json value_;
};
} }
#endif
...@@ -3,7 +3,6 @@ ...@@ -3,7 +3,6 @@
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
#include <memory>
#include <string> #include <string>
......
...@@ -41,6 +41,10 @@ Config::Config(const boost::filesystem::path& file) ...@@ -41,6 +41,10 @@ Config::Config(const boost::filesystem::path& file)
for (const auto element : object["mockData"]) { for (const auto element : object["mockData"]) {
services_.emplace_back(Service(element)); services_.emplace_back(Service(element));
} }
for (const auto element : object["script"]) {
expectations_.emplace_back(Expectation(element));
}
} }
boost::iterator_range< boost::iterator_range<
...@@ -52,18 +56,37 @@ boost::iterator_range< ...@@ -52,18 +56,37 @@ boost::iterator_range<
); );
} }
boost::iterator_range<
Config::expectation_range_type::const_iterator
> Config::getExpectations() const noexcept
{
return boost::make_iterator_range(
std::begin(expectations_), std::end(expectations_)
);
}
bool Config::verify(const nlohmann::json& object) noexcept bool Config::verify(const nlohmann::json& object) noexcept
{ {
if (!(object.count("mockData") && object["mockData"].is_array())) { if (!(object.count("mockData") && object["mockData"].is_array())) {
return false; return false;
} }
if (!(object.count("script") && object["script"].is_array())) {
return false;
}
for (const auto element : object["mockData"]) { for (const auto element : object["mockData"]) {
if (!Service::verify(element)) { if (!Service::verify(element)) {
return false; return false;
} }
} }
for (const auto element : object["script"]) {
if (!Expectation::verify(element)) {
return false;
}
}
return true; return true;
} }
......
#include "test/Expectation.h"
#include <nlohmann/json.hpp>
namespace xs { namespace test
{
Expectation::Expectation(const nlohmann::json& object)
: Expectation(object["command"].get<std::string>(), object["expect"])
{ }
bool Expectation::verify(const nlohmann::json& object) noexcept
{
if (!(object.is_object()
&& object.count("command")
&& object.count("expect"))) {
return false;
}
return (
object["command"].is_string()
&& (
object["expect"].is_string() || object["expect"].is_object()
)
);
}
Expectation::Expectation(std::string name, nlohmann::json value) noexcept
: name_(std::move(name))
, value_(std::move(value))
{ }
} }
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