aboutsummaryrefslogtreecommitdiff
path: root/javatests/dagger/internal/codegen/FullBindingGraphValidationTest.java
blob: 5c114188b092404b459a42d607a4c76ae9f5b99d (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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
/*
 * Copyright (C) 2018 The Dagger Authors.
 *
 * 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.
 */

package dagger.internal.codegen;

import static dagger.internal.codegen.TestUtils.endsWithMessage;

import androidx.room.compiler.processing.util.Source;
import com.google.common.collect.ImmutableMap;
import dagger.testing.compile.CompilerTests;
import java.util.regex.Pattern;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
public final class FullBindingGraphValidationTest {
  private static final Source MODULE_WITH_ERRORS =
      CompilerTests.javaSource(
          "test.ModuleWithErrors",
          "package test;",
          "",
          "import dagger.Binds;",
          "import dagger.Module;",
          "",
          "@Module",
          "interface ModuleWithErrors {",
          "  @Binds Object object1(String string);",
          "  @Binds Object object2(Long l);",
          "  @Binds Number missingDependency(Integer i);",
          "}");

  // Make sure the error doesn't show other bindings or a dependency trace afterwards.
  private static final Pattern MODULE_WITH_ERRORS_MESSAGE =
      endsWithMessage(
          "\033[1;31m[Dagger/DuplicateBindings]\033[0m Object is bound multiple times:",
          "    @Binds Object ModuleWithErrors.object1(String)",
          "    @Binds Object ModuleWithErrors.object2(Long)",
          "    in component: [ModuleWithErrors]",
          "",
          "======================",
          "Full classname legend:",
          "======================",
          "ModuleWithErrors: test.ModuleWithErrors",
          "========================",
          "End of classname legend:",
          "========================");

  private static final Pattern INCLUDES_MODULE_WITH_ERRORS_MESSAGE =
      endsWithMessage(
          "\033[1;31m[Dagger/DuplicateBindings]\033[0m Object is bound multiple times:",
          "    @Binds Object ModuleWithErrors.object1(String)",
          "    @Binds Object ModuleWithErrors.object2(Long)",
          "    in component: [IncludesModuleWithErrors]",
          "",
          "======================",
          "Full classname legend:",
          "======================",
          "IncludesModuleWithErrors: test.IncludesModuleWithErrors",
          "ModuleWithErrors:         test.ModuleWithErrors",
          "========================",
          "End of classname legend:",
          "========================");

  @Test
  public void moduleWithErrors_validationTypeNone() {
    CompilerTests.daggerCompiler(MODULE_WITH_ERRORS)
        .compile(
            subject -> {
              subject.hasErrorCount(0);
              subject.hasWarningCount(0);
            });
  }

  @Test
  public void moduleWithErrors_validationTypeError() {
    CompilerTests.daggerCompiler(MODULE_WITH_ERRORS)
        .withProcessingOptions(ImmutableMap.of("dagger.fullBindingGraphValidation", "ERROR"))
        .compile(
            subject -> {
              subject.hasErrorCount(1);
              subject.hasErrorContainingMatch(MODULE_WITH_ERRORS_MESSAGE.pattern())
                  .onSource(MODULE_WITH_ERRORS)
                  .onLineContaining("interface ModuleWithErrors");
            });
  }

  @Test
  public void moduleWithErrors_validationTypeWarning() {
    CompilerTests.daggerCompiler(MODULE_WITH_ERRORS)
        .withProcessingOptions(ImmutableMap.of("dagger.fullBindingGraphValidation", "WARNING"))
        .compile(
            subject -> {
              subject.hasErrorCount(0);
              subject.hasWarningCount(1);
              subject.hasWarningContainingMatch(MODULE_WITH_ERRORS_MESSAGE.pattern())
                  .onSource(MODULE_WITH_ERRORS)
                  .onLineContaining("interface ModuleWithErrors");
            });
  }

  private static final Source INCLUDES_MODULE_WITH_ERRORS =
      CompilerTests.javaSource(
          "test.IncludesModuleWithErrors",
          "package test;",
          "",
          "import dagger.Binds;",
          "import dagger.Module;",
          "",
          "@Module(includes = ModuleWithErrors.class)",
          "interface IncludesModuleWithErrors {}");

  @Test
  public void includesModuleWithErrors_validationTypeNone() {
    CompilerTests.daggerCompiler(MODULE_WITH_ERRORS, INCLUDES_MODULE_WITH_ERRORS)
        .compile(
            subject -> {
              subject.hasErrorCount(0);
              subject.hasWarningCount(0);
            });
  }

  @Test
  public void includesModuleWithErrors_validationTypeError() {
    CompilerTests.daggerCompiler(MODULE_WITH_ERRORS, INCLUDES_MODULE_WITH_ERRORS)
        .withProcessingOptions(ImmutableMap.of("dagger.fullBindingGraphValidation", "ERROR"))
        .compile(
            subject -> {
              subject.hasErrorCount(2);
              subject.hasErrorContainingMatch(MODULE_WITH_ERRORS_MESSAGE.pattern())
                  .onSource(MODULE_WITH_ERRORS)
                  .onLineContaining("interface ModuleWithErrors");
              subject.hasErrorContaining("ModuleWithErrors has errors")
                  .onSource(INCLUDES_MODULE_WITH_ERRORS)
                  .onLineContaining("ModuleWithErrors.class");
            });
  }

  @Test
  public void includesModuleWithErrors_validationTypeWarning() {
    CompilerTests.daggerCompiler(MODULE_WITH_ERRORS, INCLUDES_MODULE_WITH_ERRORS)
        .withProcessingOptions(ImmutableMap.of("dagger.fullBindingGraphValidation", "WARNING"))
        .compile(
            subject -> {
              subject.hasErrorCount(0);
              subject.hasWarningCount(2);

              subject.hasWarningContainingMatch(MODULE_WITH_ERRORS_MESSAGE.pattern())
                  .onSource(MODULE_WITH_ERRORS)
                  .onLineContaining("interface ModuleWithErrors");

              subject.hasWarningContainingMatch(INCLUDES_MODULE_WITH_ERRORS_MESSAGE.pattern())
                  .onSource(INCLUDES_MODULE_WITH_ERRORS)
                  .onLineContaining("interface IncludesModuleWithErrors");
            });
  }

  private static final Source A_MODULE =
      CompilerTests.javaSource(
          "test.AModule",
          "package test;",
          "",
          "import dagger.Binds;",
          "import dagger.Module;",
          "",
          "@Module",
          "interface AModule {",
          "  @Binds Object object(String string);",
          "}");

  private static final Source COMBINED_WITH_A_MODULE_HAS_ERRORS =
      CompilerTests.javaSource(
          "test.CombinedWithAModuleHasErrors",
          "package test;",
          "",
          "import dagger.Binds;",
          "import dagger.Module;",
          "",
          "@Module(includes = AModule.class)",
          "interface CombinedWithAModuleHasErrors {",
          "  @Binds Object object(Long l);",
          "}");

  // Make sure the error doesn't show other bindings or a dependency trace afterwards.
  private static final Pattern COMBINED_WITH_A_MODULE_HAS_ERRORS_MESSAGE =
      endsWithMessage(
          "\033[1;31m[Dagger/DuplicateBindings]\033[0m Object is bound multiple times:",
          "    @Binds Object AModule.object(String)",
          "    @Binds Object CombinedWithAModuleHasErrors.object(Long)",
          "    in component: [CombinedWithAModuleHasErrors]",
          "",
          "======================",
          "Full classname legend:",
          "======================",
          "AModule:                      test.AModule",
          "CombinedWithAModuleHasErrors: test.CombinedWithAModuleHasErrors",
          "========================",
          "End of classname legend:",
          "========================");

  @Test
  public void moduleIncludingModuleWithCombinedErrors_validationTypeNone() {
    CompilerTests.daggerCompiler(A_MODULE, COMBINED_WITH_A_MODULE_HAS_ERRORS)
        .compile(
            subject -> {
              subject.hasErrorCount(0);
              subject.hasWarningCount(0);
            });
  }

  @Test
  public void moduleIncludingModuleWithCombinedErrors_validationTypeError() {
    CompilerTests.daggerCompiler(A_MODULE, COMBINED_WITH_A_MODULE_HAS_ERRORS)
        .withProcessingOptions(ImmutableMap.of("dagger.fullBindingGraphValidation", "ERROR"))
        .compile(
            subject -> {
              subject.hasErrorCount(1);
              subject.hasErrorContainingMatch(COMBINED_WITH_A_MODULE_HAS_ERRORS_MESSAGE.pattern())
                  .onSource(COMBINED_WITH_A_MODULE_HAS_ERRORS)
                  .onLineContaining("interface CombinedWithAModuleHasErrors");
            });
  }

  @Test
  public void moduleIncludingModuleWithCombinedErrors_validationTypeWarning() {
    CompilerTests.daggerCompiler(A_MODULE, COMBINED_WITH_A_MODULE_HAS_ERRORS)
        .withProcessingOptions(ImmutableMap.of("dagger.fullBindingGraphValidation", "WARNING"))
        .compile(
            subject -> {
              subject.hasErrorCount(0);
              subject.hasWarningCount(1);
              subject.hasWarningContainingMatch(COMBINED_WITH_A_MODULE_HAS_ERRORS_MESSAGE.pattern())
                  .onSource(COMBINED_WITH_A_MODULE_HAS_ERRORS)
                  .onLineContaining("interface CombinedWithAModuleHasErrors");
            });
  }

  private static final Source SUBCOMPONENT_WITH_ERRORS =
      CompilerTests.javaSource(
          "test.SubcomponentWithErrors",
          "package test;",
          "",
          "import dagger.BindsInstance;",
          "import dagger.Subcomponent;",
          "",
          "@Subcomponent(modules = AModule.class)",
          "interface SubcomponentWithErrors {",
          "  @Subcomponent.Builder",
          "  interface Builder {",
          "    @BindsInstance Builder object(Object object);",
          "    SubcomponentWithErrors build();",
          "  }",
          "}");

  // Make sure the error doesn't show other bindings or a dependency trace afterwards.
  private static final Pattern SUBCOMPONENT_WITH_ERRORS_MESSAGE =
      endsWithMessage(
          "\033[1;31m[Dagger/DuplicateBindings]\033[0m Object is bound multiple times:",
          "    @Binds Object AModule.object(String)",
          "    @BindsInstance SubcomponentWithErrors.Builder"
              + " SubcomponentWithErrors.Builder.object(Object)",
          "    in component: [SubcomponentWithErrors]",
          "",
          "======================",
          "Full classname legend:",
          "======================",
          "AModule:                test.AModule",
          "SubcomponentWithErrors: test.SubcomponentWithErrors",
          "========================",
          "End of classname legend:",
          "========================");

  private static final Pattern MODULE_WITH_SUBCOMPONENT_WITH_ERRORS_MESSAGE =
      endsWithMessage(
          "\033[1;31m[Dagger/DuplicateBindings]\033[0m Object is bound multiple times:",
          "    @Binds Object AModule.object(String)",
          "    @BindsInstance SubcomponentWithErrors.Builder"
              + " SubcomponentWithErrors.Builder.object(Object)",
          "    in component: [ModuleWithSubcomponentWithErrors → SubcomponentWithErrors]",
          "",
          "======================",
          "Full classname legend:",
          "======================",
          "AModule:                          test.AModule",
          "ModuleWithSubcomponentWithErrors: test.ModuleWithSubcomponentWithErrors",
          "SubcomponentWithErrors:           test.SubcomponentWithErrors",
          "========================",
          "End of classname legend:",
          "========================");

  @Test
  public void subcomponentWithErrors_validationTypeNone() {
    CompilerTests.daggerCompiler(SUBCOMPONENT_WITH_ERRORS, A_MODULE)
        .compile(
            subject -> {
              subject.hasErrorCount(0);
              subject.hasWarningCount(0);
            });
  }

  @Test
  public void subcomponentWithErrors_validationTypeError() {
    CompilerTests.daggerCompiler(SUBCOMPONENT_WITH_ERRORS, A_MODULE)
        .withProcessingOptions(ImmutableMap.of("dagger.fullBindingGraphValidation", "ERROR"))
        .compile(
            subject -> {
              subject.hasErrorCount(1);
              subject.hasErrorContainingMatch(SUBCOMPONENT_WITH_ERRORS_MESSAGE.pattern())
                  .onSource(SUBCOMPONENT_WITH_ERRORS)
                  .onLineContaining("interface SubcomponentWithErrors");
            });
  }

  @Test
  public void subcomponentWithErrors_validationTypeWarning() {
    CompilerTests.daggerCompiler(SUBCOMPONENT_WITH_ERRORS, A_MODULE)
        .withProcessingOptions(ImmutableMap.of("dagger.fullBindingGraphValidation", "WARNING"))
        .compile(
            subject -> {
              subject.hasErrorCount(0);
              subject.hasWarningCount(1);
              subject.hasWarningContainingMatch(SUBCOMPONENT_WITH_ERRORS_MESSAGE.pattern())
                  .onSource(SUBCOMPONENT_WITH_ERRORS)
                  .onLineContaining("interface SubcomponentWithErrors");
            });
  }

  private static final Source MODULE_WITH_SUBCOMPONENT_WITH_ERRORS =
      CompilerTests.javaSource(
          "test.ModuleWithSubcomponentWithErrors",
          "package test;",
          "",
          "import dagger.Binds;",
          "import dagger.Module;",
          "",
          "@Module(subcomponents = SubcomponentWithErrors.class)",
          "interface ModuleWithSubcomponentWithErrors {}");

  @Test
  public void moduleWithSubcomponentWithErrors_validationTypeNone() {
    CompilerTests.daggerCompiler(
            MODULE_WITH_SUBCOMPONENT_WITH_ERRORS, SUBCOMPONENT_WITH_ERRORS, A_MODULE)
        .compile(
            subject -> {
              subject.hasErrorCount(0);
              subject.hasWarningCount(0);
            });
  }

  @Test
  public void moduleWithSubcomponentWithErrors_validationTypeError() {
    CompilerTests.daggerCompiler(
            MODULE_WITH_SUBCOMPONENT_WITH_ERRORS, SUBCOMPONENT_WITH_ERRORS, A_MODULE)
        .withProcessingOptions(ImmutableMap.of("dagger.fullBindingGraphValidation", "ERROR"))
        .compile(
            subject -> {
              subject.hasErrorCount(2);
              subject.hasErrorContainingMatch(
                      MODULE_WITH_SUBCOMPONENT_WITH_ERRORS_MESSAGE.pattern())
                  .onSource(MODULE_WITH_SUBCOMPONENT_WITH_ERRORS)
                  .onLineContaining("interface ModuleWithSubcomponentWithErrors");
              // TODO(b/130283677)
              subject.hasErrorContainingMatch(SUBCOMPONENT_WITH_ERRORS_MESSAGE.pattern())
                  .onSource(SUBCOMPONENT_WITH_ERRORS)
                  .onLineContaining("interface SubcomponentWithErrors");
            });
  }

  @Test
  public void moduleWithSubcomponentWithErrors_validationTypeWarning() {
    CompilerTests.daggerCompiler(
            MODULE_WITH_SUBCOMPONENT_WITH_ERRORS, SUBCOMPONENT_WITH_ERRORS, A_MODULE)
        .withProcessingOptions(ImmutableMap.of("dagger.fullBindingGraphValidation", "WARNING"))
        .compile(
            subject -> {
              subject.hasErrorCount(0);
              subject.hasWarningCount(2);

              subject.hasWarningContainingMatch(
                      MODULE_WITH_SUBCOMPONENT_WITH_ERRORS_MESSAGE.pattern())
                  .onSource(MODULE_WITH_SUBCOMPONENT_WITH_ERRORS)
                  .onLineContaining("interface ModuleWithSubcomponentWithErrors");

              // TODO(b/130283677): Don't repeat error.
              subject.hasWarningContainingMatch(SUBCOMPONENT_WITH_ERRORS_MESSAGE.pattern())
                  .onSource(SUBCOMPONENT_WITH_ERRORS)
                  .onLineContaining("interface SubcomponentWithErrors");
            });
  }

  private static final Source A_SUBCOMPONENT =
      CompilerTests.javaSource(
          "test.ASubcomponent",
          "package test;",
          "",
          "import dagger.BindsInstance;",
          "import dagger.Subcomponent;",
          "",
          "@Subcomponent(modules = AModule.class)",
          "interface ASubcomponent {",
          "  @Subcomponent.Builder",
          "  interface Builder {",
          "    ASubcomponent build();",
          "  }",
          "}");

  private static final Source COMBINED_WITH_A_SUBCOMPONENT_HAS_ERRORS =
      CompilerTests.javaSource(
          "test.CombinedWithASubcomponentHasErrors",
          "package test;",
          "",
          "import dagger.Binds;",
          "import dagger.Module;",
          "",
          "@Module(subcomponents = ASubcomponent.class)",
          "interface CombinedWithASubcomponentHasErrors {",
          "  @Binds Object object(Number number);",
          "}");

  // Make sure the error doesn't show other bindings or a dependency trace afterwards.
  private static final Pattern COMBINED_WITH_A_SUBCOMPONENT_HAS_ERRORS_MESSAGE =
      endsWithMessage(
          "\033[1;31m[Dagger/DuplicateBindings]\033[0m Object is bound multiple times:",
          "    @Binds Object AModule.object(String)",
          "    @Binds Object CombinedWithASubcomponentHasErrors.object(Number)",
          "    in component: [CombinedWithASubcomponentHasErrors → ASubcomponent]",
          "",
          "======================",
          "Full classname legend:",
          "======================",
          "AModule:                            test.AModule",
          "ASubcomponent:                      test.ASubcomponent",
          "CombinedWithASubcomponentHasErrors: test.CombinedWithASubcomponentHasErrors",
          "========================",
          "End of classname legend:",
          "========================");

  @Test
  public void moduleWithSubcomponentWithCombinedErrors_validationTypeNone() {
    CompilerTests.daggerCompiler(COMBINED_WITH_A_SUBCOMPONENT_HAS_ERRORS, A_SUBCOMPONENT, A_MODULE)
        .compile(
            subject -> {
              subject.hasErrorCount(0);
              subject.hasWarningCount(0);
            });
  }

  @Test
  public void moduleWithSubcomponentWithCombinedErrors_validationTypeError() {
    CompilerTests.daggerCompiler(COMBINED_WITH_A_SUBCOMPONENT_HAS_ERRORS, A_SUBCOMPONENT, A_MODULE)
        .withProcessingOptions(ImmutableMap.of("dagger.fullBindingGraphValidation", "ERROR"))
        .compile(
            subject -> {
              subject.hasErrorCount(1);
              subject.hasErrorContainingMatch(
                      COMBINED_WITH_A_SUBCOMPONENT_HAS_ERRORS_MESSAGE.pattern())
                  .onSource(COMBINED_WITH_A_SUBCOMPONENT_HAS_ERRORS)
                  .onLineContaining("interface CombinedWithASubcomponentHasErrors");
            });
  }

  @Test
  public void moduleWithSubcomponentWithCombinedErrors_validationTypeWarning() {
    CompilerTests.daggerCompiler(COMBINED_WITH_A_SUBCOMPONENT_HAS_ERRORS, A_SUBCOMPONENT, A_MODULE)
        .withProcessingOptions(ImmutableMap.of("dagger.fullBindingGraphValidation", "WARNING"))
        .compile(
            subject -> {
              subject.hasErrorCount(0);
              subject.hasWarningCount(1);
              subject.hasWarningContainingMatch(
                      COMBINED_WITH_A_SUBCOMPONENT_HAS_ERRORS_MESSAGE.pattern())
                  .onSource(COMBINED_WITH_A_SUBCOMPONENT_HAS_ERRORS)
                  .onLineContaining("interface CombinedWithASubcomponentHasErrors");
            });
  }

  @Test
  public void bothAliasesDifferentValues() {
    CompilerTests.daggerCompiler(MODULE_WITH_ERRORS)
        .withProcessingOptions(
            ImmutableMap.of(
                "dagger.moduleBindingValidation", "NONE",
                "dagger.fullBindingGraphValidation", "ERROR"))
        .compile(
            subject -> {
              subject.hasErrorCount(1);
              subject.hasErrorContaining(
                  "Only one of the equivalent options "
                      + "(-Adagger.fullBindingGraphValidation, -Adagger.moduleBindingValidation)"
                      + " should be used; prefer -Adagger.fullBindingGraphValidation");
            });
  }

  @Test
  public void bothAliasesSameValue() {
    CompilerTests.daggerCompiler(MODULE_WITH_ERRORS)
        .withProcessingOptions(
            ImmutableMap.of(
                "dagger.moduleBindingValidation", "NONE",
                "dagger.fullBindingGraphValidation", "NONE"))
        .compile(
            subject -> {
              subject.hasErrorCount(0);
              subject.hasWarningCount(1);
              subject.hasWarningContaining(
                  "Only one of the equivalent options "
                      + "(-Adagger.fullBindingGraphValidation, -Adagger.moduleBindingValidation)"
                      + " should be used; prefer -Adagger.fullBindingGraphValidation");
            });
  }
}