Skip to content

Commit

Permalink
test(model-api-gen): add test for modelql write support
Browse files Browse the repository at this point in the history
  • Loading branch information
mhuster23 committed Jan 3, 2024
1 parent c76b874 commit 9854f63
Showing 1 changed file with 158 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,41 +17,63 @@ import io.ktor.client.HttpClient
import io.ktor.server.testing.testApplication
import jetbrains.mps.baseLanguage.C_ClassConcept
import jetbrains.mps.baseLanguage.C_IntegerType
import jetbrains.mps.baseLanguage.C_MinusExpression
import jetbrains.mps.baseLanguage.C_ParameterDeclaration
import jetbrains.mps.baseLanguage.C_PlusExpression
import jetbrains.mps.baseLanguage.C_PublicVisibility
import jetbrains.mps.baseLanguage.C_ReturnStatement
import jetbrains.mps.baseLanguage.C_StaticMethodDeclaration
import jetbrains.mps.baseLanguage.C_VariableReference
import jetbrains.mps.baseLanguage.ClassConcept
import jetbrains.mps.baseLanguage.StaticMethodDeclaration
import jetbrains.mps.lang.editor.imageGen.C_ImageGenerator
import jetbrains.mps.lang.editor.imageGen.ImageGenerator
import org.modelix.apigen.test.ApigenTestLanguages
import org.modelix.metamodel.instanceOf
import org.modelix.metamodel.typed
import org.modelix.metamodel.untyped
import org.modelix.model.api.IBranch
import org.modelix.model.api.PBranch
import org.modelix.model.api.getRootNode
import org.modelix.model.api.resolve
import org.modelix.model.client.IdGenerator
import org.modelix.model.lazy.CLTree
import org.modelix.model.lazy.ObjectStoreCache
import org.modelix.model.persistent.MapBaseStore
import org.modelix.model.persistent.MapBasedStore
import org.modelix.model.server.light.LightModelServer
import org.modelix.modelql.client.ModelQLClient
import org.modelix.modelql.core.asMono
import org.modelix.modelql.core.count
import org.modelix.modelql.core.equalTo
import org.modelix.modelql.core.filter
import org.modelix.modelql.core.first
import org.modelix.modelql.core.map
import org.modelix.modelql.core.toList
import org.modelix.modelql.core.toSet
import org.modelix.modelql.core.zip
import org.modelix.modelql.gen.jetbrains.mps.baseLanguage.addMember
import org.modelix.modelql.gen.jetbrains.mps.baseLanguage.expression
import org.modelix.modelql.gen.jetbrains.mps.baseLanguage.member
import org.modelix.modelql.gen.jetbrains.mps.baseLanguage.parameter
import org.modelix.modelql.gen.jetbrains.mps.baseLanguage.setExpression
import org.modelix.modelql.gen.jetbrains.mps.baseLanguage.setVariableDeclaration
import org.modelix.modelql.gen.jetbrains.mps.baseLanguage.variableDeclaration
import org.modelix.modelql.gen.jetbrains.mps.baseLanguage.visibility
import org.modelix.modelql.gen.jetbrains.mps.lang.core.name
import org.modelix.modelql.gen.jetbrains.mps.lang.core.setName
import org.modelix.modelql.gen.jetbrains.mps.lang.editor.imageGen.node
import org.modelix.modelql.gen.jetbrains.mps.lang.editor.imageGen.node_orNull
import org.modelix.modelql.gen.jetbrains.mps.lang.editor.imageGen.setNode
import org.modelix.modelql.untyped.children
import org.modelix.modelql.untyped.conceptReference
import org.modelix.modelql.untyped.descendants
import kotlin.test.BeforeTest
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNotEquals
import kotlin.test.assertNotNull
import kotlin.test.assertNull
import kotlin.test.assertTrue

class TypedModelQLTest {
private lateinit var branch: IBranch
Expand All @@ -68,7 +90,7 @@ class TypedModelQLTest {
@BeforeTest
fun setup() {
ApigenTestLanguages.registerAll()
val tree = CLTree(ObjectStoreCache(MapBaseStore()))
val tree = CLTree(ObjectStoreCache(MapBasedStore()))
branch = PBranch(tree, IdGenerator.getInstance(1))
val rootNode = branch.getRootNode()
branch.runWrite {
Expand Down Expand Up @@ -101,6 +123,10 @@ class TypedModelQLTest {
}
}
}
// Example for optional reference
rootNode.addNewChild("imageGen", -1, C_ImageGenerator.untyped())
.typed<ImageGenerator>()
.apply { node = cls1 }
}
}

Expand Down Expand Up @@ -131,7 +157,7 @@ class TypedModelQLTest {
}

