forked from ReVanced/revanced-patcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestUtil.kt
45 lines (39 loc) · 1.3 KB
/
TestUtil.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package app.revanced.patcher.util
import org.objectweb.asm.tree.AbstractInsnNode
import org.objectweb.asm.tree.FieldInsnNode
import org.objectweb.asm.tree.LdcInsnNode
import kotlin.test.fail
object TestUtil {
fun <T: AbstractInsnNode> assertNodeEqual(expected: T, actual: T) {
val a = expected.nodeString()
val b = actual.nodeString()
if (a != b) {
fail("expected: $a,\nactual: $b\n")
}
}
private fun AbstractInsnNode.nodeString(): String {
val sb = NodeStringBuilder()
when (this) {
// TODO(Sculas): Add more types
is LdcInsnNode -> sb
.addType("cst", cst)
is FieldInsnNode -> sb
.addType("owner", owner)
.addType("name", name)
.addType("desc", desc)
}
return "(${this::class.simpleName}): (type = $type, opcode = $opcode, $sb)"
}
}
private class NodeStringBuilder {
private val sb = StringBuilder()
fun addType(name: String, value: Any): NodeStringBuilder {
sb.append("$name = \"$value\", ")
return this
}
override fun toString(): String {
if (sb.isEmpty()) return ""
val s = sb.toString()
return s.substring(0 .. (s.length - 2).coerceAtLeast(0)) // remove the last ", "
}
}