Skip to content

Commit

Permalink
Align test syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
Rawa committed Oct 2, 2023
1 parent 14040be commit b744cc3
Show file tree
Hide file tree
Showing 13 changed files with 26 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class AccountViewModel(
}
.stateIn(viewModelScope, SharingStarted.WhileSubscribed(), AccountUiState.default())

@Suppress("konsist.ensurePublicPropertiesUsePermittedNames")
@Suppress("konsist.ensure public properties use permitted names")
val enterTransitionEndAction = _enterTransitionEndAction.asSharedFlow()

fun onManageAccountClick() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ class DeviceListViewModel(
private val _loadingDevices = MutableStateFlow<List<DeviceId>>(emptyList())

private val _toastMessages = MutableSharedFlow<String>(extraBufferCapacity = 1)
@Suppress("konsist.ensurePublicPropertiesUsePermittedNames")
@Suppress("konsist.ensure public properties use permitted names")
val toastMessages = _toastMessages.asSharedFlow()

@Suppress("konsist.ensurePublicPropertiesUsePermittedNames") var accountToken: String? = null
@Suppress("konsist.ensure public properties use permitted names") var accountToken: String? = null
private var cachedDeviceList: List<Device>? = null

val uiState =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ class SelectLocationViewModel(private val serviceConnectionManager: ServiceConne
SelectLocationUiState.Loading
)

@Suppress("konsist.ensurePublicPropertiesUsePermittedNames")
@Suppress("konsist.ensure public properties use permitted names")
val uiCloseAction = _closeAction.asSharedFlow()
@Suppress("konsist.ensurePublicPropertiesUsePermittedNames")
@Suppress("konsist.ensure public properties use permitted names")
val enterTransitionEndAction = _enterTransitionEndAction.asSharedFlow()

fun selectRelay(relayItem: RelayItem?) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class SettingsViewModel(
SettingsUiState(appVersion = "", isLoggedIn = false, isUpdateAvailable = false)
)

@Suppress("konsist.ensurePublicPropertiesUsePermittedNames")
@Suppress("konsist.ensure public properties use permitted names")
val enterTransitionEndAction = _enterTransitionEndAction.asSharedFlow()

fun onTransitionAnimationEnd() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class VpnSettingsViewModel(
) : ViewModel() {

private val _toastMessages = MutableSharedFlow<String>(extraBufferCapacity = 1)
@Suppress("konsist.ensurePublicPropertiesUsePermittedNames")
@Suppress("konsist.ensure public properties use permitted names")
val toastMessages = _toastMessages.asSharedFlow()

private val dialogState =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ import org.junit.Test
class ArchitectureTests {

@Test
fun `domain layer depends on nothing`() {
fun `ensure model layer depends on nothing`() =
Konsist.scopeFromProduction().assertArchitecture {
val domain = Layer("Domain", "net.mullvad.mullvadvpn.model..")
val model = Layer("Model", "net.mullvad.mullvadvpn.model..")

domain.dependsOnNothing()
model.dependsOnNothing()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,17 @@ import org.junit.Test

class GeneralTests {
@Test
fun `package name must match file path`() {
fun `ensure package name must match file path`() =
Konsist.scopeFromProject().packages.assert { it.hasMatchingPath }
}

@Test
fun `no field should have 'm' prefix`() {
fun `ensure no field should have 'm' prefix`() =
Konsist.scopeFromProject().classes().properties().assertNot {
val secondCharacterIsUppercase = it.name.getOrNull(1)?.isUpperCase() ?: false
it.name.startsWith('m') && secondCharacterIsUppercase
}
}

@Test
fun `no empty files allowed`() {
fun `ensure no empty files allowed`() =
Konsist.scopeFromProject().files.assertNot { it.text.isEmpty() }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,23 @@ import org.junit.Test

class ViewModelTests {
@Test
fun ensureViewModelsHaveViewModelSuffix() {
fun ensureViewModelsHaveViewModelSuffix() =
allViewModels().assert { it.name.endsWith("ViewModel") }
}

// The purpose of this check is to both keep the naming consistent and also to avoid exposing
// properties that shouldn't be exposed.
@Test
fun ensurePublicPropertiesUsePermittedNames() {
fun `ensure public properties use permitted names`() =
allViewModels().properties(includeNested = false).withPublicOrDefaultModifier().assert {
property ->
property.name == "uiState" || property.name == "uiSideEffect"
}
}

@Test
fun ensurePublicFunctionsHaveNoReturnType() {
fun `ensure public functions have no return type`() =
allViewModels().functions().withPublicOrDefaultModifier().assertNot { function ->
function.hasReturnType()
}
}

private fun allViewModels() =
Konsist.scopeFromProject().classes().withAllParentsOf(ViewModel::class)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package net.mullvad.mullvadvpn.test.arch.classes

import com.lemonappdev.konsist.api.Konsist
import com.lemonappdev.konsist.api.verify.assert
import net.mullvad.mullvadvpn.test.arch.extensions.projectScope
import org.junit.Test

class ClassTests {
@Test
fun `companion object is last declaration in the class`() {
projectScope().classes(includeNested = true).assert {
fun `ensure companion object is last declaration in the class`() =
Konsist.scopeFromProject().classes(includeNested = true).assert {
val companionObject =
it.objects(includeNested = false).lastOrNull { obj -> obj.hasCompanionModifier }
if (companionObject != null) {
Expand All @@ -17,5 +17,4 @@ class ClassTests {
true
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
package net.mullvad.mullvadvpn.test.arch.classes

import com.lemonappdev.konsist.api.Konsist
import com.lemonappdev.konsist.api.ext.list.modifierprovider.withDataModifier
import com.lemonappdev.konsist.api.ext.list.properties
import com.lemonappdev.konsist.api.verify.assertNot
import net.mullvad.mullvadvpn.test.arch.extensions.projectScope
import org.junit.Test

class DataClasses {
@Test
fun `data classes use only immutable declarations`() {
projectScope()
fun `ensure data classes only use immutable properties`() =
Konsist.scopeFromProject()
.classes(includeNested = true)
.withDataModifier()
.properties(includeNested = false, includeLocal = false)
.assertNot { it.hasVarModifier }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,13 @@ import org.junit.Test

class ComposePreviewTests {
@Test
fun `all preview functions are private`() {
fun `ensure all preview functions are private`() =
allPreviewFunctions().assert { it.hasPrivateModifier }
}

@Test
fun `all preview functions are prefixed with Preview`() {
fun `ensure all preview functions are prefixed with 'Preview'`() =
allPreviewFunctions().assert { it.name.startsWith("Preview") }
}

private fun allPreviewFunctions() =
Konsist.scopeFromProduction().functions().withAllAnnotationsOf(Preview::class)
Konsist.scopeFromProduction("app").functions().withAllAnnotationsOf(Preview::class)
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ import org.junit.Test

class ComposeTests {
@Test
fun `all app composables are in the compose packages`() {
fun `ensure all app composables are in the compose package`() =
allAppComposeFunctions().assert { it.resideInPackage("net.mullvad.mullvadvpn.compose..") }
}

private fun allAppComposeFunctions() =
Konsist.scopeFromProduction("app").functions().withAllAnnotationsOf(Composable::class)
Expand Down

This file was deleted.

0 comments on commit b744cc3

Please sign in to comment.