blob: feab30f82ef22ea816ba28f3a88e6c5d9162dd0e [file] [log] [blame]
Marco Polettie00461f2017-06-12 00:51:18 +01001#!/usr/bin/env python3
2# Copyright 2016 Google Inc. All Rights Reserved.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS-IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
Marco Poletti4ded6e22019-07-20 19:21:21 -070016from absl.testing import parameterized
Marco Polettie00461f2017-06-12 00:51:18 +010017from fruit_test_common import *
18
19COMMON_DEFINITIONS = '''
20 #include "test_common.h"
21
22 struct X {
23 INJECT(X()) {
24 Assert(!constructed);
25 constructed = true;
26 }
27
28 static bool constructed;
29 };
30
31 bool X::constructed = false;
32
33 struct Y {
34 Y() {
35 Assert(!constructed);
36 constructed = true;
37 }
38
39 static bool constructed;
40 };
41
42 bool Y::constructed = false;
43
44 struct Z {
45 Z() {
46 Assert(!constructed);
47 constructed = true;
48 }
49
50 static bool constructed;
51 };
52
53 bool Z::constructed = false;
54 '''
55
Marco Poletti4ded6e22019-07-20 19:21:21 -070056class TestEagerInjection(parameterized.TestCase):
57 def test_eager_injection_deprecated(self):
58 source = '''
59 fruit::Component<X> getComponent() {
60 return fruit::createComponent()
61 .addMultibindingProvider([](){return new Y();})
62 .registerConstructor<Z()>();
63 }
64
65 int main() {
66
67 fruit::Injector<X> injector(getComponent);
68
69 Assert(!X::constructed);
70 Assert(!Y::constructed);
71 Assert(!Z::constructed);
72
73 injector.eagerlyInjectAll();
74
75 Assert(X::constructed);
76 Assert(Y::constructed);
77 // Z still not constructed, it's not reachable from Injector<X>.
78 Assert(!Z::constructed);
79
80 return 0;
81 }
82 '''
83 expect_generic_compile_error(
84 'deprecation|deprecated',
85 COMMON_DEFINITIONS,
86 source)
Marco Polettid81f3dc2018-03-04 10:50:42 +000087
Marco Poletti4ded6e22019-07-20 19:21:21 -070088 def test_eager_injection(self):
89 source = '''
90 fruit::Component<X> getComponent() {
91 return fruit::createComponent()
92 .addMultibindingProvider([](){return new Y();})
93 .registerConstructor<Z()>();
94 }
95
96 int main() {
97
98 fruit::Injector<X> injector(getComponent);
99
100 Assert(!X::constructed);
101 Assert(!Y::constructed);
102 Assert(!Z::constructed);
103
104 injector.eagerlyInjectAll();
105
106 Assert(X::constructed);
107 Assert(Y::constructed);
108 // Z still not constructed, it's not reachable from Injector<X>.
109 Assert(!Z::constructed);
110
111 return 0;
112 }
113 '''
114 expect_success(
115 COMMON_DEFINITIONS,
116 source,
117 locals(),
118 ignore_deprecation_warnings=True)
Marco Polettie00461f2017-06-12 00:51:18 +0100119
Marco Poletti5dba7172019-04-27 10:06:30 +0100120if __name__ == '__main__':
Marco Poletti4ded6e22019-07-20 19:21:21 -0700121 absltest.main()