aboutsummaryrefslogtreecommitdiff
path: root/tests/test_required_types.py
diff options
context:
space:
mode:
authorMarco Poletti <poletti.marco@gmail.com>2017-07-30 12:09:06 +0100
committerMarco Poletti <poletti.marco@gmail.com>2017-07-30 13:25:03 +0100
commitc84c7dea4871e0c8a24d68f3a12277cf80587e2a (patch)
treefb74adc3111e6d8caafc5d626bc83272cd475122 /tests/test_required_types.py
parentb95efcce188e5d56e7e58670cca6b22e09f714da (diff)
downloadgoogle-fruit-c84c7dea4871e0c8a24d68f3a12277cf80587e2a.tar.gz
Allow Component, NormalizedComponent, Injector and Provider to specify that they only require/provide a const T (instead of requiring/providing a T). This is in preparation for allowing const bindings.
Diffstat (limited to 'tests/test_required_types.py')
-rwxr-xr-xtests/test_required_types.py93
1 files changed, 93 insertions, 0 deletions
diff --git a/tests/test_required_types.py b/tests/test_required_types.py
index c2b4331..20f04cd 100755
--- a/tests/test_required_types.py
+++ b/tests/test_required_types.py
@@ -200,5 +200,98 @@ def test_required_annotated_forward_declared_success():
'''
expect_success(COMMON_DEFINITIONS, source)
+def test_required_const_forward_declared_success():
+ source = '''
+ struct X;
+ using XFactory = std::function<std::unique_ptr<X>()>;
+ struct Y {
+ XFactory xFactory;
+
+ INJECT(Y(XFactory xFactory))
+ : xFactory(xFactory) {
+ }
+
+ void doStuff();
+ };
+ fruit::Component<fruit::Required<const XFactory>, Y> getYComponent() {
+ return fruit::createComponent();
+ }
+ fruit::Component<const XFactory> getXFactoryComponent();
+ fruit::Component<Y> getComponent() {
+ return fruit::createComponent()
+ .install(getYComponent)
+ .install(getXFactoryComponent);
+ }
+ int main() {
+ fruit::Injector<Y> injector(getComponent());
+ Y* y(injector);
+ y->doStuff();
+ }
+
+ // We define X as late as possible, to make sure that all the above compiles even if X is only forward-declared.
+ struct X {
+ virtual void foo() = 0;
+ };
+ void Y::doStuff() {
+ xFactory()->foo();
+ }
+ struct XImpl : public X {
+ INJECT(XImpl()) = default;
+ void foo() override {}
+ };
+ fruit::Component<const XFactory> getXFactoryComponent() {
+ return fruit::createComponent()
+ .bind<X, XImpl>();
+ }
+ '''
+ expect_success(COMMON_DEFINITIONS, source)
+
+def test_required_const_annotated_forward_declared_success():
+ source = '''
+ struct X;
+ using XFactory = std::function<std::unique_ptr<X>()>;
+ using ConstXFactoryAnnot = fruit::Annotated<Annotation1, const XFactory>;
+ struct Y {
+ XFactory xFactory;
+
+ INJECT(Y(ANNOTATED(Annotation1, XFactory) xFactory))
+ : xFactory(xFactory) {
+ }
+
+ void doStuff();
+ };
+ fruit::Component<fruit::Required<ConstXFactoryAnnot>, Y> getYComponent() {
+ return fruit::createComponent();
+ }
+ fruit::Component<ConstXFactoryAnnot> getXFactoryComponent();
+ fruit::Component<Y> getComponent() {
+ return fruit::createComponent()
+ .install(getYComponent)
+ .install(getXFactoryComponent);
+ }
+ int main() {
+ fruit::Injector<Y> injector(getComponent());
+ Y* y(injector);
+ y->doStuff();
+ }
+
+ // We define X as late as possible, to make sure that all the above compiles even if X is only forward-declared.
+ struct X {
+ virtual void foo() = 0;
+ };
+ void Y::doStuff() {
+ xFactory()->foo();
+ }
+ struct XImpl : public X {
+ INJECT(XImpl()) = default;
+ void foo() override {}
+ };
+ fruit::Component<ConstXFactoryAnnot> getXFactoryComponent() {
+ return fruit::createComponent()
+ .bind<fruit::Annotated<Annotation1, X>, fruit::Annotated<Annotation1, XImpl>>();
+ }
+ '''
+ expect_success(COMMON_DEFINITIONS, source)
+
if __name__== '__main__':
main(__file__)