Browse Source

Merge branch 'feature-menu' into development

feature-cli
Nick Stolbov 3 years ago
parent
commit
143ceea823
  1. 41
      src/main/java/Application/MenuManager.java
  2. 50
      src/test/java/Application/MenuManagerTest.java
  3. 6
      src/test/java/Application/MenuTest.java

41
src/main/java/Application/MenuManager.java

@ -1,16 +1,33 @@
package Application; package Application;
import java.util.ArrayList;
public class MenuManager { public class MenuManager {
private Menu rootMenu;
private ArrayList<Menu> menuList;
private Menu currentMenu; private Menu currentMenu;
public MenuManager(Menu rootMenu){
this.rootMenu = rootMenu;
this.currentMenu = rootMenu;
public MenuManager() {
menuList = new ArrayList<>();
currentMenu = null;
}
public void addMenu(Menu menu) {
menuList.add(menu);
}
public int getSize() {
if(inRootMenu())
return menuList.size();
return currentMenu.getSubMenuList().size();
} }
public void select(int i) { public void select(int i) {
if(i < 0 || i >= this.getSize())
return;
if (currentMenu == null)
this.currentMenu = menuList.get(i);
else
this.currentMenu = currentMenu.getMenu(i); this.currentMenu = currentMenu.getMenu(i);
} }
@ -18,7 +35,7 @@ public class MenuManager {
if (!this.inRootMenu()) if (!this.inRootMenu())
this.currentMenu = this.currentMenu.getPreviousMenu(); this.currentMenu = this.currentMenu.getPreviousMenu();
else else
throw new Exception("Menu is a root menu, a previous menu doesn't exist");
throw new Exception("Menu is in the root menu, a previous menu doesn't exist");
} }
public Menu getCurrentMenu() { public Menu getCurrentMenu() {
@ -26,7 +43,19 @@ public class MenuManager {
} }
public boolean inRootMenu() { public boolean inRootMenu() {
return this.currentMenu.equals(this.rootMenu);
return this.currentMenu == null;
}
public String getFormattedMenuList() {
StringBuilder result = new StringBuilder();
ArrayList<Menu> baseMenuList = this.menuList;
if (!inRootMenu())
baseMenuList = currentMenu.getSubMenuList();
for (int i = 0; i < baseMenuList.size(); i++)
result.append(i + 1).append(": ").append(baseMenuList.get(i).getName()).append("\n");
return result.toString();
} }
} }

50
src/test/java/Application/MenuManagerTest.java

@ -10,18 +10,16 @@ import static org.junit.jupiter.api.Assertions.*;
class MenuManagerTest { class MenuManagerTest {
MenuManager mm; MenuManager mm;
Menu rootMenu;
@BeforeEach @BeforeEach
void setUp() { void setUp() {
rootMenu = new Menu("Root");
Menu menu1 = new Menu("Menu1"); Menu menu1 = new Menu("Menu1");
Menu menu2 = new Menu("Menu2"); Menu menu2 = new Menu("Menu2");
Menu menu1_1 = new Menu("Menu1.1"); Menu menu1_1 = new Menu("Menu1.1");
menu1.addMenu(menu1_1); menu1.addMenu(menu1_1);
rootMenu.addMenu(menu1);
rootMenu.addMenu(menu2);
mm = new MenuManager(rootMenu);
mm = new MenuManager();
mm.addMenu(menu1);
mm.addMenu(menu2);
} }
@AfterEach @AfterEach
@ -29,23 +27,33 @@ class MenuManagerTest {
} }
@Test @Test
void testSelectFunction() {
assertEquals(mm.getCurrentMenu(), rootMenu);
void select() {
assertNull(mm.getCurrentMenu());
//Negative index
mm.select(-1);
assertNull(mm.getCurrentMenu());
mm.select(0);
assertEquals(mm.getCurrentMenu().getName(), "Menu1");
//Dose nothing when index to big
mm.select(1);
assertEquals(mm.getCurrentMenu().getName(), "Menu1");
mm.select(0); mm.select(0);
assertEquals(mm.getCurrentMenu(), rootMenu.getMenu(0));
assertEquals(mm.getCurrentMenu().getName(), "Menu1.1");
mm.select(0); mm.select(0);
assertEquals(mm.getCurrentMenu(), rootMenu.getMenu(0).getMenu(0));
assertEquals(mm.getCurrentMenu().getName(), "Menu1.1");
} }
@Test @Test
void testIsRootMenuFunction() {
void inRootMenu() {
assertTrue(mm.inRootMenu()); assertTrue(mm.inRootMenu());
mm.select(0); mm.select(0);
assertFalse(mm.inRootMenu()); assertFalse(mm.inRootMenu());
} }
@Test @Test
void testBackFunction() {
void back() {
try { try {
mm.back(); mm.back();
Assertions.fail("Test failed: shouldn't be able to go back in rootMenu"); Assertions.fail("Test failed: shouldn't be able to go back in rootMenu");
@ -55,7 +63,7 @@ class MenuManagerTest {
mm.select(0); mm.select(0);
try { try {
mm.back(); mm.back();
assertEquals(mm.getCurrentMenu(), rootMenu);
assertTrue(mm.inRootMenu());
} catch (Exception e) { } catch (Exception e) {
Assertions.fail("Test failed: " + e.getMessage()); Assertions.fail("Test failed: " + e.getMessage());
} }
@ -63,9 +71,25 @@ class MenuManagerTest {
mm.select(0); mm.select(0);
try { try {
mm.back(); mm.back();
assertEquals(mm.getCurrentMenu(), rootMenu.getMenu(0));
assertEquals(mm.getCurrentMenu().getName(), "Menu1");
} catch (Exception e) { } catch (Exception e) {
Assertions.fail("Test failed: " + e.getMessage()); Assertions.fail("Test failed: " + e.getMessage());
} }
} }
@Test
void getFormattedMenuList(){
assertEquals(mm.getFormattedMenuList(), "1: Menu1\n2: Menu2\n");
mm.select(0);
assertEquals(mm.getFormattedMenuList(), "1: Menu1.1\n");
}
@Test
void getSize() {
assertEquals(mm.getSize(), 2);
mm.select(0);
assertEquals(mm.getSize(), 1);
mm.select(0);
assertEquals(mm.getSize(), 0);
}
} }

6
src/test/java/Application/MenuTest.java

@ -29,17 +29,17 @@ class MenuTest {
} }
@Test @Test
void testCorrectName() {
void getName() {
assertEquals("Root", rootMenu.getName()); assertEquals("Root", rootMenu.getName());
} }
@Test @Test
void testAddMenuFunction() {
void addMenu() {
assertTrue(rootMenu.getMenu(0).getName().equals("Menu1") && rootMenu.getMenu(1).getName().equals("Menu2") && rootMenu.getMenu(0).getMenu(0).getName().equals("Menu1.1")); assertTrue(rootMenu.getMenu(0).getName().equals("Menu1") && rootMenu.getMenu(1).getName().equals("Menu2") && rootMenu.getMenu(0).getMenu(0).getName().equals("Menu1.1"));
} }
@Test @Test
void testPreviousMenuFunction(){
void getPreviousMenu(){
assertTrue(rootMenu.getPreviousMenu() == null && rootMenu.getMenu(0).getPreviousMenu().getName().equals("Root") && rootMenu.getMenu(0).getMenu(0).getPreviousMenu().getName().equals("Menu1")); assertTrue(rootMenu.getPreviousMenu() == null && rootMenu.getMenu(0).getPreviousMenu().getName().equals("Root") && rootMenu.getMenu(0).getMenu(0).getPreviousMenu().getName().equals("Menu1"));
} }
Loading…
Cancel
Save