#!/usr/bin/env python3 # Copyright 2016 Google Inc. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS-IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from fruit_test_common import * COMMON_DEFINITIONS = ''' #include "test_common.h" struct X; struct Annotation1 {}; using XAnnot1 = fruit::Annotated; ''' def test_install_component_functions_deduped_against_previous_install_no_args(): source = ''' int num_executions = 0; fruit::Component getChildComponent() { static int n = 5; ++num_executions; return fruit::createComponent() .bindInstance(n); } fruit::Component<> getMiddleComponent() { return fruit::createComponent() .install(getChildComponent); } fruit::Component getMainComponent() { return fruit::createComponent() .install(getMiddleComponent) .installComponentFunctions(fruit::componentFunction(getChildComponent)); } int main() { fruit::Injector injector(getMainComponent); int n = injector.get(); Assert(n == 5); Assert(num_executions == 1); } ''' expect_success( COMMON_DEFINITIONS, source, locals()) def test_install_component_functions_deduped_against_following_install_no_args(): source = ''' int num_executions = 0; fruit::Component getChildComponent() { static int n = 5; ++num_executions; return fruit::createComponent() .bindInstance(n); } fruit::Component<> getMiddleComponent() { return fruit::createComponent() .installComponentFunctions(fruit::componentFunction(getChildComponent)); } fruit::Component getMainComponent() { return fruit::createComponent() .install(getMiddleComponent) .install(getChildComponent); } int main() { fruit::Injector injector(getMainComponent); int n = injector.get(); Assert(n == 5); Assert(num_executions == 1); } ''' expect_success( COMMON_DEFINITIONS, source, locals()) def test_install_component_functions_deduped_against_previous_install_with_args(): source = ''' int num_executions = 0; fruit::Component getChildComponent(int) { static int n = 5; ++num_executions; return fruit::createComponent() .bindInstance(n); } fruit::Component<> getMiddleComponent() { return fruit::createComponent() .install(getChildComponent, 42); } fruit::Component getMainComponent() { return fruit::createComponent() .install(getMiddleComponent) .installComponentFunctions(fruit::componentFunction(getChildComponent, 42)); } int main() { fruit::Injector injector(getMainComponent); int n = injector.get(); Assert(n == 5); Assert(num_executions == 1); } ''' expect_success( COMMON_DEFINITIONS, source, locals()) def test_install_component_functions_deduped_against_following_install_with_args(): source = ''' int num_executions = 0; fruit::Component getChildComponent(int) { static int n = 5; ++num_executions; return fruit::createComponent() .bindInstance(n); } fruit::Component<> getMiddleComponent() { return fruit::createComponent() .installComponentFunctions(fruit::componentFunction(getChildComponent, 42)); } fruit::Component getMainComponent() { return fruit::createComponent() .install(getMiddleComponent) .install(getChildComponent, 42); } int main() { fruit::Injector injector(getMainComponent); int n = injector.get(); Assert(n == 5); Assert(num_executions == 1); } ''' expect_success( COMMON_DEFINITIONS, source, locals()) def test_install_component_functions_same_as_with_previous_install_with_different_args(): source = ''' int num_executions = 0; fruit::Component getChildComponent(int) { static int n = 5; ++num_executions; return fruit::createComponent() .bindInstance(n); } fruit::Component<> getMiddleComponent() { return fruit::createComponent() .install(getChildComponent, 42); } fruit::Component getMainComponent() { return fruit::createComponent() .install(getMiddleComponent) .installComponentFunctions(fruit::componentFunction(getChildComponent, 2)); } int main() { fruit::Injector injector(getMainComponent); int n = injector.get(); Assert(n == 5); Assert(num_executions == 2); } ''' expect_success( COMMON_DEFINITIONS, source, locals()) def test_install_component_functions_same_as_following_install_with_different_args(): source = ''' int num_executions = 0; fruit::Component getChildComponent(int) { static int n = 5; ++num_executions; return fruit::createComponent() .bindInstance(n); } fruit::Component<> getMiddleComponent() { return fruit::createComponent() .installComponentFunctions(fruit::componentFunction(getChildComponent, 42)); } fruit::Component getMainComponent() { return fruit::createComponent() .install(getMiddleComponent) .install(getChildComponent, 2); } int main() { fruit::Injector injector(getMainComponent); (void)injector; Assert(num_executions == 2); } ''' expect_success( COMMON_DEFINITIONS, source, locals()) def test_install_component_functions_no_component_functions(): source = ''' fruit::Component<> getComponent() { return fruit::createComponent() .installComponentFunctions(); } int main() { fruit::Injector<> injector(getComponent); (void)injector; } ''' expect_success(COMMON_DEFINITIONS, source) def test_install_component_functions_one_component_function(): source = ''' struct X { int n; X(int n) : n(n) {} }; fruit::Component getParentComponent(std::string) { return fruit::createComponent() .registerProvider([]() { return X(5); }); } fruit::Component getComponent() { return fruit::createComponent() .installComponentFunctions(fruit::componentFunction(getParentComponent, std::string("Hello"))); } int main() { fruit::Injector injector(getComponent); X x = injector.get(); Assert(x.n == 5); } ''' expect_success(COMMON_DEFINITIONS, source) def test_install_component_functions_two_component_functions(): source = ''' struct X { int n; X(int n) : n(n) {} }; struct Y { int n; Y(int n) : n(n) {} }; fruit::Component getParentComponent1(std::string) { return fruit::createComponent() .registerProvider([]() { return X(5); }); } fruit::Component getParentComponent2(std::string) { return fruit::createComponent() .registerProvider([]() { return Y(42); }); } fruit::Component getComponent() { return fruit::createComponent() .installComponentFunctions( fruit::componentFunction(getParentComponent1, std::string("Hello")), fruit::componentFunction(getParentComponent2, std::string("World"))); } int main() { fruit::Injector injector(getComponent); X x = injector.get(); Y y = injector.get(); Assert(x.n == 5); Assert(y.n == 42); } ''' expect_success(COMMON_DEFINITIONS, source) def test_install_component_functions_with_template_parameter_pack_unpacking(): source = ''' template struct GetComponentHolder; struct X { int n; X(int n) : n(n) {} }; struct Y { int n; Y(int n) : n(n) {} }; template <> struct GetComponentHolder { static fruit::Component getComponent(std::string) { return fruit::createComponent() .registerProvider([]() { return X(5); }); } }; template <> struct GetComponentHolder { static fruit::Component getComponent(std::string) { return fruit::createComponent() .registerProvider([]() { return Y(42); }); } }; template fruit::Component getComponent() { return fruit::createComponent() .installComponentFunctions( fruit::componentFunction(GetComponentHolder::getComponent, std::string("Hello"))...); } int main() { fruit::Injector injector(getComponent); X x = injector.get(); Y y = injector.get(); Assert(x.n == 5); Assert(y.n == 42); } ''' expect_success(COMMON_DEFINITIONS, source) def test_install_component_functions_wrong_argument_type(): source = ''' fruit::Component<> getMainComponent() { return fruit::createComponent() .installComponentFunctions(42); } ''' expect_compile_error( 'IncorrectArgTypePassedToInstallComponentFuntionsError', 'All arguments passed to installComponentFunctions.. must be fruit::ComponentFunction<...> objects but an ' 'argument with type Arg was passed instead.', COMMON_DEFINITIONS, source, locals()) if __name__== '__main__': main(__file__)