@@ -17,41 +17,67 @@ 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.lang.editor.imageGen.C_ImageGenerator
30
+ import jetbrains.mps.lang.editor.imageGen.ImageGenerator
27
31
import org.modelix.apigen.test.ApigenTestLanguages
32
+ import org.modelix.metamodel.instanceOf
28
33
import org.modelix.metamodel.typed
34
+ import org.modelix.metamodel.untyped
29
35
import org.modelix.model.api.IBranch
30
36
import org.modelix.model.api.PBranch
31
37
import org.modelix.model.api.getRootNode
32
38
import org.modelix.model.api.resolve
33
39
import org.modelix.model.client.IdGenerator
34
40
import org.modelix.model.lazy.CLTree
35
41
import org.modelix.model.lazy.ObjectStoreCache
36
- import org.modelix.model.persistent.MapBaseStore
42
+ import org.modelix.model.persistent.MapBasedStore
37
43
import org.modelix.model.server.light.LightModelServer
38
44
import org.modelix.modelql.client.ModelQLClient
45
+ import org.modelix.modelql.core.asMono
39
46
import org.modelix.modelql.core.count
47
+ import org.modelix.modelql.core.equalTo
40
48
import org.modelix.modelql.core.filter
49
+ import org.modelix.modelql.core.first
41
50
import org.modelix.modelql.core.map
42
51
import org.modelix.modelql.core.toList
43
52
import org.modelix.modelql.core.toSet
44
53
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
45
56
import org.modelix.modelql.gen.jetbrains.mps.baseLanguage.member
46
57
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
47
60
import org.modelix.modelql.gen.jetbrains.mps.baseLanguage.variableDeclaration
48
61
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
49
66
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
50
71
import org.modelix.modelql.untyped.children
51
72
import org.modelix.modelql.untyped.conceptReference
73
+ import org.modelix.modelql.untyped.descendants
52
74
import kotlin.test.BeforeTest
53
75
import kotlin.test.Test
54
76
import kotlin.test.assertEquals
77
+ import kotlin.test.assertNotEquals
78
+ import kotlin.test.assertNotNull
79
+ import kotlin.test.assertNull
80
+ import kotlin.test.assertTrue
55
81
56
82
class TypedModelQLTest {
57
83
private lateinit var branch: IBranch
@@ -68,7 +94,7 @@ class TypedModelQLTest {
68
94
@BeforeTest
69
95
fun setup () {
70
96
ApigenTestLanguages .registerAll()
71
- val tree = CLTree (ObjectStoreCache (MapBaseStore ()))
97
+ val tree = CLTree (ObjectStoreCache (MapBasedStore ()))
72
98
branch = PBranch (tree, IdGenerator .getInstance(1 ))
73
99
val rootNode = branch.getRootNode()
74
100
branch.runWrite {
@@ -101,6 +127,10 @@ class TypedModelQLTest {
101
127
}
102
128
}
103
129
}
130
+ // Example for optional reference
131
+ rootNode.addNewChild(" imageGen" , - 1 , C_ImageGenerator .untyped())
132
+ .typed<ImageGenerator >()
133
+ .apply { node = cls1 }
104
134
}
105
135
}
106
136
@@ -131,7 +161,7 @@ class TypedModelQLTest {
131
161
}
132
162
133
163
@Test
134
- fun testReferences () = runTest { httpClient ->
164
+ fun `get references` () = runTest { httpClient ->
135
165
val client = ModelQLClient .builder().url(" http://localhost/query" ).httpClient(httpClient).build()
136
166
val usedVariables: Set <String > = client.query { root ->
137
167
root.children(" classes" ).ofConcept(C_ClassConcept )
@@ -147,7 +177,7 @@ class TypedModelQLTest {
147
177
}
148
178
149
179
@Test
150
- fun testReferencesFqName () = runTest { httpClient ->
180
+ fun `get references - fqName` () = runTest { httpClient ->
151
181
val client = ModelQLClient .builder().url(" http://localhost/query" ).httpClient(httpClient).build()
152
182
val usedVariables: Set <String > = client.query { root ->
153
183
root.children(" classes" ).ofConcept(C_ClassConcept )
@@ -170,7 +200,7 @@ class TypedModelQLTest {
170
200
}
171
201
172
202
@Test
173
- fun testNodeSerialization () = runTest { httpClient ->
203
+ fun `node serialization` () = runTest { httpClient ->
174
204
val client = ModelQLClient .builder().url(" http://localhost/query" ).httpClient(httpClient).build()
175
205
val result: List <StaticMethodDeclaration > = client.query { root ->
176
206
root.children(" classes" ).ofConcept(C_ClassConcept )
@@ -184,7 +214,7 @@ class TypedModelQLTest {
184
214
}
185
215
186
216
@Test
187
- fun returnTypedNode () = runTest { httpClient ->
217
+ fun `return typed node` () = runTest { httpClient ->
188
218
val client = ModelQLClient .builder().url(" http://localhost/query" ).httpClient(httpClient).build()
189
219
val result: List <StaticMethodDeclaration > = client.query { root ->
190
220
root.children(" classes" ).ofConcept(C_ClassConcept )
@@ -195,4 +225,149 @@ class TypedModelQLTest {
195
225
}
196
226
assertEquals(" plus" , branch.computeRead { result[0 ].name })
197
227
}
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
+ }
198
373
}
0 commit comments