aboutsummaryrefslogtreecommitdiff
path: root/tests/test_bind_interface.py
blob: 14f1b35a28503c109912f739a773ab2e3c38f8fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#!/usr/bin/env python3
#  Copyright 2016 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS-IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import pytest

from fruit_test_common import *

COMMON_DEFINITIONS = '''
    #include "test_common.h"

    struct Annotation1 {};
    struct Annotation2 {};
    '''

@pytest.mark.parametrize('XAnnot,MaybeConstXAnnot,XConstRefAnnot,YAnnot', [
    ('X', 'X', 'const X&', 'Y'),
    ('X', 'const X', 'const X&', 'Y'),
    ('X', 'X', 'const X&', 'fruit::Annotated<Annotation1, Y>'),
    ('X', 'const X', 'const X&', 'fruit::Annotated<Annotation1, Y>'),
    ('fruit::Annotated<Annotation1, X>', 'fruit::Annotated<Annotation1, X>', 'fruit::Annotated<Annotation1, const X&>', 'Y'),
    ('fruit::Annotated<Annotation1, X>', 'fruit::Annotated<Annotation1, const X>', 'fruit::Annotated<Annotation1, const X&>', 'Y'),
    ('fruit::Annotated<Annotation1, X>', 'fruit::Annotated<Annotation1, X>', 'fruit::Annotated<Annotation1, const X&>', 'fruit::Annotated<Annotation1, Y>'),
    ('fruit::Annotated<Annotation1, X>', 'fruit::Annotated<Annotation1, const X>', 'fruit::Annotated<Annotation1, const X&>', 'fruit::Annotated<Annotation1, Y>'),
    ('fruit::Annotated<Annotation1, X>', 'fruit::Annotated<Annotation1, X>', 'fruit::Annotated<Annotation1, const X&>', 'fruit::Annotated<Annotation2, Y>'),
    ('fruit::Annotated<Annotation1, X>', 'fruit::Annotated<Annotation1, const X>', 'fruit::Annotated<Annotation1, const X&>', 'fruit::Annotated<Annotation2, Y>'),
])
def test_bind_interface(XAnnot, MaybeConstXAnnot, XConstRefAnnot, YAnnot):
    source = '''
        struct X {
          virtual void f() const = 0;
        };

        struct Y : public X {
          INJECT(Y()) = default;
          
          void f() const override {
          }
        };

        fruit::Component<MaybeConstXAnnot> getComponent() {
          return fruit::createComponent()
            .bind<XAnnot, YAnnot>();
        }

        int main() {
          fruit::Injector<MaybeConstXAnnot> injector(getComponent);
          const X& x = injector.get<XConstRefAnnot>();
          x.f();
        }
    '''
    expect_success(
        COMMON_DEFINITIONS,
        source,
        locals())

@pytest.mark.parametrize('XAnnot,ConstXAnnot,XConstRefAnnot,YAnnot', [
    ('X', 'const X', 'const X&', 'Y'),
    ('X', 'const X', 'const X&', 'fruit::Annotated<Annotation1, Y>'),
    ('fruit::Annotated<Annotation1, X>', 'fruit::Annotated<Annotation1, const X>', 'fruit::Annotated<Annotation1, const X&>', 'Y'),
    ('fruit::Annotated<Annotation1, X>', 'fruit::Annotated<Annotation1, const X>', 'fruit::Annotated<Annotation1, const X&>', 'fruit::Annotated<Annotation1, Y>'),
    ('fruit::Annotated<Annotation1, X>', 'fruit::Annotated<Annotation1, const X>', 'fruit::Annotated<Annotation1, const X&>', 'fruit::Annotated<Annotation2, Y>'),
])
def test_bind_interface_to_constant(XAnnot, ConstXAnnot, XConstRefAnnot, YAnnot):
    source = '''
        struct X {
          virtual void f() const = 0;
        };

        struct Y : public X {
          void f() const override {
          }
        };
        
        const Y y{};

        fruit::Component<ConstXAnnot> getComponent() {
          return fruit::createComponent()
            .bindInstance<YAnnot, Y>(y)
            .bind<XAnnot, YAnnot>();
        }

        int main() {
          fruit::Injector<ConstXAnnot> injector(getComponent);
          const X& x = injector.get<XConstRefAnnot>();
          x.f();
        }
    '''
    expect_success(
        COMMON_DEFINITIONS,
        source,
        locals())

@pytest.mark.parametrize('XAnnot,XRefAnnot,YAnnot', [
    ('X', 'X&', 'Y'),
    ('fruit::Annotated<Annotation1, X>', 'fruit::Annotated<Annotation1, X&>', 'fruit::Annotated<Annotation2, Y>'),
])
def test_bind_interface_target_bound_in_other_component(XAnnot, XRefAnnot, YAnnot):
    source = '''
        struct X {
          virtual void f() = 0;
        };

        struct Y : public X {
          void f() override {
          }
        };

        fruit::Component<fruit::Required<YAnnot>, XAnnot> getComponent() {
          return fruit::createComponent()
            .bind<XAnnot, YAnnot>();
        }

        fruit::Component<XAnnot> getRootComponent() {
          return fruit::createComponent()
            .registerConstructor<YAnnot()>()
            .install(getComponent);
        }

        int main() {
          fruit::Injector<XAnnot> injector(getRootComponent);
          X& x = injector.get<XRefAnnot>();
          x.f();
        }
    '''
    expect_success(
        COMMON_DEFINITIONS,
        source,
        locals())

