Tuesday, 26 November 2024

Problems with Kotlin

So, I was working and it didn't work.

And I really could not understand why.

Here's the code:

    @Test
    fun test() {
        val addressInNetherlands =
            Address(housenumber = 11L, street = "Kerkstraat", city = "Amsterdam", state = null, country = "Netherlands")
        val addressInEngland = Address(12, "Morsestreet", "London", null, "United Kingdom")
        val anotherAddressInEngland = Address(28, "Trinity Ln", "Cambridge", null, "United Kingdom")
        val addressInAmerica = Address(23, "32nd Street", "Columbus", "Ohio", "United States of America")

        val mrBear = Person("Mr.", "Bear", 50, addressInNetherlands)
        val mrBell = Person("Bell", "Graham", 55, addressInAmerica);
        val mrBoole = Person("George", "Boole", 82, addressInEngland);
        val lordKelvin = Person("William","Kelvin",84, anotherAddressInEngland);

        val addressBook = listOf(mrBear, mrBell, mrBoole, lordKelvin)

        var findMrBooleInEngland = addressBook.filter {
            it.firstName == "George" &&
                    it.lastName == "Boole"
                    it.address.country == "United Kingdom"
        }

        assertThat(findMrBooleInEngland).hasSize(1)
    }

Why does the assert fail? (it's programmer error, but almost invisible)

Solution in the next blog post.

2 comments:

  1. " Boole" (with space) seems to be the reason. Undoubtedly unrelated to Kotlin

    ReplyDelete
    Replies
    1. Oooh! You noticed that! I remove the "space" from the program, thank you!

      Unfortunately, that was not the reason the assert fails.

      The Assert fails because it now returns all people from the United Kingdom in the list.

      Delete