diff --git a/src/programs/unit_tests/Components/GetObjectsTests.cc b/src/programs/unit_tests/Components/GetObjectsTests.cc index 05e8399d2..e5476e881 100644 --- a/src/programs/unit_tests/Components/GetObjectsTests.cc +++ b/src/programs/unit_tests/Components/GetObjectsTests.cc @@ -2,48 +2,106 @@ // Subject to the terms in the LICENSE file found in the top-level directory. #include +#include +#include +#include "JANA/JFactoryGenerator.h" #include "catch.hpp" -struct Obj1 : public JObject { int data; }; -struct Obj2 : public JObject { int data; }; -struct Obj3 : public JObject { int data; }; -struct Obj4 : public JObject { int data; }; +namespace jana::components::getobjects_tests { -class Src : public JEventSource { +struct Obj : public JObject { int data; }; + +struct Src : public JEventSource { Src() { + EnableGetObjects(); SetCallbackStyle(CallbackStyle::ExpertMode); } - Result Emit(JEvent& event) override { - auto obj = new Obj1; - obj->data = 21; - event.Insert(obj); + Result Emit(JEvent&) override { return Result::Success; } bool GetObjects(const std::shared_ptr&, JFactory* fac) override { - if (fac->GetObjectName() == "Obj2") { - auto obj = new Obj2; + + LOG_INFO(GetLogger()) << "GetObjects: Fac has object name '" << fac->GetObjectName() << "' and type name '" << fac->GetTypeName() << "'" << LOG_END; + + //if (fac->GetObjectName() == "jana::components::getobjects_tests::Obj") { + auto typed_fac = dynamic_cast*>(fac); + if (typed_fac != nullptr) { + auto obj = new Obj; obj->data = 22; - fac->Insert(obj); + typed_fac->Insert(obj); return true; } return false; } }; -class Fac : public JFactoryT { +struct Fac : public JFactoryT { + void Process(const std::shared_ptr&) override { + auto obj = new Obj; + obj->data = 23; + Insert(obj); + } +}; + +struct RFac : public JFactoryT { + RFac() { + SetFactoryFlag(REGENERATE); + } void Process(const std::shared_ptr&) override { - auto obj = new Obj3; + auto obj = new Obj; obj->data = 23; Insert(obj); } }; -TEST_CASE("GetObjectsTests") { - JEvent event; - JFactorySet* fs = new JFactorySet; - fs->Add(new Fac); - event.SetFactorySet(fs); - event.GetJCallGraphRecorder()->SetEnabled(true); +struct Proc : public JEventProcessor { + bool from_getobjects=true; + void Process(const std::shared_ptr& event) override { + auto objs = event->Get(); + REQUIRE(objs.size() == 1); + if (from_getobjects) { + REQUIRE(objs[0]->data == 22); + } + else { + REQUIRE(objs[0]->data == 23); + } + } +}; + +TEST_CASE("GetObjectsTests_NoFac") { + JApplication app; + app.SetParameterValue("jana:loglevel", "warn"); + app.SetParameterValue("jana:nevents", 1); + app.Add(new Src); + auto proc = new Proc; + proc->from_getobjects = true; + app.Add(proc); + app.Add(new JFactoryGeneratorT>); + app.Run(); +} + +TEST_CASE("GetObjectsTests_OverrideFac") { + JApplication app; + app.SetParameterValue("jana:loglevel", "warn"); + app.SetParameterValue("jana:nevents", 1); + app.Add(new Src); + app.Add(new JFactoryGeneratorT); + auto proc = new Proc; + proc->from_getobjects = true; + app.Add(proc); + app.Run(); +} +TEST_CASE("GetObjectsTests_Regenerate") { + JApplication app; + app.SetParameterValue("jana:loglevel", "warn"); + app.SetParameterValue("jana:nevents", 1); + app.Add(new Src); + app.Add(new JFactoryGeneratorT); + auto proc = new Proc; + proc->from_getobjects = false; + app.Add(proc); + app.Run(); } +} // namespace ...