@pytest.mark.parametrize('XAnnot,XRefAnnot,YAnnot', [
    ('X', 'X&', 'Y'),
    ('fruit::Annotated<Annotation1, X>', 'fruit::Annotated<Annotation1, X&>', 'fruit::Annotated<Annotation2, Y>'),
])
def test_bind_nonconst_interface_requires_nonconst_target(XAnnot, XRefAnnot, YAnnot):
    source = '''
        struct X {
          virtual void f() = 0;
        };

        struct Y : public X {
          void f() override {
          }
        };

        fruit::Component<fruit::Required<const YAnnot>, XAnnot> getComponent() {
          return fruit::createComponent()
            .bind<XAnnot, YAnnot>();
        }
    '''
    expect_compile_error(
        'ConstBindingDeclaredAsRequiredButNonConstBindingRequiredError<YAnnot>',
        'The type T was declared as a const Required type in the returned Component, however a non-const binding',
        COMMON_DEFINITIONS,
        source,
        locals())

@pytest.mark.parametrize('XAnnot,YAnnot', [
    ('X', 'Y'),
    ('fruit::Annotated<Annotation1, X>', 'fruit::Annotated<Annotation2, Y>'),
])
def test_bind_interface_to_constant_nonconst_required_const_bound_error(XAnnot, YAnnot):
    source = '''
        struct X {
          virtual void f() const = 0;
        };

        struct Y : public X {
          void f() const override {
          }
        };
        
        const Y y{};

        fruit::Component<XAnnot> getComponent() {
          return fruit::createComponent()
            .bindInstance<YAnnot, Y>(y)
            .bind<XAnnot, YAnnot>();
        }
    '''
    expect_compile_error(
        'NonConstBindingRequiredButConstBindingProvidedError<YAnnot>',
        'The type T was provided as constant, however one of the constructors/providers/factories in this component',
        COMMON_DEFINITIONS,
        source,
        locals())

@pytest.mark.parametrize('XAnnot,XRefAnnot,YAnnot', [
    ('X', 'X&', 'Y'),
    ('fruit::Annotated<Annotation1, X>', 'fruit::Annotated<Annotation1, X&>', 'fruit::Annotated<Annotation2, Y>'),
])
def test_bind_nonconst_interface_requires_nonconst_target_abstract(XAnnot, XRefAnnot, YAnnot):
    source = '''
        struct X {
          virtual void f() = 0;
        };

        struct Y : public X {};

        fruit::Component<fruit::Required<const YAnnot>, XAnnot> getComponent() {
          return fruit::createComponent()
            .bind<XAnnot, YAnnot>();
        }
    '''
    expect_compile_error(
        'ConstBindingDeclaredAsRequiredButNonConstBindingRequiredError<YAnnot>',
        'The type T was declared as a const Required type in the returned Component, however a non-const binding',
        COMMON_DEFINITIONS,
        source,
        locals())

@pytest.mark.parametrize('XAnnot,intAnnot', [
    ('X', 'int'),
    ('fruit::Annotated<Annotation1, X>', 'fruit::Annotated<Annotation2, int>'),
])
def test_error_not_base(XAnnot, intAnnot):
    source = '''
        struct X {};

        fruit::Component<intAnnot> getComponent() {
          return fruit::createComponent()
            .bind<XAnnot, intAnnot>();
        }
        '''
    expect_compile_error(
        'NotABaseClassOfError<X,int>',
        'I is not a base class of C.',
        COMMON_DEFINITIONS,
        source,
        locals())

# TODO: maybe the error should include the annotation here.
@pytest.mark.parametrize('XAnnot', [
    'X',
    'fruit::Annotated<Annotation1, X>',
])
def test_error_bound_to_itself(XAnnot):
    source = '''
        struct X {};

        fruit::Component<X> getComponent() {
          return fruit::createComponent()
            .bind<XAnnot, XAnnot>();
        }
        '''
    expect_compile_error(
        'InterfaceBindingToSelfError<X>',
        'The type C was bound to itself.',
        COMMON_DEFINITIONS,
        source,
        locals())

def test_bound_to_itself_with_annotation_error():
    source = '''
        struct X {};

        fruit::Component<> getComponent() {
          return fruit::createComponent()
            .registerConstructor<X()>()
            .bind<fruit::Annotated<Annotation1, X>, X>();
        }
        '''
    expect_compile_error(
        'InterfaceBindingToSelfError<X>',
        'The type C was bound to itself.',
        COMMON_DEFINITIONS,
        source)

def test_bound_chain_ok():
    source = '''
        struct X {
          virtual void f() = 0;
        };

        struct Y : public X {};

        struct Z : public Y {
          INJECT(Z()) = default;
          void f() override {
          }
        };

        fruit::Component<X> getComponent() {
          return fruit::createComponent()
            .bind<X, Y>()
            .bind<Y, Z>();
        }

        int main() {
          fruit::Injector<X> injector(getComponent);
          X& x = injector.get<X&>();
          x.f();
        }
    '''
    expect_success(COMMON_DEFINITIONS, source)

def test_bind_non_normalized_types_error():
    source = '''
        struct X {};

        struct Y : public std::shared_ptr<X> {};

        fruit::Component<> getComponent() {
          return fruit::createComponent()
            .bind<std::shared_ptr<X>, Y>();
        }
        '''
    expect_compile_error(
        'NonClassTypeError<std::shared_ptr<X>,X>',
        'A non-class type T was specified. Use C instead',
        COMMON_DEFINITIONS,
        source)

if __name__== '__main__':
    main(__file__)