• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * * Copyright 2022 Google LLC. 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  */
16 
17 package kapt
18 
19 import com.google.testing.compile.Compilation
20 import com.google.testing.compile.CompilationSubject.assertThat
21 import com.google.testing.compile.Compiler.javac
22 import com.google.testing.compile.JavaFileObjects
23 import javax.tools.JavaFileObject
24 import org.junit.Test
25 import org.junit.runner.RunWith
26 import org.junit.runners.JUnit4
27 
28 /** Sample test for [AP] that uses `com.google.testing.compile` (b/199411692). */
29 @RunWith(JUnit4::class)
30 class APTest {
31   @Test
testKTestnull32   fun testKTest() {
33     // This is the stub file kapt generates for KTest.kt as of kotlinc 1.5.31, with the original
34     // Kotlin module name shortened.
35     // Alternatively our test could run kapt to get a fresh stub file, but on the other hand we
36     // can test with a particular stub file this way, which may be useful for some regression tests.
37     val testStub = JavaFileObjects.forSourceString(
38       /* fullyQualifiedName= */ "kapt.KTest",
39       """
40         |package kapt;
41 
42         |import java.lang.System;
43 
44         |@kotlin.Metadata(mv = {1, 5, 1}, k = 1, d1 = {"\u0000\f\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0002\b\u0002\b\u0007\u0018\u00002\u00020\u0001B\u0005\u00a2\u0006\u0002\u0010\u0002\u00a8\u0006\u0003"}, d2 = {"Lkapt/KTest;", "", "()V", "shortened.java.kapt_test_kapt"})
45         |@Count(value = 3, clazz = KTest_2_1_0.class)
46         |public final class KTest {
47         |
48         |    public KTest() {
49         |        super();
50         |    }
51         |}
52         """.trimMargin()
53     )
54     val compilation: Compilation = javac().withProcessors(AP()).compile(testStub)
55     assertThat(compilation).succeededWithoutWarnings()
56     assertThat(compilation).generatedSourceFile("kapt.KTest_2")
57     assertThat(compilation).generatedSourceFile("kapt.KTest_2_1")
58     assertThat(compilation).generatedSourceFile("kapt.KTest_2_1_0")
59   }
60 }
61