From 075988f327227127a5085473e96ba27b7c96232f Mon Sep 17 00:00:00 2001 From: h7x4 Date: Mon, 26 Apr 2021 00:31:33 +0200 Subject: [PATCH] Test all branches of LanguageChangedEvent --- .../java/app/events/LanguageChangedEvent.java | 12 ++++++++---- .../app/events/LanguageChangedEventTest.java | 19 +++++++++++++++++++ 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/src/main/java/app/events/LanguageChangedEvent.java b/src/main/java/app/events/LanguageChangedEvent.java index 42709b1..492d7ea 100644 --- a/src/main/java/app/events/LanguageChangedEvent.java +++ b/src/main/java/app/events/LanguageChangedEvent.java @@ -17,18 +17,22 @@ public class LanguageChangedEvent extends Event { public LanguageChangedEvent(String language) { this.language = language; - switch (language) { + switch (language.toLowerCase()) { - case "Java": + case "java": Model.setLanguage(new Java()); break; - case "Markdown": + case "markdown": Model.setLanguage(new Markdown()); break; - default: + case "empty": + Model.setLanguage(new Empty()); break; + + default: + throw new IllegalArgumentException("Could not recognize language: " + language); } } diff --git a/src/test/java/app/events/LanguageChangedEventTest.java b/src/test/java/app/events/LanguageChangedEventTest.java index e3c686f..ffbc255 100644 --- a/src/test/java/app/events/LanguageChangedEventTest.java +++ b/src/test/java/app/events/LanguageChangedEventTest.java @@ -1,9 +1,12 @@ package app.events; +import static org.junit.jupiter.api.Assertions.assertThrows; + import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import app.model.Model; +import app.model.languages.Empty; import app.model.languages.Java; import app.testing.EventTestTemplate; @@ -15,5 +18,21 @@ public class LanguageChangedEventTest extends EventTestTemplate { new LanguageChangedEvent("Java"); this.mockModel.verify(() -> Model.setLanguage(new Java())); } + + + @Test + @DisplayName("Change into every possible language") + private void checkPossibleLanguages() { + new LanguageChangedEvent("Java"); + this.mockModel.verify(() -> Model.setLanguage(new Java())); + + new LanguageChangedEvent("Markdown"); + this.mockModel.verify(() -> Model.setLanguage(new Java())); + + new LanguageChangedEvent("Empty"); + this.mockModel.verify(() -> Model.setLanguage(new Empty())); + + assertThrows(IllegalArgumentException.class, () -> new LanguageChangedEvent("")); + } }