Skip to content

Commit

Permalink
Adapt quick assist test suite for new quickAssists extension point
Browse files Browse the repository at this point in the history
Most of the tests are bad and because they rely on UI behavior they
can't even be run by the build server. But the tests exist and I don't
want to remove them. If someone has more time available it would be
nice if this person could move tests completely away from UI classes.
  • Loading branch information
kiritsuku committed Oct 5, 2014
1 parent 1ac6a11 commit 86e7141
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 112 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package org.scalaide.core.quickassist
import org.junit.Test

class ChangeCaseTests {
import QuickFixesTests._
import UiQuickAssistTests._

@Test def changeCase() {
withManyQuickFixesPerLine("changecase/ChangeCase.scala")(
Expand All @@ -17,4 +17,4 @@ class ChangeCaseTests {
)
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package org.scalaide.core.quickassist
import org.junit.Test

class CreateClassTests {
import QuickFixesTests._
import UiQuickAssistTests._

@Test
def createClassQuickFixes() {
withQuickFixes("createclass/UsesMissingClass.scala")("Create class 'ThisClassDoesNotExist'")
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package org.scalaide.core.quickassist
import org.junit.Test

class TypeMismatchTests {
import QuickFixesTests._
import UiQuickAssistTests._

val stringPattern = "Transform expression: %s => %s"
val stringPattern: String = "Transform expression: %s => %s"

@Test
def basicTypeMismatchQuickFixes() {
Expand Down Expand Up @@ -60,4 +60,4 @@ class TypeMismatchTests {
// )
))
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package org.scalaide.core
package quickassist

import org.eclipse.jdt.core.compiler.IProblem
import org.eclipse.jdt.ui.JavaUI
import org.eclipse.jface.text.quickassist.IQuickAssistInvocationContext
import org.junit.Assert._
import org.scalaide.core.internal.jdt.model.ScalaCompilationUnit
import org.scalaide.core.internal.quickassist.QuickAssistProcessor

import testsetup.TestProjectSetup

/**
* Provides test behavior that relies on a working UI environment.
*/
object UiQuickAssistTests extends TestProjectSetup("quickassist") {

def assertNumberOfProblems(nProblems: Int, problems: Array[IProblem]) {
if (problems.length != nProblems) {
val buf = new StringBuffer("Wrong number of problems, is: ")
buf.append(problems.length).append(", expected: ").append(nProblems).append('\n')
for (problem <- problems) {
buf.append(problem).append(" at ")
buf.append('[').append(problem.getSourceStart()).append(" ,").append(problem.getSourceEnd()).append(']')
buf.append('\n')
}

assertEquals(buf.toString, nProblems, problems.length)
}
}

def withQuickFixes(pathToSource: String)(expectedQuickFixes: String*) {
withManyQuickFixesPerLine(pathToSource)(expectedQuickFixes.map(List(_)).toList)
}

def withManyQuickFixesPerLine(pathToSource: String)(expectedQuickFixesList: List[List[String]]) {
val unit = compilationUnit(pathToSource).asInstanceOf[ScalaCompilationUnit]

unit.withSourceFile { (src, compiler) =>
compiler.askReload(List(unit)).get

val problems = compiler.problemsOf(unit)
assertTrue("No problems found.", problems.nonEmpty)
assertNumberOfProblems(expectedQuickFixesList.size, problems.toArray)

val part = JavaUI.openInEditor(unit.getCompilationUnit)

for ((problem, expectedQuickFixes) <- problems zip expectedQuickFixesList) {
val offset = problem.getSourceStart
val length = problem.getSourceEnd + 1 - offset
val processor = new QuickAssistProcessor(part.getEditorInput, QuickAssistProcessor.DefaultId)
val proposals = processor.computeQuickAssistProposals(new IQuickAssistInvocationContext {
override def getOffset = offset
override def getLength = length
override def getSourceViewer = null
})
val corrections = proposals.map(_.getDisplayString)

for (quickFix <- expectedQuickFixes) {
assertTrue("Quick fix " + quickFix + " was not offered. Offered were: " + corrections.mkString(", "),
corrections contains quickFix)
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package org.scalaide.core.quickassist.createmethod

import org.junit.Test
import org.scalaide.core.quickassist.QuickFixesTests
import org.scalaide.core.quickassist.UiQuickAssistTests

class CreateMethodTests {
import QuickFixesTests._
import UiQuickAssistTests._

@Test def createMethod() {
withQuickFixes("createmethod/CreateMethod.scala")(
Expand Down

0 comments on commit 86e7141

Please sign in to comment.