|
1 | 1 | package org.modelix.model.mpsadapters
|
2 | 2 |
|
3 |
| -import org.junit.Ignore |
| 3 | +import jetbrains.mps.smodel.SNode |
| 4 | +import jetbrains.mps.smodel.adapter.MetaAdapterByDeclaration |
4 | 5 | import org.modelix.model.api.BuiltinLanguages
|
5 | 6 | import org.modelix.model.api.ConceptReference
|
6 | 7 | import org.modelix.model.api.INode
|
7 | 8 | import org.modelix.model.api.IReplaceableNode
|
8 | 9 |
|
9 |
| -@Ignore("Replacing a node through MPS-model-adapters is broken. See MODELIX-920") |
10 | 10 | class ReplaceNodeTest : MpsAdaptersTestBase("SimpleProject") {
|
11 | 11 |
|
12 |
| - fun testReplaceNode() { |
13 |
| - readAction { |
14 |
| - assertEquals(1, mpsProject.projectModules.size) |
15 |
| - } |
| 12 | + private val sampleConcept = MetaAdapterByDeclaration.asInstanceConcept( |
| 13 | + MPSConcept.tryParseUID(BuiltinLanguages.jetbrains_mps_lang_core.BaseConcept.getUID())!!.concept, |
| 14 | + ) |
| 15 | + |
| 16 | + fun `test replace node with parent and module (aka regular node)`() = runCommandOnEDT { |
| 17 | + val rootNode = getRootUnderTest() |
| 18 | + val nodeToReplace = rootNode.allChildren.first() as IReplaceableNode |
| 19 | + val oldContainmentLink = nodeToReplace.getContainmentLink() |
| 20 | + val nodesToKeep = rootNode.allChildren.drop(1) |
| 21 | + val oldProperties = nodeToReplace.getAllProperties().toSet() |
| 22 | + check(oldProperties.isNotEmpty()) { "Test should replace node with properties." } |
| 23 | + val oldReferences = nodeToReplace.getAllReferenceTargetRefs().toSet() |
| 24 | + check(oldReferences.isNotEmpty()) { "Test should replace node with references." } |
| 25 | + val oldChildren = nodeToReplace.allChildren.toList() |
| 26 | + check(oldChildren.isNotEmpty()) { "Test should replace node with children." } |
| 27 | + val newConcept = ConceptReference("mps:f3061a53-9226-4cc5-a443-f952ceaf5816/1083245097125") |
| 28 | + |
| 29 | + val newNode = nodeToReplace.replaceNode(newConcept) |
| 30 | + |
| 31 | + assertEquals(listOf(newNode) + nodesToKeep, rootNode.allChildren.toList()) |
| 32 | + assertEquals((nodeToReplace as MPSNode).node.nodeId, (newNode as MPSNode).node.nodeId) |
| 33 | + assertEquals(oldContainmentLink, newNode.getContainmentLink()) |
| 34 | + assertEquals(newConcept, newNode.getConceptReference()) |
| 35 | + assertEquals(oldProperties, newNode.getAllProperties().toSet()) |
| 36 | + assertEquals(oldReferences, newNode.getAllReferenceTargetRefs().toSet()) |
| 37 | + assertEquals(oldChildren, newNode.allChildren.toList()) |
| 38 | + } |
| 39 | + |
| 40 | + fun `test replace node without parent but with module (aka root node)`() = runCommandOnEDT { |
| 41 | + val rootNode = getRootUnderTest() |
| 42 | + val oldContainmentLink = rootNode.getContainmentLink() |
| 43 | + val model = getModelUnderTest() |
| 44 | + val newConcept = ConceptReference("mps:f3061a53-9226-4cc5-a443-f952ceaf5816/1083245097125") |
| 45 | + |
| 46 | + val newNode = rootNode.replaceNode(newConcept) |
| 47 | + |
| 48 | + assertEquals(listOf(newNode), model.getChildren(BuiltinLanguages.MPSRepositoryConcepts.Model.rootNodes)) |
| 49 | + assertEquals((rootNode as MPSNode).node.nodeId, (newNode as MPSNode).node.nodeId) |
| 50 | + assertEquals(oldContainmentLink, newNode.getContainmentLink()) |
| 51 | + assertEquals(newConcept, newNode.getConceptReference()) |
| 52 | + } |
| 53 | + |
| 54 | + fun `test replace node without parent and without module (aka free-floating node)`() = runCommandOnEDT { |
| 55 | + val untouchedRootNode = getRootUnderTest() |
| 56 | + val model = getModelUnderTest() |
| 57 | + val freeFloatingSNode = SNode(sampleConcept) |
| 58 | + val freeFloatingNode = MPSNode(freeFloatingSNode) |
| 59 | + val oldContainmentLink = freeFloatingNode.getContainmentLink() |
| 60 | + val newConcept = ConceptReference("mps:f3061a53-9226-4cc5-a443-f952ceaf5816/1083245097125") |
16 | 61 |
|
17 |
| - val repositoryNode: INode = MPSRepositoryAsNode(mpsProject.repository) |
| 62 | + val newNode = freeFloatingNode.replaceNode(newConcept) |
18 | 63 |
|
19 |
| - runCommandOnEDT { |
20 |
| - val module = repositoryNode.getChildren(BuiltinLanguages.MPSRepositoryConcepts.Repository.modules) |
21 |
| - .single { it.getPropertyValue(BuiltinLanguages.jetbrains_mps_lang_core.INamedConcept.name) == "Solution1" } |
22 |
| - val model = module.getChildren(BuiltinLanguages.MPSRepositoryConcepts.Module.models) |
23 |
| - .single { it.getPropertyValue(BuiltinLanguages.jetbrains_mps_lang_core.INamedConcept.name) == "Solution1.model1" } |
| 64 | + assertEquals(listOf(untouchedRootNode), model.getChildren(BuiltinLanguages.MPSRepositoryConcepts.Model.rootNodes)) |
| 65 | + assertEquals(freeFloatingNode.node.nodeId, (newNode as MPSNode).node.nodeId) |
| 66 | + assertEquals(oldContainmentLink, newNode.getContainmentLink()) |
| 67 | + assertEquals(newConcept, newNode.getConceptReference()) |
| 68 | + } |
| 69 | + |
| 70 | + fun `test replace node with parent but without module (aka descendant of free-floating node)`() = runCommandOnEDT { |
| 71 | + val freeFloatingSNode = SNode(sampleConcept) |
| 72 | + val freeFloatingNode = MPSNode(freeFloatingSNode) |
| 73 | + val nodeToReplace = freeFloatingNode.addNewChild( |
| 74 | + BuiltinLanguages.MPSRepositoryConcepts.Model.usedLanguages, |
| 75 | + -1, |
| 76 | + BuiltinLanguages.jetbrains_mps_lang_core.BaseConcept, |
| 77 | + ) as IReplaceableNode |
| 78 | + val oldContainmentLink = nodeToReplace.getContainmentLink() |
| 79 | + val newConcept = ConceptReference("mps:f3061a53-9226-4cc5-a443-f952ceaf5816/1083245097125") |
| 80 | + |
| 81 | + val newNode = nodeToReplace.replaceNode(newConcept) |
| 82 | + |
| 83 | + assertEquals(listOf(newNode), freeFloatingNode.allChildren.toList()) |
| 84 | + assertEquals((nodeToReplace as MPSNode).node.nodeId, (newNode as MPSNode).node.nodeId) |
| 85 | + assertEquals(oldContainmentLink, newNode.getContainmentLink()) |
| 86 | + assertEquals(newConcept, newNode.getConceptReference()) |
| 87 | + } |
24 | 88 |
|
25 |
| - val rootNode = model.getChildren(BuiltinLanguages.MPSRepositoryConcepts.Model.rootNodes).single() as IReplaceableNode |
| 89 | + fun `test fail to replace node with null concept`() = runCommandOnEDT { |
| 90 | + val rootNode = getRootUnderTest() |
| 91 | + val nodeToReplace = rootNode.allChildren.first() as IReplaceableNode |
26 | 92 |
|
27 |
| - val oldProperties = rootNode.getAllProperties().toSet() |
28 |
| - val oldReferences = rootNode.getAllReferenceTargetRefs().toSet() |
29 |
| - val oldChildren = rootNode.allChildren.toList() |
| 93 | + val expectedMessage = "Cannot replace node `method1` with a null concept. Explicitly specify a concept (e.g., `BaseConcept`)." |
| 94 | + assertThrows(IllegalArgumentException::class.java, expectedMessage) { |
| 95 | + nodeToReplace.replaceNode(null) |
| 96 | + } |
| 97 | + } |
30 | 98 |
|
31 |
| - val newConcept = ConceptReference("mps:f3061a53-9226-4cc5-a443-f952ceaf5816/1083245097125") |
32 |
| - val newNode = rootNode.replaceNode(newConcept) |
| 99 | + fun `test fail to replace node with non mps concept`() = runCommandOnEDT { |
| 100 | + val rootNode = getRootUnderTest() |
| 101 | + val nodeToReplace = rootNode.allChildren.first() as IReplaceableNode |
| 102 | + val newConcept = ConceptReference("notMpsConcept") |
33 | 103 |
|
34 |
| - assertEquals(oldProperties, newNode.getAllProperties().toSet()) |
35 |
| - assertEquals(oldReferences, newNode.getAllReferenceTargetRefs().toSet()) |
36 |
| - assertEquals(oldChildren, newNode.allChildren.toList()) |
37 |
| - assertEquals(newConcept, newNode.getConceptReference()) |
| 104 | + val expectedMessage = "Concept UID `notMpsConcept` cannot be parsed as MPS concept." |
| 105 | + assertThrows(IllegalArgumentException::class.java, expectedMessage) { |
| 106 | + nodeToReplace.replaceNode(newConcept) |
38 | 107 | }
|
39 | 108 | }
|
| 109 | + |
| 110 | + private fun getModelUnderTest(): INode { |
| 111 | + val repositoryNode = MPSRepositoryAsNode(mpsProject.repository) |
| 112 | + val module = repositoryNode.getChildren(BuiltinLanguages.MPSRepositoryConcepts.Repository.modules) |
| 113 | + .single { it.getPropertyValue(BuiltinLanguages.jetbrains_mps_lang_core.INamedConcept.name) == "Solution1" } |
| 114 | + return module.getChildren(BuiltinLanguages.MPSRepositoryConcepts.Module.models).single() |
| 115 | + } |
| 116 | + |
| 117 | + private fun getRootUnderTest(): IReplaceableNode = getModelUnderTest() |
| 118 | + .getChildren(BuiltinLanguages.MPSRepositoryConcepts.Model.rootNodes).single() as IReplaceableNode |
40 | 119 | }
|
0 commit comments