forked from square/dagger
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Check if user added methods have name that is a java reserved keyword.
Fixes #2576 RELNOTES=Check if user added methods have name that is a java reserved keyword. PiperOrigin-RevId: 384781488
- Loading branch information
1 parent
e152ea0
commit 436ac2e
Showing
7 changed files
with
209 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
javatests/dagger/internal/codegen/ComponentValidationKtTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/* | ||
* Copyright (C) 2021 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 com.google.common.truth.Truth.assertThat; | ||
import static dagger.testing.compile.CompilerTests.kotlinCompiler; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import com.tschuchort.compiletesting.KotlinCompilation; | ||
import com.tschuchort.compiletesting.KotlinCompilation.ExitCode; | ||
import com.tschuchort.compiletesting.SourceFile; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.JUnit4; | ||
|
||
@RunWith(JUnit4.class) | ||
public final class ComponentValidationKtTest { | ||
@Test | ||
public void creatorMethodNameIsJavaKeyword_compilationError() { | ||
SourceFile componentSrc = | ||
SourceFile.Companion.kotlin( | ||
"FooComponent.kt", | ||
String.join( | ||
"\n", | ||
"package test", | ||
"", | ||
"import dagger.BindsInstance", | ||
"import dagger.Component", | ||
"", | ||
"@Component", | ||
"interface FooComponent {", | ||
" @Component.Builder", | ||
" interface Builder {", | ||
" @BindsInstance public fun int(str: Int): Builder", | ||
" public fun build(): FooComponent", | ||
" }", | ||
"}"), | ||
false); | ||
KotlinCompilation compilation = kotlinCompiler(); | ||
compilation.setSources(ImmutableList.of(componentSrc)); | ||
|
||
KotlinCompilation.Result result = compilation.compile(); | ||
|
||
// TODO(b/192396673): Add error count when the feature request is fulfilled. | ||
assertThat(result.getExitCode()).isEqualTo(ExitCode.COMPILATION_ERROR); | ||
assertThat(result.getMessages()) | ||
.contains("Can not use a Java keyword as method name: int(I)Ltest/FooComponent$Builder"); | ||
} | ||
|
||
@Test | ||
public void componentMethodNameIsJavaKeyword_compilationError() { | ||
SourceFile componentSrc = | ||
SourceFile.Companion.kotlin( | ||
"FooComponent.kt", | ||
String.join( | ||
"\n", | ||
"package test", | ||
"", | ||
"import dagger.BindsInstance", | ||
"import dagger.Component", | ||
"", | ||
"@Component(modules = [TestModule::class])", | ||
"interface FooComponent {", | ||
" fun int(str: Int): String", | ||
"}"), | ||
false); | ||
SourceFile moduleSrc = | ||
SourceFile.Companion.kotlin( | ||
"TestModule.kt", | ||
String.join( | ||
"\n", | ||
"package test", | ||
"", | ||
"import dagger.Module", | ||
"", | ||
"@Module", | ||
"interface TestModule {", | ||
" fun providesString(): String {", | ||
" return \"test\"", | ||
" }", | ||
"}"), | ||
false); | ||
KotlinCompilation compilation = kotlinCompiler(); | ||
compilation.setSources(ImmutableList.of(componentSrc, moduleSrc)); | ||
|
||
KotlinCompilation.Result result = compilation.compile(); | ||
|
||
assertThat(result.getExitCode()).isEqualTo(ExitCode.COMPILATION_ERROR); | ||
assertThat(result.getMessages()) | ||
.contains("Can not use a Java keyword as method name: int(I)Ljava/lang/String"); | ||
} | ||
} |