Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom printing of Strings to easly distinguish single digit String from Int #121

Open
igorwojda opened this issue Nov 9, 2018 · 3 comments

Comments

@igorwojda
Copy link

igorwojda commented Nov 9, 2018

here is a real-life scenario that made me very confused

listOf(1, 2, "fizz", 4, "buzz") shouldEqual listOf(1, 2, "fizz", 4, "buzz") //ok
listOf(1, 2, "fizz", 4, "buzz") shouldEqual listOf("1", "2", "fizz", 4, "buzz") //fail

The reason of the confusion was this error message :
java.lang.AssertionError: Expected <[1, 2, fizz, 4, buzz]>, actual <[1, 2, fizz, 4, buzz]>.

I think we should consider custom toString conversion to make distinction Int vs String more explicit for all "string prints". The code is not the best but we will get the idea:

val list = listOf(1, "2", "fizz", 4, "buzz")
        println(list) //prints: [1, 2, fizz, 4, buzz]

        println(list.joinToString(
            transform = {
                if (it is String)
                    "\"$it\""
                else
                    it.toString()
            },
            prefix = "[",
            postfix = "]"
        )) //prints: [1, "2", "fizz", 4, "buzz"]

As a result we would get much better error message giving instant hint what is wrong
Before:
java.lang.AssertionError: Expected <[1, 2, fizz, 4, buzz]>, actual <[1, 2, fizz, 4, buzz]>

After
java.lang.AssertionError: Expected <[1, 2, fizz, 4, buzz]>, actual <["1", "2", "fizz", "4", "buzz"]>

As an alternative we could consider handling in special wahy the case where test fails but actuall.toString() == expected.toString(). Then we could make decision about display more information like data types in error message.

Also, keep in mind this is a simplified example, in reality, we will have a function that returns the list making mistake harder to spot in code
listOf(1, 2, "fizz", 4, "buzz") shouldEqual getMyList() //fail

@MarkusAmshove
Copy link
Owner

MarkusAmshove commented Nov 11, 2018

This is a good idea!

At the moment we're using assertEquals from kotlin.test, which does the comparison and failure message internally, so we would have to write our own implementation

@AarthiT
Copy link

AarthiT commented Oct 6, 2021

hello, Is this issue up for grabs? can I take this up?

@MarkusAmshove
Copy link
Owner

hello, Is this issue up for grabs? can I take this up?

Feel free to take it :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants