Skip to content

Commit

Permalink
Added unit tests for ElementPath.kt, and added an extra check to ensu…
Browse files Browse the repository at this point in the history
…re ElementPaths with nodes containing whitespaces cannot be created
  • Loading branch information
ArkoSammy12 committed Dec 22, 2024
1 parent f3cad9a commit 4bff8a7
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ data class ElementPath(val string: String) {
init {
val nodes: List<String> = this.string.split(".")
for (node: String in nodes) {
if (node.contains("\\s+")) {
if (node.contains("\\s+") || node.contains(" ")) {
throw IllegalArgumentException("Setting nodes cannot contain whitespaces!")
}
if (node.isBlank() || node.isEmpty()) {
Expand Down
5 changes: 3 additions & 2 deletions src/test/kotlin/io/arkosammy12/monkeyconfig/Main.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.arkosammy12.monkeyconfig

import io.arkosammy12.monkeyconfig.builders.tomlConfigManager
import io.arkosammy12.monkeyconfig.settings.TestNumberSettingImplementation
import io.arkosammy12.monkeyconfig.settings.TestNumberSetting
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths
Expand All @@ -22,13 +22,14 @@ object Main {
booleanSetting("testTopLevelBooleanSetting", true) {
comment = "Test comment"
}

section("testSection") {
comment = "Test comment here"
numberSetting<Int>("testNumberSetting", 0) {
comment = "Test comment"
minValue = 0
maxValue = 10
implementation = { builder -> TestNumberSettingImplementation(builder) }
implementation = { builder -> TestNumberSetting(builder) }
}
section("testSubsection") {
comment = "Test comment"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package io.arkosammy12.monkeyconfig.settings

import io.arkosammy12.monkeyconfig.builders.NumberSettingBuilder

class TestNumberSettingImplementation<T : Number>(settingBuilder: NumberSettingBuilder<T>) : NumberSetting<T>(settingBuilder) {
class TestNumberSetting<T : Number>(settingBuilder: NumberSettingBuilder<T>) : NumberSetting<T>(settingBuilder) {

init {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package io.arkosammy12.monkeyconfig.util

import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
import kotlin.test.assertEquals

class TestElementPath {

@Test
fun `ElementPath should not accept empty nodes`() {

assertThrows<IllegalArgumentException> {
ElementPath("")
}

assertThrows<IllegalArgumentException> {
ElementPath("node1", "", "node3")
}

assertThrows<IllegalArgumentException> {
ElementPath(".")
}

assertThrows<IllegalArgumentException> {
ElementPath("node1..node3")
}

}

@Test
fun `Element Path should not accept nodes with whitespaces`() {

assertThrows<IllegalArgumentException> {
ElementPath(" ")
}

assertThrows<IllegalArgumentException> {
ElementPath("node1", " node2", "node3")
}

assertThrows<IllegalArgumentException> {
ElementPath(". ")
}

assertThrows<IllegalArgumentException> {
ElementPath("node1. .node3")
}

}

@Test
fun `Element Path creation with node lists results in proper string representation`() {

assertEquals(ElementPath("node").string, "node")

assertEquals(ElementPath("node1", "node2").string, "node1.node2")

assertEquals(ElementPath(listOf("node1", "node2", "node3")).string, "node1.node2.node3")

}

@Test
fun `Element Path creation with strings results in proper list representation`() {

assertEquals(ElementPath("node").asList, listOf("node"))

assertEquals(ElementPath("node1.node2").asList, listOf("node1", "node2"))

assertEquals(ElementPath("node1.node2.node3").asList, listOf("node1", "node2", "node3"))

}

}

0 comments on commit 4bff8a7

Please sign in to comment.