Skip to content

Commit

Permalink
Merge pull request #256 from gonzaponte/protection
Browse files Browse the repository at this point in the history
Add protection for wrong keys in object factory
  • Loading branch information
paolafer authored Apr 26, 2024
2 parents d08e319 + bb8cad9 commit f128439
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
21 changes: 21 additions & 0 deletions pytest/macros_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,24 @@ def test_run_full_examples(capsys, config_tmpdir, output_tmpdir, NEXUSDIR, macro
print(f'*** Full simulations - some of them are slow ***')

execute_example_jobs(capsys, config_tmpdir, output_tmpdir, NEXUSDIR, macro_list[1])


def test_name_tags(capsys, config_tmpdir, output_tmpdir, NEXUSDIR):
my_env = os.environ.copy()
macro = NEXUSDIR + "/macros/NEW.init.mac"
init_macro = copy_and_modify_macro(config_tmpdir, output_tmpdir, macro)

# register a wrong generator
contents = open(init_macro).read()
contents = contents.replace( "/nexus/RegisterGenerator IonGenerator"
, "/nexus/RegisterGenerator DoesNotExist")
open(init_macro, "w").write(contents)

nexus_exe = NEXUSDIR + '/bin/nexus'
command = [nexus_exe, '-b', init_macro]
with capsys.disabled():
print(f'Checking modified {macro} for wrong generator name')
p = subprocess.run(command, env=my_env, capture_output=True)
assert "G4Exception" in p.stderr.decode()
assert "Unknown key" in p.stderr.decode()
assert "DoesNotExist" in p.stderr.decode()
5 changes: 4 additions & 1 deletion source/base/FactoryBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,11 @@ class ObjFactory
}
}

//
std::unique_ptr<T> CreateObject(const std::string& tag) {
if (registry_.find(tag) == registry_.end()) {
auto msg = std::string("Unknown key for ") + typeid(T).name() + ": " + tag;
G4Exception("ObjFactory::CreateObject()", "", FatalException, msg.c_str());
}
return registry_[tag]->CreateObject();
}

Expand Down

0 comments on commit f128439

Please sign in to comment.