Though I've recently come to love working with Groovy, I'm deep enough where I'm starting to see some of its warts.
The one that hit me nearly right away was the nastiness about groovy.lang.GString not extending java.lang.String. This is mainly the fault of the latter being final and not extendable, but it means that GString instances aren't automatically comparable with plain Java strings.
In Groovy, string literals can be quoted using both single and double-quotes. GString appears when you use double-quotes around a string and expand variables into it. See "Strings and GStrings" for more information.
In any case, when you call a method that takes a String, a GString will automatically be converted to String. However, when you call a method that takes Object, it won't. This includes JUnit's Assert.assertEquals method. Specifically:
Both of these tests will fail because the GString's won't be coerced into Strings.
In our current project, we're toying with [ab]using Groovy's ExpandoMetaClass to extend the equals methods in both String and GString to automatically convert GStrings to Strings whenever they come into contact:
I can't see why this isn't the default behavior, frankly.
The one that hit me nearly right away was the nastiness about groovy.lang.GString not extending java.lang.String. This is mainly the fault of the latter being final and not extendable, but it means that GString instances aren't automatically comparable with plain Java strings.
In Groovy, string literals can be quoted using both single and double-quotes. GString appears when you use double-quotes around a string and expand variables into it. See "Strings and GStrings" for more information.
In any case, when you call a method that takes a String, a GString will automatically be converted to String. However, when you call a method that takes Object, it won't. This includes JUnit's Assert.assertEquals method. Specifically:
Both of these tests will fail because the GString's won't be coerced into Strings.
In our current project, we're toying with [ab]using Groovy's ExpandoMetaClass to extend the equals methods in both String and GString to automatically convert GStrings to Strings whenever they come into contact:
I can't see why this isn't the default behavior, frankly.
Comments
Post a Comment