From 87459dab50bd48f7e4175e973f87176839049080 Mon Sep 17 00:00:00 2001 From: Nick Stolbov Date: Thu, 13 Jan 2022 15:00:22 +0100 Subject: [PATCH] Added back function to MenuManager. --- src/main/java/Application/MenuManager.java | 7 +++++ .../java/Application/MenuManagerTest.java | 30 +++++++++++++++++-- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/src/main/java/Application/MenuManager.java b/src/main/java/Application/MenuManager.java index 31979c8..383e5f8 100644 --- a/src/main/java/Application/MenuManager.java +++ b/src/main/java/Application/MenuManager.java @@ -14,6 +14,13 @@ public class MenuManager { this.currentMenu = currentMenu.getMenu(i); } + public void back() throws Exception { + if(!this.inRootMenu()) + this.currentMenu = this.currentMenu.getPreviousMenu(); + else + throw new Exception("Menu is a root menu, a previous menu doesn't exist"); + } + public Menu getCurrentMenu(){ return this.currentMenu; } diff --git a/src/test/java/Application/MenuManagerTest.java b/src/test/java/Application/MenuManagerTest.java index 87bd8e3..d6031de 100644 --- a/src/test/java/Application/MenuManagerTest.java +++ b/src/test/java/Application/MenuManagerTest.java @@ -1,6 +1,7 @@ package Application; import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -28,7 +29,7 @@ class MenuManagerTest { } @Test - void testSelectFunction(){ + void testSelectFunction() { assertEquals(mm.getCurrentMenu(), rootMenu); mm.select(0); assertEquals(mm.getCurrentMenu(), rootMenu.getMenu(0)); @@ -37,9 +38,34 @@ class MenuManagerTest { } @Test - void testIsRootMenuFunction(){ + void testIsRootMenuFunction() { assertTrue(mm.inRootMenu()); mm.select(0); assertFalse(mm.inRootMenu()); } + + @Test + void testBackFunction() { + try { + mm.back(); + Assertions.fail("Test failed: shouldn't be able to go back in rootMenu"); + } catch (Exception e) { + //Should throw Exception because currently in rootMenu + } + mm.select(0); + try { + mm.back(); + assertEquals(mm.getCurrentMenu(), rootMenu); + } catch (Exception e) { + Assertions.fail("Test failed: " + e.getMessage()); + } + mm.select(0); + mm.select(0); + try { + mm.back(); + assertEquals(mm.getCurrentMenu(), rootMenu.getMenu(0)); + } catch (Exception e) { + Assertions.fail("Test failed: " + e.getMessage()); + } + } } \ No newline at end of file