Skip to content

Commit

Permalink
other tests for refactorings.lib
Browse files Browse the repository at this point in the history
  • Loading branch information
LorenzoBettini committed Dec 11, 2019
1 parent a7905c8 commit 442a7c0
Show file tree
Hide file tree
Showing 2 changed files with 156 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import org.eclipse.emf.ecore.EClass
import org.eclipse.emf.ecore.EPackage
import org.eclipse.emf.ecore.EcoreFactory
import org.eclipse.emf.ecore.EcorePackage
import org.eclipse.emf.ecore.EEnum
import org.eclipse.emf.ecore.EEnumLiteral

abstract class AbstractTest {

Expand All @@ -27,6 +29,26 @@ abstract class AbstractTest {
]
}

def protected createEEnum(EPackage epackage, String name) {
val e = createEEnum(name)
epackage.EClassifiers += e
return e
}

protected def EEnum createEEnum(String name) {
factory.createEEnum => [
it.name = name
]
}

def protected EEnumLiteral createEEnumLiteral(EEnum en, String name) {
val e = factory.createEEnumLiteral => [
it.name = name
]
en.ELiterals += e
return e
}

def protected createEAttribute(EClass eclass, String name) {
val a = factory.createEAttribute => [
it.name = name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import gssi.refactorings.MMrefactorings
import org.junit.Before
import org.junit.Test

import static org.assertj.core.api.Assertions.*
import static extension org.assertj.core.api.Assertions.*
import org.eclipse.emf.ecore.EAttribute
import org.eclipse.emf.ecore.EReference
import org.eclipse.emf.ecore.EClass

class MMrefactoringsTest extends AbstractTest {
var MMrefactorings refactorings
Expand All @@ -25,4 +27,135 @@ class MMrefactoringsTest extends AbstractTest {
.returns(stringDataType, [EAttributeType])
.returns(true, [isRequired])
}

@Test
def void test_mergeReferences() {
val refType = createEClass("RefType")
val c = createEClass("C1") => [
createEReference("ref1") => [
EType = refType
]
createEReference("ref2") => [
EType = refType
]
]
val merged = refactorings.mergeReferences("test", refType,
c.EStructuralFeatures.filter(EReference).toList
)
assertThat(c.EStructuralFeatures).isEmpty
assertThat(merged)
.returns("test", [name])
.returns(refType, [EReferenceType])
}

@Test
def void test_mergeAttributes() {
val c = createEClass("C1") => [
createEAttribute("a1") => [
EType = stringDataType
]
createEAttribute("a2") => [
EType = stringDataType
]
]
val merged = refactorings.mergeAttributes("test", stringDataType,
c.EStructuralFeatures.filter(EAttribute).toList
)
assertThat(c.EStructuralFeatures).isEmpty
assertThat(merged)
.returns("test", [name])
.returns(stringDataType, [EAttributeType])
}

@Test
def void test_introduceSubclasses() {
val p = factory.createEPackage
val enum = p.createEEnum("AnEnum") => [
createEEnumLiteral("Lit1")
createEEnumLiteral("Lit2")
]
val c = p.createEClass("C1") => [
abstract = false
]
val attr = c.createEAttribute("attr") => [
EType = enum
]
refactorings.introduceSubclasses(attr, enum, c)
assertThat(c.isAbstract).isTrue
assertThat(c.EStructuralFeatures).isEmpty
assertThat(p.EClassifiers.filter(EClass))
.hasSize(3)
.allSatisfy[
if (name != "C1") {
assertThat(name).startsWith("Lit")
assertThat(ESuperTypes)
.containsExactly(c)
}
]
}

@Test
def void test_extractSuperclass() {
val p = factory.createEPackage
val superClass = p.createEClass("SuperClass")
val c1 = p.createEClass("C1")
val c2 = p.createEClass("C2")
val attr1 = c1.createEAttribute("attr")
val attr2 = c2.createEAttribute("attr")
assertThat(superClass.EStructuralFeatures).isEmpty
assertThat(c1.EStructuralFeatures).isNotEmpty
assertThat(c2.EStructuralFeatures).isNotEmpty

refactorings.extractSuperclass(superClass, #[attr1, attr2])

assertThat(c1.EStructuralFeatures).isEmpty
assertThat(c2.EStructuralFeatures).isEmpty
assertThat(superClass.EStructuralFeatures)
.containsExactly(attr1)
assertThat(c1.ESuperTypes).containsExactly(superClass)
assertThat(c2.ESuperTypes).containsExactly(superClass)
}

@Test
def void test_extractMetaClass() {
val p = factory.createEPackage
val person = p.createEClass("Person")
val workPlace = p.createEClass("WorkPlace")
val personWorks = person.createEReference("works") => [
lowerBound = 1
]
val workPlacePersons = workPlace.createEReference("persons") => [
EOpposite = personWorks
EType = person
]
personWorks.EType = workPlace
personWorks.EOpposite = workPlacePersons
val workingPosition = p.createEClass("WorkingPosition")

assertThat(workingPosition.EStructuralFeatures).isEmpty
assertThat(workPlace.EStructuralFeatures)
.contains(workPlacePersons)

refactorings.extractMetaClass(workingPosition, personWorks, "position", "works")

assertThat(workingPosition.EStructuralFeatures)
.hasSize(2)
.contains(workPlacePersons)
.anySatisfy[
assertThat
.returns("works", [name])
.returns(workPlace, [EType])
.returns(1, [lowerBound])
]
assertThat(workPlace.EStructuralFeatures)
.hasSize(1)
.doesNotContain(workPlacePersons)
.anySatisfy[
assertThat
.returns("position", [name])
.returns(workingPosition, [EType])
]
assertThat(person.EStructuralFeatures)
.containsExactly(personWorks)
}
}

0 comments on commit 442a7c0

Please sign in to comment.