• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 The Dagger Authors.
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  */
16 
17 package dagger.internal.codegen;
18 
19 import static dagger.internal.codegen.base.ComponentCreatorKind.FACTORY;
20 import static dagger.internal.codegen.binding.ErrorMessages.creatorMessagesFor;
21 import static java.util.stream.Collectors.joining;
22 
23 import androidx.room.compiler.processing.util.Source;
24 import dagger.internal.codegen.base.ComponentCreatorAnnotation;
25 import dagger.internal.codegen.base.ComponentCreatorKind;
26 import dagger.internal.codegen.binding.ErrorMessages;
27 import dagger.testing.compile.CompilerTests;
28 import java.util.Arrays;
29 import java.util.stream.Stream;
30 
31 /**
32  * Base class for component creator codegen tests that are written in terms of builders and
33  * transformed, either by automatic string processing or using a {@code JavaFileBuilder}, to test
34  * factories as well.
35  */
36 abstract class ComponentCreatorTestHelper {
37 
38   private final CompilerMode compilerMode;
39 
40   protected final ComponentCreatorKind creatorKind;
41   protected final ErrorMessages.ComponentCreatorMessages messages;
42 
ComponentCreatorTestHelper( CompilerMode compilerMode, ComponentCreatorAnnotation componentCreatorAnnotation)43   ComponentCreatorTestHelper(
44       CompilerMode compilerMode, ComponentCreatorAnnotation componentCreatorAnnotation) {
45     this.compilerMode = compilerMode;
46     this.creatorKind = componentCreatorAnnotation.creatorKind();
47     this.messages = creatorMessagesFor(componentCreatorAnnotation);
48   }
49 
50   // For tests where code for both builders and factories can be largely equivalent, i.e. when there
51   // is nothing to set, just preprocess the lines to change code written for a builder to code for a
52   // factory.
53   // For more complicated code, use a JavaFileBuilder to add different code depending on the creator
54   // kind.
55 
56   /**
57    * Processes the given lines, replacing builder-related names with factory-related names if the
58    * creator kind is {@code FACTORY}.
59    */
process(String... lines)60   String process(String... lines) {
61     Stream<String> stream = Arrays.stream(lines);
62     if (creatorKind.equals(FACTORY)) {
63       stream =
64           stream.map(
65               line ->
66                   line.replace("Builder", "Factory")
67                       .replace("builder", "factory")
68                       .replace("build", "createComponent"));
69     }
70     return stream.collect(joining("\n"));
71   }
72 
73   /**
74    * Returns a Java source with the {@linkplain #process(String...)} processed} versions of the
75    * given lines.
76    */
preprocessedJavaSource(String fullyQualifiedName, String... lines)77   Source preprocessedJavaSource(String fullyQualifiedName, String... lines) {
78     return CompilerTests.javaSource(fullyQualifiedName, process(lines));
79   }
80 
81   /** Returns a file builder for the current creator kind. */
javaFileBuilder(String qualifiedName)82   JavaFileBuilder javaFileBuilder(String qualifiedName) {
83     return new JavaFileBuilder(qualifiedName).withSettings(compilerMode, creatorKind);
84   }
85 }
86