当前位置: 动力学知识库 > 问答 > 编程问答 >

groovy - Condition not satisfied when comparing empty Strings in Spock

问题描述:

I'm trying to check that 2 strings are equal but I'm hitting. Both Strings are empty:

Condition not satisfied:

versionTagPrefix == ""

| |

"" false

2 differences (0% similarity)

("")

(--)

I've also tried equals:

Condition not satisfied:

"".equals(versionTagPrefix)

| |

false ""

Versions:

compile 'org.codehaus.groovy:groovy-all:2.0.1'

testCompile "org.spockframework:spock-core:0.7-groovy-2.0"

网友答案:

As far as i know, groovy has many implementations of Strings.
When you see messages in output/error stream you are probably looking at value of toString() instead of actual content of object.

To go in deep and debug, use .properties attribute on objects.

For example :

1 org.codehaus.groovy.runtime.GStringImpl

groovy:000> i=10; s1="$i"; "type=${s1.class}\n
                            content=${s1.properties}\n
                            out=${s1.toString()}"
===> type=class org.codehaus.groovy.runtime.GStringImpl
     content=[values:[10],
              class:class org.codehaus.groovy.runtime.GStringImpl,
              bytes:[49, 48], strings:[, ], valueCount:1]
     out=10

2. java.lang.String

groovy:000>s2="10"; "type=${s2.class}\n
                        contents=${s2.properties}\n
                        out=${s2.toString()}"
===> type=class java.lang.String
     contents=[class:class java.lang.String, bytes:[49, 48], empty:false]
     out=10

And can be many implementations....


Though in this case s1 == s2, you might have a situation where s1 != s2 but s1.toString() == s2.toString()

assert versionTagPrefix?.toString() == ""
分享给朋友:
您可能感兴趣的文章:
随机阅读: