@@ -17,41 +17,71 @@ import io.ktor.client.HttpClient
17
17
import io.ktor.server.testing.testApplication
18
18
import jetbrains.mps.baseLanguage.C_ClassConcept
19
19
import jetbrains.mps.baseLanguage.C_IntegerType
20
+ import jetbrains.mps.baseLanguage.C_MinusExpression
21
+ import jetbrains.mps.baseLanguage.C_ParameterDeclaration
20
22
import jetbrains.mps.baseLanguage.C_PlusExpression
21
23
import jetbrains.mps.baseLanguage.C_PublicVisibility
22
24
import jetbrains.mps.baseLanguage.C_ReturnStatement
23
25
import jetbrains.mps.baseLanguage.C_StaticMethodDeclaration
24
26
import jetbrains.mps.baseLanguage.C_VariableReference
25
27
import jetbrains.mps.baseLanguage.ClassConcept
26
28
import jetbrains.mps.baseLanguage.StaticMethodDeclaration
29
+ import jetbrains.mps.core.xml.C_XmlComment
30
+ import jetbrains.mps.core.xml.C_XmlCommentLine
31
+ import jetbrains.mps.core.xml.C_XmlDocument
32
+ import jetbrains.mps.core.xml.C_XmlFile
33
+ import jetbrains.mps.lang.editor.imageGen.C_ImageGenerator
34
+ import jetbrains.mps.lang.editor.imageGen.ImageGenerator
27
35
import org.modelix.apigen.test.ApigenTestLanguages
36
+ import org.modelix.metamodel.instanceOf
28
37
import org.modelix.metamodel.typed
38
+ import org.modelix.metamodel.untyped
29
39
import org.modelix.model.api.IBranch
30
40
import org.modelix.model.api.PBranch
31
41
import org.modelix.model.api.getRootNode
32
42
import org.modelix.model.api.resolve
33
43
import org.modelix.model.client.IdGenerator
34
44
import org.modelix.model.lazy.CLTree
35
45
import org.modelix.model.lazy.ObjectStoreCache
36
- import org.modelix.model.persistent.MapBaseStore
46
+ import org.modelix.model.persistent.MapBasedStore
37
47
import org.modelix.model.server.light.LightModelServer
38
48
import org.modelix.modelql.client.ModelQLClient
49
+ import org.modelix.modelql.core.asMono
39
50
import org.modelix.modelql.core.count
51
+ import org.modelix.modelql.core.equalTo
40
52
import org.modelix.modelql.core.filter
53
+ import org.modelix.modelql.core.first
41
54
import org.modelix.modelql.core.map
42
55
import org.modelix.modelql.core.toList
43
56
import org.modelix.modelql.core.toSet
44
57
import org.modelix.modelql.core.zip
58
+ import org.modelix.modelql.gen.jetbrains.mps.baseLanguage.addToMember
59
+ import org.modelix.modelql.gen.jetbrains.mps.baseLanguage.expression
45
60
import org.modelix.modelql.gen.jetbrains.mps.baseLanguage.member
46
61
import org.modelix.modelql.gen.jetbrains.mps.baseLanguage.parameter
62
+ import org.modelix.modelql.gen.jetbrains.mps.baseLanguage.setExpression
63
+ import org.modelix.modelql.gen.jetbrains.mps.baseLanguage.setVariableDeclaration
47
64
import org.modelix.modelql.gen.jetbrains.mps.baseLanguage.variableDeclaration
48
65
import org.modelix.modelql.gen.jetbrains.mps.baseLanguage.visibility
66
+ import org.modelix.modelql.gen.jetbrains.mps.core.xml.addToLines
67
+ import org.modelix.modelql.gen.jetbrains.mps.core.xml.document
68
+ import org.modelix.modelql.gen.jetbrains.mps.core.xml.lines
69
+ import org.modelix.modelql.gen.jetbrains.mps.core.xml.setDocument
49
70
import org.modelix.modelql.gen.jetbrains.mps.lang.core.name
71
+ import org.modelix.modelql.gen.jetbrains.mps.lang.core.setName
72
+ import org.modelix.modelql.gen.jetbrains.mps.lang.editor.imageGen.node
73
+ import org.modelix.modelql.gen.jetbrains.mps.lang.editor.imageGen.node_orNull
74
+ import org.modelix.modelql.gen.jetbrains.mps.lang.editor.imageGen.setNode
50
75
import org.modelix.modelql.untyped.children
51
76
import org.modelix.modelql.untyped.conceptReference
77
+ import org.modelix.modelql.untyped.descendants
52
78
import kotlin.test.BeforeTest
53
79
import kotlin.test.Test
54
80
import kotlin.test.assertEquals
81
+ import kotlin.test.assertNotEquals
82
+ import kotlin.test.assertNotNull
83
+ import kotlin.test.assertNull
84
+ import kotlin.test.assertTrue
55
85
56
86
class TypedModelQLTest {
57
87
private lateinit var branch: IBranch
@@ -68,7 +98,7 @@ class TypedModelQLTest {
68
98
@BeforeTest
69
99
fun setup () {
70
100
ApigenTestLanguages .registerAll()
71
- val tree = CLTree (ObjectStoreCache (MapBaseStore ()))
101
+ val tree = CLTree (ObjectStoreCache (MapBasedStore ()))
72
102
branch = PBranch (tree, IdGenerator .getInstance(1 ))
73
103
val rootNode = branch.getRootNode()
74
104
branch.runWrite {
@@ -101,6 +131,16 @@ class TypedModelQLTest {
101
131
}
102
132
}
103
133
}
134
+ // Example for optional reference
135
+ rootNode.addNewChild(" imageGen" , - 1 , C_ImageGenerator .untyped())
136
+ .typed<ImageGenerator >()
137
+ .apply { node = cls1 }
138
+
139
+ // Example for single non-abstract child
140
+ rootNode.addNewChild(" xmlFile" , - 1 , C_XmlFile .untyped())
141
+
142
+ // Example for mulitple non-abstract child
143
+ rootNode.addNewChild(" xmlComment" , - 1 , C_XmlComment .untyped())
104
144
}
105
145
}
106
146
@@ -131,7 +171,7 @@ class TypedModelQLTest {
131
171
}
132
172
133
173
@Test
134
- fun testReferences () = runTest { httpClient ->
174
+ fun `get references` () = runTest { httpClient ->
135
175
val client = ModelQLClient .builder().url(" http://localhost/query" ).httpClient(httpClient).build()
136
176
val usedVariables: Set <String > = client.query { root ->
137
177
root.children(" classes" ).ofConcept(C_ClassConcept )
@@ -147,7 +187,7 @@ class TypedModelQLTest {
147
187
}
148
188
149
189
@Test
150
- fun testReferencesFqName () = runTest { httpClient ->
190
+ fun `get references - fqName` () = runTest { httpClient ->
151
191
val client = ModelQLClient .builder().url(" http://localhost/query" ).httpClient(httpClient).build()
152
192
val usedVariables: Set <String > = client.query { root ->
153
193
root.children(" classes" ).ofConcept(C_ClassConcept )
@@ -170,7 +210,7 @@ class TypedModelQLTest {
170
210
}
171
211
172
212
@Test
173
- fun testNodeSerialization () = runTest { httpClient ->
213
+ fun `node serialization` () = runTest { httpClient ->
174
214
val client = ModelQLClient .builder().url(" http://localhost/query" ).httpClient(httpClient).build()
175
215
val result: List <StaticMethodDeclaration > = client.query { root ->
176
216
root.children(" classes" ).ofConcept(C_ClassConcept )
@@ -184,7 +224,7 @@ class TypedModelQLTest {
184
224
}
185
225
186
226
@Test
187
- fun returnTypedNode () = runTest { httpClient ->
227
+ fun `return typed node` () = runTest { httpClient ->
188
228
val client = ModelQLClient .builder().url(" http://localhost/query" ).httpClient(httpClient).build()
189
229
val result: List <StaticMethodDeclaration > = client.query { root ->
190
230
root.children(" classes" ).ofConcept(C_ClassConcept )
@@ -195,4 +235,167 @@ class TypedModelQLTest {
195
235
}
196
236
assertEquals(" plus" , branch.computeRead { result[0 ].name })
197
237
}
238
+
239
+ @Test
240
+ fun `set property` () = runTest { httpClient ->
241
+ val client = ModelQLClient .builder().url(" http://localhost/query" ).httpClient(httpClient).build()
242
+ val expected = " myRenamedMethod"
243
+ client.query { root ->
244
+ root.children(" classes" ).ofConcept(C_ClassConcept )
245
+ .member
246
+ .ofConcept(C_StaticMethodDeclaration )
247
+ .first()
248
+ .setName(expected.asMono())
249
+ }
250
+ val actual = client.query { root ->
251
+ root.children(" classes" ).ofConcept(C_ClassConcept )
252
+ .member
253
+ .ofConcept(C_StaticMethodDeclaration )
254
+ .first()
255
+ }
256
+ assertEquals(expected, actual.name)
257
+ }
258
+
259
+ @Test
260
+ fun `set reference` () = runTest { httpClient ->
261
+ val client = ModelQLClient .builder().url(" http://localhost/query" ).httpClient(httpClient).build()
262
+
263
+ val oldValue = client.query { root ->
264
+ root.children(" classes" ).ofConcept(C_ClassConcept )
265
+ .member
266
+ .ofConcept(C_StaticMethodDeclaration )
267
+ .descendants()
268
+ .ofConcept(C_ParameterDeclaration )
269
+ .first()
270
+ .name
271
+ }
272
+ val expected = " b"
273
+ client.query { root ->
274
+ val descendants = root.children(" classes" ).ofConcept(C_ClassConcept )
275
+ .member
276
+ .ofConcept(C_StaticMethodDeclaration )
277
+ .descendants()
278
+
279
+ val target = descendants.ofConcept(C_ParameterDeclaration )
280
+ .filter { it.name.equalTo(expected) }
281
+ .first()
282
+
283
+ descendants.ofConcept(C_VariableReference )
284
+ .first()
285
+ .setVariableDeclaration(target)
286
+ }
287
+
288
+ val actual = client.query { root ->
289
+ root.children(" classes" ).ofConcept(C_ClassConcept )
290
+ .member
291
+ .ofConcept(C_StaticMethodDeclaration )
292
+ .descendants()
293
+ .ofConcept(C_VariableReference )
294
+ .first()
295
+ }
296
+ assertNotEquals(expected, oldValue)
297
+ assertEquals(expected, actual.variableDeclaration.name)
298
+ }
299
+
300
+ @Test
301
+ fun `set reference - null` () = runTest { httpClient ->
302
+ val client = ModelQLClient .builder().url(" http://localhost/query" ).httpClient(httpClient).build()
303
+ val oldValue = client.query { root ->
304
+ root.children(" imageGen" ).ofConcept(C_ImageGenerator ).first().node
305
+ }
306
+
307
+ client.query { root ->
308
+ root.children(" imageGen" ).ofConcept(C_ImageGenerator ).first().setNode(null )
309
+ }
310
+
311
+ val actual = client.query { root ->
312
+ root.children(" imageGen" ).ofConcept(C_ImageGenerator ).first().node_orNull
313
+ }
314
+ assertNotNull(oldValue)
315
+ assertNull(actual)
316
+ }
317
+
318
+ @Test
319
+ fun `add new child` () = runTest { httpClient ->
320
+ val client = ModelQLClient .builder().url(" http://localhost/query" ).httpClient(httpClient).build()
321
+
322
+ val oldNumChildren = client.query { root ->
323
+ root.children(" classes" ).ofConcept(C_ClassConcept )
324
+ .first()
325
+ .member
326
+ .ofConcept(C_StaticMethodDeclaration )
327
+ .count()
328
+ }
329
+
330
+ client.query { root ->
331
+ root.children(" classes" ).ofConcept(C_ClassConcept )
332
+ .first()
333
+ .addToMember(C_StaticMethodDeclaration )
334
+ }
335
+
336
+ val children = client.query { root ->
337
+ root.children(" classes" ).ofConcept(C_ClassConcept )
338
+ .first()
339
+ .member
340
+ .ofConcept(C_StaticMethodDeclaration )
341
+ .toList()
342
+ }
343
+
344
+ assertEquals(oldNumChildren + 1 , children.size)
345
+ assertEquals(C_StaticMethodDeclaration .untyped().getUID(), children.last().untyped().concept?.getUID())
346
+ }
347
+
348
+ @Test
349
+ fun `add new child - default concept` () = runTest { httpClient ->
350
+ val client = ModelQLClient .builder().url(" http://localhost/query" ).httpClient(httpClient).build()
351
+ client.query { root ->
352
+ root.descendants().ofConcept(C_XmlComment )
353
+ .first()
354
+ .addToLines()
355
+ }
356
+
357
+ val actual = client.query { root ->
358
+ root.descendants().ofConcept(C_XmlComment )
359
+ .first()
360
+ .lines
361
+ .first()
362
+ }
363
+
364
+ assertTrue { actual.instanceOf(C_XmlCommentLine ) }
365
+ }
366
+
367
+ @Test
368
+ fun `set child` () = runTest { httpClient ->
369
+ val client = ModelQLClient .builder().url(" http://localhost/query" ).httpClient(httpClient).build()
370
+
371
+ client.query { root ->
372
+ root.descendants().ofConcept(C_ReturnStatement )
373
+ .first()
374
+ .setExpression(C_MinusExpression )
375
+ }
376
+
377
+ val actual = client.query { root ->
378
+ root.descendants().ofConcept(C_ReturnStatement ).first().expression
379
+ }
380
+ assertNotNull(actual)
381
+ assertTrue(actual.instanceOf(C_MinusExpression ))
382
+ }
383
+
384
+ @Test
385
+ fun `set child - default concept` () = runTest { httpClient ->
386
+ val client = ModelQLClient .builder().url(" http://localhost/query" ).httpClient(httpClient).build()
387
+ client.query { root ->
388
+ root.descendants().ofConcept(C_XmlFile )
389
+ .first()
390
+ .setDocument()
391
+ }
392
+
393
+ val actual = client.query { root ->
394
+ root.descendants().ofConcept(C_XmlFile )
395
+ .first()
396
+ .document
397
+ }
398
+
399
+ assertTrue { actual.instanceOf(C_XmlDocument ) }
400
+ }
198
401
}
0 commit comments