From 878bd29b67572cc9d85f80df06b047ee830e9d80 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Tue, 26 Nov 2024 10:48:01 +0100 Subject: [PATCH] Ensure `Fake classes must be named using Fake and the interface it fakes` is detecting error by adding failing cases. --- .../konsist/failures/FakeWrongClassName.kt | 22 +++++++++++++++++++ .../tests/konsist/KonsistClassNameTest.kt | 14 +++++++++++- 2 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 tests/konsist/src/main/kotlin/io/element/android/tests/konsist/failures/FakeWrongClassName.kt diff --git a/tests/konsist/src/main/kotlin/io/element/android/tests/konsist/failures/FakeWrongClassName.kt b/tests/konsist/src/main/kotlin/io/element/android/tests/konsist/failures/FakeWrongClassName.kt new file mode 100644 index 0000000000..68ea557b53 --- /dev/null +++ b/tests/konsist/src/main/kotlin/io/element/android/tests/konsist/failures/FakeWrongClassName.kt @@ -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 diff --git a/tests/konsist/src/test/kotlin/io/element/android/tests/konsist/KonsistClassNameTest.kt b/tests/konsist/src/test/kotlin/io/element/android/tests/konsist/KonsistClassNameTest.kt index adc2b9ceb7..cc76c3fedd 100644 --- a/tests/konsist/src/test/kotlin/io/element/android/tests/konsist/KonsistClassNameTest.kt +++ b/tests/konsist/src/test/kotlin/io/element/android/tests/konsist/KonsistClassNameTest.kt @@ -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