1
1
package org.modelix.model.mpsadapters
2
2
3
+ import jetbrains.mps.smodel.SNode
4
+ import jetbrains.mps.smodel.adapter.MetaAdapterByDeclaration
3
5
import org.modelix.model.api.BuiltinLanguages
4
6
import org.modelix.model.api.ConceptReference
7
+ import org.modelix.model.api.INode
5
8
import org.modelix.model.api.IReplaceableNode
6
9
7
10
class ReplaceNodeTest : MpsAdaptersTestBase (" SimpleProject" ) {
8
11
9
- fun `test replace node` () = runCommandOnEDT {
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 {
10
17
val rootNode = getRootUnderTest()
11
18
val nodeToReplace = rootNode.allChildren.first() as IReplaceableNode
19
+ val oldContainmentLink = nodeToReplace.getContainmentLink()
20
+ val nodesToKeep = rootNode.allChildren.drop(1 )
12
21
val oldProperties = nodeToReplace.getAllProperties().toSet()
13
22
check(oldProperties.isNotEmpty()) { " Test should replace node with properties." }
14
23
val oldReferences = nodeToReplace.getAllReferenceTargetRefs().toSet()
@@ -19,25 +28,62 @@ class ReplaceNodeTest : MpsAdaptersTestBase("SimpleProject") {
19
28
20
29
val newNode = nodeToReplace.replaceNode(newConcept)
21
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())
22
35
assertEquals(oldProperties, newNode.getAllProperties().toSet())
23
36
assertEquals(oldReferences, newNode.getAllReferenceTargetRefs().toSet())
24
37
assertEquals(oldChildren, newNode.allChildren.toList())
25
- assertEquals(newConcept, newNode.getConceptReference())
26
38
}
27
39
28
- fun `test fail to replace node without parent` () = runCommandOnEDT {
40
+ fun `test replace node without parent but with module (aka root node) ` () = runCommandOnEDT {
29
41
val rootNode = getRootUnderTest()
30
- val oldChildren = rootNode.allChildren.toList()
31
- check(oldChildren.isNotEmpty()) { " Test should replace node with children." }
42
+ val oldContainmentLink = rootNode.getContainmentLink()
43
+ val model = getModelUnderTest()
44
+ val newConcept = ConceptReference (" mps:f3061a53-9226-4cc5-a443-f952ceaf5816/1083245097125" )
32
45
33
- val newConcept = ConceptReference ( BuiltinLanguages .jetbrains_mps_lang_core. BaseConcept .getUID() )
46
+ val newNode = rootNode.replaceNode(newConcept )
34
47
35
- val expectedMessage = " Cannot replace node `Class1` because it has no parent."
36
- assertThrows(IllegalArgumentException ::class .java, expectedMessage) {
37
- rootNode.replaceNode(newConcept)
38
- }
39
- // Assert that precondition is check before children are deleted.
40
- assertEquals(oldChildren, rootNode.allChildren.toList())
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" )
61
+
62
+ val newNode = freeFloatingNode.replaceNode(newConcept)
63
+
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())
41
87
}
42
88
43
89
fun `test fail to replace node with null concept` () = runCommandOnEDT {
@@ -61,11 +107,13 @@ class ReplaceNodeTest : MpsAdaptersTestBase("SimpleProject") {
61
107
}
62
108
}
63
109
64
- private fun getRootUnderTest (): IReplaceableNode {
110
+ private fun getModelUnderTest (): INode {
65
111
val repositoryNode = MPSRepositoryAsNode (mpsProject.repository)
66
112
val module = repositoryNode.getChildren(BuiltinLanguages .MPSRepositoryConcepts .Repository .modules)
67
113
.single { it.getPropertyValue(BuiltinLanguages .jetbrains_mps_lang_core.INamedConcept .name) == " Solution1" }
68
- val model = module.getChildren(BuiltinLanguages .MPSRepositoryConcepts .Module .models).single()
69
- return model.getChildren(BuiltinLanguages .MPSRepositoryConcepts .Model .rootNodes).single() as IReplaceableNode
114
+ return module.getChildren(BuiltinLanguages .MPSRepositoryConcepts .Module .models).single()
70
115
}
116
+
117
+ private fun getRootUnderTest (): IReplaceableNode = getModelUnderTest()
118
+ .getChildren(BuiltinLanguages .MPSRepositoryConcepts .Model .rootNodes).single() as IReplaceableNode
71
119
}
0 commit comments