Shouldn't this work?


#1

Two functions in a junit test:

assertEquals(unit1.isAlarm(), unit2.isAalrm())

Both functions return a Boolean.

I had to do this instead:

val one = unit1.isAlarm()
val two = unit2.isAlarm()
assertEquals(one, two)


#2

Yes, the first one should work. Doesn’t it work for you?


#3

It should work without the typo.


#4

OK I wrote that post in haste over the weekend. Here is the code. Actually I was compairing the isAlarm() result from one nullable type and one not nullable type. I’m confused because they both produce Boolean values (not nullable Booleans) so why isn’t the result of isAlarm() being evaluated? Or is it and I’m missing something?

data class Alarm(val date : Long, val type : Int);

class Unit
{
    val alarmList = mutableListOf<Alarm>();

    fun isAlarm() = {alarmList.size > 0}
}

Here’s the unit test:

import org.junit.Test

import org.junit.Assert.*
import org.junit.Before

class UnitTest
{
    var one : Unit? = null;

    @Before
    fun setupOne()
    {
        one = Unit();
    }

    @Test
    fun getAlarmList()
    {
    }

    @Test
    fun isAlarm()
    {
        val two = Unit();
        assertEquals(one?.isAlarm(), two.isAlarm())
    }

What am I missing?


#5

isAlarm as written doesn’t return a boolean, it returns a function (of type () -> Boolean), because you wrapped it in {}

try with this:

fun isAlarm() = alarmList.isNotEmpty()

#6

Thank you! I keep getting tripped up by that in Kotlin.


#7

There is a feature in the editor that will get intellij/android studio to display the actual return type used for the function even if you don’t write it (unless obvious). That feature may help you.