Skip to content

Commit

Permalink
test: 💍 add tests for GumTree
Browse files Browse the repository at this point in the history
  • Loading branch information
Xanonymous-GitHub committed Jul 18, 2024
1 parent 7369437 commit 5bce218
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 10 deletions.
18 changes: 8 additions & 10 deletions core/src/main/kotlin/tw/xcc/gumtree/model/GumTree.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,21 @@ package tw.xcc.gumtree.model
import tw.xcc.gumtree.api.tree.Comparable
import java.util.concurrent.atomic.AtomicReference

class GumTree(type: TreeType, private val label: String = EMPTY_LABEL) : BasicTree<GumTree>(), Comparable<GumTree> {
class GumTree(type: TreeType, val label: String = EMPTY_LABEL) : BasicTree<GumTree>(), Comparable<GumTree> {
private val compareHelper = CompareHelper(this)

val pos: Int = -1
val length: Int = -1
var pos: Int = -1
var length: Int = -1

private val _type = AtomicReference(TreeType.empty())
private val type: TreeType // TODO: may not be private
val type: TreeType
get() = _type.get()

private val _metrics = AtomicReference(TreeMetrics.empty()) // TODO: calculate metrics
val metrics: TreeMetrics
get() = _metrics.get()

// TODO: may not be private
private fun setTypeTo(value: TreeType) =
fun setTypeTo(value: TreeType) =
synchronized(this) {
_type.set(value)
}
Expand All @@ -33,9 +32,8 @@ class GumTree(type: TreeType, private val label: String = EMPTY_LABEL) : BasicTr
child: GumTree
) {
synchronized(this) {
val childCount = childCount()
if (pos < 0 || pos > childCount) {
throw IndexOutOfBoundsException("Index: $pos, Size: $childCount")
if (pos < 0) {
throw IndexOutOfBoundsException("The position $pos should be non-negative.")
}

if (childrenMap.get().containsKey(pos)) {
Expand Down Expand Up @@ -76,4 +74,4 @@ class GumTree(type: TreeType, private val label: String = EMPTY_LABEL) : BasicTr
init {
setTypeTo(type)
}
}
}
92 changes: 92 additions & 0 deletions core/src/test/kotlin/GumTreeTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import org.junit.jupiter.api.parallel.Execution
import org.junit.jupiter.api.parallel.ExecutionMode
import tw.xcc.gumtree.model.GumTree
import tw.xcc.gumtree.model.TreeMetrics
import tw.xcc.gumtree.model.TreeType
import kotlin.test.BeforeTest
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFalse

@Execution(ExecutionMode.CONCURRENT)
internal class GumTreeTest {
private val givenTreeType = TreeType("testType")
private val givenTreeLabel = "testLabel"
private lateinit var givenTree: GumTree

@BeforeTest
fun setUp() {
givenTree = GumTree(givenTreeType, givenTreeLabel)
}

@Test
fun `test default constructor`() {
val actualTreeType = givenTree.type
val actualTreeLabel = givenTree.label
assertEquals(givenTreeType, actualTreeType, "TreeType should be equal")
assertEquals(givenTreeLabel, actualTreeLabel, "TreeLabel should be equal")
}

@Test
fun `test tree pos`() {
val actualTreePos = givenTree.pos
assertEquals(-1, actualTreePos)
val givenNewTreePos = 9487
givenTree.pos = givenNewTreePos
val newActualTreePos = givenTree.pos
assertEquals(givenNewTreePos, newActualTreePos)
}

@Test
fun `test tree length`() {
val actualTreeLength = givenTree.length
assertEquals(-1, actualTreeLength)
val givenNewTreeLength = 9487
givenTree.length = givenNewTreeLength
val newActualTreeLength = givenTree.length
assertEquals(givenNewTreeLength, newActualTreeLength)
}

@Test
fun `test setTypeTo`() {
val newTreeType = TreeType("newType")
givenTree.setTypeTo(newTreeType)
val actualTreeType = givenTree.type
assertEquals(newTreeType, actualTreeType, "TreeType should be equal")
}

@Test
fun `test setMetricsTo`() {
val newTreeMetrics = TreeMetrics(1, 2, 3, 4)
givenTree.setMetricsTo(newTreeMetrics)
val actualTreeMetrics = givenTree.metrics
assertEquals(newTreeMetrics, actualTreeMetrics, "TreeMetrics should be equal")
}

@Test
fun `test insertChildAt`() {
val childTreeType = TreeType("childType")
val childTreeLabel = "childLabel"
val childTree = GumTree(childTreeType, childTreeLabel)
val givenInsertPos = 9487
givenTree.insertChildAt(givenInsertPos, childTree)
val actualChildTree = givenTree.childAt(givenInsertPos)
assertEquals(childTree, actualChildTree, "Child tree should be equal")
}

@Test
fun `test hasSameTypeAs`() {
val otherTreeType = TreeType("otherType")
val otherTree = GumTree(otherTreeType, givenTreeLabel)
val actualResult = givenTree hasSameTypeAs otherTree
assertFalse(actualResult, "TreeType should not be equal")
}

@Test
fun `test hasSameLabelAs`() {
val otherTreeLabel = "otherLabel"
val otherTree = GumTree(givenTreeType, otherTreeLabel)
val actualResult = givenTree hasSameLabelAs otherTree
assertFalse(actualResult, "TreeLabel should not be equal")
}
}

0 comments on commit 5bce218

Please sign in to comment.