Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spock destroys method disambiguation completely #1022

Vampire opened this issue Sep 1, 2019 · 0 comments


Copy link

commented Sep 1, 2019

Given this in a Java class:

    static abstract class Java1 {
        public Java1(String s) { System.out.println("String"); }
        public Java1(Pattern p) { System.out.println("Pattern"); }

    static class Java2 extends Java1 {
        public Java2(String s) { super(s); }
        public Java2(Pattern p) { super(p); }

    static class Java3 extends Java1 {
        public Java3(String s) { super((String) s); }
        public Java3(Pattern p) { super((Pattern) p); }

    static class Java4 {
        public Java4(String s) { System.out.println("String"); }
        public Java4(Pattern p) { System.out.println("Pattern"); }

and this in a Spock specification:

    static class Groovy1 extends Java1 {
        Groovy1(String s) { super(s) }
        Groovy1(Pattern p) { super(p) }

    static class Groovy2 extends Java1 {
        Groovy2(String s) { super(s as String) }
        Groovy2(Pattern p) { super(p as Pattern) }

    static abstract class Groovy3 {
        Groovy3(String s) { println "String" }
        Groovy3(Pattern p) { println "Pattern" }

    static class Groovy4 extends Groovy3 {
        Groovy4(String s) { super(s) }
        Groovy4(Pattern p) { super(p) }

    static class Groovy5 extends Groovy3 {
        Groovy5(String s) { super(s as String) }
        Groovy5(Pattern p) { super(p as Pattern) }

    static class Groovy6 {
        Groovy6(String s) { println "String" }
        Groovy6(Pattern p) { println "Pattern" }

If you then do in the Java class

    public static void main(String[] args) {
        new Java1((String) null) { };
        new Java2((String) null);
        new Java3((String) null);
        new Java4((String) null);

all works well and four times String is printed.

Now we add to the Spock specification these tests:

    def foo1() {
            new Java1(null as String) { }

    def foo2() {
            new Java2(null as String)

    def foo3() {
            new Java3(null as String)

    def foo4() {
            new Java4(null as String)

    def foo5() {
            new Groovy1(null as String)

    def foo6() {
            new Groovy2(null as String)

    def foo7() {
            new Groovy3(null as String) { }

    def foo8() {
            new Groovy4(null as String)

    def foo9() {
            new Groovy5(null as String)

    def foo10() {
            new Groovy6(null as String)

And we surprisingly find that all of them fail.
Some I actually would expect to fail.
foo7 and foo8 are expected to fail, because they even fail in a plain Groovy console.
But foo9 and foo10 should work fine for example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
1 participant
You can’t perform that action at this time.