mirror of
https://github.com/Anvilcraft/modpacktools
synced 2024-06-11 06:59:28 +02:00
more test
small bug fix in ConfigToml
This commit is contained in:
parent
614477ef06
commit
db43778715
|
@ -32,7 +32,7 @@ class ConfigToml : Toml() {
|
|||
@Suppress("UNCHECKED_CAST")
|
||||
fun <T> getPath(vararg path: String): T? {
|
||||
var toml: Toml = this
|
||||
path.slice(0..path.size - 2).forEach {toml = toml.getTable(it)}
|
||||
path.slice(0..path.size - 2).forEach {toml = toml.getTable(it) ?: return null}
|
||||
return toml.get(path[path.size - 1]) as? T
|
||||
}
|
||||
|
||||
|
|
|
@ -2,6 +2,7 @@ package ley.anvil.modpacktools.util
|
|||
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Test
|
||||
import org.junit.rules.TemporaryFolder
|
||||
import java.io.File
|
||||
import java.net.URL
|
||||
|
||||
|
@ -11,4 +12,18 @@ class UtilTest {
|
|||
|
||||
@Test
|
||||
fun testMergeTo() = assertEquals(File("testing/dir"), File("testing") mergeTo File("dir"))
|
||||
|
||||
@Test
|
||||
fun testReadAsJson() {
|
||||
val tmpDir = TemporaryFolder().apply {create()}
|
||||
val testFile = tmpDir.newFile()
|
||||
|
||||
testFile.writeText("""
|
||||
{
|
||||
"someKey": "someValue"
|
||||
}
|
||||
""".trimIndent())
|
||||
|
||||
assertEquals("someValue", testFile.readAsJson()["someKey"].asString)
|
||||
}
|
||||
}
|
|
@ -0,0 +1,41 @@
|
|||
package ley.anvil.modpacktools.util.config
|
||||
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertNull
|
||||
import org.junit.Test
|
||||
|
||||
class ConfigTomlTest {
|
||||
val toml = ConfigToml().read("""
|
||||
[SomeCategory]
|
||||
someValue = 123
|
||||
""".trimIndent()) as ConfigToml
|
||||
|
||||
@Test
|
||||
fun testGetPath() {
|
||||
//String getPath
|
||||
assertEquals(
|
||||
123L,
|
||||
toml.getPath("SomeCategory/someValue")!!
|
||||
)
|
||||
|
||||
//should be null if invalid category
|
||||
assertNull(toml.getPath("NonExistentCategory/val"))
|
||||
|
||||
//should be null if invalid value
|
||||
assertNull(toml.getPath("SomeCategory/val"))
|
||||
}
|
||||
|
||||
//should throw exception
|
||||
@Test(expected = MissingConfigValueException::class)
|
||||
fun testPathOrExceptionMissing() {
|
||||
toml.pathOrException<Long>("Asd/asd")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testPathOrException() {
|
||||
assertEquals(
|
||||
123L,
|
||||
toml.pathOrException("SomeCategory/someValue")
|
||||
)
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue