Add model test

This commit is contained in:
Oystein Kristoffer Tveit 2021-04-25 23:13:44 +02:00
parent 81c76ae9d4
commit e8b34b7164
2 changed files with 100 additions and 6 deletions

View File

@ -15,8 +15,8 @@ import javafx.scene.Scene;
public class Model { public class Model {
private static boolean fileIsSaved; private static boolean fileIsSaved;
private static Optional<Path> activeFilePath; private static Optional<Path> activeFilePath;
private static Optional<Path> currentProjectPath; private static Optional<Path> activeProjectPath;
private static ProgrammingLanguage currentProgrammingLanguage; private static ProgrammingLanguage activeProgrammingLanguage;
private static String theme; private static String theme;
private static Scene scene; private static Scene scene;
private static SettingsProvider settings; private static SettingsProvider settings;
@ -26,19 +26,23 @@ public class Model {
} }
public static void setActiveFilePath(Optional<Path> path) { public static void setActiveFilePath(Optional<Path> path) {
if (path == null)
throw new IllegalArgumentException("path can not be null");
Model.activeFilePath = path; Model.activeFilePath = path;
} }
public static Optional<Path> getProjectPath() { public static Optional<Path> getProjectPath() {
return currentProjectPath; return activeProjectPath;
} }
public static void setProjectPath(Optional<Path> path) { public static void setProjectPath(Optional<Path> path) {
Model.currentProjectPath = path; if (path == null)
throw new IllegalArgumentException("path can not be null");
Model.activeProjectPath = path;
} }
public static ProgrammingLanguage getLanguage() { public static ProgrammingLanguage getLanguage() {
return currentProgrammingLanguage; return activeProgrammingLanguage;
} }
public static Scene getScene() { public static Scene getScene() {
@ -58,14 +62,20 @@ public class Model {
} }
public static void setTheme(String theme) { public static void setTheme(String theme) {
if (theme == null)
throw new IllegalArgumentException("theme can not be null");
Model.theme = theme; Model.theme = theme;
} }
public static void setLanguage(ProgrammingLanguage language) { public static void setLanguage(ProgrammingLanguage language) {
Model.currentProgrammingLanguage = language; if (language == null)
throw new IllegalArgumentException("language can not be null");
Model.activeProgrammingLanguage = language;
} }
public static void setScene(Scene scene) { public static void setScene(Scene scene) {
if (scene == null)
throw new IllegalArgumentException("scene can not be null");
Model.scene = scene; Model.scene = scene;
} }
@ -74,6 +84,8 @@ public class Model {
} }
public static void setSettingsProvider(SettingsProvider settings) { public static void setSettingsProvider(SettingsProvider settings) {
if (settings == null)
throw new IllegalArgumentException("settings can not be null");
Model.settings = settings; Model.settings = settings;
} }

View File

@ -0,0 +1,82 @@
package app.model;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.mock;
import java.nio.file.Path;
import java.util.Optional;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import app.settings.SettingsProvider;
import javafx.scene.Scene;
public class ModelTest {
@Test
@DisplayName("Test get/set fileIsSaved")
public void testFileIsSaved() {
boolean fileIsSaved = false;
Model.setFileIsSaved(fileIsSaved);
assertEquals(fileIsSaved, Model.getFileIsSaved());
}
@Test
@DisplayName("Test get/set activeFilePath")
public void testActiveFilePath() {
Optional<Path> activeFilePath = Optional.empty();
Model.setActiveFilePath(activeFilePath);
assertEquals(activeFilePath, Model.getActiveFilePath());
assertThrows(IllegalArgumentException.class, () -> Model.setActiveFilePath(null));
}
@Test
@DisplayName("Test get/set projectPath")
public void testActiveProjectPath() {
Optional<Path> projectPath = Optional.empty();
Model.setProjectPath(projectPath);
assertEquals(projectPath, Model.getProjectPath());
assertThrows(IllegalArgumentException.class, () -> Model.setProjectPath(null));
}
@Test
@DisplayName("Test get/set programmingLanguage")
public void testActiveProgrammingLanguage() {
ProgrammingLanguage lang = mock(ProgrammingLanguage.class);
Model.setLanguage(lang);
assertEquals(lang, Model.getLanguage());
assertThrows(IllegalArgumentException.class, () -> Model.setLanguage(null));
}
@Test
@DisplayName("Test get/set theme")
public void testTheme() {
String theme = "Monokai";
Model.setTheme(theme);
assertEquals(theme, Model.getTheme());
assertThrows(IllegalArgumentException.class, () -> Model.setTheme(null));
}
@Test
@DisplayName("Test get/set scene")
public void testScene() {
Scene scene = mock(Scene.class);
Model.setScene(scene);
assertEquals(scene, Model.getScene());
assertThrows(IllegalArgumentException.class, () -> Model.setScene(null));
}
@Test
@DisplayName("Test get/set settingsProvider")
public void testSettingsProvider() {
SettingsProvider settings = mock(SettingsProvider.class);
Model.setSettingsProvider(settings);
assertEquals(settings, Model.getSettingsProvider());
assertThrows(IllegalArgumentException.class, () -> Model.setSettingsProvider(null));
}
}