forked from INRIA/spoon
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: sort type references by position and qualified name (may be BREA…
…KING) (INRIA#3355)
- Loading branch information
Showing
7 changed files
with
222 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
src/test/java/spoon/support/util/QualifiedNameBasedSortedSetTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package spoon.support.util; | ||
|
||
import org.junit.Test; | ||
import spoon.reflect.cu.CompilationUnit; | ||
import spoon.reflect.cu.SourcePosition; | ||
import spoon.reflect.declaration.CtType; | ||
import spoon.reflect.reference.CtTypeReference; | ||
import spoon.support.visitor.java.JavaReflectionTreeBuilder; | ||
import spoon.testing.utils.ModelUtils; | ||
|
||
import java.io.File; | ||
import java.util.ArrayList; | ||
import java.util.Iterator; | ||
import java.util.LinkedList; | ||
import java.util.List; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
public class QualifiedNameBasedSortedSetTest { | ||
@Test | ||
public void testIteratorOrdering() { | ||
// contract: elements without source position should appear before any element with source position, | ||
// and be ordered between themselves by qualified name. Elements with source position should be ordered | ||
// between themselves by source position. | ||
final CtType<?> linkedListType = new JavaReflectionTreeBuilder(ModelUtils.createFactory()) | ||
.scan(LinkedList.class); | ||
|
||
final MockSourcePosition smallerSourcePos = new MockSourcePosition(10, 12); | ||
final MockSourcePosition largerSourcePos = new MockSourcePosition(14, 15); | ||
|
||
QualifiedNameBasedSortedSet<CtTypeReference<?>> superInterfaces = new QualifiedNameBasedSortedSet<>( | ||
linkedListType.getSuperInterfaces()); | ||
List<CtTypeReference<?>> expectedInterfaceOrder = new ArrayList<>(superInterfaces); | ||
|
||
CtTypeReference<?> largerSourcePosElem = expectedInterfaceOrder.remove(0); | ||
CtTypeReference<?> smallerSourcePosElem = expectedInterfaceOrder.remove(0); | ||
// setting the source positions will reorder the elements when fetched from the original set | ||
largerSourcePosElem.setPosition(largerSourcePos); | ||
smallerSourcePosElem.setPosition(smallerSourcePos); | ||
expectedInterfaceOrder.add(smallerSourcePosElem); | ||
expectedInterfaceOrder.add(largerSourcePosElem); | ||
|
||
Iterator<CtTypeReference<?>> expected = expectedInterfaceOrder.iterator(); | ||
Iterator<CtTypeReference<?>> actual = superInterfaces.iterator(); | ||
|
||
assertTrue(expected.hasNext()); | ||
while (expected.hasNext()) { | ||
assertEquals(expected.next(), actual.next()); | ||
} | ||
assertFalse(actual.hasNext()); | ||
} | ||
|
||
|
||
private static class MockSourcePosition implements SourcePosition { | ||
final int sourceStart; | ||
final int sourceEnd; | ||
|
||
public MockSourcePosition(int sourceStart, int sourceEnd) { | ||
this.sourceStart = sourceStart; | ||
this.sourceEnd = sourceEnd; | ||
} | ||
|
||
@Override | ||
public boolean isValidPosition() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public int getSourceEnd() { | ||
return sourceEnd; | ||
} | ||
|
||
@Override | ||
public int getSourceStart() { | ||
return sourceStart; | ||
} | ||
|
||
/* | ||
* Methods below here don't matter | ||
*/ | ||
|
||
@Override | ||
public File getFile() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public CompilationUnit getCompilationUnit() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public int getLine() { | ||
return 0; | ||
} | ||
|
||
@Override | ||
public int getEndLine() { | ||
return 0; | ||
} | ||
|
||
@Override | ||
public int getColumn() { | ||
return 0; | ||
} | ||
|
||
@Override | ||
public int getEndColumn() { | ||
return 0; | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
src/test/java/spoon/test/ctType/testclasses/MultiInterfaceImplementation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package spoon.test.ctType.testclasses; | ||
|
||
public class MultiInterfaceImplementation implements | ||
java.util.function.Supplier<Integer>, | ||
java.util.function.Consumer<Integer>, | ||
java.lang.Comparable<java.lang.Integer> { | ||
public int compareTo(Integer i) { | ||
return 0; | ||
} | ||
|
||
public Integer get() { | ||
return 1; | ||
} | ||
|
||
public void accept(Integer i) { | ||
// thanks! | ||
} | ||
} | ||
|
||
|