Skip to content
Snippets Groups Projects
Commit 6115fba4 authored by vaeng's avatar vaeng Committed by Christian Willner
Browse files

fix: more detailed control for slashes in names

Closes #379
parent 791429e1
No related branches found
No related tags found
No related merge requests found
...@@ -203,7 +203,7 @@ namespace ChimeraTK { ...@@ -203,7 +203,7 @@ namespace ChimeraTK {
InversionOfControlAccessor<Derived>::InversionOfControlAccessor(Module* owner, const std::string& name, InversionOfControlAccessor<Derived>::InversionOfControlAccessor(Module* owner, const std::string& name,
VariableDirection direction, std::string unit, size_t nElements, UpdateMode mode, const std::string& description, VariableDirection direction, std::string unit, size_t nElements, UpdateMode mode, const std::string& description,
const std::type_info* valueType, const std::unordered_set<std::string>& tags) const std::type_info* valueType, const std::unordered_set<std::string>& tags)
: _node(owner, static_cast<Derived*>(this), ChimeraTK::Utilities::raiseIftrailingSlash(name), direction, unit, : _node(owner, static_cast<Derived*>(this), ChimeraTK::Utilities::raiseIftrailingSlash(name, false), direction, unit,
nElements, mode, completeDescription(owner, description), valueType, tags) { nElements, mode, completeDescription(owner, description), valueType, tags) {
static_assert(std::is_base_of<InversionOfControlAccessor<Derived>, Derived>::value, static_assert(std::is_base_of<InversionOfControlAccessor<Derived>, Derived>::value,
"InversionOfControlAccessor<> must be used in a curiously recurring template pattern!"); "InversionOfControlAccessor<> must be used in a curiously recurring template pattern!");
......
...@@ -52,15 +52,9 @@ namespace ChimeraTK::Utilities { ...@@ -52,15 +52,9 @@ namespace ChimeraTK::Utilities {
void setThreadName(const std::string& name); void setThreadName(const std::string& name);
/** /**
* Strips trailing slashes * Raises logic error if name ends in a slash, or if it contains consecutive slashes.
* * Modules are allowed to be named "/", if the bool isModule is true.
*/
std::string stripTrailingSlashes(const std::string& name);
/**
* Raises logic error if name ends in a slash
*
*/ */
std::string raiseIftrailingSlash(const std::string& name); std::string raiseIftrailingSlash(const std::string& name, bool isModule);
} // namespace ChimeraTK::Utilities } // namespace ChimeraTK::Utilities
...@@ -12,7 +12,7 @@ namespace ChimeraTK { ...@@ -12,7 +12,7 @@ namespace ChimeraTK {
Module::Module(EntityOwner* owner, const std::string& name, const std::string& description, Module::Module(EntityOwner* owner, const std::string& name, const std::string& description,
const std::unordered_set<std::string>& tags) const std::unordered_set<std::string>& tags)
: EntityOwner(ChimeraTK::Utilities::stripTrailingSlashes(name), description, tags), _owner(owner) { : EntityOwner(ChimeraTK::Utilities::raiseIftrailingSlash(name, true), description, tags), _owner(owner) {
if(_owner != nullptr) { if(_owner != nullptr) {
_owner->registerModule(this); _owner->registerModule(this);
} }
......
...@@ -110,15 +110,19 @@ namespace ChimeraTK::Utilities { ...@@ -110,15 +110,19 @@ namespace ChimeraTK::Utilities {
/********************************************************************************************************************/ /********************************************************************************************************************/
std::string stripTrailingSlashes(const std::string& name) { // removes trailing slashes, used for variable names
if(name.empty() || name[name.size() - 1] != '/') return name; // raises for multiple slashes
return stripTrailingSlashes(name.substr(0, name.size() - 1)); std::string raiseIftrailingSlash(const std::string& name, bool isModule) {
} if(isModule && name == "/") {
return name;
/********************************************************************************************************************/ }
if(name[name.size() - 1] == '/') {
std::string raiseIftrailingSlash(const std::string& name) { throw ChimeraTK::logic_error(name + ": " + (isModule ? "module" : "variable") + " names cannot end with /");
if(name[name.size() - 1] == '/') throw ChimeraTK::logic_error("variable names cannot end with /"); }
if(name.find("//") != std::string::npos) {
throw ChimeraTK::logic_error(name + " variable names cannot contain consecutive slashes");
}
bool a = true ?: false;
return name; return name;
} }
......
...@@ -1125,31 +1125,16 @@ namespace Tests::testModules { ...@@ -1125,31 +1125,16 @@ namespace Tests::testModules {
} }
/*********************************************************************************************************************/ /*********************************************************************************************************************/
/* test tailing slashes in module names and group names */ /* test tailing slashes in module names and group names will throw error*/
struct SlashedGroup : ctk::VariableGroup {
using ctk::VariableGroup::VariableGroup;
ctk::ScalarPushInput<std::string> inGroup{this, "inGroup", "", "", {}};
ctk::ArrayPushInput<int64_t> alsoInGroup{this, "alsoInGroup", "", 16, "", {}};
};
struct SlashModule : public ctk::ApplicationModule { struct SlashModule : public ctk::ApplicationModule {
using ctk::ApplicationModule::ApplicationModule; using ctk::ApplicationModule::ApplicationModule;
ctk::ScalarPushInput<int> someInput{this, "nameOfSomeInput", "", "", {}};
ctk::ScalarOutput<double> someOutput{this, "someOutput", "", "", {}};
SlashedGroup someGroup{this, "someGroup/", ""};
struct AnotherGroup : ctk::VariableGroup { struct AnotherGroup : ctk::VariableGroup {
using ctk::VariableGroup::VariableGroup; using ctk::VariableGroup::VariableGroup;
ctk::ScalarPushInput<uint8_t> foo{this, "foo", "", "", {}}; ctk::ScalarPushInput<uint8_t> foo{this, "foo", "", "", {}};
// variable names ending in slashes will produce a ctk::logic_error } anotherGroup{this, "anotherGroupName", ""};
ctk::ScalarPushInput<uint8_t> slash{this, "slash", "", "", {}};
ctk::ArrayPushInput<int64_t> slashArray{this, "array", "", 16, "", {}};
} anotherGroup{this, "anotherGroupName////", ""};
void mainLoop() override {} void mainLoop() override {}
}; };
...@@ -1157,14 +1142,10 @@ namespace Tests::testModules { ...@@ -1157,14 +1142,10 @@ namespace Tests::testModules {
// module group // module group
struct SlashApp : public ctk::Application { struct SlashApp : public ctk::Application {
SlashApp() : Application("slashApp") {} SlashApp() : Application("myApp") {}
~SlashApp() override { shutdown(); } ~SlashApp() override { shutdown(); }
SlashModule slashModule{this, "slashModule/", ""}; SlashModule slashModule{this, "slashModule/", ""};
struct slashModGroup : ctk::ModuleGroup {
using ctk::ModuleGroup::ModuleGroup;
SlashModule nestedSlashModule{this, "nestedSlashModule//", ""};
} slashModGroup{this, "slashModGroupName/", ""};
}; };
BOOST_AUTO_TEST_CASE(test_trailingSlashes) { BOOST_AUTO_TEST_CASE(test_trailingSlashes) {
...@@ -1174,37 +1155,14 @@ namespace Tests::testModules { ...@@ -1174,37 +1155,14 @@ namespace Tests::testModules {
std::cout << "==> test_trailingSlashes" << std::endl; std::cout << "==> test_trailingSlashes" << std::endl;
std::cout << std::endl; std::cout << std::endl;
SlashApp app; BOOST_CHECK_THROW(SlashApp app, ctk::logic_error);
ctk::TestFacility tf{app};
tf.runApplication();
// check module name
BOOST_CHECK(app.slashModule.getName() == "slashModule");
// check variable group names
BOOST_CHECK(app.slashModule.someGroup.getName() == "someGroup");
// anotherGroupName has many slashes
BOOST_CHECK(app.slashModule.anotherGroup.getName() == "anotherGroupName");
// check module group names
BOOST_CHECK(app.slashModGroup.getName() == "slashModGroupName");
BOOST_CHECK(app.slashModGroup.nestedSlashModule.getName() == "nestedSlashModule");
// check variables
// variables will return a fully qualified name:
BOOST_CHECK(app.slashModule.someInput.getName() == "/slashModule/nameOfSomeInput");
BOOST_CHECK(app.slashModule.someOutput.getName() == "/slashModule/someOutput");
BOOST_CHECK(app.slashModule.someGroup.inGroup.getName() == "/slashModule/someGroup/inGroup");
BOOST_CHECK(app.slashModule.someGroup.alsoInGroup.getName() == "/slashModule/someGroup/alsoInGroup");
BOOST_CHECK(app.slashModule.anotherGroup.foo.getName() == "/slashModule/anotherGroupName/foo");
BOOST_CHECK(app.slashModule.anotherGroup.slash.getName() == "/slashModule/anotherGroupName/slash");
BOOST_CHECK(app.slashModule.anotherGroup.slashArray.getName() == "/slashModule/anotherGroupName/array");
} }
/*********************************************************************************************************************/ /*********************************************************************************************************************/
/* test tailing slashes in scalar variable name */ /* test tailing slashes in scalar variable name */
struct VariableSlashScalarApp : public ctk::Application { struct VariableSlashScalarApp : public ctk::Application {
VariableSlashScalarApp() : Application("VariableSlashScalarApp") {} VariableSlashScalarApp() : Application("myApp") {}
~VariableSlashScalarApp() override { shutdown(); } ~VariableSlashScalarApp() override { shutdown(); }
struct SomeModule : public ctk::ApplicationModule { struct SomeModule : public ctk::ApplicationModule {
...@@ -1229,7 +1187,7 @@ namespace Tests::testModules { ...@@ -1229,7 +1187,7 @@ namespace Tests::testModules {
/* test tailing slashes in variable names */ /* test tailing slashes in variable names */
struct VariableSlashArrayApp : public ctk::Application { struct VariableSlashArrayApp : public ctk::Application {
VariableSlashArrayApp() : Application("VariableSlashArrayApp") {} VariableSlashArrayApp() : Application("myApp") {}
~VariableSlashArrayApp() override { shutdown(); } ~VariableSlashArrayApp() override { shutdown(); }
struct SomeModule : public ctk::ApplicationModule { struct SomeModule : public ctk::ApplicationModule {
...@@ -1250,4 +1208,113 @@ namespace Tests::testModules { ...@@ -1250,4 +1208,113 @@ namespace Tests::testModules {
BOOST_CHECK_THROW(VariableSlashArrayApp app, ctk::logic_error); BOOST_CHECK_THROW(VariableSlashArrayApp app, ctk::logic_error);
} }
/*********************************************************************************************************************/
/* test slash as variable name */
struct OnlySlashNameArrayApp : public ctk::Application {
OnlySlashNameArrayApp() : Application("myApp") {}
~OnlySlashNameArrayApp() override { shutdown(); }
struct SomeModule : public ctk::ApplicationModule {
using ctk::ApplicationModule::ApplicationModule;
ctk::ArrayPushInput<int64_t> array{this, "/", "", 16, "", {}};
void mainLoop() override {}
} someModule{this, "someModule", ""};
};
BOOST_AUTO_TEST_CASE(test_onlySlashAsVariableName) {
std::cout << "***************************************************************"
"******************************************************"
<< std::endl;
std::cout << "==> test_trailingSlashesInArrayVariableNames" << std::endl;
std::cout << std::endl;
BOOST_CHECK_THROW(OnlySlashNameArrayApp app, ctk::logic_error);
}
/*********************************************************************************************************************/
/* test tailing slash as module name */
struct OnlySlashModuleName : public ctk::Application {
OnlySlashModuleName() : Application("myApp") {}
~OnlySlashModuleName() override { shutdown(); }
struct SomeModule : public ctk::ApplicationModule {
using ctk::ApplicationModule::ApplicationModule;
ctk::ArrayPushInput<int64_t> array{this, "someArray", "", 16, "", {}};
void mainLoop() override {}
} someModule{this, "/", ""};
};
BOOST_AUTO_TEST_CASE(test_onlySlashasModuleName) {
std::cout << "***************************************************************"
"******************************************************"
<< std::endl;
std::cout << "==> test_trailingSlashesInArrayVariableNames" << std::endl;
std::cout << std::endl;
OnlySlashModuleName app;
ctk::TestFacility tf{app};
tf.runApplication();
BOOST_CHECK(app.someModule.getName() == "/");
BOOST_CHECK(app.someModule.array.getName() == "/someArray");
}
/*********************************************************************************************************************/
/* test multiple slashes in module name */
struct MultiSlashModule : public ctk::Application {
MultiSlashModule() : Application("myApp") {}
~MultiSlashModule() override { shutdown(); }
struct SomeModule : public ctk::ApplicationModule {
using ctk::ApplicationModule::ApplicationModule;
ctk::ArrayPushInput<int64_t> array{this, "someArray", "", 16, "", {}};
void mainLoop() override {}
} someModule{this, "aModule//withSlahsesInTheName/", ""};
};
BOOST_AUTO_TEST_CASE(test_multipleSlashesInModuleName) {
std::cout << "***************************************************************"
"******************************************************"
<< std::endl;
std::cout << "==> test_trailingSlashesInArrayVariableNames" << std::endl;
std::cout << std::endl;
BOOST_CHECK_THROW(MultiSlashModule app, ctk::logic_error);
;
}
/*********************************************************************************************************************/
/* test multiple slashes in variable name */
struct MultiSlashVarModule : public ctk::Application {
MultiSlashVarModule() : Application("myApp") {}
~MultiSlashVarModule() override { shutdown(); }
struct SomeModule : public ctk::ApplicationModule {
using ctk::ApplicationModule::ApplicationModule;
ctk::ArrayPushInput<int64_t> array{this, "someArray/with//multiple///slashes", "", 16, "", {}};
void mainLoop() override {}
} someModule{this, "someModule", ""};
};
BOOST_AUTO_TEST_CASE(test_multipleSlashesInVariableName) {
std::cout << "***************************************************************"
"******************************************************"
<< std::endl;
std::cout << "==> test_trailingSlashesInArrayVariableNames" << std::endl;
std::cout << std::endl;
BOOST_CHECK_THROW(MultiSlashVarModule app, ctk::logic_error);
;
}
} // namespace Tests::testModules } // namespace Tests::testModules
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment