Home > Mobile >  Can someone explain to me what are the difference between this two results?
Can someone explain to me what are the difference between this two results?

Time:09-22

I'm doing tests in android studio for a tutorial and the test results show me this:

expected:<2,00[ ]$> but was:<2,00[ ]$>
Expected :2,00 $
Actual   :2,00 $

I'm a bit confused ! both are the same string....

EDIT:

This is the method i'm testing

@VisibleForTesting
internal fun calculateTip(
    amount: Double, percent: Double = 15.0, roundUp: Boolean): String{

    var tip = percent / 100 * amount
    if (roundUp)
        tip = kotlin.math.ceil(tip)
    return NumberFormat.getCurrencyInstance().format(tip)
}

This is the test assertion:

@Test
    fun calculate_20_percent_tip_no_roundup() {
        val amount = 10.00
        val tipPercent = 20.00
        val expectedTip = "2,00 $"
        val actualTip = calculateTip(amount = amount, percent = tipPercent, false)
        assertEquals(expectedTip, actualTip)

    }

CodePudding user response:

Adding this to shed light on the difference...

In order to recreate I had to use the locale

java.text.NumberFormat.getCurrencyInstance( Locale("fr", "CA" )).format(2.00)

In the run log for the test it will show:

enter image description here

And if you click on Click to see difference it shows:

enter image description here

So the NumberFormat for that particular locale currency introduces the NBSP. Unfortunately cannot explain why it would do that.

MFP:

@Test
fun test_currencyComparison() {
    val expected = "2,00 $"
    val actual = java.text.NumberFormat.getCurrencyInstance( Locale("fr", "CA" )).format(2.00)
    println("$expected $actual")
    assertEquals(expected, actual)
}

One way to overcome this in your case would be to use the same NumberFormat to generate the expected:

@Test
fun calculate_20_percent_tip_no_roundup() {
    val amount = 10.00
    val tipPercent = 20.00
    val expectedTip = java.text.NumberFormat.getCurrencyInstance().format(2.00)
    val actualTip = calculateTip(amount = amount, percent = tipPercent, false)
    assertEquals(expectedTip, actualTip)

}
  • Related