13
13
*/
14
14
package org.modelix.modelql.typed
15
15
16
- import io.ktor.client.HttpClient
17
- import io.ktor.server.testing.testApplication
18
16
import jetbrains.mps.baseLanguage.C_ClassConcept
19
17
import jetbrains.mps.baseLanguage.C_IntegerType
20
18
import jetbrains.mps.baseLanguage.C_MinusExpression
@@ -32,19 +30,12 @@ import jetbrains.mps.core.xml.C_XmlDocument
32
30
import jetbrains.mps.core.xml.C_XmlFile
33
31
import jetbrains.mps.lang.editor.imageGen.C_ImageGenerator
34
32
import jetbrains.mps.lang.editor.imageGen.ImageGenerator
35
- import org.modelix.apigen.test.ApigenTestLanguages
36
33
import org.modelix.metamodel.instanceOf
37
34
import org.modelix.metamodel.typed
38
35
import org.modelix.metamodel.untyped
39
- import org.modelix.model.api.IBranch
40
- import org.modelix.model.api.PBranch
41
- import org.modelix.model.api.getRootNode
36
+ import org.modelix.model.api.INode
37
+ import org.modelix.model.api.remove
42
38
import org.modelix.model.api.resolve
43
- import org.modelix.model.client.IdGenerator
44
- import org.modelix.model.lazy.CLTree
45
- import org.modelix.model.lazy.ObjectStoreCache
46
- import org.modelix.model.persistent.MapBasedStore
47
- import org.modelix.model.server.light.LightModelServer
48
39
import org.modelix.modelql.client.ModelQLClient
49
40
import org.modelix.modelql.core.asMono
50
41
import org.modelix.modelql.core.count
@@ -75,78 +66,62 @@ import org.modelix.modelql.gen.jetbrains.mps.lang.editor.imageGen.setNode
75
66
import org.modelix.modelql.untyped.children
76
67
import org.modelix.modelql.untyped.conceptReference
77
68
import org.modelix.modelql.untyped.descendants
78
- import kotlin.test.BeforeTest
79
69
import kotlin.test.Test
80
70
import kotlin.test.assertEquals
81
71
import kotlin.test.assertNotEquals
82
72
import kotlin.test.assertNotNull
83
73
import kotlin.test.assertNull
84
74
import kotlin.test.assertTrue
85
75
86
- class TypedModelQLTest {
87
- private lateinit var branch: IBranch
88
-
89
- private fun runTest (block : suspend (HttpClient ) -> Unit ) = testApplication {
90
- application {
91
- LightModelServer (80 , branch.getRootNode()).apply { installHandlers() }
92
- }
93
- val httpClient = createClient {
94
- }
95
- block(httpClient)
96
- }
97
-
98
- @BeforeTest
99
- fun setup () {
100
- ApigenTestLanguages .registerAll()
101
- val tree = CLTree (ObjectStoreCache (MapBasedStore ()))
102
- branch = PBranch (tree, IdGenerator .getInstance(1 ))
103
- val rootNode = branch.getRootNode()
104
- branch.runWrite {
105
- val cls1 = rootNode.addNewChild(" classes" , - 1 , C_ClassConcept .untyped()).typed<ClassConcept >()
106
- cls1.apply {
107
- name = " Math"
108
- member.addNew(C_StaticMethodDeclaration ).apply {
109
- name = " plus"
110
- returnType.setNew(C_IntegerType )
111
- visibility.setNew(C_PublicVisibility )
112
- val a = parameter.addNew().apply {
113
- name = " a"
114
- type.setNew(C_IntegerType )
115
- }
116
- val b = parameter.addNew().apply {
117
- name = " b"
118
- type.setNew(C_IntegerType )
119
- }
120
- body.setNew().apply {
121
- statement.addNew(C_ReturnStatement ).apply {
122
- expression.setNew(C_PlusExpression ).apply {
123
- leftExpression.setNew(C_VariableReference ).apply {
124
- variableDeclaration = a
125
- }
126
- rightExpression.setNew(C_VariableReference ).apply {
127
- variableDeclaration = b
128
- }
76
+ abstract class TypedModelQLTest {
77
+
78
+ abstract fun runTest (block : suspend (ModelQLClient ) -> Unit )
79
+
80
+ protected fun createTestData (rootNode : INode ) {
81
+ rootNode.allChildren.forEach { it.remove() }
82
+ val cls1 = rootNode.addNewChild(" classes" , - 1 , C_ClassConcept .untyped()).typed<ClassConcept >()
83
+ cls1.apply {
84
+ name = " Math"
85
+ member.addNew(C_StaticMethodDeclaration ).apply {
86
+ name = " plus"
87
+ returnType.setNew(C_IntegerType )
88
+ visibility.setNew(C_PublicVisibility )
89
+ val a = parameter.addNew().apply {
90
+ name = " a"
91
+ type.setNew(C_IntegerType )
92
+ }
93
+ val b = parameter.addNew().apply {
94
+ name = " b"
95
+ type.setNew(C_IntegerType )
96
+ }
97
+ body.setNew().apply {
98
+ statement.addNew(C_ReturnStatement ).apply {
99
+ expression.setNew(C_PlusExpression ).apply {
100
+ leftExpression.setNew(C_VariableReference ).apply {
101
+ variableDeclaration = a
102
+ }
103
+ rightExpression.setNew(C_VariableReference ).apply {
104
+ variableDeclaration = b
129
105
}
130
106
}
131
107
}
132
108
}
133
109
}
134
- // Example for optional reference
135
- rootNode.addNewChild(" imageGen" , - 1 , C_ImageGenerator .untyped())
136
- .typed<ImageGenerator >()
137
- .apply { node = cls1 }
110
+ }
111
+ // Example for optional reference
112
+ rootNode.addNewChild(" imageGen" , - 1 , C_ImageGenerator .untyped())
113
+ .typed<ImageGenerator >()
114
+ .apply { node = cls1 }
138
115
139
- // Example for single non-abstract child
140
- rootNode.addNewChild(" xmlFile" , - 1 , C_XmlFile .untyped())
116
+ // Example for single non-abstract child
117
+ rootNode.addNewChild(" xmlFile" , - 1 , C_XmlFile .untyped())
141
118
142
- // Example for mulitple non-abstract child
143
- rootNode.addNewChild(" xmlComment" , - 1 , C_XmlComment .untyped())
144
- }
119
+ // Example for mulitple non-abstract child
120
+ rootNode.addNewChild(" xmlComment" , - 1 , C_XmlComment .untyped())
145
121
}
146
122
147
123
@Test
148
- fun simpleTest () = runTest { httpClient ->
149
- val client = ModelQLClient .builder().url(" http://localhost/query" ).httpClient(httpClient).build()
124
+ fun `simple query` () = runTest { client ->
150
125
val result: Int = client.query { root ->
151
126
root.children(" classes" ).ofConcept(C_ClassConcept )
152
127
.member
@@ -157,22 +132,22 @@ class TypedModelQLTest {
157
132
}
158
133
159
134
@Test
160
- fun test () = runTest { httpClient ->
161
- val client = ModelQLClient .builder().url(" http://localhost/query" ).httpClient(httpClient).build()
135
+ fun `complex query` () = runTest { client ->
162
136
val result: List <Pair <String , String >> = client.query { root ->
163
137
root.children(" classes" ).ofConcept(C_ClassConcept )
164
138
.member
165
139
.ofConcept(C_StaticMethodDeclaration )
166
140
.filter { it.visibility.instanceOf(C_PublicVisibility ) }
167
141
.map { it.name.zip(it.parameter.name.toList(), it.untyped().conceptReference()) }
168
142
.toList()
169
- }.map { it.first to it.first + " (" + it.second.joinToString(" , " ) + " ) [" + it.third?.resolve()?.getLongName() + " ]" }
143
+ }.map {
144
+ it.first to it.first + " (" + it.second.joinToString(" , " ) + " ) [" + it.third?.resolve()?.getLongName() + " ]"
145
+ }
170
146
assertEquals(listOf (" plus" to " plus(a, b) [jetbrains.mps.baseLanguage.StaticMethodDeclaration]" ), result)
171
147
}
172
148
173
149
@Test
174
- fun `get references` () = runTest { httpClient ->
175
- val client = ModelQLClient .builder().url(" http://localhost/query" ).httpClient(httpClient).build()
150
+ fun `get references` () = runTest { client ->
176
151
val usedVariables: Set <String > = client.query { root ->
177
152
root.children(" classes" ).ofConcept(C_ClassConcept )
178
153
.member
@@ -187,8 +162,7 @@ class TypedModelQLTest {
187
162
}
188
163
189
164
@Test
190
- fun `get references - fqName` () = runTest { httpClient ->
191
- val client = ModelQLClient .builder().url(" http://localhost/query" ).httpClient(httpClient).build()
165
+ fun `get references - fqName` () = runTest { client ->
192
166
val usedVariables: Set <String > = client.query { root ->
193
167
root.children(" classes" ).ofConcept(C_ClassConcept )
194
168
.member
@@ -210,8 +184,7 @@ class TypedModelQLTest {
210
184
}
211
185
212
186
@Test
213
- fun `node serialization` () = runTest { httpClient ->
214
- val client = ModelQLClient .builder().url(" http://localhost/query" ).httpClient(httpClient).build()
187
+ fun `node serialization` () = runTest { client ->
215
188
val result: List <StaticMethodDeclaration > = client.query { root ->
216
189
root.children(" classes" ).ofConcept(C_ClassConcept )
217
190
.member
@@ -220,25 +193,23 @@ class TypedModelQLTest {
220
193
.untyped()
221
194
.toList()
222
195
}.map { it.typed<StaticMethodDeclaration >() }
223
- assertEquals(" plus" , branch.computeRead { result[0 ].name } )
196
+ assertEquals(" plus" , result[0 ].name)
224
197
}
225
198
226
199
@Test
227
- fun `return typed node` () = runTest { httpClient ->
228
- val client = ModelQLClient .builder().url(" http://localhost/query" ).httpClient(httpClient).build()
200
+ fun `return typed node` () = runTest { client ->
229
201
val result: List <StaticMethodDeclaration > = client.query { root ->
230
202
root.children(" classes" ).ofConcept(C_ClassConcept )
231
203
.member
232
204
.ofConcept(C_StaticMethodDeclaration )
233
205
.filter { it.visibility.instanceOf(C_PublicVisibility ) }
234
206
.toList()
235
207
}
236
- assertEquals(" plus" , branch.computeRead { result[0 ].name } )
208
+ assertEquals(" plus" , result[0 ].name)
237
209
}
238
210
239
211
@Test
240
- fun `set property` () = runTest { httpClient ->
241
- val client = ModelQLClient .builder().url(" http://localhost/query" ).httpClient(httpClient).build()
212
+ fun `set property` () = runTest { client ->
242
213
val expected = " myRenamedMethod"
243
214
client.query { root ->
244
215
root.children(" classes" ).ofConcept(C_ClassConcept )
@@ -257,9 +228,7 @@ class TypedModelQLTest {
257
228
}
258
229
259
230
@Test
260
- fun `set reference` () = runTest { httpClient ->
261
- val client = ModelQLClient .builder().url(" http://localhost/query" ).httpClient(httpClient).build()
262
-
231
+ fun `set reference` () = runTest { client ->
263
232
val oldValue = client.query { root ->
264
233
root.children(" classes" ).ofConcept(C_ClassConcept )
265
234
.member
@@ -298,8 +267,7 @@ class TypedModelQLTest {
298
267
}
299
268
300
269
@Test
301
- fun `set reference - null` () = runTest { httpClient ->
302
- val client = ModelQLClient .builder().url(" http://localhost/query" ).httpClient(httpClient).build()
270
+ fun `set reference - null` () = runTest { client ->
303
271
val oldValue = client.query { root ->
304
272
root.children(" imageGen" ).ofConcept(C_ImageGenerator ).first().node
305
273
}
@@ -316,9 +284,7 @@ class TypedModelQLTest {
316
284
}
317
285
318
286
@Test
319
- fun `add new child` () = runTest { httpClient ->
320
- val client = ModelQLClient .builder().url(" http://localhost/query" ).httpClient(httpClient).build()
321
-
287
+ fun `add new child` () = runTest { client ->
322
288
val oldNumChildren = client.query { root ->
323
289
root.children(" classes" ).ofConcept(C_ClassConcept )
324
290
.first()
@@ -346,8 +312,7 @@ class TypedModelQLTest {
346
312
}
347
313
348
314
@Test
349
- fun `add new child - default concept` () = runTest { httpClient ->
350
- val client = ModelQLClient .builder().url(" http://localhost/query" ).httpClient(httpClient).build()
315
+ fun `add new child - default concept` () = runTest { client ->
351
316
client.query { root ->
352
317
root.descendants().ofConcept(C_XmlComment )
353
318
.first()
@@ -365,9 +330,7 @@ class TypedModelQLTest {
365
330
}
366
331
367
332
@Test
368
- fun `set child` () = runTest { httpClient ->
369
- val client = ModelQLClient .builder().url(" http://localhost/query" ).httpClient(httpClient).build()
370
-
333
+ fun `set child` () = runTest { client ->
371
334
client.query { root ->
372
335
root.descendants().ofConcept(C_ReturnStatement )
373
336
.first()
@@ -382,8 +345,7 @@ class TypedModelQLTest {
382
345
}
383
346
384
347
@Test
385
- fun `set child - default concept` () = runTest { httpClient ->
386
- val client = ModelQLClient .builder().url(" http://localhost/query" ).httpClient(httpClient).build()
348
+ fun `set child - default concept` () = runTest { client ->
387
349
client.query { root ->
388
350
root.descendants().ofConcept(C_XmlFile )
389
351
.first()
0 commit comments