How to cover lambda function in coverage kotlin - android

I am using jococo for test coverage. I want to cover every package, but I added this code
#Retention(AnnotationRetention.BINARY)
annotation class NoCoverageGenerated
to not excluded that class which I don't want to cover. But there is problem inside that class if we have any lambda function it not covering that lambda function. How can we cover lamda function ?
#NoCoverageGenerated
class ABC {
private val OnSuccessListener: (any: Any?) -> Unit = { _ ->
xyz()
}
}

Related

How to make kotlin operator a Composable with parameters having default value

I'm trying to make Kotlin's invoke operator a #Composable, everything works fine, until I add a parameter to it, which should have a default value. See the code below:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent{
Button()
}
}
}
object Button{
#Composable
operator fun invoke(text: String = "SomeText"){
println(text) // prints: null
}
}
When the operator is not annotated as #Composable the output is SomeText, as it should be.
Is this some error in Jetpack Compose, or am I missing something?
The behavior is the same on the latest stable Compose v 1.1.1 and on 1.2.0-beta01. Kotlin 1.6.21
Based on the info provided in the comments, I decided to answer:
I'll maybe think of something better, but off the top of my head, this is what you can do for now
enum class ButtonType {
Primary,
Secondary,
Tertiary
}
Return the correct type of Button
#Composable
fun MasterButton(type: ButtonType) {
when(type) {
primary -> PrimaryButton()
secondary -> SecondaryButton()
else -> TertiaryButton() // Must provide an 'else' branch
}
}
This will do the job for you.
CORRECT APPROACH I:
I just got the correct one the moment I started typing the first approach.
#Composable
fun ( #Composable () -> Unit ).Primary(...) {
PrimaryButton()
}
Make copies for every other button.
STRONG NOTICE: This is a RIDICULOUS way of "cleaning" up the code. Nobody should ever use anything remotely resembling this ever, but since that is just what the question is about, this is how you go about doing it. Know that this will attach an extension function called Primary(...) to every single #Composable function, and that cannot change. You can't apply it to select Composable(s) only, since this is basically just an extension function that I have applied on a general labmda, since 'extension functions for extension functions' are not something that exist as of now.
I am going to take this as your question (even though it is in the comments) and try to answer the way I achieve this.
What I'm trying to achieve is a way to clean up the namespace, so that
not all Composables are available as a top-level function. The general
idea is to group all flavors of let's say Buttons (Primary, Secondary,
Tertiary) to be Composables declared as a function of object Button.
But I would like to be able to use also this Button object as a
default Button (let it be Primary) in a Compose way, so just by using
it as it would be a function, thus invoke() operator. I would have
Button.Primary(), Button.Secondary() and Button() which would be an
"alias" for Button.Primary().
My implementation is quite simple,
Expose only one top-level Composable function to have a cleaner namespace.
Pass an argument that denotes the type of the required Composable, using a sealed class.
Button Type
sealed class MyIconButtonType(
open val typeName: String,
) {
data class Default(
override val typeName: String = "default",
) : MyIconButtonType(
typeName = typeName,
)
data class BorderIconButton(
override val typeName: String = "border",
// The variant specific attributes can be added here
val borderWidth: Int,
) : MyIconButtonType(
typeName = typeName,
)
}
Button (The only composable exposed to other files)
#Composable
fun MyTestIconButton(
onClickLabel: String,
modifier: Modifier = Modifier,
data: MyIconButtonType = MyIconButtonType.Default(),
onClick: () -> Unit,
content: #Composable () -> Unit,
) {
when (data) {
is MyIconButtonType.Default -> {
// This composable should be private
MyTestIconDefaultButton(
// parameter as required
)
}
is MyIconButtonType.BorderIconButton -> {
// This composable should be private
MyTestIconDefaultButton(
// parameter as required, also make sure to pass variant specific attributes here
)
}
}
}
Usage
// For default impl
MyTestIconButton(
// default parameters
) {
}
// For specific variants
MyTestIconButton(
// default parameters
data = MyIconButtonType.BorderIconButton(
borderWidth = 10,
),
) {
}
Note:
Data class requires at least one attribute. Use object if no attributes like the typeName are required.
Like this,
sealed class MyIconButtonType {
object Default : MyIconButtonType()
data class BorderIconButton(
val borderWidth: Int,
) : MyIconButtonType()
}
Kotlin concepts that are used for reference,
Sealed classes, data classes and objects
when statement
Visibility modifiers

