Skip to content

Commit

Permalink
Adding automatic junit test case for extract method refactoring. It d…
Browse files Browse the repository at this point in the history
…oesn't work from maven yet, so it is ignored :(
  • Loading branch information
javierfernandes committed Oct 8, 2015
1 parent 11b05fd commit 005e154
Show file tree
Hide file tree
Showing 19 changed files with 207 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package org.uqbar.project.wollok.launch.setup

import com.google.inject.Binder
import com.google.inject.Guice
import com.google.inject.name.Names
import org.eclipse.xtext.parser.antlr.Lexer
import org.eclipse.xtext.ui.LexerUIBindings
import org.uqbar.project.wollok.WollokDslStandaloneSetup
import org.uqbar.project.wollok.launch.WollokLauncherParameters
import com.google.inject.Binder
import org.uqbar.project.wollok.parser.antlr.internal.InternalWollokDslLexer

class WollokLauncherSetup extends WollokDslStandaloneSetup {

Expand All @@ -24,6 +28,9 @@ class WollokLauncherSetup extends WollokDslStandaloneSetup {
override configure(Binder binder) {
super.configure(binder)
binder.bind(WollokLauncherParameters).toInstance(params)

// for testing refactors
binder.bind(Lexer).annotatedWith(Names.named(LexerUIBindings.HIGHLIGHTING)).to(InternalWollokDslLexer)
}

}
1 change: 1 addition & 0 deletions org.uqbar.project.wollok.tests/.classpath
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry exported="true" kind="lib" path="lib/mockito-all-1.9.5.jar"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
<classpathentry kind="src" path="src/"/>
Expand Down
12 changes: 10 additions & 2 deletions org.uqbar.project.wollok.tests/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ Bundle-Name: %pluginName
Bundle-Vendor: %providerName
Bundle-Localization: plugin
Bundle-Version: 1.1.0.qualifier
Bundle-ClassPath: .
Bundle-ClassPath: .,
lib/mockito-all-1.9.5.jar
Bundle-SymbolicName: org.uqbar.project.wollok.tests;singleton:=true
Bundle-ActivationPolicy: lazy
Require-Bundle: org.uqbar.project.wollok;visibility:=reexport,
Expand All @@ -19,7 +20,14 @@ Require-Bundle: org.uqbar.project.wollok;visibility:=reexport,
org.uqbar.project.wollok.lib,
org.uqbar.project.wollok.typeSystem.xsemantics,
org.uqbar.project.wollok.ui,
org.uqbar.project.wollok.launch
org.uqbar.project.wollok.launch,
org.eclipse.e4.ui.workbench;bundle-version="1.2.2",
org.eclipse.e4.ui.model.workbench;bundle-version="1.1.0",
org.eclipse.osgi.services;bundle-version="3.4.0",
org.eclipse.e4.core.di;bundle-version="1.4.0",
org.eclipse.core.filesystem;bundle-version="1.4.100",
org.eclipse.xtext.ui.shared;bundle-version="2.7.3",
org.eclipse.xtext.xbase.ui;bundle-version="2.7.3"
Import-Package: org.apache.log4j,
org.junit;version="4.5.0",
org.junit.runner;version="4.5.0",
Expand Down
3 changes: 2 additions & 1 deletion org.uqbar.project.wollok.tests/build.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ source.. = src/,\
bin.includes = META-INF/,\
.,\
log4j.properties,\
plugin.properties
plugin.properties,\
lib/mockito-all-1.9.5.jar
Binary file not shown.
15 changes: 13 additions & 2 deletions org.uqbar.project.wollok.tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,15 @@
<packaging>eclipse-test-plugin</packaging>

<name>Wollok Tests</name>


<dependencies>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.9.5</version>
</dependency>
</dependencies>

<build>
<testSourceDirectory>src</testSourceDirectory>
<plugins>
Expand Down Expand Up @@ -96,6 +104,8 @@
<excludes>
<exclude>**/Abstract*</exclude>
<exclude>**/WollokXPectTest.java</exclude>
<!-- TODO: I WAS NOT ABLE TO MAKE THEM WORK FROM MAVEN :( -->
<exclude>**/ExtractMethodRefactoringTest.java</exclude>
<exclude>**/WollokContentAssistTest.java</exclude>
</excludes>
</configuration>
Expand All @@ -114,7 +124,8 @@
<configuration>
<includes>
<include>**/WollokXPectTest.java</include>
<!--
<!-- TODO: I WAS NOT ABLE TO MAKE THEM WORK FROM MAVEN :(
<include>**/ExtractMethodRefactoringTest.java</include>
<include>**/WollokContentAssistTest.java</include>
-->
</includes>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import org.uqbar.project.wollok.launch.setup.WollokLauncherSetup
class WollokReplInjector extends WollokDslInjectorProvider {

override protected internalCreateInjector() {
return new WollokLauncherSetup().createInjectorAndDoEMFRegistration();
new WollokLauncherSetup().createInjectorAndDoEMFRegistration
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package org.uqbar.project.wollok.tests.refactoring

import com.google.inject.Inject
import org.eclipse.core.runtime.IProgressMonitor
import org.eclipse.core.runtime.NullProgressMonitor
import org.eclipse.ltk.core.refactoring.RefactoringStatus
import org.eclipse.ui.IWorkbenchPartSite
import org.eclipse.ui.texteditor.IDocumentProvider
import org.eclipse.xtext.resource.DefaultLocationInFileProvider
import org.eclipse.xtext.ui.editor.XtextEditor
import org.eclipse.xtext.ui.editor.model.DocumentTokenSource
import org.eclipse.xtext.ui.editor.model.XtextDocument
import org.eclipse.xtext.ui.editor.model.edit.ITextEditComposer
import org.eclipse.xtext.ui.refactoring.impl.ProjectUtil
import org.eclipse.xtext.ui.refactoring.impl.StatusWrapper
import org.eclipse.xtext.xbase.ui.document.DocumentRewriter
import org.eclipse.xtext.xbase.ui.imports.ReplaceConverter
import org.eclipse.xtext.xbase.ui.refactoring.ExpressionUtil
import org.junit.Test
import org.uqbar.project.wollok.refactoring.ExtractMethodRefactoring
import org.uqbar.project.wollok.tests.interpreter.AbstractWollokInterpreterTestCase
import org.uqbar.project.wollok.wollokDsl.WBinaryOperation
import org.uqbar.project.wollok.wollokDsl.WExpression
import org.xpect.util.ReflectionUtil

import static org.mockito.Matchers.*
import static org.mockito.Mockito.*

/**
*
* @author jfernandes
*/
class ExtractMethodRefactoringTest extends AbstractWollokInterpreterTestCase {

//TODO: the actual generated method must contain the return statement
@Test
def void extractSimpleMethodWithoutParameters() {
assertRefactor('''
class A {
method foo() {
return 100 + 23 * 2
}
}
''',
"calculus",
'''
class A {
method foo() {
return 100 + this.calculus()
}
method calculus() {
23 * 2
}
}
''')
}

// *****************************
// ** utility methods
// *****************************

@Inject DocumentRewriter.Factory rProvider
@Inject DocumentTokenSource tokenSource
@Inject ITextEditComposer composer

def void assertRefactor(String code, String newMethodName, String refactored) {

val editor = createEditor(code)

val selection = code.interpretPropagatingErrors.eAllContents.filter(WBinaryOperation).findFirst[e | e.feature == "*"] as WExpression

val ExtractMethodRefactoring r = createRefactoring(newMethodName)
r.initialize(editor, #[selection], false)

val pm = new NullProgressMonitor

// checks
assertEquals(#[].toString, r.checkInitialConditions(pm).entries.toString)
assertEquals(#[].toString, r.checkFinalConditions(pm).entries.toString)

// perform
val change = r.createChange(pm)
change.perform(pm)

// assert
val output = editor.document.get
assertEquals(refactored, output)
}

def createRefactoring(String newMethodName) {
val fileProvider = new DefaultLocationInFileProvider
new ExtractMethodRefactoring => [
methodName = newMethodName
statusProvider = [| new StatusWrapper => [
forceField("status", new RefactoringStatus)
forceField("projectUtil", mock(ProjectUtil))
forceField("locationInFileProvider", fileProvider)
]]
expressionUtil = new ExpressionUtil => [
forceField("locationInFileProvider", fileProvider)
]
rewriterFactory = rProvider
locationInFileProvider = fileProvider
replaceConverter = new ReplaceConverter
]
}

def forceField(Object obj, String field, Object value) {
ReflectionUtil.writeField(obj.class, obj, field, value)
}

def createEditor(String code) {
val xtexdoc = new XtextDocument(tokenSource, composer)
xtexdoc.set(code)

val provider = mock(IDocumentProvider) => [
when(getDocument(any)).thenReturn(xtexdoc)
]

mock(XtextEditor) => [
when(documentProvider).thenReturn(provider)
when(document).thenReturn(xtexdoc)
when(editorInput).thenReturn(null)
when(site).thenReturn(mock(IWorkbenchPartSite))
]
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ abstract class AbstractWollokTypeSystemTestCase extends AbstractWollokInterprete

// FINDS

def findByText(EObject model, String token) {
def static findByText(EObject model, String token) {
val found = NodeModelUtils.findActualNodeFor(model).asTreeIterable //
.findFirst[n|
escapeNodeTextToCompare(n.text) == token && n.hasDirectSemanticElement
Expand All @@ -103,7 +103,7 @@ abstract class AbstractWollokTypeSystemTestCase extends AbstractWollokInterprete
found.semanticElement
}

def String escapeNodeTextToCompare(String nodeText) {
def static String escapeNodeTextToCompare(String nodeText) {
if (nodeText.startsWith("\n"))
nodeText.substring(1).escapeNodeTextToCompare
else if (nodeText.startsWith("\t"))
Expand Down
5 changes: 3 additions & 2 deletions org.uqbar.project.wollok.ui/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Bundle-Localization: plugin
Require-Bundle: org.uqbar.project.wollok;visibility:=reexport,
org.eclipse.xtext.ui;bundle-version="[2.7.3,2.8.0)",
org.eclipse.ui.editors;bundle-version="3.5.0",
org.eclipse.ui.ide;bundle-version="3.5.0",
org.eclipse.ui.ide;bundle-version="3.5.0";visibility:=reexport,
org.eclipse.xtext.ui.shared;bundle-version="2.7.3",
org.eclipse.ui,
org.eclipse.xtext.builder;bundle-version="[2.7.3,2.8.0)",
Expand All @@ -36,7 +36,8 @@ Require-Bundle: org.uqbar.project.wollok;visibility:=reexport,
Import-Package: org.apache.log4j,
org.eclipse.xtext.xbase.lib
Bundle-RequiredExecutionEnvironment: JavaSE-1.7
Export-Package: org.uqbar.project.wollok.ui,
Export-Package: org.uqbar.project.wollok.refactoring,
org.uqbar.project.wollok.ui,
org.uqbar.project.wollok.ui.contentassist,
org.uqbar.project.wollok.ui.contentassist.antlr,
org.uqbar.project.wollok.ui.editor,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class ExtractMethodHandler extends AbstractHandler {
val extractMethodRefactoring = refactoringProvider.get
if (extractMethodRefactoring.initialize(editor, expressions, true)) {
updateSelection(editor, expressions)
val wizard = wizardFactory.create(extractMethodRefactoring);
val wizard = wizardFactory.create(extractMethodRefactoring)
val openOperation = new RefactoringWizardOpenOperation_NonForking(wizard)
openOperation.run(editor.site.shell, "Extract Method");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import org.eclipse.jface.text.BadLocationException
import org.eclipse.ltk.core.refactoring.Refactoring
import org.eclipse.ltk.core.refactoring.RefactoringStatus
import org.eclipse.text.edits.TextEdit
import org.eclipse.ui.texteditor.ITextEditor
import org.eclipse.xtend.lib.annotations.Accessors
import org.eclipse.xtext.EcoreUtil2
import org.eclipse.xtext.common.types.JvmIdentifiableElement
Expand Down Expand Up @@ -55,6 +56,7 @@ import static org.eclipse.xtext.util.Strings.*

import static extension org.uqbar.project.wollok.model.WMethodContainerExtensions.*
import static extension org.uqbar.project.wollok.refactoring.RefactoringExtensions.*
import static extension org.uqbar.project.wollok.ui.utils.XTendUtilExtensions.*

/**
*
Expand All @@ -63,6 +65,7 @@ import static extension org.uqbar.project.wollok.refactoring.RefactoringExtensio
// this needs to view reviewed for:
// - extracting a method that returns a value
// - any unnecessary code (since it was based on xtend)
@Accessors
class ExtractMethodRefactoring extends Refactoring {
static val Logger LOG = Logger.getLogger(ExtractMethodRefactoring)

Expand All @@ -80,7 +83,7 @@ class ExtractMethodRefactoring extends Refactoring {
WMethodDeclaration originalMethod
IXtextDocument document
boolean doSave
XtextEditor editor
ITextEditor editor
List<WExpression> expressions
WExpression firstExpression
WExpression lastExpression
Expand All @@ -94,7 +97,7 @@ class ExtractMethodRefactoring extends Refactoring {
override getName() { "Extract Method Refactoring" }

def initialize(XtextEditor editor, List<WExpression> expressions, boolean doSave) {
if(expressions.isEmpty() || editor.getDocument() == null)
if (expressions.empty || editor.document == null)
return false

this.document = editor.document
Expand Down Expand Up @@ -193,7 +196,8 @@ class ExtractMethodRefactoring extends Refactoring {
}

def handleException(Exception exc, StatusWrapper status) {
status.add(RefactoringStatus.FATAL, "Error during refactoring: {0}", exc, LOG);
exc.printStackTrace
status.add(RefactoringStatus.FATAL, "Error during refactoring: {0}", exc.toString)
}

def isEndOfOriginalMethod() {
Expand Down Expand Up @@ -227,17 +231,11 @@ class ExtractMethodRefactoring extends Refactoring {

def validateParameters() {
val status = new RefactoringStatus
val Set<String> namesSoFar = newHashSet

for (ParameterInfo parameterInfo : parameterInfos) {
val newName = parameterInfo.getNewName
if (namesSoFar.contains(newName))
status.addError("Duplicate parameter name '" + newName + "'");
if (!equal(newName, parameterInfo.oldName) && localFeatureNames.contains(newName))
status.addError("'" + newName + "' is already used as a name in the selected code")
// nameUtil.checkNewFeatureName(newName, false, status);
namesSoFar.add(newName)
}
parameterInfos.map[newName].duplicates.forEach[ status.addError("Duplicate parameter name '" + it + "'") ]

parameterInfos.filter[ !equal(newName, oldName) && localFeatureNames.contains(newName) ].forEach[ status.addError("'" + newName + "' is already used as a name in the selected code") ]

status
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ class ExtractMethodWizard extends RefactoringWizard {
}

new(Refactoring refactoring, ExtractMethodUserInputPage userInputPage) {
super(refactoring, RefactoringWizard.DIALOG_BASED_USER_INTERFACE);
super(refactoring, RefactoringWizard.DIALOG_BASED_USER_INTERFACE)
this.userInputPage = userInputPage;
}

override def getWindowTitle() { "Extract Method" }

override def addUserInputPages() {
userInputPage.setRefactoring(refactoring as ExtractMethodRefactoring)
addPage(userInputPage);
addPage(userInputPage)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@
import org.eclipse.xtext.xbase.jvmmodel.IJvmModelAssociations;
import org.uqbar.project.wollok.manifest.BasicWollokManifestFinder;
import org.uqbar.project.wollok.manifest.WollokManifestFinder;
import org.uqbar.project.wollok.refactoring.DummyJvmModelAssociations;
import org.uqbar.project.wollok.refactoring.DummyJvmTypeProvider;
import org.uqbar.project.wollok.refactoring.DummyJvmTypeProviderFactory;
import org.uqbar.project.wollok.ui.autoedit.TokenTypeToPartitionMapper;
import org.uqbar.project.wollok.ui.editor.WollokSourceViewerConfiguration;
import org.uqbar.project.wollok.ui.editor.annotations.WOverrideIndicatorModelListener;
Expand All @@ -39,6 +36,8 @@
import org.uqbar.project.wollok.ui.hover.WollokEObjectHoverProvider;
import org.uqbar.project.wollok.ui.wizard.WollokProjectCreator;
import org.uqbar.project.wollok.ui.wizard.WollokProjectFactory;
import org.uqbar.project.wollok.utils.DummyJvmModelAssociations;
import org.uqbar.project.wollok.utils.DummyJvmTypeProviderFactory;

import com.google.inject.Binder;
import com.google.inject.Provider;
Expand Down
Loading

0 comments on commit 005e154

Please sign in to comment.