/* * %injeqt copyright begin% * Copyright 2016 RafaƂ Malinowski (rafal.przemyslaw.malinowski@gmail.com) * %injeqt copyright end% * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ #include #include #include #include #include using injeqt::make_type; using injeqt::type; #define ROLE_1 "role1" #define ROLE_2 "role2" std::set instantiated_types; class role_1_type : public QObject { Q_OBJECT INJEQT_TYPE_ROLE(ROLE_1) public: Q_INVOKABLE role_1_type() { instantiated_types.insert(make_type()); } }; class no_role_required_by_role_2 : public QObject { Q_OBJECT public: Q_INVOKABLE no_role_required_by_role_2() { instantiated_types.insert(make_type()); } }; class role_2_type : public QObject { Q_OBJECT INJEQT_TYPE_ROLE(ROLE_2) public: Q_INVOKABLE role_2_type() { instantiated_types.insert(make_type()); } private slots: INJEQT_SET void set_no_role_required_by_role_2(no_role_required_by_role_2 *) {} }; class instantiate_all_with_type_role_test : public QObject { Q_OBJECT private: injeqt::injector create_injector(); private slots: void should_create_all_role_instances_with_dependencied_when_requested(); }; injeqt::injector instantiate_all_with_type_role_test::create_injector() { class m : public injeqt::module { public: m() { add_type(); add_type(); add_type(); } virtual ~m() {} }; auto modules = std::vector>{}; modules.emplace_back(std::unique_ptr{new m{}}); return injeqt::injector{std::move(modules)}; } void instantiate_all_with_type_role_test::should_create_all_role_instances_with_dependencied_when_requested() { auto injector = create_injector(); instantiated_types.clear(); injector.instantiate_all_with_type_role(ROLE_2); QCOMPARE(instantiated_types, (std::set{make_type(), make_type()})); instantiated_types.clear(); injector.instantiate_all_with_type_role(ROLE_1); QCOMPARE(instantiated_types, (std::set{make_type()})); } QTEST_APPLESS_MAIN(instantiate_all_with_type_role_test) #include "instantiate-all-with-type-role-test.moc"