Collect transformed StateFlow in Composable

There is function collectAsState() applicable to a StateFlow property in order to observe it in a Composable.
A composable requires a StateFlow because StateFlow guarantees an initial value. A Flow doesn't come with that guarantee.
Now, what is the way to go if I have a StateFlow property but I want to apply an operator (like map) before collecting the Flow in the Composable?
Here an example:
Let's say a repository exposes a StateFlow<MyClass>
val myClassStateFlow: StateFlow<MyClass>
data class MyClass(val a: String)
... and a view model has a dependency on the repository and wants to expose only the property a to its Composable...
val aFlow = myClassState.Flow.map { it.a } // <- this is of type Flow<String>
The map operator changes the type from StateFlow<MyClass> to Flow<String>.
Is it semantically justified that aFlow has no initial value anymore? After all its first emission is derived from the initial value of myClassStateFlow.
It's required to convert Flow back into StateFlow at some point. Which is the more idiomatic place for this?
In the view model using stateIn()? How would the code look like?
In the composable using collectAsState(initial: MyClass) and come up with an initial value (although myClassStateFlow had an initial value)?
See this issue on GitHub
Currently there is no built-in way to transform StateFlows, only Flows. But you can write your own.
Way I ended up solving was to use the example in that post.
First create a notion of a DerivedStateFlow.
class DerivedStateFlow<T>(
private val getValue: () -> T,
private val flow: Flow<T>
) : StateFlow<T> {
override val replayCache: List<T>
get () = listOf(value)
override val value: T
get () = getValue()
#InternalCoroutinesApi
override suspend fun collect(collector: FlowCollector<T>) {
flow.collect(collector)
}
}
Then have an extension on StateFlow like the current map extension on Flow
fun <T1, R> StateFlow<T1>.mapState(transform: (a: T1) -> R): StateFlow<R> {
return DerivedStateFlow(
getValue = { transform(this.value) },
flow = this.map { a -> transform(a) }
)
}
Now in your Repository or ViewModel, you can use it as below.
class MyViewModel( ... ) {
private val originalStateFlow:StateFlow<SomeT> = ...
val someStateFlowtoExposeToCompose =
originalStateFlow
.mapState { item ->
yourTransform(item)
}
}
Now you can consume it as you expect in Compose without any special work, since it returns a StateFlow.

Android - How to generate type safe builder function using Kotlin Poet library

I am trying to generate one kotlin class using kotlin poet library which should have one function and one inner static class as given below.
class SampleClass{
class TestClass{
lateinit var id: String
lateinit var name: String
}
fun function1(init: TestClass.() -> Unit) {
val trackPhoneNumberClicked = TestClass().apply(init)
val event = Event.Builder.from(testData.getTestDataById("testdataid")!!)
.apply {
addProperty("id", trackPhoneNumberClicked.id)
addProperty("name", trackPhoneNumberClicked.name)
}
.build()
}
}
I can generate SampleClass and inner class TestClass but I am not able to create function1 with this argument and body.
github like of kotlin poet library. https://github.com/square/kotlinpoet
Can anyone provide any solution for this?
I got the answer that how we can generate function1(init: TestClass.() -> Unit) this type of argument in function. We need to use LambdaTypeName class of kotlin poet lib.
val buildParameter = ParameterSpec.builder(FUNCTION_PARAMETER_NAME,
LambdaTypeName.get(ClassName("", CLASS_TYPE_NAME),
returnType = Unit::class.asTypeName())).build()

