Skip to content

Commit 8233140

Browse files
committed
test(model-api-gen): add test for modelql write support
1 parent 12682cc commit 8233140

File tree

1 file changed

+181
-6
lines changed
  • model-api-gen-gradle-test/kotlin-generation/src/test/kotlin/org/modelix/modelql/typed

1 file changed

+181
-6
lines changed

model-api-gen-gradle-test/kotlin-generation/src/test/kotlin/org/modelix/modelql/typed/TypedModelQLTest.kt

Lines changed: 181 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,41 +17,67 @@ import io.ktor.client.HttpClient
1717
import io.ktor.server.testing.testApplication
1818
import jetbrains.mps.baseLanguage.C_ClassConcept
1919
import jetbrains.mps.baseLanguage.C_IntegerType
20+
import jetbrains.mps.baseLanguage.C_MinusExpression
21+
import jetbrains.mps.baseLanguage.C_ParameterDeclaration
2022
import jetbrains.mps.baseLanguage.C_PlusExpression
2123
import jetbrains.mps.baseLanguage.C_PublicVisibility
2224
import jetbrains.mps.baseLanguage.C_ReturnStatement
2325
import jetbrains.mps.baseLanguage.C_StaticMethodDeclaration
2426
import jetbrains.mps.baseLanguage.C_VariableReference
2527
import jetbrains.mps.baseLanguage.ClassConcept
2628
import jetbrains.mps.baseLanguage.StaticMethodDeclaration
29+
import jetbrains.mps.lang.editor.imageGen.C_ImageGenerator
30+
import jetbrains.mps.lang.editor.imageGen.ImageGenerator
2731
import org.modelix.apigen.test.ApigenTestLanguages
32+
import org.modelix.metamodel.instanceOf
2833
import org.modelix.metamodel.typed
34+
import org.modelix.metamodel.untyped
2935
import org.modelix.model.api.IBranch
3036
import org.modelix.model.api.PBranch
3137
import org.modelix.model.api.getRootNode
3238
import org.modelix.model.api.resolve
3339
import org.modelix.model.client.IdGenerator
3440
import org.modelix.model.lazy.CLTree
3541
import org.modelix.model.lazy.ObjectStoreCache
36-
import org.modelix.model.persistent.MapBaseStore
42+
import org.modelix.model.persistent.MapBasedStore
3743
import org.modelix.model.server.light.LightModelServer
3844
import org.modelix.modelql.client.ModelQLClient
45+
import org.modelix.modelql.core.asMono
3946
import org.modelix.modelql.core.count
47+
import org.modelix.modelql.core.equalTo
4048
import org.modelix.modelql.core.filter
49+
import org.modelix.modelql.core.first
4150
import org.modelix.modelql.core.map
4251
import org.modelix.modelql.core.toList
4352
import org.modelix.modelql.core.toSet
4453
import org.modelix.modelql.core.zip
54+
import org.modelix.modelql.gen.jetbrains.mps.baseLanguage.addToMember
55+
import org.modelix.modelql.gen.jetbrains.mps.baseLanguage.expression
4556
import org.modelix.modelql.gen.jetbrains.mps.baseLanguage.member
4657
import org.modelix.modelql.gen.jetbrains.mps.baseLanguage.parameter
58+
import org.modelix.modelql.gen.jetbrains.mps.baseLanguage.setExpression
59+
import org.modelix.modelql.gen.jetbrains.mps.baseLanguage.setVariableDeclaration
4760
import org.modelix.modelql.gen.jetbrains.mps.baseLanguage.variableDeclaration
4861
import org.modelix.modelql.gen.jetbrains.mps.baseLanguage.visibility
62+
import org.modelix.modelql.gen.jetbrains.mps.core.xml.addToLines
63+
import org.modelix.modelql.gen.jetbrains.mps.core.xml.document
64+
import org.modelix.modelql.gen.jetbrains.mps.core.xml.lines
65+
import org.modelix.modelql.gen.jetbrains.mps.core.xml.setDocument
4966
import org.modelix.modelql.gen.jetbrains.mps.lang.core.name
67+
import org.modelix.modelql.gen.jetbrains.mps.lang.core.setName
68+
import org.modelix.modelql.gen.jetbrains.mps.lang.editor.imageGen.node
69+
import org.modelix.modelql.gen.jetbrains.mps.lang.editor.imageGen.node_orNull
70+
import org.modelix.modelql.gen.jetbrains.mps.lang.editor.imageGen.setNode
5071
import org.modelix.modelql.untyped.children
5172
import org.modelix.modelql.untyped.conceptReference
73+
import org.modelix.modelql.untyped.descendants
5274
import kotlin.test.BeforeTest
5375
import kotlin.test.Test
5476
import kotlin.test.assertEquals
77+
import kotlin.test.assertNotEquals
78+
import kotlin.test.assertNotNull
79+
import kotlin.test.assertNull
80+
import kotlin.test.assertTrue
5581

