Skip to content

Commit

Permalink
Merge pull request #182 from LorenzoBettini/diagnostic-helper
Browse files Browse the repository at this point in the history
Diagnostic helper
  • Loading branch information
LorenzoBettini authored May 25, 2020
2 parents 612bf8d + 71922cd commit 6975620
Show file tree
Hide file tree
Showing 7 changed files with 395 additions and 345 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package edelta.tests

import com.google.inject.Inject
import edelta.interpreter.EdeltaInterpreterDiagnosticHelper
import edelta.resource.derivedstate.EdeltaDerivedStateHelper
import org.eclipse.xtext.testing.InjectWith
import org.eclipse.xtext.testing.XtextRunner
import org.junit.Test
import org.junit.runner.RunWith

import static org.eclipse.xtext.xbase.XbasePackage.Literals.*

@RunWith(XtextRunner)
@InjectWith(EdeltaInjectorProviderCustom)
class EdeltaInterpreterDiagnosticHelperTest extends EdeltaAbstractTest {

@Inject EdeltaInterpreterDiagnosticHelper diagnosticHelper

@Inject EdeltaDerivedStateHelper derivedStateHelper

@Test def void testAddErrorWithCurrentExpression() {
val input = '''
metamodel "foo"
modifyEcore aTest epackage foo {
val s = null
}
'''
input.parseWithTestEcore => [
val exp = lastModifyEcoreOperation.body.blockLastExpression
diagnosticHelper.setCurrentExpression(exp)
diagnosticHelper.addError(null, "issueCode", "an error")
assertError(
XVARIABLE_DECLARATION,
"issueCode",
input.lastIndexOf("val s = null"),
"val s = null".length,
"an error"
)
]
}

@Test def void testAddErrorWithDifferentCorrespondingExpression() {
val input = '''
metamodel "foo"
modifyEcore aTest epackage foo {
var s = null
s = null
}
'''
input.parseWithTestEcore => [
// just an ENamedElement to pass to show method as
// the problematic object
val problematic = copiedEPackages.last
// associate it to the first expression in the derived state
val correspondingExpression = lastModifyEcoreOperation.body.blockFirstExpression
derivedStateHelper.getEnamedElementXExpressionMap(eResource)
.put(problematic, correspondingExpression)
val currentExpression = lastModifyEcoreOperation.body.blockLastExpression
diagnosticHelper.setCurrentExpression(currentExpression)
diagnosticHelper.addError(problematic, "issueCode", "an error")
// the error is associated to the associated expression to problematic object
// not with the current expression
assertError(
XVARIABLE_DECLARATION,
"issueCode",
input.lastIndexOf("val s = null"),
"val s = null".length,
"an error"
)
]
}

@Test def void testAddWarningWithCurrentExpression() {
val input = '''
metamodel "foo"
modifyEcore aTest epackage foo {
val s = null
}
'''
input.parseWithTestEcore => [
val exp = lastModifyEcoreOperation.body.blockLastExpression
diagnosticHelper.setCurrentExpression(exp)
diagnosticHelper.addWarning(null, "issueCode", "a warning")
assertWarning(
XVARIABLE_DECLARATION,
"issueCode",
input.lastIndexOf("val s = null"),
"val s = null".length,
"a warning"
)
]
}

@Test def void testAddWarningWithDifferentCorrespondingExpression() {
val input = '''
metamodel "foo"
modifyEcore aTest epackage foo {
var s = null
s = null
}
'''
input.parseWithTestEcore => [
// just an ENamedElement to pass to show method as
// the problematic object
val problematic = copiedEPackages.last
// associate it to the first expression in the derived state
val correspondingExpression = lastModifyEcoreOperation.body.blockFirstExpression
derivedStateHelper.getEnamedElementXExpressionMap(eResource)
.put(problematic, correspondingExpression)
val currentExpression = lastModifyEcoreOperation.body.blockLastExpression
diagnosticHelper.setCurrentExpression(currentExpression)
diagnosticHelper.addWarning(problematic, "issueCode", "a warning")
// the warning is associated to the associated expression to problematic object
// not with the current expression
assertWarning(
XVARIABLE_DECLARATION,
"issueCode",
input.lastIndexOf("val s = null"),
"val s = null".length,
"a warning"
)
]
}

}
Original file line number Diff line number Diff line change
@@ -1,157 +1,45 @@
package edelta.tests

import com.google.inject.Inject
import edelta.interpreter.EdeltaInterpreterDiagnosticHelper
import edelta.interpreter.EdeltaInterpreterEdeltaImpl
import edelta.resource.derivedstate.EdeltaDerivedStateHelper
import edelta.validation.EdeltaValidator
import org.apache.log4j.Level
import org.eclipse.emf.ecore.EcoreFactory
import org.eclipse.xtext.testing.InjectWith
import org.eclipse.xtext.testing.XtextRunner
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith

import static org.eclipse.xtext.xbase.XbasePackage.Literals.*
import static org.junit.Assert.*
import static org.mockito.Mockito.*

