From 04a3aa8763fd8625555ba170c474cdca7fdb00c2 Mon Sep 17 00:00:00 2001 From: Alexander Udalov Date: Fri, 19 Oct 2012 21:29:06 +0400 Subject: [PATCH] Test JetPositionManager.getSourcePosition() --- .../jet/plugin/debugger/MockLocation.java | 88 +++++++++++++++++++ .../debugger/PositionManagerTestCase.java | 22 +++-- 2 files changed, 104 insertions(+), 6 deletions(-) create mode 100644 idea/tests/org/jetbrains/jet/plugin/debugger/MockLocation.java diff --git a/idea/tests/org/jetbrains/jet/plugin/debugger/MockLocation.java b/idea/tests/org/jetbrains/jet/plugin/debugger/MockLocation.java new file mode 100644 index 00000000000..727b3f44321 --- /dev/null +++ b/idea/tests/org/jetbrains/jet/plugin/debugger/MockLocation.java @@ -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(); + } +} diff --git a/idea/tests/org/jetbrains/jet/plugin/debugger/PositionManagerTestCase.java b/idea/tests/org/jetbrains/jet/plugin/debugger/PositionManagerTestCase.java index cd2fb739146..5d1c4f76ab2 100644 --- a/idea/tests/org/jetbrains/jet/plugin/debugger/PositionManagerTestCase.java +++ b/idea/tests/org/jetbrains/jet/plugin/debugger/PositionManagerTestCase.java @@ -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 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 {