Skip to content

Commit 13e8772

Browse files
authored
Merge pull request #1552 from vector-im/feature/bma/konsist2
Konsist: add more test
2 parents a2601c0 + 634095c commit 13e8772

File tree

22 files changed

+301
-172
lines changed

22 files changed

+301
-172
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright (c) 2023 New Vector Ltd
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 io.element.android.app
18+
19+
import com.lemonappdev.konsist.api.Konsist
20+
import com.lemonappdev.konsist.api.ext.list.constructors
21+
import com.lemonappdev.konsist.api.ext.list.modifierprovider.withSealedModifier
22+
import com.lemonappdev.konsist.api.ext.list.parameters
23+
import com.lemonappdev.konsist.api.ext.list.withNameEndingWith
24+
import com.lemonappdev.konsist.api.ext.list.withoutName
25+
import com.lemonappdev.konsist.api.verify.assertEmpty
26+
import com.lemonappdev.konsist.api.verify.assertTrue
27+
import org.junit.Test
28+
29+
class KonsistArchitectureTest {
30+
@Test
31+
fun `Data class state MUST not have default value`() {
32+
Konsist
33+
.scopeFromProject()
34+
.classes()
35+
.withNameEndingWith("State")
36+
.withoutName(
37+
"CameraPositionState",
38+
)
39+
.constructors
40+
.parameters
41+
.assertTrue { parameterDeclaration ->
42+
parameterDeclaration.defaultValue == null &&
43+
// Using parameterDeclaration.defaultValue == null is not enough apparently,
44+
// Also check that the text does not contain an equal sign
45+
parameterDeclaration.text.contains("=").not()
46+
}
47+
}
48+
49+
@Test
50+
fun `Events MUST be sealed interface`() {
51+
Konsist.scopeFromProject()
52+
.classes()
53+
.withSealedModifier()
54+
.withNameEndingWith("Events")
55+
.assertEmpty(additionalMessage = "Events class MUST be sealed interface")
56+
}
57+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright (c) 2023 New Vector Ltd
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 io.element.android.app
18+
19+
import androidx.compose.ui.tooling.preview.PreviewParameterProvider
20+
import com.bumble.appyx.core.node.Node
21+
import com.lemonappdev.konsist.api.Konsist
22+
import com.lemonappdev.konsist.api.ext.list.withAllParentsOf
23+
import com.lemonappdev.konsist.api.verify.assertTrue
24+
import io.element.android.libraries.architecture.Presenter
25+
import org.junit.Test
26+
27+
class KonsistClassNameTest {
28+
@Test
29+
fun `Classes extending 'Presenter' should have 'Presenter' suffix`() {
30+
Konsist.scopeFromProject()
31+
.classes()
32+
.withAllParentsOf(Presenter::class)
33+
.assertTrue {
34+
it.name.endsWith("Presenter")
35+
}
36+
}
37+
38+
@Test
39+
fun `Classes extending 'Node' should have 'Node' suffix`() {
40+
Konsist.scopeFromProject()
41+
.classes()
42+
.withAllParentsOf(Node::class)
43+
.assertTrue {
44+
it.name.endsWith("Node")
45+
}
46+
}
47+
48+
@Test
49+
fun `Classes extending 'PreviewParameterProvider' name MUST end with "Provider" and MUST contain provided class name`() {
50+
Konsist.scopeFromProject()
51+
.classes()
52+
.withAllParentsOf(PreviewParameterProvider::class)
53+
.assertTrue {
54+
// Cannot find a better way to get the type of the generic
55+
val providedType = it.text
56+
.substringBefore(">")
57+
.substringAfter("<")
58+
.removeSuffix("?")
59+
.replace(".", "")
60+
it.name.endsWith("Provider") && it.name.contains(providedType)
61+
}
62+
}
63+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright (c) 2023 New Vector Ltd
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 io.element.android.app
18+
19+
import androidx.compose.runtime.Composable
20+
import com.lemonappdev.konsist.api.KoModifier
21+
import com.lemonappdev.konsist.api.Konsist
22+
import com.lemonappdev.konsist.api.ext.list.modifierprovider.withoutModifier
23+
import com.lemonappdev.konsist.api.ext.list.withAllAnnotationsOf
24+
import com.lemonappdev.konsist.api.ext.list.withTopLevel
25+
import com.lemonappdev.konsist.api.ext.list.withoutName
26+
import com.lemonappdev.konsist.api.ext.list.withoutNameEndingWith
27+
import com.lemonappdev.konsist.api.verify.assertTrue
28+
import org.junit.Test
29+
30+
class KonsistComposableTest {
31+
@Test
32+
fun `Top level function with '@Composable' annotation starting with a upper case should be placed in a file with the same name`() {
33+
Konsist
34+
.scopeFromProject()
35+
.functions()
36+
.withTopLevel()
37+
.withoutModifier(KoModifier.PRIVATE)
38+
.withoutNameEndingWith("Preview")
39+
.withAllAnnotationsOf(Composable::class)
40+
.withoutName(
41+
// Add some exceptions...
42+
"OutlinedButton",
43+
"TextButton",
44+
"SimpleAlertDialogContent",
45+
)
46+
.assertTrue(
47+
additionalMessage =
48+
"""
49+
Please check the filename. It should match the top level Composable function. If the filename is correct:
50+
- consider making the Composable private or moving it to its own file
51+
- at last resort, you can add an exception in the Konsist test
52+
""".trimIndent()
53+
) {
54+
if (it.name.first().isLowerCase()) {
55+
true
56+
} else {
57+
val fileName = it.containingFile.name.removeSuffix(".kt")
58+
fileName == it.name
59+
}
60+
}
61+
}
62+
}
Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,23 @@
1414
* limitations under the License.
1515
*/
1616

17-
package io.element.android.libraries.designsystem.utils
17+
package io.element.android.app
1818

19-
import androidx.compose.ui.tooling.preview.PreviewParameterProvider
19+
import com.lemonappdev.konsist.api.Konsist
20+
import com.lemonappdev.konsist.api.ext.list.properties
21+
import com.lemonappdev.konsist.api.verify.assertFalse
22+
import org.junit.Test
2023

21-
open class PairCombinedProvider<T1, T2>(
22-
private val provider: Pair<PreviewParameterProvider<T1>, PreviewParameterProvider<T2>>
23-
) : PreviewParameterProvider<Pair<T1, T2>> {
24-
override val values: Sequence<Pair<T1, T2>>
25-
get() = provider.first.values.flatMap { first ->
26-
provider.second.values.map { second ->
27-
first to second
24+
class KonsistFieldTest {
25+
@Test
26+
fun `no field should have 'm' prefix`() {
27+
Konsist
28+
.scopeFromProject()
29+
.classes()
30+
.properties()
31+
.assertFalse {
32+
val secondCharacterIsUppercase = it.name.getOrNull(1)?.isUpperCase() ?: false
33+
it.name.startsWith('m') && secondCharacterIsUppercase
2834
}
29-
}
35+
}
3036
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright (c) 2023 New Vector Ltd
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 io.element.android.app
18+
19+
import com.lemonappdev.konsist.api.Konsist
20+
import com.lemonappdev.konsist.api.ext.list.withAllAnnotationsOf
21+
import com.lemonappdev.konsist.api.verify.assertTrue
22+
import io.element.android.libraries.designsystem.preview.PreviewsDayNight
23+
import org.junit.Test
24+
25+
class KonsistPreviewTest {
26+
@Test
27+
fun `Functions with '@PreviewsDayNight' annotation should have 'Preview' suffix`() {
28+
Konsist
29+
.scopeFromProject()
30+
.functions()
31+
.withAllAnnotationsOf(PreviewsDayNight::class)
32+
.assertTrue {
33+
it.hasNameEndingWith("Preview") &&
34+
it.hasNameEndingWith("LightPreview").not() &&
35+
it.hasNameEndingWith("DarkPreview").not()
36+
}
37+
}
38+
}

app/src/test/kotlin/io/element/android/app/KonsistTest.kt

Lines changed: 0 additions & 139 deletions
This file was deleted.

0 commit comments

Comments
 (0)