4
0
Fork 0
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:
LordMZTE 2020-08-03 16:08:34 +02:00
parent 614477ef06
commit db43778715
3 changed files with 57 additions and 1 deletions

View file

@ -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
}

View file

@ -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)
}
}

View file

@ -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")
)
}
}