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 absl.testing import parameterized
17from fruit_test_common import *
18
19COMMON_DEFINITIONS = '''
20    #include "test_common.h"
21
22    struct Annotation1 {};
23    struct Annotation2 {};
24    struct Annotation3 {};
25    '''
26
27class TestInjectorUnsafeGet(parameterized.TestCase):
28    @parameterized.parameters([
29        ('X', 'Y', 'Z'),
30        ('fruit::Annotated<Annotation1, X>', 'fruit::Annotated<Annotation2, Y>', 'fruit::Annotated<Annotation3, Z>'),
31    ])
32    def test_success(self, XAnnot, YAnnot, ZAnnot):
33        source = '''
34            struct Y {
35              using Inject = Y();
36              Y() = default;
37            };
38
39            struct X {
40              using Inject = X(YAnnot);
41              X(Y) {
42              }
43            };
44
45            struct Z {};
46
47            fruit::Component<XAnnot> getComponent() {
48              return fruit::createComponent();
49            }
50
51            fruit::Component<> getRootComponent() {
52              return fruit::createComponent()
53                  .install(getComponent);
54            }
55
56            int main() {
57              fruit::Injector<> injector(getRootComponent);
58              const X* x = fruit::impl::InjectorAccessorForTests::unsafeGet<XAnnot>(injector);
59              const Y* y = fruit::impl::InjectorAccessorForTests::unsafeGet<YAnnot>(injector);
60              const Z* z = fruit::impl::InjectorAccessorForTests::unsafeGet<ZAnnot>(injector);
61
62              (void) x;
63              (void) y;
64              (void) z;
65              Assert(x != nullptr);
66              Assert(y != nullptr);
67              Assert(z == nullptr);
68            }
69            '''
70        expect_success(
71            COMMON_DEFINITIONS,
72            source,
73            locals())
74
75if __name__ == '__main__':
76    absltest.main()
77