Menu

Issues with comparing. 42 and 42.000000, 0.5 and 0.50 are considered different

Help
Hari Rao
2018-01-10
2021-06-30
  • Hari Rao

    Hari Rao - 2018-01-10

    Issues with comparing. 42 and 42.000000, 0.5 and 0.50 are considered different. Is there anything available to supress this behaviour?

     
  • Stefan Bodewig

    Stefan Bodewig - 2018-01-10

    Well, 42 and 42.0 are certainly different :-)

    Basically you need to provide a DifferenceEvaluator that knows when your text difference is really comparing numbers - probably based on the element name of the parent or the name the attribute that is the target of the current comparision. Have this DifferenceEvaluator try to parse the text as a number and then perform a numeric comparison.

    XMLUnit 1.x has something close in the examples folder - https://sourceforge.net/p/xmlunit/code/HEAD/tree/trunk/src/java/org/custommonkey/xmlunit/examples/FloatingPointTolerantDifferenceListener.java - which could be ported to the XMLUnit 2.x API as a DifferenceEvaluator easily.

     
  • Hari Rao

    Hari Rao - 2018-01-17

    thank you very much. After a week of back and forth discusion with the stake holders we are going to leave this as it is cause 42 and 42.0000 are different ... :)

     
  • Jaspreet Singh Ahuja

    I have to ignore differences such as
    Expected text value '1.3440000000000001E1' but was '13.44'.
    I have multiple such nodes with that kind of difference- so I can't predetermine which node will have that kind of values.
    I wish I had stakeholders like Hari Rao. :'-( , but that isn't the case and would appreciate some help.
    <parent>
    <child1>11.5 </child1>
    <child2> 1.3440000000000001E1 <child2>
    <child3> some string <child3>
    </child3></child3></child2></child2></parent>
    V/S
    <parent>
    <child1>11.5 </child1>
    <child2>13.44 <child2>
    <child3> some string <child3>
    </child3></child3></child2></child2></parent>

     

    Last edit: Jaspreet Singh Ahuja 2021-06-29
  • Stefan Bodewig

    Stefan Bodewig - 2021-06-30

    But do you know which elements are expected to hold numbers? If so, you can still use the DifferenceEvaluator approach I described. Otherwise - if you don't know which elements are to be considered numbers how should XMLUnit know? :-)

     

Log in to post a comment.

MongoDB Logo MongoDB