Ensure Fake classes must be named using Fake and the interface it fakes is detecting error by adding failing cases.

This commit is contained in:
Benoit Marty 2024-11-26 10:48:01 +01:00
parent 1b490488a2
commit 878bd29b67
2 changed files with 35 additions and 1 deletions

View file

@ -0,0 +1,22 @@
/*
* Copyright 2024 New Vector Ltd.
*
* SPDX-License-Identifier: AGPL-3.0-only
* Please see LICENSE in the repository root for full details.
*/
package io.element.android.tests.konsist.failures
// Make test `Fake classes must be named using Fake and the interface it fakes` fails
interface MyInterface
// This class should be named FakeMyInterface
class FakeWrongClassName : MyInterface
class MyClass {
interface MyFactory
}
// This class should be named FakeMyClassMyFactory
class FakeWrongClassSubInterfaceName : MyClass.MyFactory

View file

@ -73,6 +73,11 @@ class KonsistClassNameTest {
@Test
fun `Fake classes must be named using Fake and the interface it fakes`() {
var failingCases = 0
val failingCasesList = listOf(
"FakeWrongClassName",
"FakeWrongClassSubInterfaceName",
)
Konsist.scopeFromProject()
.classes()
.withNameContaining("Fake")
@ -84,7 +89,7 @@ class KonsistClassNameTest {
val interfaceName = it.name
.replace("FakeRust", "")
.replace("Fake", "")
(it.name.startsWith("Fake") || it.name.startsWith("FakeRust")) &&
val result = (it.name.startsWith("Fake") || it.name.startsWith("FakeRust")) &&
it.parents().any { parent ->
// Workaround to get the parent name. For instance:
// parent.name used to return `UserListPresenter.Factory` but is now returning `Factory`.
@ -93,7 +98,14 @@ class KonsistClassNameTest {
val parentName = parent.fullyQualifiedName!!.substringAfter("$packageName.").replace(".", "")
parentName == interfaceName
}
if (!result && it.name in failingCasesList) {
failingCases++
true
} else {
result
}
}
assertThat(failingCases).isEqualTo(failingCasesList.size)
}
@Test