@RunWith(XtextRunner)
@InjectWith(EdeltaInjectorProviderCustom)
class EdeltaInterpreterEdeltaImplTest extends EdeltaAbstractTest {
var EdeltaInterpreterEdeltaImpl edelta

@Inject EdeltaDerivedStateHelper derivedStateHelper
var EdeltaInterpreterDiagnosticHelper diagnosticHelper

@Before
def void setup() {
edelta = new EdeltaInterpreterEdeltaImpl(#[], derivedStateHelper)
edelta.logger = spy(edelta.logger)
diagnosticHelper = mock(EdeltaInterpreterDiagnosticHelper)
edelta = new EdeltaInterpreterEdeltaImpl(#[], diagnosticHelper)
}

@Test def void testFirstEPackageHasPrecedence() {
val p1 = EcoreFactory.eINSTANCE.createEPackage => [name = "Test"]
val p2 = EcoreFactory.eINSTANCE.createEPackage => [name = "Test"]
edelta = new EdeltaInterpreterEdeltaImpl(#[p1, p2], derivedStateHelper)
edelta = new EdeltaInterpreterEdeltaImpl(#[p1, p2], diagnosticHelper)
assertSame(p1, edelta.getEPackage("Test"))
}

@Test def void testShowErrorWithNullCurrentExpression() {
edelta.showError(null, "an error")
verify(edelta.logger).log(Level.ERROR, ": an error")
@Test def void testShowError() {
val element = EcoreFactory.eINSTANCE.createEClass
edelta.showError(element, "a message")
verify(diagnosticHelper)
.addError(element, EdeltaValidator.LIVE_VALIDATION_ERROR, "a message")
}

@Test def void testShowErrorWithCurrentExpression() {
val input = '''
metamodel "foo"
modifyEcore aTest epackage foo {
val s = null
}
'''
input.parseWithTestEcore => [
val exp = lastModifyEcoreOperation.body.blockLastExpression
edelta.setCurrentExpression(exp)
edelta.showError(null, "an error")
assertError(
XVARIABLE_DECLARATION,
EdeltaValidator.LIVE_VALIDATION_ERROR,
input.lastIndexOf("val s = null"),
"val s = null".length,
"an error"
)
]
}

@Test def void testShowErrorWithDifferentCorrespondingExpression() {
val input = '''
metamodel "foo"
modifyEcore aTest epackage foo {
var s = null
s = null
}
'''
input.parseWithTestEcore => [
// just an ENamedElement to pass to show method as
// the problematic object
val problematic = copiedEPackages.last
// associate it to the first expression in the derived state
val correspondingExpression = lastModifyEcoreOperation.body.blockFirstExpression
derivedStateHelper.getEnamedElementXExpressionMap(eResource)
.put(problematic, correspondingExpression)
val currentExpression = lastModifyEcoreOperation.body.blockLastExpression
edelta.setCurrentExpression(currentExpression)
edelta.showError(problematic, "an error")
// the error is associated to the associated expression to problematic object
// not with the current expression
assertError(
XVARIABLE_DECLARATION,
EdeltaValidator.LIVE_VALIDATION_ERROR,
input.lastIndexOf("val s = null"),
"val s = null".length,
"an error"
)
]
}

@Test def void testShowWarningWithNullCurrentExpression() {
edelta.showWarning(null, "an error")
verify(edelta.logger).log(Level.WARN, ": an error")
}

@Test def void testShowWarningWithCurrentExpression() {
val input = '''
metamodel "foo"
modifyEcore aTest epackage foo {
val s = null
}
'''
input.parseWithTestEcore => [
val exp = lastModifyEcoreOperation.body.blockLastExpression
edelta.setCurrentExpression(exp)
edelta.showWarning(null, "a warning")
assertWarning(
XVARIABLE_DECLARATION,
EdeltaValidator.LIVE_VALIDATION_WARNING,
input.lastIndexOf("val s = null"),
"val s = null".length,
"a warning"
)
]
}

@Test def void testShowWarningWithDifferentCorrespondingExpression() {
val input = '''
metamodel "foo"
modifyEcore aTest epackage foo {
var s = null
s = null
}
'''
input.parseWithTestEcore => [
// just an ENamedElement to pass to show method as
// the problematic object
val problematic = copiedEPackages.last
// associate it to the first expression in the derived state
val correspondingExpression = lastModifyEcoreOperation.body.blockFirstExpression
derivedStateHelper.getEnamedElementXExpressionMap(eResource)
.put(problematic, correspondingExpression)
val currentExpression = lastModifyEcoreOperation.body.blockLastExpression
edelta.setCurrentExpression(currentExpression)
edelta.showWarning(problematic, "a warning")
// the warning is associated to the associated expression to problematic object
// not with the current expression
assertWarning(
XVARIABLE_DECLARATION,
EdeltaValidator.LIVE_VALIDATION_WARNING,
input.lastIndexOf("val s = null"),
"val s = null".length,
"a warning"
)
]
@Test def void testShowWarning() {
val element = EcoreFactory.eINSTANCE.createEClass
edelta.showWarning(element, "a message")
verify(diagnosticHelper)
.addWarning(element, EdeltaValidator.LIVE_VALIDATION_WARNING, "a message")
}

}
Loading

0 comments on commit 6975620

Please sign in to comment.