aboutsummaryrefslogtreecommitdiff
path: root/tests/test_binding_clash.py
blob: 03662dbd6156595143ce0294c9c524de89542079 (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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
#!/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.

from absl.testing import parameterized
from fruit_test_common import *

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

    struct X;
    struct Y;
    struct Z;

    struct Annotation1 {};
    using XAnnot1 = fruit::Annotated<Annotation1, X>;
    using YAnnot1 = fruit::Annotated<Annotation1, Y>;
    using ZAnnot1 = fruit::Annotated<Annotation1, Z>;
    using ConstXAnnot1 = fruit::Annotated<Annotation1, const X>;
    using ConstYAnnot1 = fruit::Annotated<Annotation1, const Y>;
    using ConstZAnnot1 = fruit::Annotated<Annotation1, const Z>;

    struct Annotation2 {};
    using XAnnot2 = fruit::Annotated<Annotation2, X>;
    using YAnnot2 = fruit::Annotated<Annotation2, Y>;
    using ZAnnot2 = fruit::Annotated<Annotation2, Z>;
    using ConstXAnnot2 = fruit::Annotated<Annotation2, const X>;
    using ConstYAnnot2 = fruit::Annotated<Annotation2, const Y>;
    using ConstZAnnot2 = fruit::Annotated<Annotation2, const Z>;

    struct Annotation3 {};
    '''

CONSTRUCTOR_BINDING = (
    '',
    '.registerConstructor<XAnnot()>()')
INTERFACE_BINDING = (
    '''
        struct Y : public X {};
    ''',
    '''
        .bind<XAnnot, YAnnot>()
        .registerConstructor<YAnnot()>()
    ''')
INTERFACE_BINDING2 = (
    '''
        struct Y2 : public X {};
    ''',
    '''
        .bind<XAnnot, Y2Annot>()
        .registerConstructor<Y2Annot()>()
    ''')
INSTALL = (
    '''
        fruit::Component<XAnnot> getParentComponent() {
          return fruit::createComponent()
            .registerConstructor<XAnnot()>();
        }
    ''',
    '.install(getParentComponent)')
INSTALL2 = (
    '''
        fruit::Component<XAnnot> getParentComponent2() {
          return fruit::createComponent()
            .registerConstructor<XAnnot()>();
        }
    ''',
    '.install(getParentComponent2)')
CONST_BINDING_FROM_INSTALL = (
    '''
        fruit::Component<const XAnnot> getParentComponent() {
          return fruit::createComponent()
            .registerConstructor<XAnnot()>();
        }
    ''',
    '.install(getParentComponent)')
CONST_BINDING_FROM_INSTALL2 = (
    '''
        fruit::Component<const XAnnot> getParentComponent2() {
          return fruit::createComponent()
            .registerConstructor<XAnnot()>();
        }
    ''',
    '.install(getParentComponent2)')
CONST_BINDING = (
    '''
        const X x{};
    ''',
    '.bindInstance<XAnnot, X>(x)')
CONST_BINDING2 = (
    '''
        const X x2{};
    ''',
    '.bindInstance<XAnnot, X>(x2)')

class TestBindingClash(parameterized.TestCase):
    @multiple_named_parameters([
        ('CONSTRUCTOR_BINDING + INSTALL',) + CONSTRUCTOR_BINDING + INSTALL,
        ('INTERFACE_BINDING + INSTALL',) + INTERFACE_BINDING + INSTALL,
        ('INSTALL + INSTALL2',) + INSTALL + INSTALL2,
        ('CONSTRUCTOR_BINDING + CONST_BINDING_FROM_INSTALL',) + CONSTRUCTOR_BINDING + CONST_BINDING_FROM_INSTALL,
        ('INTERFACE_BINDING + CONST_BINDING_FROM_INSTALL',) + INTERFACE_BINDING + CONST_BINDING_FROM_INSTALL,
        ('INSTALL2 + CONST_BINDING_FROM_INSTALL',) + INSTALL2 + CONST_BINDING_FROM_INSTALL,
        ('CONST_BINDING_FROM_INSTALL + INSTALL2',) + CONST_BINDING_FROM_INSTALL + INSTALL2,
        ('CONST_BINDING + INSTALL2',) + CONST_BINDING + INSTALL2,
        ('CONST_BINDING_FROM_INSTALL + CONST_BINDING_FROM_INSTALL2',) + CONST_BINDING_FROM_INSTALL + CONST_BINDING_FROM_INSTALL2,
        ('CONST_BINDING + CONST_BINDING_FROM_INSTALL',) + CONST_BINDING + CONST_BINDING_FROM_INSTALL,
    ], [
        ('No annotation', 'X', 'Y', 'Y2'),
        ('With annotation', 'fruit::Annotated<Annotation1, X>', 'fruit::Annotated<Annotation2, Y>', 'fruit::Annotated<Annotation3, Y2>'),
    ])
    def test_clash_with_install(self,
            binding1_preparation, binding1, binding2_preparation, binding2, XAnnot, YAnnot, Y2Annot):
        source = '''
            struct X{};
    
            %s
            %s
    
            fruit::Component<XAnnot> getComponent() {
              return fruit::createComponent()
                  %s
                  %s;
            }
            ''' % (binding1_preparation, binding2_preparation, binding1, binding2)
        expect_compile_error(
            'DuplicateTypesInComponentError<XAnnot>',
            'The installed component provides some types that are already provided by the current component.',
            COMMON_DEFINITIONS,
            source,
            locals())

    @multiple_named_parameters([
        ('CONSTRUCTOR_BINDING + CONSTRUCTOR_BINDING',) + CONSTRUCTOR_BINDING + CONSTRUCTOR_BINDING,
        ('CONSTRUCTOR_BINDING + INTERFACE_BINDING',) + CONSTRUCTOR_BINDING + INTERFACE_BINDING,
        ('INTERFACE_BINDING + CONSTRUCTOR_BINDING',) + INTERFACE_BINDING + CONSTRUCTOR_BINDING,
        ('INTERFACE_BINDING + INTERFACE_BINDING2',) + INTERFACE_BINDING + INTERFACE_BINDING2,
        ('INSTALL + CONSTRUCTOR_BINDING',) + INSTALL + CONSTRUCTOR_BINDING,
        ('INSTALL + INTERFACE_BINDING',) + INSTALL + INTERFACE_BINDING,
        ('CONST_BINDING_FROM_INSTALL + CONSTRUCTOR_BINDING',) + CONST_BINDING_FROM_INSTALL + CONSTRUCTOR_BINDING,
        ('CONST_BINDING_FROM_INSTALL + INTERFACE_BINDING',) + CONST_BINDING_FROM_INSTALL + INTERFACE_BINDING,
        ('CONST_BINDING + CONSTRUCTOR_BINDING',) + CONST_BINDING + CONSTRUCTOR_BINDING,
        ('CONST_BINDING + INTERFACE_BINDING',) + CONST_BINDING + INTERFACE_BINDING,
        ('CONSTRUCTOR_BINDING + CONST_BINDING',) + CONSTRUCTOR_BINDING + CONST_BINDING,
        ('INTERFACE_BINDING + CONST_BINDING',) + INTERFACE_BINDING + CONST_BINDING,
        ('INSTALL2 + CONST_BINDING',) + INSTALL2 + CONST_BINDING,
        ('CONST_BINDING_FROM_INSTALL + CONST_BINDING',) + CONST_BINDING_FROM_INSTALL + CONST_BINDING,
        ('CONST_BINDING + CONST_BINDING2',) + CONST_BINDING + CONST_BINDING2,
    ], [
        ('No annotation', 'X', 'Y', 'Y2'),
        ('With annotation', 'fruit::Annotated<Annotation1, X>', 'fruit::Annotated<Annotation2, Y>', 'fruit::Annotated<Annotation3, Y2>'),
    ])
    def test_clash_with_binding(self, binding1_preparation, binding1, binding2_preparation, binding2, XAnnot, YAnnot, Y2Annot):
        source = '''
            struct X{};
    
            %s
            %s
    
            fruit::Component<XAnnot> getComponent() {
              return fruit::createComponent()
                  %s
                  %s;
            }
    
            ''' % (binding1_preparation, binding2_preparation, binding1, binding2)
        expect_compile_error(
            'TypeAlreadyBoundError<XAnnot>',
            'Trying to bind C but it is already bound.',
            COMMON_DEFINITIONS,
            source,
            locals())

    CONSTRUCTOR_BINDING_ANNOT1 = (
        '',
        '.registerConstructor<XAnnot1()>()')
    CONSTRUCTOR_BINDING_ANNOT2 = (
        '',
        '.registerConstructor<XAnnot2()>()')
    INTERFACE_BINDING_ANNOT1 = (
        '''
            struct Y : public X {};
        ''',
        '''
            .bind<XAnnot1, YAnnot1>()
            .registerConstructor<YAnnot1()>()
        ''')
    INTERFACE_BINDING_ANNOT2 = (
        '''
            struct Z : public X {};
        ''',
        '''
            .bind<XAnnot2, ZAnnot2>()
            .registerConstructor<ZAnnot2()>()
        ''')
    INSTALL_ANNOT1 = (
        '''
            fruit::Component<XAnnot1> getParentComponent1() {
              return fruit::createComponent()
                .registerConstructor<XAnnot1()>();
            }
        ''',
        '.install(getParentComponent1)')
    INSTALL_ANNOT2 = (
        '''
            fruit::Component<XAnnot2> getParentComponent2() {
              return fruit::createComponent()
                .registerConstructor<XAnnot2()>();
            }
        ''',
        '.install(getParentComponent2)')
    CONST_BINDING_FROM_INSTALL_ANNOT1 = (
        '''
            fruit::Component<ConstXAnnot1> getParentComponent1() {
              return fruit::createComponent()
                .registerConstructor<XAnnot1()>();
            }
        ''',
        '.install(getParentComponent1)')
    CONST_BINDING_FROM_INSTALL_ANNOT2 = (
        '''
            fruit::Component<ConstXAnnot2> getParentComponent2() {
              return fruit::createComponent()
                .registerConstructor<XAnnot2()>();
            }
        ''',
        '.install(getParentComponent2)')
    CONST_BINDING_ANNOT1 = (
        '''
            const X x1{};
        ''',
        '.bindInstance<XAnnot1, X>(x1)')
    CONST_BINDING_ANNOT2 = (
        '''
            const X x2{};
        ''',
        '.bindInstance<XAnnot2, X>(x2)')

    @parameterized.named_parameters([
        ('CONSTRUCTOR_BINDING_ANNOT1 + CONSTRUCTOR_BINDING_ANNOT2',) + CONSTRUCTOR_BINDING_ANNOT1 + CONSTRUCTOR_BINDING_ANNOT2,
        ('CONSTRUCTOR_BINDING_ANNOT1 + INTERFACE_BINDING_ANNOT2',) + CONSTRUCTOR_BINDING_ANNOT1 + INTERFACE_BINDING_ANNOT2,
        ('INTERFACE_BINDING_ANNOT1 + CONSTRUCTOR_BINDING_ANNOT2',) + INTERFACE_BINDING_ANNOT1 + CONSTRUCTOR_BINDING_ANNOT2,
        ('INTERFACE_BINDING_ANNOT1 + INTERFACE_BINDING_ANNOT2',) + INTERFACE_BINDING_ANNOT1 + INTERFACE_BINDING_ANNOT2,
        ('INSTALL_ANNOT1 + CONSTRUCTOR_BINDING_ANNOT2',) + INSTALL_ANNOT1 + CONSTRUCTOR_BINDING_ANNOT2,
        ('INSTALL_ANNOT1 + INTERFACE_BINDING_ANNOT2',) + INSTALL_ANNOT1 + INTERFACE_BINDING_ANNOT2,
        ('CONST_BINDING_FROM_INSTALL_ANNOT1 + CONSTRUCTOR_BINDING_ANNOT2',) + CONST_BINDING_FROM_INSTALL_ANNOT1 + CONSTRUCTOR_BINDING_ANNOT2,
        ('CONST_BINDING_ANNOT1 + CONSTRUCTOR_BINDING_ANNOT2',) + CONST_BINDING_ANNOT1 + CONSTRUCTOR_BINDING_ANNOT2,
        ('CONST_BINDING_FROM_INSTALL_ANNOT1 + INTERFACE_BINDING_ANNOT2',) + CONST_BINDING_FROM_INSTALL_ANNOT1 + INTERFACE_BINDING_ANNOT2,
        ('CONST_BINDING_ANNOT1 + INTERFACE_BINDING_ANNOT2',) + CONST_BINDING_ANNOT1 + INTERFACE_BINDING_ANNOT2,
        ('CONSTRUCTOR_BINDING_ANNOT1 + INSTALL_ANNOT2',) + CONSTRUCTOR_BINDING_ANNOT1 + INSTALL_ANNOT2,
        ('INTERFACE_BINDING_ANNOT1 + INSTALL_ANNOT2',) + INTERFACE_BINDING_ANNOT1 + INSTALL_ANNOT2,
        ('CONSTRUCTOR_BINDING_ANNOT1 + CONST_BINDING_FROM_INSTALL_ANNOT2',) + CONSTRUCTOR_BINDING_ANNOT1 + CONST_BINDING_FROM_INSTALL_ANNOT2,
        ('CONSTRUCTOR_BINDING_ANNOT1 + CONST_BINDING_ANNOT2',) + CONSTRUCTOR_BINDING_ANNOT1 + CONST_BINDING_ANNOT2,
        ('INTERFACE_BINDING_ANNOT1 + CONST_BINDING_FROM_INSTALL_ANNOT2',) + INTERFACE_BINDING_ANNOT1 + CONST_BINDING_FROM_INSTALL_ANNOT2,
        ('INTERFACE_BINDING_ANNOT1 + CONST_BINDING_ANNOT2',) + INTERFACE_BINDING_ANNOT1 + CONST_BINDING_ANNOT2,
        ('INSTALL_ANNOT1 + INSTALL_ANNOT2',) + INSTALL_ANNOT1 + INSTALL_ANNOT2,
        ('CONST_BINDING_FROM_INSTALL_ANNOT1 + INSTALL_ANNOT2',) + CONST_BINDING_FROM_INSTALL_ANNOT1 + INSTALL_ANNOT2,
        ('CONST_BINDING_ANNOT1 + INSTALL_ANNOT2',) + CONST_BINDING_ANNOT1 + INSTALL_ANNOT2,
        ('INSTALL_ANNOT1 + CONST_BINDING_FROM_INSTALL_ANNOT2',) + INSTALL_ANNOT1 + CONST_BINDING_FROM_INSTALL_ANNOT2,
        ('INSTALL_ANNOT1 + CONST_BINDING_ANNOT2',) + INSTALL_ANNOT1 + CONST_BINDING_ANNOT2,
        ('CONST_BINDING_FROM_INSTALL_ANNOT1 + CONST_BINDING_FROM_INSTALL_ANNOT2',) + CONST_BINDING_FROM_INSTALL_ANNOT1 + CONST_BINDING_FROM_INSTALL_ANNOT2,
        ('CONST_BINDING_ANNOT1 + CONST_BINDING_ANNOT2',) + CONST_BINDING_ANNOT1 + CONST_BINDING_ANNOT2,
        ('CONST_BINDING_ANNOT1 + CONST_BINDING_FROM_INSTALL_ANNOT2',) + CONST_BINDING_ANNOT1 + CONST_BINDING_FROM_INSTALL_ANNOT2,
    ])
    def test_no_clash_with_different_annotations(self, binding1_preparation, binding1, binding2_preparation, binding2):
        source = '''
            struct X {};
    
            %s
            %s
    
            fruit::Component<const XAnnot1, const XAnnot2> getComponent() {
              return fruit::createComponent()
                  %s
                  %s;
            }
    
            int main() {
                fruit::Injector<const XAnnot1, const XAnnot2> injector(getComponent);
                injector.get<XAnnot1>();
                injector.get<XAnnot2>();
            }
            ''' % (binding1_preparation, binding2_preparation, binding1, binding2)
        expect_success(
            COMMON_DEFINITIONS,
            source)

    @parameterized.parameters([
        ('X', 'X', 'X'),
        ('const X', 'X', 'X'),
        ('X', 'const X', 'X'),
        ('const X', 'const X', 'X'),
        ('fruit::Annotated<Annotation1, X>', 'fruit::Annotated<Annotation1, X>', 'fruit::Annotated<Annotation1, X>'),
        ('fruit::Annotated<Annotation1, const X>', 'fruit::Annotated<Annotation1, X>', 'fruit::Annotated<Annotation1, X>'),
        ('fruit::Annotated<Annotation1, X>', 'fruit::Annotated<Annotation1, const X>', 'fruit::Annotated<Annotation1, X>'),
        ('fruit::Annotated<Annotation1, const X>', 'fruit::Annotated<Annotation1, const X>', 'fruit::Annotated<Annotation1, X>'),
    ])
    def test_during_component_merge(self, NormalizedComponentXAnnot, ComponentXAnnot, XAnnot):
        source = '''
            struct X {};
    
            fruit::Component<NormalizedComponentXAnnot> getComponent1() {
              return fruit::createComponent()
                .registerConstructor<XAnnot()>();
            }
    
            fruit::Component<ComponentXAnnot> getComponent2() {
              return fruit::createComponent()
                .registerConstructor<XAnnot()>();
            }
    
            void f() {
              fruit::NormalizedComponent<NormalizedComponentXAnnot> nc(getComponent1);
              fruit::Injector<> injector(nc, getComponent2);
              (void) injector;
            }
            '''
        expect_compile_error(
            'DuplicateTypesInComponentError<XAnnot>',
            'The installed component provides some types that are already provided',
            COMMON_DEFINITIONS,
            source,
            locals())

    def test_during_component_merge_with_different_annotation_ok(self):
        source = '''
            struct X {};
    
            fruit::Component<XAnnot1> getComponent1() {
              return fruit::createComponent()
                .registerConstructor<XAnnot1()>();
            }
    
            fruit::Component<XAnnot2> getComponent2() {
              return fruit::createComponent()
                .registerConstructor<XAnnot2()>();
            }
    
            int main() {
              fruit::NormalizedComponent<XAnnot1> nc(getComponent1);
              fruit::Injector<XAnnot1, XAnnot2> injector(nc, getComponent2);
              injector.get<XAnnot1>();
              injector.get<XAnnot2>();
            }
            '''
        expect_success(
            COMMON_DEFINITIONS,
            source)

    @parameterized.parameters([
        ('X', '(struct )?X'),
        ('fruit::Annotated<Annotation1, X>', '(struct )?fruit::Annotated<(struct )?Annotation1, ?(struct )?X>'),
    ])
    def test_bind_instance_and_bind_instance_runtime(self, XAnnot, XAnnotRegex):
        source = '''
            struct X {};
    
            fruit::Component<> getComponentForInstanceHelper() {
              // Note: don't do this in real code, leaks memory.
              return fruit::createComponent()
                .bindInstance<XAnnot, X>(*(new X()));
            }
            
            fruit::Component<XAnnot> getComponentForInstance() {
              // Note: don't do this in real code, leaks memory.
              return fruit::createComponent()
                .install(getComponentForInstanceHelper)
                .bindInstance<XAnnot, X>(*(new X()));
            }
    
            int main() {
              fruit::Injector<XAnnot> injector(getComponentForInstance);
              injector.get<XAnnot>();
            }
            '''
        expect_runtime_error(
            'Fatal injection error: the type XAnnotRegex was provided more than once, with different bindings.',
            COMMON_DEFINITIONS,
            source,
            locals())

    @parameterized.parameters([
        ('X', '(struct )?X'),
        ('fruit::Annotated<Annotation1, X>', '(struct )?fruit::Annotated<(struct )?Annotation1, ?(struct )?X>'),
    ])
    def test_bind_instance_and_binding_runtime(self, XAnnot, XAnnotRegex):
        source = '''
            struct X {};
    
            fruit::Component<> getComponentForInstanceHelper(X* x) {
              return fruit::createComponent()
                .bindInstance<XAnnot, X>(*x);
            }
            
            fruit::Component<XAnnot> getComponentForInstance(X* x) {
              return fruit::createComponent()
                .install(getComponentForInstanceHelper, x)
                .registerConstructor<XAnnot()>();
            }
    
            int main() {
              X x;
              fruit::Injector<XAnnot> injector(getComponentForInstance, &x);
              injector.get<XAnnot>();
            }
            '''
        expect_runtime_error(
            'Fatal injection error: the type XAnnotRegex was provided more than once, with different bindings.',
            COMMON_DEFINITIONS,
            source,
            locals())

    @parameterized.parameters([
        'X',
        'fruit::Annotated<Annotation1, X>',
    ])
    def test_during_component_merge_consistent_ok(self, XAnnot):
        source = '''
            struct X : public ConstructionTracker<X> {
              using Inject = X();
            };
    
            fruit::Component<XAnnot> getComponent() {
              return fruit::createComponent();
            }
    
            fruit::Component<> getRootComponent() {
              return fruit::createComponent()
                  .install(getComponent);
            }
    
            int main() {
              fruit::NormalizedComponent<> normalizedComponent(getRootComponent);
              fruit::Injector<XAnnot> injector(normalizedComponent, getComponent);
    
              Assert(X::num_objects_constructed == 0);
              injector.get<XAnnot>();
              Assert(X::num_objects_constructed == 1);
            }
            '''
        expect_success(
            COMMON_DEFINITIONS,
            source,
            locals())

if __name__ == '__main__':
    absltest.main()