• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/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
16from fruit_test_common import *
17
18COMMON_DEFINITIONS = '''
19    #include "test_common.h"
20
21    struct X {
22      INJECT(X()) {
23        Assert(!constructed);
24        constructed = true;
25      }
26
27      static bool constructed;
28    };
29
30    bool X::constructed = false;
31
32    struct Y {
33      Y() {
34        Assert(!constructed);
35        constructed = true;
36      }
37
38      static bool constructed;
39    };
40
41    bool Y::constructed = false;
42
43    struct Z {
44      Z() {
45        Assert(!constructed);
46        constructed = true;
47      }
48
49      static bool constructed;
50    };
51
52    bool Z::constructed = false;
53    '''
54
55def test_eager_injection_deprecated():
56    source = '''
57        fruit::Component<X> getComponent() {
58          return fruit::createComponent()
59            .addMultibindingProvider([](){return new Y();})
60            .registerConstructor<Z()>();
61        }
62
63        int main() {
64
65          fruit::Injector<X> injector(getComponent);
66
67          Assert(!X::constructed);
68          Assert(!Y::constructed);
69          Assert(!Z::constructed);
70
71          injector.eagerlyInjectAll();
72
73          Assert(X::constructed);
74          Assert(Y::constructed);
75          // Z still not constructed, it's not reachable from Injector<X>.
76          Assert(!Z::constructed);
77
78          return 0;
79        }
80        '''
81    expect_generic_compile_error(
82        'deprecation|deprecated',
83        COMMON_DEFINITIONS,
84        source)
85
86def test_eager_injection():
87    source = '''
88        fruit::Component<X> getComponent() {
89          return fruit::createComponent()
90            .addMultibindingProvider([](){return new Y();})
91            .registerConstructor<Z()>();
92        }
93
94        int main() {
95
96          fruit::Injector<X> injector(getComponent);
97
98          Assert(!X::constructed);
99          Assert(!Y::constructed);
100          Assert(!Z::constructed);
101
102          injector.eagerlyInjectAll();
103
104          Assert(X::constructed);
105          Assert(Y::constructed);
106          // Z still not constructed, it's not reachable from Injector<X>.
107          Assert(!Z::constructed);
108
109          return 0;
110        }
111        '''
112    expect_success(
113        COMMON_DEFINITIONS,
114        source,
115        locals(),
116        ignore_deprecation_warnings=True)
117
118if __name__== '__main__':
119    main(__file__)
120