How to verify that a specific lambda method has been passed as argument with mockito

I have a Kotlin class (The problem was simplified to have a basic example), in it there is a method testedMethod() I want to test interactions on.
I want to make sure the correct parameter is passed to anotherMethod(), my problem is that the parameter is not a value but a lambda function.
A simple solution in Kotlin is what I am looking for, it may or may not be based on this solution for java.
class TestedClass {
fun testedMethod() {
anotherMethod({ passedMethod() })
}
fun passedMethod() {}
fun anotherMethod(lambdaParameter: () -> Unit) {
// Does something with lambdaParameter
}
}
This is the solution I ended up using.
Basically you create a spy of the tested class to be able to verify its own method, and you use argumentCaptor to capture the passed lambda and be able to invoke what is in it.
import com.nhaarman.mockito_kotlin.argumentCaptor
import com.nhaarman.mockito_kotlin.spy
import com.nhaarman.mockito_kotlin.verify
#Test
fun lambdaParameterPassedIsCorrect() {
val testedClass = TestedClass()
val spyOfTestedClass = spy(testedClass)
val captor = argumentCaptor<() -> Unit>()
spyOfTestedClass.testedMethod()
verify(spyOfTestedClass).anotherMethod(captor.capture())
// Invoke the function passed as a parameter
// and then verify that the expected method was called
val function = captor.firstValue
function.invoke()
verify(spyOfTestedClass).passedMethod()
}
I would still be interested in a simpler solution.
You can use mockito_kotlin library, which have the function "verify".
verify(TestedClass, times(1)).anotherMethod(eq(passedMethod))
Which verify that the method "anotherMethod" of class "TestedClass" invoked once with the parameter equal to "passedMethod"

Mocktito ArgumentCaptor for Kotlin lambda with arguments

I am trying to test this on Kotlin:
verify(myInterface).doSomething(argumentCaptor.capture())
capture.value.invoke(0L)
Where doSomething is:
doSomething((Long) -> Unit)
How can I create an ArgumentCaptor for this? Right now I am doing this
inline fun <reified T : Any> argumentCaptor() = ArgumentCaptor.forClass(T::class.java)!!
val captor = argumentCaptor<(Long) -> Unit>()
verify(mainApiInterface!!).downloadUserProfilePicture(captor.capture())
captor.value.invoke(0L)
But I am getting java.lang.IllegalStateException: captor.capture() must not be null
I also tried integrating mockito-kotlin but I get a PowerMockito error:
No instance field named "reported" could be found in the class hierarchy of org.mockito.internal.MockitoCore.
Using mockito-kotlin like this seems to work:
val myService = mock<MyInterface>()
myService.doSomething {
println(it)
}
verify(myService).doSomething(capture { function ->
function.invoke(123)
})
Edit: removed unnecessary argumentCaptor<(Long) -> Unit>().apply {} - it wasn't used
as with kotlin 1.3.72 and com.nhaarman.mockitokotlin2:mockito-kotlin:2.2.0 the following works fine for me:
create an argument captor via val captor = argumentCaptor<() -> Unit>() and call captor.capture() on it.
There's also a variant for nullable captors with nullableArgumentCaptor()
The following unit-test captures the lambda of type () -> Unit that is given to diff.open(). To capture it at runtime it then uses captor.capture()
// given
val onClose = argumentCaptor<() -> Unit>()
// when
diff.open(file, serialized) { onDiffClosed(clusterResource, documentBeforeDiff) }
// then
verify(diff).open(any(), any(), onClose.capture())
The nhaarman wrapper for mockito creates a wrapper KArgumentCaptor for the mockito class ArgumentCaptor. The nhaarman wrapper fixes your error by creating an instance instead of null as in mockito.

Categories

Resources