From 9e38a8f7f46b524f9b225db4daa54340d83992c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Delorme?= Date: Sun, 10 Oct 2021 13:11:34 +0200 Subject: [PATCH 1/2] feat(configTests): Add Configuration tests - create feature file - add DefsSteps classes - update Configuration and properties file to take ina account new values. fix #54 --- .../core/config/Configuration.java | 8 ++- .../java/features/ConfigurationStepdefs.java | 57 +++++++++++++++++++ src/test/resources/camera-1-scene.properties | 1 + src/test/resources/config-test.properties | 15 +++++ .../Configuration_is_loaded_from_file.feature | 17 ++++++ src/test/resources/no-scene.properties | 1 + src/test/resources/test-game-args.properties | 1 + src/test/resources/test-pe.properties | 1 + src/test/resources/test-render.properties | 1 + src/test/resources/test-scene.properties | 1 + src/test/resources/test-sm.properties | 1 + src/test/resources/win-test.properties | 13 +++++ 12 files changed, 116 insertions(+), 1 deletion(-) create mode 100644 src/test/java/features/ConfigurationStepdefs.java create mode 100644 src/test/resources/config-test.properties create mode 100644 src/test/resources/features/Configuration_is_loaded_from_file.feature create mode 100644 src/test/resources/win-test.properties diff --git a/src/main/java/fr/snapgames/fromclasstogame/core/config/Configuration.java b/src/main/java/fr/snapgames/fromclasstogame/core/config/Configuration.java index f4e70b80..63cb0c54 100644 --- a/src/main/java/fr/snapgames/fromclasstogame/core/config/Configuration.java +++ b/src/main/java/fr/snapgames/fromclasstogame/core/config/Configuration.java @@ -6,10 +6,11 @@ public class Configuration { - private ResourceBundle defaultConfig; + public ResourceBundle defaultConfig; public String title = "fromClassToGame"; + public int defaultScreen; public int width = 320; public int height = 200; public double scale = 1.0; @@ -33,10 +34,12 @@ public Configuration(String configurationPath) { public void readValuesFromFile() { this.debugLevel = Integer.parseInt(defaultConfig.getString("game.setup.debugLevel")); + this.defaultScreen = Integer.parseInt(defaultConfig.getString("game.setup.screen")); this.width = Integer.parseInt(defaultConfig.getString("game.setup.width")); this.height = Integer.parseInt(defaultConfig.getString("game.setup.height")); this.scale = Double.parseDouble(defaultConfig.getString("game.setup.scale")); this.FPS = Double.parseDouble(defaultConfig.getString("game.setup.fps")); + this.title = defaultConfig.getString("game.setup.title"); this.scenes = defaultConfig.getString("game.setup.scenes"); this.defaultScene = defaultConfig.getString("game.setup.scene.default"); @@ -69,6 +72,9 @@ public Configuration parseArgs(String[] argv) throws UnknownArgumentException { case "scene": this.defaultScene = values[1]; break; + case "screen": + this.defaultScreen = Integer.parseInt(values[1]); + break; default: throw new UnknownArgumentException(String.format("The argument %s is unknown", arg)); } diff --git a/src/test/java/features/ConfigurationStepdefs.java b/src/test/java/features/ConfigurationStepdefs.java new file mode 100644 index 00000000..7ce44aba --- /dev/null +++ b/src/test/java/features/ConfigurationStepdefs.java @@ -0,0 +1,57 @@ +package features; + +import fr.snapgames.fromclasstogame.core.config.Configuration; +import io.cucumber.java8.En; +import org.junit.Assert; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; + +import static org.junit.Assert.*; + + +public class ConfigurationStepdefs implements En { + private Configuration config; + + public ConfigurationStepdefs() { + Given("the Configuration object is initialized with {string}", (String configFilename) -> { + config = new Configuration(configFilename); + }); + Then("the properties are loaded", () -> { + assertNotNull("properties have not been loaded", config.defaultConfig); + }); + And("the default title is {string}", (String title) -> { + assertEquals("the title has not been set to the correct value ", title, config.title); + }); + And("the default game width is {int}", (Integer width) -> { + assertEquals("the default width has not been set to " + width, width, config.width, 0.1); + }); + And("the default game height is {int}", (Integer height) -> { + assertEquals("the default height has not been set to " + height, height, config.height, 0.1); + }); + And("the default game scale is {int}", (Integer scale) -> { + assertEquals("the default scale has not been set to " + scale, scale, config.scale, 0.1); + }); + And("the default screen is {int}", (Integer screenId) -> { + assertEquals("the default scale has not been set to " + screenId, screenId, config.defaultScreen, 0.1); + }); + And("the default world gravity is {double}", (Double gravity) -> { + assertEquals("the default scale has not been set to " + gravity, gravity, config.gravity, 0.1); + }); + And("the scene {string} is {string}", (String name, String className) -> { + String[] scenes = config.scenes.split(","); + Map mapScenes = new HashMap<>(); + Arrays.asList(scenes).forEach(s -> { + String[] values = s.split(":"); + mapScenes.put(values[0], values[1]); + }); + + assertTrue("the scene " + name + " has not been set to " + className, mapScenes.containsKey(name) && mapScenes.get(name).equals(className)); + }); + And("the default scene is {string}", (String defaultSceneName) -> { + assertEquals("the default Scene has not been set correctly to " + defaultSceneName, defaultSceneName, config.defaultScene); + }); + + } +} diff --git a/src/test/resources/camera-1-scene.properties b/src/test/resources/camera-1-scene.properties index 16a0ced4..e42260fd 100644 --- a/src/test/resources/camera-1-scene.properties +++ b/src/test/resources/camera-1-scene.properties @@ -7,6 +7,7 @@ game.setup.width=320 game.setup.height=200 game.setup.scale=1.0 game.setup.fps=30 +game.setup.screen=-1 game.setup.scenes=testCamera:fr.snapgames.fromclasstogame.test.scenes.TestCameraScene game.setup.scene.default=testCamera game.setup.world.gravity=0.981 diff --git a/src/test/resources/config-test.properties b/src/test/resources/config-test.properties new file mode 100644 index 00000000..1c4a7b4b --- /dev/null +++ b/src/test/resources/config-test.properties @@ -0,0 +1,15 @@ +# From a Class To game +# (c) Fr�d�ric Delorme - 2021-08-02 +# No Scene Test configuration file +game.setup.debugLevel=2 +game.setup.title=config-test +game.setup.width=320 +game.setup.height=200 +game.setup.scale=1.0 +game.setup.fps=30 +game.setup.screen=-1 +game.setup.scenes=test1:fr.snapgames.fromclasstogame.test.scenes.TestScene,\ + test2:fr.snapgames.fromclasstogame.test.scenes.TestScene,\ + test3:fr.snapgames.fromclasstogame.test.scenes.TestScene +game.setup.scene.default=test1 +game.setup.world.gravity=0.981 diff --git a/src/test/resources/features/Configuration_is_loaded_from_file.feature b/src/test/resources/features/Configuration_is_loaded_from_file.feature new file mode 100644 index 00000000..2cbced79 --- /dev/null +++ b/src/test/resources/features/Configuration_is_loaded_from_file.feature @@ -0,0 +1,17 @@ +Feature: The configuration is loaded from a properties file + + All the default configuration values are loaded from a java properties file + + Scenario: Load a set of default values + Given the Configuration object is initialized with "config-test" + Then the properties are loaded + And the default title is "config-test" + And the default game width is 320 + And the default game height is 200 + And the default game scale is 1 + And the default screen is -1 + And the default world gravity is 0.981 + And the scene "test1" is "fr.snapgames.fromclasstogame.test.scenes.TestScene" + And the scene "test2" is "fr.snapgames.fromclasstogame.test.scenes.TestScene" + And the scene "test3" is "fr.snapgames.fromclasstogame.test.scenes.TestScene" + And the default scene is "test1" \ No newline at end of file diff --git a/src/test/resources/no-scene.properties b/src/test/resources/no-scene.properties index 18baed95..e51d74f5 100644 --- a/src/test/resources/no-scene.properties +++ b/src/test/resources/no-scene.properties @@ -7,6 +7,7 @@ game.setup.width=320 game.setup.height=200 game.setup.scale=1.0 game.setup.fps=30 +game.setup.screen=-1 game.setup.scenes= game.setup.scene.default= game.setup.world.gravity=0.981 diff --git a/src/test/resources/test-game-args.properties b/src/test/resources/test-game-args.properties index 7be08761..bf225fdb 100644 --- a/src/test/resources/test-game-args.properties +++ b/src/test/resources/test-game-args.properties @@ -7,6 +7,7 @@ game.setup.width=320 game.setup.height=200 game.setup.scale=1.0 game.setup.fps=30 +game.setup.screen=-1 game.setup.scenes=test:fr.snapgames.fromclasstogame.test.scenes.TestScene game.setup.scene.default=test game.setup.world.gravity=0.981 diff --git a/src/test/resources/test-pe.properties b/src/test/resources/test-pe.properties index af1228a3..a18f2c79 100644 --- a/src/test/resources/test-pe.properties +++ b/src/test/resources/test-pe.properties @@ -7,6 +7,7 @@ game.setup.width=320 game.setup.height=200 game.setup.scale=1.0 game.setup.fps=30 +game.setup.screen=-1 game.setup.scenes=test:fr.snapgames.fromclasstogame.test.scenes.TestScene game.setup.scene.default=test game.setup.world.gravity=0.981 diff --git a/src/test/resources/test-render.properties b/src/test/resources/test-render.properties index 8f433aea..66d4914d 100644 --- a/src/test/resources/test-render.properties +++ b/src/test/resources/test-render.properties @@ -7,6 +7,7 @@ game.setup.width=320 game.setup.height=200 game.setup.scale=1.0 game.setup.fps=30 +game.setup.screen=-1 game.setup.scenes=testrender:fr.snapgames.fromclasstogame.test.scenes.TestRenderScene game.setup.scene.default=testrender game.setup.world.gravity=0.981 diff --git a/src/test/resources/test-scene.properties b/src/test/resources/test-scene.properties index 2ba11539..69c811b0 100644 --- a/src/test/resources/test-scene.properties +++ b/src/test/resources/test-scene.properties @@ -7,6 +7,7 @@ game.setup.width=320 game.setup.height=200 game.setup.scale=1.0 game.setup.fps=30 +game.setup.screen=-1 game.setup.scenes=test1:fr.snapgames.fromclasstogame.test.scenes.TestScene,test2:fr.snapgames.fromclasstogame.test.scenes.TestScene,test3:fr.snapgames.fromclasstogame.test.scenes.TestScene game.setup.scene.default=test3 game.setup.world.gravity=0.981 diff --git a/src/test/resources/test-sm.properties b/src/test/resources/test-sm.properties index 0046c17c..0b03d4b2 100644 --- a/src/test/resources/test-sm.properties +++ b/src/test/resources/test-sm.properties @@ -7,6 +7,7 @@ game.setup.width=320 game.setup.height=200 game.setup.scale=1.0 game.setup.fps=30 +game.setup.screen=-1 game.setup.scenes= game.setup.scene.default= game.setup.world.gravity=0.981 \ No newline at end of file diff --git a/src/test/resources/win-test.properties b/src/test/resources/win-test.properties new file mode 100644 index 00000000..4b0bacbc --- /dev/null +++ b/src/test/resources/win-test.properties @@ -0,0 +1,13 @@ +# From a Class To game +# (c) Fr�d�ric Delorme - 2021-08-02 +# No Scene Test configuration file +game.setup.debugLevel=2 +game.setup.title=window-title-test +game.setup.width=320 +game.setup.height=200 +game.setup.scale=1.0 +game.setup.fps=30 +game.setup.screen=-1 +game.setup.scenes=test1:fr.snapgames.fromclasstogame.test.scenes.TestScene,test2:fr.snapgames.fromclasstogame.test.scenes.TestScene,test3:fr.snapgames.fromclasstogame.test.scenes.TestScene +game.setup.scene.default=test1 +game.setup.world.gravity=0.981 From 2b01ad3a7b27e845da4b7ca4533336eb3c1b6b47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Delorme?= Date: Sun, 10 Oct 2021 15:49:48 +0200 Subject: [PATCH 2/2] fix(codacy): codacy standard fix import --- src/test/java/features/ConfigurationStepdefs.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/test/java/features/ConfigurationStepdefs.java b/src/test/java/features/ConfigurationStepdefs.java index 7ce44aba..37b48bfe 100644 --- a/src/test/java/features/ConfigurationStepdefs.java +++ b/src/test/java/features/ConfigurationStepdefs.java @@ -2,7 +2,6 @@ import fr.snapgames.fromclasstogame.core.config.Configuration; import io.cucumber.java8.En; -import org.junit.Assert; import java.util.Arrays; import java.util.HashMap;