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