#!/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 {}; template using WithNoAnnot = T; template using WithAnnot1 = fruit::Annotated; ''' @pytest.mark.parametrize('WithAnnot', [ 'WithNoAnnot', 'WithAnnot1', ]) @pytest.mark.parametrize('ConstructX,XPtr', [ ('X()', 'X'), ('new X()', 'X*'), ]) def test_register_provider_success(WithAnnot,ConstructX, XPtr): source = ''' struct X : public ConstructionTracker { int value = 5; }; fruit::Component> getComponent() { return fruit::createComponent() .registerProvider()>([](){return ConstructX;}); } int main() { fruit::Injector> injector(getComponent); Assert((injector.get>(). value == 5)); Assert((injector.get>()->value == 5)); Assert((injector.get>(). value == 5)); Assert((injector.get>(). value == 5)); Assert((injector.get>()->value == 5)); Assert((injector.get>(). value == 5)); Assert((injector.get>>()->value == 5)); Assert(X::num_objects_constructed == 1); } ''' expect_success( COMMON_DEFINITIONS, source, locals()) @pytest.mark.parametrize('WithAnnot', [ 'WithNoAnnot', 'WithAnnot1', ]) def test_register_provider_abstract_class_ok(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() .registerProvider()>([](){return static_cast(new X());}); } int main() { fruit::Injector> injector(getComponent); Assert(injector.get>()->foo() == 5); } ''' expect_success( COMMON_DEFINITIONS, source, locals()) @pytest.mark.parametrize('WithAnnot', [ 'WithNoAnnot', 'WithAnnot1', ]) def test_register_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() .registerProvider()>([](){return static_cast(new X());}); } ''' expect_compile_error( 'ProviderReturningPointerToAbstractClassWithNoVirtualDestructorError', 'registerProvider\(\) was called with a lambda that returns a pointer to T, but T is an abstract class', COMMON_DEFINITIONS, source, locals()) @pytest.mark.parametrize('ConstructX', [ 'X()', 'new X()', ]) def test_register_provider_not_copyable_success(ConstructX): source = ''' struct X { X() = default; X(X&&) = default; X(const X&) = delete; }; fruit::Component getComponent() { return fruit::createComponent() .registerProvider([](){return ConstructX;}); } int main() { fruit::Injector injector(getComponent); injector.get(); } ''' expect_success( COMMON_DEFINITIONS, source, locals()) def test_register_provider_not_movable_returning_pointer_success(): source = ''' struct X { X() = default; X(X&&) = delete; X(const X&) = delete; }; fruit::Component getComponent() { return fruit::createComponent() .registerProvider([](){return new X();}); } int main() { fruit::Injector injector(getComponent); injector.get(); } ''' expect_success( COMMON_DEFINITIONS, source) @pytest.mark.parametrize('XAnnot', [ 'X', 'fruit::Annotated', ]) def test_register_provider_error_not_function(XAnnot): source = ''' struct X { X(int) {} }; fruit::Component getComponent() { int n = 3; return fruit::createComponent() .registerProvider([=]{return X(n);}); } ''' expect_compile_error( 'FunctorUsedAsProviderError<.*>', 'A stateful lambda or a non-lambda functor was used as provider', COMMON_DEFINITIONS, source, locals()) @pytest.mark.parametrize('intAnnot', [ 'int', 'fruit::Annotated', ]) def test_register_provider_error_malformed_signature(intAnnot): source = ''' fruit::Component getComponent() { return fruit::createComponent() .registerProvider([](){return 42;}); } ''' expect_compile_error( 'NotASignatureError', 'CandidateSignature was specified as parameter, but it.s not a signature. Signatures are of the form', COMMON_DEFINITIONS, source, locals()) @pytest.mark.parametrize('XAnnot,XPtrAnnot,XAnnotRegex', [ ('X', 'X*', '(struct )?X'), ('fruit::Annotated', 'fruit::Annotated', '(struct )?fruit::Annotated<(struct )?Annotation1, ?(struct )?X>'), ]) def test_register_provider_error_returned_nullptr(XAnnot, XPtrAnnot, XAnnotRegex): source = ''' struct X {}; fruit::Component getComponent() { return fruit::createComponent() .registerProvider([](){return (X*)nullptr;}); } int main() { fruit::Injector injector(getComponent); injector.get(); } ''' expect_runtime_error( 'Fatal injection error: attempting to get an instance for the type XAnnotRegex but the provider returned nullptr', 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_register_provider_with_param_success(ConstructX, XPtr, WithAnnot, YVariant): source = ''' struct Y {}; struct X {}; fruit::Component> getYComponent() { return fruit::createComponent() .registerConstructor()>(); } fruit::Component getComponent() { return fruit::createComponent() .install(getYComponent) .registerProvider)>([](YVariant){ return ConstructX; }); } int main() { fruit::Injector injector(getComponent); injector.get(); } ''' 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_register_provider_with_param_const_binding_success(ConstructX, XPtr, WithAnnot, YVariant): source = ''' struct Y {}; struct X {}; const Y y{}; fruit::Component> getYComponent() { return fruit::createComponent() .bindInstance, Y>(y); } fruit::Component getComponent() { return fruit::createComponent() .install(getYComponent) .registerProvider)>([](YVariant){ return ConstructX; }); } int main() { fruit::Injector injector(getComponent); injector.get(); } ''' 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_register_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) .registerProvider)>([](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_register_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() .registerProvider)>([](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_register_provider_requiring_nonconst_then_requiring_const_ok(): source = ''' struct X {}; struct Y {}; struct Z {}; fruit::Component getRootComponent() { return fruit::createComponent() .registerProvider([](X&) { return Y();}) .registerProvider([](const X&) { return Z();}) .registerConstructor(); } int main() { fruit::Injector injector(getRootComponent); injector.get(); injector.get(); } ''' expect_success( COMMON_DEFINITIONS, source, locals()) def test_register_provider_requiring_nonconst_then_requiring_const_declaring_const_requirement_error(): source = ''' struct X {}; struct Y {}; struct Z {}; fruit::Component, Y, Z> getRootComponent() { return fruit::createComponent() .registerProvider([](X&) { return Y();}) .registerProvider([](const X&) { return Z();}); } ''' 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_register_provider_requiring_const_then_requiring_nonconst_ok(): source = ''' struct X {}; struct Y {}; struct Z {}; fruit::Component getRootComponent() { return fruit::createComponent() .registerProvider([](const X&) { return Y();}) .registerProvider([](X&) { return Z();}) .registerConstructor(); } int main() { fruit::Injector injector(getRootComponent); injector.get(); injector.get(); } ''' expect_success( COMMON_DEFINITIONS, source, locals()) def test_register_provider_requiring_const_then_requiring_nonconst_declaring_const_requirement_error(): source = ''' struct X {}; struct Y {}; struct Z {}; fruit::Component, Y, Z> getRootComponent() { return fruit::createComponent() .registerProvider([](const X&) { return Y();}) .registerProvider([](X&) { return Z();}); } ''' 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('YVariant,YVariantRegex', [ ('Y**', r'Y\*\*'), ('std::shared_ptr*', r'std::shared_ptr\*'), ('std::nullptr_t', r'(std::)?nullptr(_t)?'), ('Y*&', r'Y\*&'), ('Y(*)()', r'Y(\((__cdecl)?\*\))?\((void)?\)'), ]) def test_register_provider_with_param_error_type_not_injectable(ConstructX, XPtr, YVariant, YVariantRegex): source = ''' struct Y {}; struct X {}; fruit::Component<> getComponent() { return fruit::createComponent() .registerProvider([](YVariant){ return ConstructX; }); } ''' expect_compile_error( 'NonInjectableTypeError', 'The type T is not injectable.', COMMON_DEFINITIONS, source, locals()) if __name__== '__main__': main(__file__)