Skip to content

Commit 1181458

Browse files
KON-118 Add KoImportDeclaration.isWildcard Property (#132)
* add KoImportDeclarationImpl.isWildcard property and tests for this * add extensions: KoImportDeclarationSequenceExt.withWildcard() and KoImportDeclarationSequenceExt.withoutWildcard()
1 parent 9b20966 commit 1181458

File tree

7 files changed

+83
-1
lines changed

7 files changed

+83
-1
lines changed

lib/src/integrationTest/kotlin/com/lemonappdev/konsist/core/declaration/koimportdeclaration/KoImportDeclarationTest.kt

+24-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ class KoImportDeclarationTest {
2424
@Test
2525
fun `import-name-has-import-alias`() {
2626
// given
27-
val sut = getSnippetFile("import-name-has-import-alias").imports()
27+
val sut = getSnippetFile("import-name-has-import-alias")
28+
.imports()
2829

2930
// then
3031
assertSoftly(sut.toList()) {
@@ -34,6 +35,28 @@ class KoImportDeclarationTest {
3435
}
3536
}
3637

38+
@Test
39+
fun `import-with-wildcard`() {
40+
// given
41+
val sut = getSnippetFile("import-with-wildcard")
42+
.imports()
43+
.first()
44+
45+
// then
46+
sut.isWildcard shouldBeEqualTo true
47+
}
48+
49+
@Test
50+
fun `import-without-wildcard`() {
51+
// given
52+
val sut = getSnippetFile("import-without-wildcard")
53+
.imports()
54+
.first()
55+
56+
// then
57+
sut.isWildcard shouldBeEqualTo false
58+
}
59+
3760
private fun getSnippetFile(fileName: String) =
3861
TestSnippetProvider.getSnippetKoScope("core/declaration/koimportdeclaration/snippet/", fileName)
3962
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import com.lemonappdev.konsist.testdata.*
2+
3+
class SampleTopLevelClass(val sampleProperty2: SampleClass)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import com.lemonappdev.konsist.testdata.SampleClass
2+
3+
class SampleTopLevelClass(val sampleProperty2: SampleClass)

lib/src/main/kotlin/com/lemonappdev/konsist/api/declaration/KoImportDeclaration.kt

+5
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,9 @@ interface KoImportDeclaration : KoNamedDeclaration {
88
* Alias of the import.
99
*/
1010
val alias: String
11+
12+
/**
13+
* Whether this import is a wildcard.
14+
*/
15+
val isWildcard: Boolean
1116
}

lib/src/main/kotlin/com/lemonappdev/konsist/api/ext/sequence/KoImportDeclarationSequenceExt.kt

+10
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,13 @@ fun Sequence<KoImportDeclaration>.withoutAlias(vararg names: String): Sequence<K
2121
else -> names.none { name -> it.alias == name }
2222
}
2323
}
24+
25+
/**
26+
* Sequence containing all declarations that have a wildcard.
27+
*/
28+
fun Sequence<KoImportDeclaration>.withWildcard(): Sequence<KoImportDeclaration> = filter { it.isWildcard }
29+
30+
/**
31+
* Sequence containing all declarations that don't have a wildcard.
32+
*/
33+
fun Sequence<KoImportDeclaration>.withoutWildcard(): Sequence<KoImportDeclaration> = filterNot { it.isWildcard }

lib/src/main/kotlin/com/lemonappdev/konsist/core/declaration/KoImportDeclarationImpl.kt

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ internal class KoImportDeclarationImpl private constructor(private val ktImportD
1212

1313
override val alias by lazy { ktImportDirective.alias?.name ?: name }
1414

15+
override val isWildcard by lazy { ktImportDirective.text.endsWith('*') }
16+
1517
internal companion object {
1618
private val cache = KoDeclarationCache<KoImportDeclarationImpl>()
1719

lib/src/test/kotlin/com/lemonappdev/konsist/api/ext/sequence/KoImportDeclarationSequenceExtTest.kt

+36
Original file line numberDiff line numberDiff line change
@@ -114,4 +114,40 @@ class KoImportDeclarationSequenceExtTest {
114114
// then
115115
sut.toList() shouldBeEqualTo listOf(import3, import4)
116116
}
117+
118+
@Test
119+
fun `withWildcard() returns import with wildcard`() {
120+
// given
121+
val import1: KoImportDeclarationImpl = mockk {
122+
every { isWildcard } returns true
123+
}
124+
val import2: KoImportDeclarationImpl = mockk {
125+
every { isWildcard } returns false
126+
}
127+
val imports = sequenceOf(import1, import2)
128+
129+
// when
130+
val sut = imports.withWildcard()
131+
132+
// then
133+
sut.toList() shouldBeEqualTo listOf(import1)
134+
}
135+
136+
@Test
137+
fun `withoutWildcard() returns import without wildcard`() {
138+
// given
139+
val import1: KoImportDeclarationImpl = mockk {
140+
every { isWildcard } returns true
141+
}
142+
val import2: KoImportDeclarationImpl = mockk {
143+
every { isWildcard } returns false
144+
}
145+
val imports = sequenceOf(import1, import2)
146+
147+
// when
148+
val sut = imports.withoutWildcard()
149+
150+
// then
151+
sut.toList() shouldBeEqualTo listOf(import2)
152+
}
117153
}

0 commit comments

Comments
 (0)