Test JetPositionManager.getSourcePosition()

This commit is contained in:
Alexander Udalov
2012-10-19 21:29:06 +04:00
parent 1469c1fe5e
commit 04a3aa8763
2 changed files with 104 additions and 6 deletions
@@ -0,0 +1,88 @@
/*
* Copyright 2010-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.jet.plugin.debugger;
import com.sun.jdi.*;
import org.jetbrains.annotations.NotNull;
public class MockLocation implements Location {
@NotNull private final ReferenceType declaringType;
@NotNull private final String sourceName;
private final int lineNumber;
public MockLocation(@NotNull ReferenceType declaringType, @NotNull String sourceName, int lineNumber) {
this.declaringType = declaringType;
this.sourceName = sourceName;
this.lineNumber = lineNumber;
}
@Override
public ReferenceType declaringType() {
return declaringType;
}
@Override
public String sourceName() {
return sourceName;
}
@Override
public int lineNumber() {
return lineNumber;
}
@Override
public Method method() {
throw new UnsupportedOperationException();
}
@Override
public long codeIndex() {
throw new UnsupportedOperationException();
}
@Override
public String sourceName(String s) throws AbsentInformationException {
throw new UnsupportedOperationException();
}
@Override
public String sourcePath() throws AbsentInformationException {
throw new UnsupportedOperationException();
}
@Override
public String sourcePath(String s) throws AbsentInformationException {
throw new UnsupportedOperationException();
}
@Override
public int lineNumber(String s) {
throw new UnsupportedOperationException();
}
@Override
public int compareTo(Location o) {
throw new UnsupportedOperationException();
}
@Override
public VirtualMachine virtualMachine() {
throw new UnsupportedOperationException();
}
}
@@ -31,6 +31,7 @@ import com.intellij.psi.PsiDocumentManager;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.refactoring.MultiFileTestCase;
import com.intellij.testFramework.PsiTestUtil;
import com.sun.jdi.Location;
import com.sun.jdi.ReferenceType;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.codegen.ClassFileFactory;
@@ -104,8 +105,7 @@ public abstract class PositionManagerTestCase extends MultiFileTestCase {
public void run() {
try {
for (Breakpoint breakpoint : breakpoints) {
SourcePosition position = SourcePosition.createFromLine(breakpoint.file, breakpoint.lineNumber);
assertPositionIsValid(position, breakpoint.className, positionManager);
assertBreakpointIsHandledCorrectly(breakpoint, positionManager);
}
}
catch (NoDataException e) {
@@ -154,15 +154,25 @@ public abstract class PositionManagerTestCase extends MultiFileTestCase {
return events;
}
private static void assertPositionIsValid(SourcePosition position, String className, PositionManager positionManager) throws NoDataException {
private static void assertBreakpointIsHandledCorrectly(Breakpoint breakpoint, PositionManager positionManager) throws NoDataException {
SourcePosition position = SourcePosition.createFromLine(breakpoint.file, breakpoint.lineNumber);
List<ReferenceType> classes = positionManager.getAllClasses(position);
assertNotNull(classes);
assertEquals(1, classes.size());
ReferenceType type = classes.get(0);
if (!className.contains("$src$")) // don't want to deal with hashCodes in test
assertEquals(className, type.name());
if (!breakpoint.className.contains("$src$")) // don't want to deal with hashCodes in test
assertEquals(breakpoint.className, type.name());
else
assertTrue(type.name().startsWith(className));
assertTrue(type.name().startsWith(breakpoint.className));
// JDI names are of form "package.Class$InnerClass"
ReferenceType typeWithFqName = new MockReferenceType(type.name().replace('/', '.'));
Location location = new MockLocation(typeWithFqName, breakpoint.file.getName(), breakpoint.lineNumber + 1);
SourcePosition actualPosition = positionManager.getSourcePosition(location);
assertNotNull(actualPosition);
assertEquals(position.getFile(), actualPosition.getFile());
assertEquals(position.getLine(), actualPosition.getLine());
}
private static class Breakpoint {