Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
junkdog committed Nov 18, 2023
1 parent cf72a3c commit 654e5af
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 63 deletions.
2 changes: 1 addition & 1 deletion core/src/main/kotlin/sift/core/element/FieldNode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class FieldNode private constructor(
private val kprop: KotlinProperty?,
override val annotations: List<AnnotationNode>,
internal val signature: FieldSignatureNode? = fn.signature(cn.signature?.formalParameters ?: listOf()),
private val originalCn: ClassNode? = null, // when field is inherited
internal val originalCn: ClassNode? = null, // when field is inherited
) : Element(), Trait.HasType {

init {
Expand Down
2 changes: 2 additions & 0 deletions core/src/main/kotlin/sift/core/tree/ElementNode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,11 @@ private fun Element.properties(): List<String> {
is FieldNode -> listOfNotNull(
"synth".takeIf { access and Opcodes.ACC_SYNTHETIC != 0 },
"static".takeIf { access and Opcodes.ACC_STATIC != 0 },
"inherited".takeIf { originalCn != null },
)
is MethodNode -> listOfNotNull(
"abstract".takeIf { isAbstract },
"inherited".takeIf { originalCn != null },
"static".takeIf { access and Opcodes.ACC_STATIC != 0 },
"synth".takeIf { access and Opcodes.ACC_SYNTHETIC != 0 },
)
Expand Down
51 changes: 0 additions & 51 deletions core/src/test/kotlin/sift/core/KnownLimitationsTest.kt
Original file line number Diff line number Diff line change
@@ -1,19 +1,10 @@
package sift.core

import org.assertj.core.api.Assertions
import org.junit.jupiter.api.Disabled
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith
import org.objectweb.asm.tree.ClassNode
import sift.core.api.*
import sift.core.api.debug.debugTraces
import sift.core.api.testdata.set1.Payload
import sift.core.api.testdata.set2.HandlerFn
import sift.core.api.testdata.set2.HandlerOfFns
import sift.core.asm.classNode
import sift.core.dsl.annotatedBy
import sift.core.dsl.classes
import sift.core.entity.Entity
import sift.core.entity.EntityService
import sift.core.junit.LogActiveTestExtension

Expand All @@ -25,48 +16,6 @@ class KnownLimitationsTest {
debugLog = true
}

@Test
fun `correctly identify relations when scanning instantiations`() {
val cns = listOf(
classNode<HandlerFn>(),
classNode<Payload>(),
classNode<HandlerOfFns>(),
)

val handler = Entity.Type("handler")
val data = Entity.Type("data")

classes {
scope("scan handler") {
methods {
annotatedBy<HandlerFn>()
entity(handler)

parameters {
parameter(0)
explodeType {
entity(data)
}
}

// works; reverse lookup via "backtrack" children
// data.instantiations["sent-by"] = handler

instantiationsOf(data) {
data["sent-by"] = handler
}
}
}
}.execute(cns) { es ->
// assertThat(es[data].values.first().children["sent-by"]!!.map(Entity::toString))
Assertions.assertThat(es[data].values.first().children["sent-by"]!!.map(Entity::toString))
.containsExactlyInAnyOrder(
"Entity(HandlerOfFns::on, type=handler)",
"Entity(HandlerOfFns::boo, type=handler)",
)
}
}

private fun Action<Unit, Unit>.execute(
cns: List<ClassNode>,
block: (EntityService) -> Unit
Expand Down
89 changes: 78 additions & 11 deletions core/src/test/kotlin/sift/core/dsl/DslTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2021,6 +2021,44 @@ class DslTest {
}
}

@Test
fun `correctly identify relations when scanning instantiations with invocations-of`() {
val cns = listOf(
classNode<HandlerFn>(),
classNode<Payload>(),
classNode<HandlerOfFns>(),
)

val handler = Entity.Type("handler")
val data = Entity.Type("data")

classes {
scope("scan handler") {
methods {
annotatedBy<HandlerFn>()
entity(handler)

parameters {
parameter(0)
explodeType {
entity(data)
}
}

instantiationsOf(data) {
data["sent-by"] = handler
}
}
}
}.expecting(cns) { es ->
assertThat((es[data].values.first().children["sent-by"]!!.map(Entity::toString)))
.containsExactlyInAnyOrder(
"Entity(HandlerOfFns::on, type=handler, element-id=1, element-type=MethodNode)",
"Entity(HandlerOfFns::boo, type=handler, element-id=2, element-type=MethodNode)",
)
}
}

@Test
fun `associate controller as parent of endpoints`() {
val controller = Entity.Type("controller")
Expand Down Expand Up @@ -2435,31 +2473,60 @@ class DslTest {
)
}

@Test @Disabled("need to resolve generic type of inherited methods")
fun `methods from delegated instances are not duplicated`() {
class Hello : HelloG<String> {
override fun hello(): String = ""
@Test
fun `delegated methods`() {
class Greeting

class Hello : HelloG<Greeting> {
override fun hello(): Greeting = Greeting()
}

class FooBar(hello: Helloer) : HelloG<String> {
override fun hello(): String = ""
class FooBar(hello: Hello) : HelloG<Greeting> by hello

class FooBarOverrideDelegate(hello: Hello) : HelloG<Greeting> by hello {
override fun hello(): Greeting = TODO("no greeting instantiated")
}

val cns = listOf(
classNode(Hello::class),
classNode(HelloG::class),
classNode(Greeting::class),
classNode(FooBar::class),
classNode(FooBarOverrideDelegate::class),
)

val m = Entity.Type("hello-method")
val greet = Entity.Type("greeting")

template {
classes {
filter("Greeting")
entity(greet, label("Greeting"))
}
classes {
filter(Regex("\\.FooBar$"))
methods(inherited) {
entity(m, label("\${name}()"), property("name", readName()))
m["instantiates"] = greet.instantiations
}
}
}.expecting(cns, m, """
── hello-method
└─ hello()
└─ Greeting
"""
)

template {
classes {
filter(Regex("\\.FooBar"))
methods(inherited + abstractMethods) {
entity(m, label("\${name}()"),
property("name", readName())
)
filter("Greeting")
entity(greet, label("Greeting"))
}
classes {
filter(Regex("\\.FooBarOverrideDelegate"))
methods(inherited) {
entity(m, label("\${name}()"), property("name", readName()))
m["instantiates"] = greet.instantiations
}
}
}.expecting(cns, m, """
Expand Down

0 comments on commit 654e5af

Please sign in to comment.