5682
class TypedModelQLTest {
5783
private lateinit var branch: IBranch
@@ -68,7 +94,7 @@ class TypedModelQLTest {
6894
@BeforeTest
6995
fun setup() {
7096
ApigenTestLanguages.registerAll()
71-
val tree = CLTree(ObjectStoreCache(MapBaseStore()))
97+
val tree = CLTree(ObjectStoreCache(MapBasedStore()))
7298
branch = PBranch(tree, IdGenerator.getInstance(1))
7399
val rootNode = branch.getRootNode()
74100
branch.runWrite {
@@ -101,6 +127,10 @@ class TypedModelQLTest {
101127
}
102128
}
103129
}
130+
// Example for optional reference
131+
rootNode.addNewChild("imageGen", -1, C_ImageGenerator.untyped())
132+
.typed<ImageGenerator>()
133+
.apply { node = cls1 }
104134
}
105135
}
106136

@@ -131,7 +161,7 @@ class TypedModelQLTest {
131161
}
132162

133163
@Test
134-
fun testReferences() = runTest { httpClient ->
164+
fun `get references`() = runTest { httpClient ->
135165
val client = ModelQLClient.builder().url("http://localhost/query").httpClient(httpClient).build()
136166
val usedVariables: Set<String> = client.query { root ->
137167
root.children("classes").ofConcept(C_ClassConcept)
@@ -147,7 +177,7 @@ class TypedModelQLTest {
147177
}
148178

149179
@Test
150-
fun testReferencesFqName() = runTest { httpClient ->
180+
fun `get references - fqName`() = runTest { httpClient ->
151181
val client = ModelQLClient.builder().url("http://localhost/query").httpClient(httpClient).build()
152182
val usedVariables: Set<String> = client.query { root ->
153183
root.children("classes").ofConcept(C_ClassConcept)
@@ -170,7 +200,7 @@ class TypedModelQLTest {
170200
}
171201

172202
@Test
173-
fun testNodeSerialization() = runTest { httpClient ->
203+
fun `node serialization`() = runTest { httpClient ->
174204
val client = ModelQLClient.builder().url("http://localhost/query").httpClient(httpClient).build()
175205
val result: List<StaticMethodDeclaration> = client.query { root ->
176206
root.children("classes").ofConcept(C_ClassConcept)
@@ -184,7 +214,7 @@ class TypedModelQLTest {
184214
}
185215

186216
@Test
187-
fun returnTypedNode() = runTest { httpClient ->
217+
fun `return typed node`() = runTest { httpClient ->
188218
val client = ModelQLClient.builder().url("http://localhost/query").httpClient(httpClient).build()
189219
val result: List<StaticMethodDeclaration> = client.query { root ->
190220
root.children("classes").ofConcept(C_ClassConcept)
@@ -195,4 +225,149 @@ class TypedModelQLTest {
195225
}
196226
assertEquals("plus", branch.computeRead { result[0].name })
197227
}
228+
229+
@Test
230+
fun `set property`() = runTest { httpClient ->
231+
val client = ModelQLClient.builder().url("http://localhost/query").httpClient(httpClient).build()
232+
val expected = "myRenamedMethod"
233+
client.query { root ->
234+
root.children("classes").ofConcept(C_ClassConcept)
235+
.member
236+
.ofConcept(C_StaticMethodDeclaration)
237+
.first()
238+
.setName(expected.asMono())
239+
}
240+
val actual = client.query { root ->
241+
root.children("classes").ofConcept(C_ClassConcept)
242+
.member
243+
.ofConcept(C_StaticMethodDeclaration)
244+
.first()
245+
}
246+
assertEquals(expected, actual.name)
247+
}
248+
249+
@Test
250+
fun `set reference`() = runTest { httpClient ->
251+
val client = ModelQLClient.builder().url("http://localhost/query").httpClient(httpClient).build()
252+
253+
val oldValue = client.query { root ->
254+
root.children("classes").ofConcept(C_ClassConcept)
255+
.member
256+
.ofConcept(C_StaticMethodDeclaration)
257+
.descendants()
258+
.ofConcept(C_ParameterDeclaration)
259+
.first()
260+
.name
261+
}
262+
val expected = "b"
263+
client.query { root ->
264+
val descendants = root.children("classes").ofConcept(C_ClassConcept)
265+
.member
266+
.ofConcept(C_StaticMethodDeclaration)
267+
.descendants()
268+
269+
val target = descendants.ofConcept(C_ParameterDeclaration)
270+
.filter { it.name.equalTo(expected) }
271+
.first()
272+
273+
descendants.ofConcept(C_VariableReference)
274+
.first()
275+
.setVariableDeclaration(target)
276+
}
277+
278+
val actual = client.query { root ->
279+
root.children("classes").ofConcept(C_ClassConcept)
280+
.member
281+
.ofConcept(C_StaticMethodDeclaration)
282+
.descendants()
283+
.ofConcept(C_VariableReference)
284+
.first()
285+
}
286+
assertNotEquals(expected, oldValue)
287+
assertEquals(expected, actual.variableDeclaration.name)
288+
}
289+
290+
@Test
291+
fun `set reference - null`() = runTest { httpClient ->
292+
val client = ModelQLClient.builder().url("http://localhost/query").httpClient(httpClient).build()
293+
val oldValue = client.query { root ->
294+
root.children("imageGen").ofConcept(C_ImageGenerator).first().node
295+
}
296+
297+
client.query { root ->
298+
root.children("imageGen").ofConcept(C_ImageGenerator).first().setNode(null)
299+
}
300+
301+
val actual = client.query { root ->
302+
root.children("imageGen").ofConcept(C_ImageGenerator).first().node_orNull
303+
}
304+
assertNotNull(oldValue)
305+
assertNull(actual)
306+
}
307+
308+
@Test
309+
fun `add new child`() = runTest { httpClient ->
310+
val client = ModelQLClient.builder().url("http://localhost/query").httpClient(httpClient).build()
311+
312+
val oldNumChildren = client.query { root ->
313+
root.children("classes").ofConcept(C_ClassConcept)
314+
.first()
315+
.member
316+
.ofConcept(C_StaticMethodDeclaration)
317+
.count()
318+
}
319+
320+
client.query { root ->
321+
root.children("classes").ofConcept(C_ClassConcept)
322+
.first()
323+
.addToMember(C_StaticMethodDeclaration)
324+
}
325+
326+
val children = client.query { root ->
327+
root.children("classes").ofConcept(C_ClassConcept)
328+
.first()
329+
.member
330+
.ofConcept(C_StaticMethodDeclaration)
331+
.toList()
332+
}
333+
334+
assertEquals(oldNumChildren + 1, children.size)
335+
assertEquals(C_StaticMethodDeclaration.untyped().getUID(), children.last().untyped().concept?.getUID())
336+
}
337+
338+
@Test
339+
fun `add new child - default concept`() = runTest { httpClient ->
340+
val client = ModelQLClient.builder().url("http://localhost/query").httpClient(httpClient).build()
341+
client.query { root ->
342+
root.descendants().ofConcept(C_XmlComment)
343+
.first()
344+
.addToLines()
345+
}
346+
347+
val actual = client.query { root ->
348+
root.descendants().ofConcept(C_XmlComment)
349+
.first()
350+
.lines
351+
.first()
352+
}
353+
354+
assertTrue { actual.instanceOf(C_XmlCommentLine) }
355+
}
356+
357+
@Test
358+
fun `set child`() = runTest { httpClient ->
359+
val client = ModelQLClient.builder().url("http://localhost/query").httpClient(httpClient).build()
360+
361+
client.query { root ->
362+
root.descendants().ofConcept(C_ReturnStatement)
363+
.first()
364+
.setExpression(C_MinusExpression)
365+
}
366+
367+
val actual = client.query { root ->
368+
root.descendants().ofConcept(C_ReturnStatement).first().expression
369+
}
370+
assertNotNull(actual)
371+
assertTrue(actual.instanceOf(C_MinusExpression))
372+
}
198373
}

0 commit comments

Comments
 (0)