From 904bd8634a24ca7c9ee7065fa1efeeca66be3c5d Mon Sep 17 00:00:00 2001 From: Ben Woo <30431861+benwoo1110@users.noreply.github.com> Date: Thu, 31 Aug 2023 23:21:27 +0800 Subject: [PATCH] Improve tests for world config file --- .../core/world/WorldConfigFileTest.kt | 69 +++++++++++++++++++ .../multiverse/core/world/WorldConfigTest.kt | 55 +++++++-------- .../multiverse/core/world/WorldManagerTest.kt | 3 +- src/test/resources/default_worlds.yml | 2 +- src/test/resources/newworld_worlds.yml | 2 +- 5 files changed, 98 insertions(+), 33 deletions(-) create mode 100644 src/test/java/org/mvplugins/multiverse/core/world/WorldConfigFileTest.kt diff --git a/src/test/java/org/mvplugins/multiverse/core/world/WorldConfigFileTest.kt b/src/test/java/org/mvplugins/multiverse/core/world/WorldConfigFileTest.kt new file mode 100644 index 00000000..31d83841 --- /dev/null +++ b/src/test/java/org/mvplugins/multiverse/core/world/WorldConfigFileTest.kt @@ -0,0 +1,69 @@ +package org.mvplugins.multiverse.core.world + +import com.onarandombox.MultiverseCore.worldnew.config.WorldConfig +import com.onarandombox.MultiverseCore.worldnew.config.WorldsConfigFile +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Assertions.assertTrue +import org.mvplugins.multiverse.core.TestWithMockBukkit +import java.io.File +import java.nio.file.Path +import kotlin.io.path.absolutePathString +import kotlin.test.BeforeTest +import kotlin.test.Test +import kotlin.test.assertNotNull + +class WorldConfigFileTest : TestWithMockBukkit() { + + private lateinit var worldConfigFile : WorldsConfigFile + + @BeforeTest + fun setUp() { + val defaultConfig = getResourceAsText("/default_worlds.yml") + assertNotNull(defaultConfig) + File(Path.of(multiverseCore.dataFolder.absolutePath, "worlds2.yml").absolutePathString()).writeText(defaultConfig) + + worldConfigFile = WorldsConfigFile(multiverseCore) + worldConfigFile.load() + } + + + @Test + fun `World config is loaded`() { + assertTrue(worldConfigFile.isLoaded) + } + + @Test + fun `Old world config is migrated`() { + // TODO: When logic is implemented, check that the old config is migrated + } + + @Test + fun `Add a new world to config`() { + val worldConfig = WorldConfig(worldConfigFile.getWorldConfigSection("newworld")) + worldConfigFile.save() + compareConfigFile("worlds2.yml", "/newworld_worlds.yml") + } + + @Test + fun `Updating existing world properties`() { + val worldConfig = WorldConfig(worldConfigFile.getWorldConfigSection("world")) + worldConfig.setProperty("adjust-spawn", true) + worldConfig.setProperty("alias", "newalias") + worldConfigFile.save() + compareConfigFile("worlds2.yml", "/properties_worlds.yml") + } + + @Test + fun `Delete world section from config`() { + worldConfigFile.deleteWorldConfigSection("world") + worldConfigFile.save() + compareConfigFile("worlds2.yml", "/delete_worlds.yml") + } + + private fun compareConfigFile(configPath: String, comparePath: String) { + val config = multiverseCore.dataFolder.toPath().resolve(configPath).toFile().readText() + val configCompare = getResourceAsText(comparePath) + assertNotNull(configCompare) + assertEquals(configCompare, config) + } +} \ No newline at end of file diff --git a/src/test/java/org/mvplugins/multiverse/core/world/WorldConfigTest.kt b/src/test/java/org/mvplugins/multiverse/core/world/WorldConfigTest.kt index e0113e70..5d57836b 100644 --- a/src/test/java/org/mvplugins/multiverse/core/world/WorldConfigTest.kt +++ b/src/test/java/org/mvplugins/multiverse/core/world/WorldConfigTest.kt @@ -15,6 +15,7 @@ import kotlin.test.assertTrue class WorldConfigTest : TestWithMockBukkit() { private lateinit var worldConfigFile : WorldsConfigFile + private lateinit var worldConfig : WorldConfig @BeforeTest fun setUp() { @@ -24,52 +25,46 @@ class WorldConfigTest : TestWithMockBukkit() { worldConfigFile = WorldsConfigFile(multiverseCore) worldConfigFile.load() + worldConfig = WorldConfig(worldConfigFile.getWorldConfigSection("world")) } @Test - fun `World config is loaded`() { - assertTrue(worldConfigFile.isLoaded) + fun `Getting existing world property with getProperty returns expected value`() { + assertEquals("my world", worldConfig.getProperty("alias").get()) + assertEquals(false, worldConfig.getProperty("hidden").get()) } @Test - fun `Old world config is migrated`() { - // TODO + fun `Getting non-existing world property with getProperty returns null`() { + assertTrue(worldConfig.getProperty("invalid-property").isFailure) + assertTrue(worldConfig.getProperty("version").isFailure) } @Test - fun `Add a new world to config`() { - val worldConfig = WorldConfig(worldConfigFile.getWorldConfigSection("newworld")) - worldConfigFile.save() - - compareConfigFile("worlds2.yml", "/newworld_worlds.yml") + fun `Getting existing world property by getter returns expected value`() { + assertEquals("my world", worldConfig.alias) + assertEquals(false, worldConfig.isHidden) } @Test - fun `Updating existing world properties`() { - val worldConfig = WorldConfig(worldConfigFile.getWorldConfigSection("world")) - worldConfig.setProperty("adjust-spawn", true) - worldConfig.setProperty("alias", "newalias") - - worldConfigFile.save() - + fun `Updating an existing world property with setProperty reflects the changes in getProperty`() { + assertTrue(worldConfig.setProperty("adjust-spawn", true).isSuccess) assertEquals(true, worldConfig.getProperty("adjust-spawn").get()) - assertEquals("newalias", worldConfig.getProperty("alias").get()) - compareConfigFile("worlds2.yml", "/properties_worlds.yml") + assertTrue(worldConfig.setProperty("alias", "abc").isSuccess) + assertEquals("abc", worldConfig.getProperty("alias").get()) + + assertTrue(worldConfig.setProperty("scale", 2.0).isSuccess) + assertEquals(2.0, worldConfig.getProperty("scale").get()) + + val blacklists = listOf("a", "b", "c") + assertTrue(worldConfig.setProperty("world-blacklist", blacklists).isSuccess) + assertEquals(blacklists, worldConfig.getProperty("world-blacklist").get()) } @Test - fun `Delete world section from config`() { - worldConfigFile.deleteWorldConfigSection("world") - worldConfigFile.save() - - compareConfigFile("worlds2.yml", "/delete_worlds.yml") - } - - private fun compareConfigFile(configPath: String, comparePath: String) { - val config = multiverseCore.dataFolder.toPath().resolve(configPath).toFile().readText() - val configCompare = getResourceAsText(comparePath) - assertNotNull(configCompare) - assertEquals(configCompare, config) + fun `Updating a non-existing property with setProperty returns false`() { + assertTrue(worldConfig.setProperty("invalid-property", false).isFailure) + assertTrue(worldConfig.setProperty("version", 1.1).isFailure) } } diff --git a/src/test/java/org/mvplugins/multiverse/core/world/WorldManagerTest.kt b/src/test/java/org/mvplugins/multiverse/core/world/WorldManagerTest.kt index 0fd856cd..2366c62d 100644 --- a/src/test/java/org/mvplugins/multiverse/core/world/WorldManagerTest.kt +++ b/src/test/java/org/mvplugins/multiverse/core/world/WorldManagerTest.kt @@ -5,7 +5,8 @@ import org.mvplugins.multiverse.core.TestWithMockBukkit import kotlin.test.BeforeTest import kotlin.test.Test -class WorldManagerTest : TestWithMockBukkit() { +class WorldManagerTest : TestWithMockBukkit() { + private lateinit var worldManager: WorldManager @BeforeTest diff --git a/src/test/resources/default_worlds.yml b/src/test/resources/default_worlds.yml index 631963b1..37f8528b 100644 --- a/src/test/resources/default_worlds.yml +++ b/src/test/resources/default_worlds.yml @@ -1,6 +1,6 @@ world: adjust-spawn: false - alias: '' + alias: my world allow-flight: false allow-weather: true auto-heal: true diff --git a/src/test/resources/newworld_worlds.yml b/src/test/resources/newworld_worlds.yml index 7852a07e..745cc596 100644 --- a/src/test/resources/newworld_worlds.yml +++ b/src/test/resources/newworld_worlds.yml @@ -1,6 +1,6 @@ world: adjust-spawn: false - alias: '' + alias: my world allow-flight: false allow-weather: true auto-heal: true