Skip to content
Snippets Groups Projects
Commit 0a1eeaf0 authored by Nicolas Pope's avatar Nicolas Pope
Browse files

Configurable unit tests

parent f67e319a
Branches
Tags
No related merge requests found
Pipeline #11113 failed
......@@ -16,3 +16,5 @@ target_include_directories(ftlcommon PUBLIC
PRIVATE src)
target_link_libraries(ftlcommon glog::glog ${OpenCV_LIBS} ${PCL_LIBRARIES})
add_subdirectory(test)
......@@ -19,6 +19,15 @@ void Configurable::_trigger(const string &name) {
}
}
void Configurable::on(const string &prop, function<void(Configurable*, const string&)> f) {
auto ix = observers_.find(prop);
if (ix == observers_.end()) {
observers_[prop] = {f};
} else {
(*ix).second.push_back(f);
}
}
void Configurable::__changeURI(const string &uri, Configurable *cfg) {
}
\ No newline at end of file
### Configurable Unit ################################################################
add_executable(configurable_unit
./tests.cpp
../src/configurable.cpp
./configurable_unit.cpp
)
target_include_directories(configurable_unit PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/../include")
target_link_libraries(configurable_unit
${URIPARSER_LIBRARIES}
glog::glog)
add_test(ConfigurableUnitTest configurable_unit)
Source diff could not be displayed: it is too large. Options to address this: view the blob.
#include "catch.hpp"
#include <ftl/configurable.hpp>
using ftl::Configurable;
using std::string;
SCENARIO( "Configurable::get()" ) {
GIVEN( "a non-existent property" ) {
nlohmann::json json = {{"test",5}};
Configurable cfg(json);
auto r = cfg.get<int>("test2");
REQUIRE( !r );
}
GIVEN( "a valid property" ) {
nlohmann::json json = {{"test",5}};
Configurable cfg(json);
auto r = cfg.get<int>("test");
REQUIRE( r );
REQUIRE( *r == 5 );
}
}
SCENARIO( "Configurable::on()" ) {
GIVEN( "a changed property with no handlers" ) {
nlohmann::json json = {{"test",5}};
Configurable cfg(json);
cfg.set("test", 55);
REQUIRE( *(cfg.get<int>("test")) == 55 );
}
GIVEN( "a changed property one handler" ) {
nlohmann::json json = {{"test",5}};
Configurable cfg(json);
bool trig = false;
cfg.on("test", [&trig](Configurable *c, const string &n) {
trig = true;
});
cfg.set("test", 55);
REQUIRE( trig );
}
GIVEN( "a changed property two handlers" ) {
nlohmann::json json = {{"test",5}};
Configurable cfg(json);
bool trig1 = false;
bool trig2 = false;
cfg.on("test", [&trig1](Configurable *c, const string &n) {
trig1 = true;
});
cfg.on("test", [&trig2](Configurable *c, const string &n) {
trig2 = true;
});
cfg.set("test", 55);
REQUIRE( (trig1 && trig2) );
}
}
#define CATCH_CONFIG_MAIN
#include "catch.hpp"
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment