From eee3dc527afe38d2e4bbb9eab89f4711bade0374 Mon Sep 17 00:00:00 2001 From: OmkarTenkale Date: Sat, 27 Jul 2024 19:37:53 +0530 Subject: [PATCH] Add tests --- .../nodal/NodeDependenciesTest.kt | 38 +++++++++++++++++++ .../kotlin/dev/omkartenkale/nodal/NodeTest.kt | 30 +++++++++++++++ .../NodeMissingEmptyConstructorException.kt | 5 --- 3 files changed, 68 insertions(+), 5 deletions(-) delete mode 100644 nodal/src/commonMain/kotlin/dev.omkartenkale.nodal/exceptions/NodeMissingEmptyConstructorException.kt diff --git a/nodal/src/androidUnitTest/kotlin/dev/omkartenkale/nodal/NodeDependenciesTest.kt b/nodal/src/androidUnitTest/kotlin/dev/omkartenkale/nodal/NodeDependenciesTest.kt index cd54300..3898f90 100644 --- a/nodal/src/androidUnitTest/kotlin/dev/omkartenkale/nodal/NodeDependenciesTest.kt +++ b/nodal/src/androidUnitTest/kotlin/dev/omkartenkale/nodal/NodeDependenciesTest.kt @@ -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 @@ -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() + + override val providesDependencies: DependencyDeclaration = { + providesSelf { + Some() + } + } + } + + val rootNode = Node.createRootNode( + klass = RootNode::class, + nodalConfig = NodalConfig(createEagerInstances = true), + onRequestRemove = { }) {} as RootNode + + val nodeA = rootNode.addChild() + + assert(nodeA.dependencies.getOrNull() == 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() + } + } + } \ No newline at end of file diff --git a/nodal/src/androidUnitTest/kotlin/dev/omkartenkale/nodal/NodeTest.kt b/nodal/src/androidUnitTest/kotlin/dev/omkartenkale/nodal/NodeTest.kt index 3e15c48..550a89d 100644 --- a/nodal/src/androidUnitTest/kotlin/dev/omkartenkale/nodal/NodeTest.kt +++ b/nodal/src/androidUnitTest/kotlin/dev/omkartenkale/nodal/NodeTest.kt @@ -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 @@ -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() @@ -74,6 +79,12 @@ class NodeTest { val nodeA = rootNode.addChild() assert(nodeA.isDead.not()) } + @Test + fun `verify child not found`() = runTest { + assertFailsWith { + rootNode.child() + } + } @Test fun `verify node isDead property is updated after removed`() = runTest { @@ -117,4 +128,23 @@ class NodeTest { } } + + @Test + fun `verify child cannot be added on a dead node`() = runTest { + val nodeA = rootNode.addChild() + nodeA.removeSelf() + assertFailsWith(DisallowedNodeAdditionException::class){ + nodeA.addChild() + } + } + + @Test + fun `verify error is thrown when node could not be instantiated`() = runTest { + class SomeNode(unit: Unit): Node() + + assertFailsWith(NodeCreationException::class){ + rootNode.addChild() + } + } + } \ No newline at end of file diff --git a/nodal/src/commonMain/kotlin/dev.omkartenkale.nodal/exceptions/NodeMissingEmptyConstructorException.kt b/nodal/src/commonMain/kotlin/dev.omkartenkale.nodal/exceptions/NodeMissingEmptyConstructorException.kt deleted file mode 100644 index 10b47f8..0000000 --- a/nodal/src/commonMain/kotlin/dev.omkartenkale.nodal/exceptions/NodeMissingEmptyConstructorException.kt +++ /dev/null @@ -1,5 +0,0 @@ -package dev.omkartenkale.nodal.exceptions - -import kotlin.reflect.KClass - -public class NodeMissingEmptyConstructorException(public val node: KClass<*>, e: Exception) : NodalException("${node.simpleName} must have an empty constructor", e) \ No newline at end of file