@Test
fun testReferences() = runTest { httpClient ->
fun `get references`() = runTest { httpClient ->
val client = ModelQLClient.builder().url("http://localhost/query").httpClient(httpClient).build()
val usedVariables: Set<String> = client.query { root ->
root.children("classes").ofConcept(C_ClassConcept)
Expand All @@ -147,7 +173,7 @@ class TypedModelQLTest {
}

@Test
fun testReferencesFqName() = runTest { httpClient ->
fun `get references - fqName`() = runTest { httpClient ->
val client = ModelQLClient.builder().url("http://localhost/query").httpClient(httpClient).build()
val usedVariables: Set<String> = client.query { root ->
root.children("classes").ofConcept(C_ClassConcept)
Expand All @@ -170,7 +196,7 @@ class TypedModelQLTest {
}

@Test
fun testNodeSerialization() = runTest { httpClient ->
fun `node serialization`() = runTest { httpClient ->
val client = ModelQLClient.builder().url("http://localhost/query").httpClient(httpClient).build()
val result: List<StaticMethodDeclaration> = client.query { root ->
root.children("classes").ofConcept(C_ClassConcept)
Expand All @@ -184,7 +210,7 @@ class TypedModelQLTest {
}

@Test
fun returnTypedNode() = runTest { httpClient ->
fun `return typed node`() = runTest { httpClient ->
val client = ModelQLClient.builder().url("http://localhost/query").httpClient(httpClient).build()
val result: List<StaticMethodDeclaration> = client.query { root ->
root.children("classes").ofConcept(C_ClassConcept)
Expand All @@ -195,4 +221,130 @@ class TypedModelQLTest {
}
assertEquals("plus", branch.computeRead { result[0].name })
}

@Test
fun `set property`() = runTest { httpClient ->
val client = ModelQLClient.builder().url("http://localhost/query").httpClient(httpClient).build()
val expected = "myRenamedMethod"
client.query { root ->
root.children("classes").ofConcept(C_ClassConcept)
.member
.ofConcept(C_StaticMethodDeclaration)
.first()
.setName(expected.asMono())
}
val actual = client.query { root ->
root.children("classes").ofConcept(C_ClassConcept)
.member
.ofConcept(C_StaticMethodDeclaration)
.first()
}
assertEquals(expected, actual.name)
}

@Test
fun `set reference`() = runTest { httpClient ->
val client = ModelQLClient.builder().url("http://localhost/query").httpClient(httpClient).build()

val oldValue = client.query { root ->
root.children("classes").ofConcept(C_ClassConcept)
.member
.ofConcept(C_StaticMethodDeclaration)
.descendants()
.ofConcept(C_ParameterDeclaration)
.first()
.name
}
val expected = "b"
client.query { root ->
val descendants = root.children("classes").ofConcept(C_ClassConcept)
.member
.ofConcept(C_StaticMethodDeclaration)
.descendants()

val target = descendants.ofConcept(C_ParameterDeclaration)
.filter { it.name.equalTo(expected) }
.first()

descendants.ofConcept(C_VariableReference)
.first()
.setVariableDeclaration(target)
}

val actual = client.query { root ->
root.children("classes").ofConcept(C_ClassConcept)
.member
.ofConcept(C_StaticMethodDeclaration)
.descendants()
.ofConcept(C_VariableReference)
.first()
}
assertNotEquals(expected, oldValue)
assertEquals(expected, actual.variableDeclaration.name)
}

@Test
fun `set reference - null`() = runTest { httpClient ->
val client = ModelQLClient.builder().url("http://localhost/query").httpClient(httpClient).build()
val oldValue = client.query { root ->
root.children("imageGen").ofConcept(C_ImageGenerator).first().node
}

client.query { root ->
root.children("imageGen").ofConcept(C_ImageGenerator).first().setNode(null)
}

val actual = client.query { root ->
root.children("imageGen").ofConcept(C_ImageGenerator).first().node_orNull
}
assertNotNull(oldValue)
assertNull(actual)
}

@Test
fun `add new child`() = runTest { httpClient ->
val client = ModelQLClient.builder().url("http://localhost/query").httpClient(httpClient).build()

val oldNumChildren = client.query { root ->
root.children("classes").ofConcept(C_ClassConcept)
.first()
.member
.ofConcept(C_StaticMethodDeclaration)
.count()
}

client.query { root ->
root.children("classes").ofConcept(C_ClassConcept)
.first()
.addMember(C_StaticMethodDeclaration)
}

val children = client.query { root ->
root.children("classes").ofConcept(C_ClassConcept)
.first()
.member
.ofConcept(C_StaticMethodDeclaration)
.toList()
}

assertEquals(oldNumChildren + 1, children.size)
assertEquals(C_StaticMethodDeclaration.untyped().getUID(), children.last().untyped().concept?.getUID())
}

@Test
fun `set child`() = runTest { httpClient ->
val client = ModelQLClient.builder().url("http://localhost/query").httpClient(httpClient).build()

client.query { root ->
root.descendants().ofConcept(C_ReturnStatement)
.first()
.setExpression(C_MinusExpression)
}

val actual = client.query { root ->
root.descendants().ofConcept(C_ReturnStatement).first().expression
}
assertNotNull(actual)
assertTrue(actual.instanceOf(C_MinusExpression))
}
}

0 comments on commit 9854f63

Please sign in to comment.