#!/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. import pytest from fruit_test_common import * COMMON_DEFINITIONS = ''' #include "test_common.h" struct Annotation1 {}; struct Annotation2 {}; template using WithNoAnnot = T; template using WithAnnot1 = fruit::Annotated; ''' @pytest.mark.parametrize('ConstructX', [ 'X()', 'new X()', ]) def test_bind_multibinding_provider_success(ConstructX): source = ''' struct X : public ConstructionTracker { INJECT(X()) = default; }; fruit::Component<> getComponent() { return fruit::createComponent() .addMultibindingProvider([](){return ConstructX;}); } int main() { fruit::Injector<> injector(getComponent); Assert(X::num_objects_constructed == 0); Assert(injector.getMultibindings().size() == 1); Assert(X::num_objects_constructed == 1); } ''' expect_success( COMMON_DEFINITIONS, source, locals()) @pytest.mark.parametrize('WithAnnot', [ 'WithNoAnnot', 'WithAnnot1', ]) def test_bind_multibinding_provider_abstract_class_success(WithAnnot): source = ''' struct I { virtual int foo() = 0; virtual ~I() = default; }; struct X : public I { int foo() override { return 5; } }; fruit::Component<> getComponent() { return fruit::createComponent() .addMultibindingProvider()>([](){return static_cast(new X());}); } int main() { fruit::Injector<> injector(getComponent); Assert(injector.getMultibindings>().size() == 1); Assert(injector.getMultibindings>()[0]->foo() == 5); } ''' expect_success( COMMON_DEFINITIONS, source, locals()) @pytest.mark.parametrize('WithAnnot', [ 'WithNoAnnot', 'WithAnnot1', ]) def test_bind_multibinding_provider_abstract_class_with_no_virtual_destructor_error(WithAnnot): source = ''' struct I { virtual int foo() = 0; }; struct X : public I { int foo() override { return 5; } }; fruit::Component<> getComponent() { return fruit::createComponent() .addMultibindingProvider()>([](){return static_cast(new X());}); } ''' expect_compile_error( 'MultibindingProviderReturningPointerToAbstractClassWithNoVirtualDestructorError', 'registerMultibindingProvider\(\) was called with a lambda that returns a pointer to T, but T is an abstract class with no virtual destructor', COMMON_DEFINITIONS, source, locals()) @pytest.mark.parametrize('ConstructX,XPtr', [ ('X()', 'X'), ('new X()', 'X*'), ]) @pytest.mark.parametrize('WithAnnot', [ 'WithNoAnnot', 'WithAnnot1', ]) @pytest.mark.parametrize('YVariant', [ 'Y', 'const Y', 'Y*', 'const Y*', 'Y&', 'const Y&', 'std::shared_ptr', 'fruit::Provider', 'fruit::Provider', ]) def test_bind_multibinding_provider_with_param_success(ConstructX, XPtr, WithAnnot, YVariant): source = ''' struct Y {}; struct X : public ConstructionTracker {}; fruit::Component> getYComponent() { return fruit::createComponent() .registerConstructor()>(); } fruit::Component<> getComponent() { return fruit::createComponent() .install(getYComponent) .addMultibindingProvider)>([](YVariant){ return ConstructX; }); } int main() { fruit::Injector<> injector(getComponent); Assert(X::num_objects_constructed == 0); Assert(injector.getMultibindings().size() == 1); Assert(X::num_objects_constructed == 1); } ''' expect_success( COMMON_DEFINITIONS, source, locals()) @pytest.mark.parametrize('ConstructX,XPtr', [ ('X()', 'X'), ('new X()', 'X*'), ]) @pytest.mark.parametrize('WithAnnot', [ 'WithNoAnnot', 'WithAnnot1', ]) @pytest.mark.parametrize('YVariant', [ 'Y', 'const Y', 'const Y*', 'const Y&', 'fruit::Provider', ]) def test_bind_multibinding_provider_with_param_const_binding_success(ConstructX, XPtr, WithAnnot, YVariant): source = ''' struct Y {}; struct X : public ConstructionTracker {}; const Y y{}; fruit::Component> getYComponent() { return fruit::createComponent() .bindInstance, Y>(y); } fruit::Component<> getComponent() { return fruit::createComponent() .install(getYComponent) .addMultibindingProvider)>([](YVariant){ return ConstructX; }); } int main() { fruit::Injector<> injector(getComponent); Assert(X::num_objects_constructed == 0); Assert(injector.getMultibindings().size() == 1); Assert(X::num_objects_constructed == 1); } ''' expect_success( COMMON_DEFINITIONS, source, locals()) @pytest.mark.parametrize('ConstructX,XPtr', [ ('X()', 'X'), ('new X()', 'X*'), ]) @pytest.mark.parametrize('WithAnnot,YAnnotRegex', [ ('WithNoAnnot', 'Y'), ('WithAnnot1', 'fruit::Annotated'), ]) @pytest.mark.parametrize('YVariant', [ 'Y*', 'Y&', 'std::shared_ptr', 'fruit::Provider', ]) def test_bind_multibinding_provider_with_param_error_nonconst_param_required(ConstructX, XPtr, WithAnnot, YAnnotRegex, YVariant): source = ''' struct Y {}; struct X {}; fruit::Component> getYComponent(); fruit::Component<> getComponent() { return fruit::createComponent() .install(getYComponent) .addMultibindingProvider)>([](YVariant){ return ConstructX; }); } ''' expect_compile_error( 'NonConstBindingRequiredButConstBindingProvidedError', 'The type T was provided as constant, however one of the constructors/providers/factories in this component', COMMON_DEFINITIONS, source, locals()) @pytest.mark.parametrize('ConstructX,XPtr', [ ('X()', 'X'), ('new X()', 'X*'), ]) @pytest.mark.parametrize('WithAnnot,YAnnotRegex', [ ('WithNoAnnot', 'Y'), ('WithAnnot1', 'fruit::Annotated'), ]) @pytest.mark.parametrize('YVariant', [ 'Y*', 'Y&', 'std::shared_ptr', 'fruit::Provider', ]) def test_bind_multibinding_provider_with_param_error_nonconst_param_required_install_after(ConstructX, XPtr, WithAnnot, YAnnotRegex, YVariant): source = ''' struct Y {}; struct X {}; fruit::Component> getYComponent(); fruit::Component<> getComponent() { return fruit::createComponent() .addMultibindingProvider)>([](YVariant){ return ConstructX; }) .install(getYComponent); } ''' expect_compile_error( 'NonConstBindingRequiredButConstBindingProvidedError', 'The type T was provided as constant, however one of the constructors/providers/factories in this component', COMMON_DEFINITIONS, source, locals()) def test_bind_multibinding_provider_requiring_nonconst_then_requiring_const_ok(): source = ''' struct X {}; struct Y {}; fruit::Component<> getRootComponent() { return fruit::createComponent() .addMultibindingProvider([](X&) { return Y(); }) .addMultibindingProvider([](const X&) { return Y(); }) .registerConstructor(); } int main() { fruit::Injector<> injector(getRootComponent); injector.getMultibindings(); } ''' expect_success( COMMON_DEFINITIONS, source, locals()) def test_bind_multibinding_provider_requiring_nonconst_then_requiring_const_declaring_const_requirement_error(): source = ''' struct X {}; struct Y {}; fruit::Component> getRootComponent() { return fruit::createComponent() .addMultibindingProvider([](X&) { return Y(); }) .addMultibindingProvider([](const X&) { return Y(); }); } ''' expect_compile_error( 'ConstBindingDeclaredAsRequiredButNonConstBindingRequiredError', 'The type T was declared as a const Required type in the returned Component, however', COMMON_DEFINITIONS, source, locals()) def test_bind_multibinding_provider_requiring_const_then_requiring_nonconst_ok(): source = ''' struct X {}; struct Y {}; fruit::Component<> getRootComponent() { return fruit::createComponent() .addMultibindingProvider([](const X&) { return Y(); }) .addMultibindingProvider([](X&) { return Y(); }) .registerConstructor(); } int main() { fruit::Injector<> injector(getRootComponent); injector.getMultibindings(); } ''' expect_success( COMMON_DEFINITIONS, source, locals()) def test_bind_multibinding_provider_requiring_const_then_requiring_nonconst_declaring_const_requirement_error(): source = ''' struct X {}; struct Y {}; fruit::Component> getRootComponent() { return fruit::createComponent() .addMultibindingProvider([](const X&) { return Y(); }) .addMultibindingProvider([](X&) { return Y(); }); } ''' expect_compile_error( 'ConstBindingDeclaredAsRequiredButNonConstBindingRequiredError', 'The type T was declared as a const Required type in the returned Component, however', COMMON_DEFINITIONS, source, locals()) @pytest.mark.parametrize('ConstructX,XPtr', [ ('X()', 'X'), ('new X()', 'X*'), ]) @pytest.mark.parametrize('YAnnot,ConstYAnnot,YVariant,YVariantRegex', [ ('Y', 'Y', 'Y**', 'Y\*\*'), ('Y', 'Y', 'std::shared_ptr*', 'std::shared_ptr\*'), ('Y', 'const Y', 'Y**', 'Y\*\*'), ('Y', 'const Y', 'std::shared_ptr*', 'std::shared_ptr\*'), ('fruit::Annotated', 'fruit::Annotated', 'Y**', 'Y\*\*'), ('fruit::Annotated', 'fruit::Annotated', 'Y**', 'Y\*\*'), ]) def test_bind_multibinding_provider_with_param_error_type_not_injectable(ConstructX, XPtr, YAnnot, ConstYAnnot, YVariant, YVariantRegex): source = ''' struct Y {}; struct X {}; fruit::Component<> getComponent() { return fruit::createComponent() .addMultibindingProvider([](YVariant){ return ConstructX; }); } ''' expect_compile_error( 'NonInjectableTypeError', 'The type T is not injectable.', COMMON_DEFINITIONS, source, locals()) @pytest.mark.parametrize('ConstructX,XAnnot,XPtrAnnot', [ ('X()', 'X', 'X'), ('X()', 'fruit::Annotated', 'fruit::Annotated'), ('new X()', 'X', 'X*'), ('new X()', 'fruit::Annotated', 'fruit::Annotated'), ]) def test_bind_multibinding_provider_explicit_signature_success(ConstructX, XAnnot, XPtrAnnot): source = ''' struct X : public ConstructionTracker { INJECT(X()) = default; static bool constructed; }; fruit::Component<> getComponent() { return fruit::createComponent() .addMultibindingProvider([](){return ConstructX;}); } int main() { fruit::Injector<> injector(getComponent); Assert(X::num_objects_constructed == 0); Assert(injector.getMultibindings().size() == 1); Assert(X::num_objects_constructed == 1); } ''' expect_success( COMMON_DEFINITIONS, source, locals()) @pytest.mark.parametrize('ConstructX,XAnnot,XPtrAnnot', [ ('X()', 'X', 'X'), ('X()', 'fruit::Annotated', 'fruit::Annotated'), ('new X()', 'X', 'X*'), ('new X()', 'fruit::Annotated', 'fruit::Annotated'), ]) def test_bind_multibinding_provider_explicit_signature_with_normalized_component_success(ConstructX, XAnnot, XPtrAnnot): source = ''' struct X : public ConstructionTracker { INJECT(X()) = default; static bool constructed; }; fruit::Component<> getComponent() { return fruit::createComponent() .addMultibindingProvider([](){return ConstructX;}); } fruit::Component<> getEmptyComponent() { return fruit::createComponent(); } int main() { fruit::NormalizedComponent<> normalizedComponent(getComponent); fruit::Injector<> injector(normalizedComponent, getEmptyComponent); Assert(X::num_objects_constructed == 0); Assert(injector.getMultibindings().size() == 1); Assert(X::num_objects_constructed == 1); } ''' expect_success( COMMON_DEFINITIONS, source, locals()) @pytest.mark.parametrize('XAnnot,XPtrAnnot,intAnnot', [ ('X', 'X*', 'int'), ('fruit::Annotated', 'fruit::Annotated', 'fruit::Annotated'), ]) def test_multiple_providers(XAnnot, XPtrAnnot, intAnnot): source = ''' struct X {}; fruit::Component<> getComponent() { return fruit::createComponent() .registerProvider([](){return 42;}) .addMultibindingProvider([](int){return X();}) .addMultibindingProvider([](int){return new X();}); } int main() { fruit::Injector<> injector(getComponent); std::vector multibindings = injector.getMultibindings(); Assert(multibindings.size() == 2); } ''' expect_success( COMMON_DEFINITIONS, source, locals()) @pytest.mark.parametrize('ConstructX', [ 'X()', 'new X()', ]) @pytest.mark.parametrize('XAnnot', [ 'X', 'fruit::Annotated', ]) def test_bind_multibinding_provider_malformed_signature(ConstructX, XAnnot): source = ''' struct X {}; fruit::Component<> getComponent() { return fruit::createComponent() .addMultibindingProvider([](){return ConstructX;}); } ''' expect_compile_error( 'NotASignatureError', 'CandidateSignature was specified as parameter, but it.s not a signature.', COMMON_DEFINITIONS, source, locals()) @pytest.mark.parametrize('ConstructX', [ 'X(n)', 'new X(n)', ]) @pytest.mark.parametrize('XAnnot', [ 'X', 'fruit::Annotated', ]) def test_bind_multibinding_provider_lambda_with_captures_error(ConstructX, XAnnot): source = ''' struct X { X(int) {} }; fruit::Component<> getComponent() { int n = 3; return fruit::createComponent() .addMultibindingProvider([=]{return ConstructX;}); } ''' expect_compile_error( 'FunctorUsedAsProviderError<.*>', 'A stateful lambda or a non-lambda functor was used as provider', COMMON_DEFINITIONS, source, locals()) # TODO: should XPtrAnnot be just XAnnot in the signature? # Make sure the behavior here is consistent with registerProvider() and registerFactory(). @pytest.mark.parametrize('XAnnot,XPtrAnnot,XAnnotRegex', [ ('X', 'X*', '(struct )?X'), ('fruit::Annotated', 'fruit::Annotated', '(struct )?fruit::Annotated<(struct )?Annotation1, ?(struct )?X>'), ]) def test_provider_returns_nullptr_error(XAnnot, XPtrAnnot, XAnnotRegex): source = ''' struct X {}; fruit::Component<> getComponent() { return fruit::createComponent() .addMultibindingProvider([](){return (X*)nullptr;}); } int main() { fruit::Injector<> injector(getComponent); injector.getMultibindings(); } ''' expect_runtime_error( 'Fatal injection error: attempting to get an instance for the type XAnnotRegex but the provider returned nullptr', COMMON_DEFINITIONS, source, locals()) if __name__== '__main__': main(__file__)