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

Open
Vampire opened this issue Sep 1, 2019 · 0 comments

Comments

@Vampire
Copy link
Contributor

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() {
        expect:
            new Java1(null as String) { }
    }

    def foo2() {
        expect:
            new Java2(null as String)
    }

    def foo3() {
        expect:
            new Java3(null as String)
    }

    def foo4() {
        expect:
            new Java4(null as String)
    }

    def foo5() {
        expect:
            new Groovy1(null as String)
    }

    def foo6() {
        expect:
            new Groovy2(null as String)
    }

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

    def foo8() {
        expect:
            new Groovy4(null as String)
    }

    def foo9() {
        expect:
            new Groovy5(null as String)
    }

    def foo10() {
        expect:
            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
Projects
None yet
1 participant
You can’t perform that action at this time.