groovy

the equals operator(==) error

Details

  • Type: Bug Bug
  • Status: Closed Closed
  • Priority: Major Major
  • Resolution: Fixed
  • Affects Version/s: 1.5.1
  • Fix Version/s: 1.5.5, 1.6-beta-1
  • Component/s: XML Processing
  • Labels:
    None
  • Environment:
    groovy 1.5.3
  • Number of attachments :
    0

Description

the example is :

def CAR_RECORDS = '''
<records>
<car name='HSV Maloo' make='Holden' year='2006'>
<country>Australia</country>
<record type='speed'>Production Pickup Truck with speed of 271kph</record>
</car>
<car name='P50' make='Peel' year='1962'>
<country>Isle of Man</country>
<record type='size'>Smallest Street-Legal Car at 99cm wide and 59 kg in weight</record>
</car>
<car name='Royale' make='Bugatti' year='1931'>
<country>France</country>
<record type='price'>Most Valuable Car at $15 million</record>
</car>
</records>
'''
def slurperRoot = new XmlSlurper().parseText(CAR_RECORDS)
assert slurperRoot.car[1].@name=='P50'//OK
assert 'P50'==slurperRoot.car[1].@name//ERROR(cast error)

Activity

Hide
Paul King added a comment - - edited

Possibly not what you are after but these variations work:

assert 'P50' == slurperRoot.car[1].@name.text()
assert 'P50' == slurperRoot.car[1].@name.toString()
Show
Paul King added a comment - - edited Possibly not what you are after but these variations work:
assert 'P50' == slurperRoot.car[1].@name.text()
assert 'P50' == slurperRoot.car[1].@name.toString()
Hide
Ford Guo added a comment -

Thanks Paul,you code works and the XmlSurper document's example code use the '.text()' method .
But I think it is a little strange that ".@name=='P50'" is ok but the "'P50'==" is error.

Show
Ford Guo added a comment - Thanks Paul,you code works and the XmlSurper document's example code use the '.text()' method . But I think it is a little strange that ".@name=='P50'" is ok but the "'P50'==" is error.
Hide
Paul King added a comment -

The equals() method in GPathResult (parent of Attribute) contains a .toString(). I'll have to dive into the code to see why the other way around throws a cast error. The equals() method in String returns false when given a non string.

Show
Paul King added a comment - The equals() method in GPathResult (parent of Attribute) contains a .toString(). I'll have to dive into the code to see why the other way around throws a cast error. The equals() method in String returns false when given a non string.
Hide
Ford Guo added a comment -

good idea ,Thanks and good luck

Show
Ford Guo added a comment - good idea ,Thanks and good luck
Hide
Paul King added a comment -

Groovy now returns false for {{assert 'P50'==slurperRoot.car[1].@name}}. Given that String and Attributes are unrelated, Groovy just tries the Java equals method for String which returns false for non-String arguments. This isn't quite what you wanted but is now consistent with Java. Arguably we might want to make the String equals method smarter to do a toString() on its argument if not given a String but I guess that belongs in a separate feature enhancement request rather than a bug.

Show
Paul King added a comment - Groovy now returns false for {{assert 'P50'==slurperRoot.car[1].@name}}. Given that String and Attributes are unrelated, Groovy just tries the Java equals method for String which returns false for non-String arguments. This isn't quite what you wanted but is now consistent with Java. Arguably we might want to make the String equals method smarter to do a toString() on its argument if not given a String but I guess that belongs in a separate feature enhancement request rather than a bug.
Hide
Paul King added a comment -

close off release 1.5.4

Show
Paul King added a comment - close off release 1.5.4
Hide
Paul King added a comment -

update fix version

Show
Paul King added a comment - update fix version

People

Vote (0)
Watch (0)

Dates

  • Created:
    Updated:
    Resolved: