Skip to content

Commit

Permalink
Fix for #794: search generated methods apart from original w/ default(s)
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Aug 18, 2020
1 parent de77839 commit 046a634
Show file tree
Hide file tree
Showing 20 changed files with 618 additions and 293 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2019 the original author or authors.
* Copyright 2009-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,23 +16,22 @@
package org.eclipse.jdt.core.groovy.tests;

import org.codehaus.jdt.groovy.model.GroovyCompilationUnit;
import org.eclipse.jdt.core.search.SearchEngine;
import org.eclipse.jdt.core.search.SearchMatch;
import org.eclipse.jdt.internal.core.search.JavaSearchDocument;
import org.eclipse.jdt.internal.core.search.JavaSearchParticipant;
import org.eclipse.jdt.internal.core.search.matching.PossibleMatch;

public class MockPossibleMatch extends PossibleMatch {

public MockPossibleMatch(GroovyCompilationUnit unit) {
super(null, unit.getResource(), unit, new JavaSearchDocument(unit.getResource().getFullPath().toPortableString(), new JavaSearchParticipant()), false);
public MockPossibleMatch(final GroovyCompilationUnit unit) {
super(null, unit.getResource(), unit, new JavaSearchDocument(unit.getResource().getFullPath().toPortableString(), SearchEngine.getDefaultSearchParticipant()), false);
}

public static String printMatch(SearchMatch match) {
return "Match at: (" + match.getOffset() + ", " + match.getLength() + ")," +
" accuracy: " + accuracy(match) + "\n Matched object: " + match.getElement() + "\n";
public static String printMatch(final SearchMatch match) {
return "Match at: (" + match.getOffset() + ", " + match.getLength() + ")," + " accuracy: " + accuracy(match) + "\n Matched object: " + match.getElement() + "\n";
}

public static String accuracy(SearchMatch match) {
return match.getAccuracy() == SearchMatch.A_ACCURATE ? "ACCURATE" : "INACCURATE";
public static String accuracy(final SearchMatch match) {
return (match.getAccuracy() == SearchMatch.A_ACCURATE ? "ACCURATE" : "INACCURATE");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -261,37 +261,60 @@ public void testConstructorReferences10() throws Exception {
public void testConstructorReferences11() throws Exception {
GroovyCompilationUnit foo = createUnit("p", "Foo", "package p\n" +
"class Foo {\n" +
" Foo(int i = 0) {}\n" + // search for this
" Foo(int i=42) {}\n" + // search for this
" Foo(String s) {}\n" +
"}");
createUnit("", "Bar", "import p.Foo\n" +
"new Foo()\n" + // yes
"new Foo()\n" + // no
"new Foo(0)\n" + // yes
"new Foo('')\n"); // no

long ctorRefs = searchForReferences(foo.getType("Foo").getMethods()[0]).stream()
.filter(match -> ((IMethod) match.getElement()).getResource().getName().equals("Bar.groovy"))
.count();
assertEquals(2, ctorRefs);
List<SearchMatch> ctorRefs = searchForReferences(foo.getType("Foo").getMethods()[0]);

assertEquals(1, ctorRefs.size());
assertEquals(27, ctorRefs.get(0).getOffset());
assertEquals(SearchMatch.A_ACCURATE, ctorRefs.get(0).getAccuracy());
}

@Test // same-unit references exercise patch in Verifier.addDefaultParameterConstructors
@Test // default value generates a synthetic constructor
public void testConstructorReferences11a() throws Exception {
GroovyCompilationUnit foo = createUnit("p", "Foo", "package p\n" +
"class Foo {\n" +
" Foo(int i = 0) {}\n" + // search for this
" Foo(String s) {this()}\n" + // yes
" Foo(int i=42) {}\n" + // search for this
" Foo(String s) {}\n" +
"}");
createUnit("", "Bar", "import p.Foo\n" +
"new Foo()\n" + // yes
"new Foo(0)\n" + // no
"new Foo('')\n"); // no

List<SearchMatch> ctorRefs = searchForReferences(foo.getType("Foo").getMethods()[1]);

assertEquals(1, ctorRefs.size());
assertEquals(17, ctorRefs.get(0).getOffset());
assertEquals(SearchMatch.A_ACCURATE, ctorRefs.get(0).getAccuracy());
}

@Test // same-unit references exercise patch in Verifier.addDefaultParameterConstructors
public void testConstructorReferences11b() throws Exception {
GroovyCompilationUnit foo = createUnit("p", "Foo", "package p\n" +
"class Foo {\n" +
" Foo(int i=42) {}\n" + // search for this
" Foo(String s) {this(666)}\n" + // yes
" def m() {\n" +
" new Foo()\n" + // yes
" new Foo()\n" + // no
" new Foo(0)\n" + // yes
" new Foo('')\n" + // no
" }\n" +
"}");

long ctorRefs = searchForReferences(foo.getType("Foo").getMethods()[0]).stream()
.filter(match -> ((IMethod) match.getElement()).getResource().getName().equals("Foo.groovy"))
.count();
assertEquals(3, ctorRefs);
List<SearchMatch> ctorRefs = searchForReferences(foo.getType("Foo").getMethods()[0]);

assertEquals(2, ctorRefs.size());
assertEquals(58, ctorRefs.get(0).getOffset());
assertEquals(SearchMatch.A_ACCURATE, ctorRefs.get(0).getAccuracy());
assertEquals(103, ctorRefs.get(1).getOffset());
assertEquals(SearchMatch.A_ACCURATE, ctorRefs.get(1).getAccuracy());
}

@Test // default value generates a synthetic constructor
Expand All @@ -302,33 +325,56 @@ public void testConstructorReferences12() throws Exception {
" Foo(String s = '') {}\n" + // search for this
"}");
createUnit("", "Bar", "import p.Foo\n" +
"new Foo()\n" + // yes
"new Foo()\n" + // no
"new Foo(0)\n" + // no
"new Foo('')\n"); // yes

long ctorRefs = searchForReferences(foo.getType("Foo").getMethods()[1]).stream()
.filter(match -> ((IMethod) match.getElement()).getResource().getName().equals("Bar.groovy"))
.count();
assertEquals(2, ctorRefs);
List<SearchMatch> ctorRefs = searchForReferences(foo.getType("Foo").getMethods()[1]);

assertEquals(1, ctorRefs.size());
assertEquals(38, ctorRefs.get(0).getOffset());
assertEquals(SearchMatch.A_ACCURATE, ctorRefs.get(0).getAccuracy());
}

@Test // same-unit references exercise patch in Verifier.addDefaultParameterConstructors
@Test // default value generates a synthetic constructor
public void testConstructorReferences12a() throws Exception {
GroovyCompilationUnit foo = createUnit("p", "Foo", "package p\n" +
"class Foo {\n" +
" Foo(int i) {this()}\n" + // yes
" Foo(int i) {}\n" +
" Foo(String s = '') {}\n" + // search for this
"}");
createUnit("", "Bar", "import p.Foo\n" +
"new Foo()\n" + // yes
"new Foo(0)\n" + // no
"new Foo('')\n"); // no

List<SearchMatch> ctorRefs = searchForReferences(foo.getType("Foo").getMethods()[2]);

assertEquals(1, ctorRefs.size());
assertEquals(17, ctorRefs.get(0).getOffset());
assertEquals(SearchMatch.A_ACCURATE, ctorRefs.get(0).getAccuracy());
}

@Test // same-unit references exercise patch in Verifier.addDefaultParameterConstructors
public void testConstructorReferences12b() throws Exception {
GroovyCompilationUnit foo = createUnit("p", "Foo", "package p\n" +
"class Foo {\n" +
" Foo(int i) {this('xxx')}\n" + // yes
" Foo(String s = '') {}\n" + // search for this
" def m() {\n" +
" new Foo()\n" + // yes
" new Foo()\n" + // no
" new Foo(0)\n" + // no
" new Foo('')\n" + // yes
" }\n" +
"}");

long ctorRefs = searchForReferences(foo.getType("Foo").getMethods()[1]).stream()
.filter(match -> ((IMethod) match.getElement()).getResource().getName().equals("Foo.groovy"))
.count();
assertEquals(3, ctorRefs);
List<SearchMatch> ctorRefs = searchForReferences(foo.getType("Foo").getMethods()[1]);

assertEquals(2, ctorRefs.size());
assertEquals(36, ctorRefs.get(0).getOffset());
assertEquals(SearchMatch.A_ACCURATE, ctorRefs.get(0).getAccuracy());
assertEquals(122, ctorRefs.get(1).getOffset());
assertEquals(SearchMatch.A_ACCURATE, ctorRefs.get(1).getAccuracy());
}

@Test
Expand Down
Loading

0 comments on commit 046a634

Please sign in to comment.