diff options
author | Marco Poletti <poletti.marco@gmail.com> | 2017-01-02 16:33:59 +0100 |
---|---|---|
committer | Marco Poletti <poletti.marco@gmail.com> | 2017-01-02 16:33:59 +0100 |
commit | 7562d52b7a4dde9d4b61a5035729007dad1757f6 (patch) | |
tree | 91b5d428492d64865364d853869129dce8e7588d /tests/test_install.py | |
parent | f91cc9e6661ee6fe32bdc1f651917fe7d29416ae (diff) | |
download | google-fruit-7562d52b7a4dde9d4b61a5035729007dad1757f6.tar.gz |
Add some e2e tests for PartialComponent::install().
Diffstat (limited to 'tests/test_install.py')
-rwxr-xr-x | tests/test_install.py | 121 |
1 files changed, 121 insertions, 0 deletions
diff --git a/tests/test_install.py b/tests/test_install.py new file mode 100755 index 0000000..1d2c264 --- /dev/null +++ b/tests/test_install.py @@ -0,0 +1,121 @@ +#!/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 nose2.tools.params import params + +from fruit_test_common import * + +COMMON_DEFINITIONS = ''' + #include "test_common.h" + + struct X; + + struct Annotation1 {}; + using XAnnot1 = fruit::Annotated<Annotation1, X>; + ''' + +def test_success(): + source = ''' + struct X { + int n; + X(int n) : n(n) {} + }; + + fruit::Component<X> getParentComponent() { + return fruit::createComponent() + .registerProvider([]() { return X(5); }); + } + + fruit::Component<X> getComponent() { + return fruit::createComponent() + .install(getParentComponent()); + } + + int main() { + fruit::Injector<X> injector(getComponent()); + X x = injector.get<X>(); + Assert(x.n == 5); + } + ''' + expect_success(COMMON_DEFINITIONS, source) + +def test_with_requirements_success(): + source = ''' + struct X { + int n; + X(int n) : n(n) {} + }; + + struct Y { + X x; + Y(X x): x(x) {} + }; + + fruit::Component<fruit::Required<X>, Y> getParentYComponent() { + return fruit::createComponent() + .registerProvider([](X x) { return Y(x); }); + } + + fruit::Component<fruit::Required<X>, Y> getYComponent() { + return fruit::createComponent() + .install(getParentYComponent()); + } + + fruit::Component<Y> getComponent() { + return fruit::createComponent() + .registerProvider([]() { return X(5); }) + .install(getYComponent()); + } + + int main() { + fruit::Injector<Y> injector(getComponent()); + Y y = injector.get<Y>(); + Assert(y.x.n == 5); + } + ''' + expect_success(COMMON_DEFINITIONS, source) + +def test_with_requirements_not_specified_in_child_component_error(): + source = ''' + struct X { + int n; + X(int n) : n(n) {} + }; + + struct Y { + X x; + Y(X x): x(x) {} + }; + + fruit::Component<fruit::Required<X>, Y> getParentYComponent() { + return fruit::createComponent() + .registerProvider([](X x) { return Y(x); }); + } + + // We intentionally don't have fruit::Required<X> here, we want to test that this results in an error. + fruit::Component<Y> getYComponent() { + return fruit::createComponent() + .install(getParentYComponent()); + } + ''' + expect_compile_error( + 'NoBindingFoundError<X>', + 'No explicit binding nor C::Inject definition was found for T', + COMMON_DEFINITIONS, + source) + +if __name__ == '__main__': + import nose2 + nose2.main() |