Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
omkar-tenkale committed Jul 27, 2024
1 parent 2f30b77 commit eee3dc5
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.omkartenkale.nodal

import dev.omkartenkale.nodal.exceptions.DependencyNotFoundException
import dev.omkartenkale.nodal.plugin.NodalPlugins
import dev.omkartenkale.nodal.util.MainDispatcherRule
import kotlinx.coroutines.ExperimentalCoroutinesApi
Expand Down Expand Up @@ -82,5 +83,42 @@ class NodeDependenciesTest {
}
}

@Test
fun `verify providesSelf does not expose dependencies to children`(){
class Some
class NodeA: Node()

class RootNode : Node() {
val some: Some by dependencies<Some>()

override val providesDependencies: DependencyDeclaration = {
providesSelf<Some> {
Some()
}
}
}

val rootNode = Node.createRootNode(
klass = RootNode::class,
nodalConfig = NodalConfig(createEagerInstances = true),
onRequestRemove = { }) {} as RootNode

val nodeA = rootNode.addChild<NodeA>()

assert(nodeA.dependencies.getOrNull<Some>() == null)
}

@Test
fun `verify get dependency should throw error if not available`(){
class Some
class RootNode : Node()

val rootNode = Node.createRootNode(klass = RootNode::class, onRequestRemove = { }) {} as RootNode

assertFailsWith(DependencyNotFoundException::class){
rootNode.dependencies.get<Some>()
}
}


}
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package dev.omkartenkale.nodal

import dev.omkartenkale.nodal.Node.Companion.ui
import dev.omkartenkale.nodal.exceptions.ChildNodeNotFoundException
import dev.omkartenkale.nodal.exceptions.DisallowedNodeAdditionException
import dev.omkartenkale.nodal.exceptions.NodeCreationException
import dev.omkartenkale.nodal.util.MainDispatcherRule
import dev.omkartenkale.nodal.util.child
import dev.omkartenkale.nodal.util.doOnAdded
import dev.omkartenkale.nodal.util.doOnRemoved
import dev.omkartenkale.nodal.util.isAdded
Expand All @@ -13,6 +17,7 @@ import org.junit.Before
import org.junit.Rule
import org.junit.Test
import kotlin.test.assertFails
import kotlin.test.assertFailsWith

class NodeTest {
class RootNode : Node()
Expand Down Expand Up @@ -74,6 +79,12 @@ class NodeTest {
val nodeA = rootNode.addChild<ANode>()
assert(nodeA.isDead.not())
}
@Test
fun `verify child not found`() = runTest {
assertFailsWith<ChildNodeNotFoundException> {
rootNode.child<BNode>()
}
}

@Test
fun `verify node isDead property is updated after removed`() = runTest {
Expand Down Expand Up @@ -117,4 +128,23 @@ class NodeTest {
}
}


@Test
fun `verify child cannot be added on a dead node`() = runTest {
val nodeA = rootNode.addChild<ANode>()
nodeA.removeSelf()
assertFailsWith(DisallowedNodeAdditionException::class){
nodeA.addChild<ANode>()
}
}

@Test
fun `verify error is thrown when node could not be instantiated`() = runTest {
class SomeNode(unit: Unit): Node()

assertFailsWith(NodeCreationException::class){
rootNode.addChild<SomeNode>()
}
}

}

This file was deleted.

0 comments on commit eee3dc5

Please sign in to comment.