Browse Source

Added previousMenu function to Menu and did refactoring

feature-menu
Nick Stolbov 3 years ago
parent
commit
95dc251cd8
  1. 15
      src/main/java/Application/Menu.java
  2. 30
      src/test/java/Application/MenuTest.java

15
src/main/java/Application/Menu.java

@ -5,14 +5,17 @@ import java.util.ArrayList;
public class Menu {
private String name;
private Menu previousMenu;
private ArrayList<Menu> subMenuList;
public Menu(String name){
setName(name);
setPreviousMenu(null);
subMenuList = new ArrayList<>();
}
public void addMenu(Menu menu){
menu.setPreviousMenu(this);
subMenuList.add(menu);
}
@ -24,7 +27,19 @@ public class Menu {
this.name = name;
}
public Menu getMenu(int i){
return getSubMenuList().get(i);
}
public ArrayList<Menu> getSubMenuList() {
return this.subMenuList;
}
private void setPreviousMenu(Menu menu){
this.previousMenu = menu;
}
public Menu getPreviousMenu(){
return this.previousMenu;
}
}

30
src/test/java/Application/MenuTest.java

@ -6,12 +6,22 @@ import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
class MenuTest {
Menu rootMenu;
@BeforeEach
void setUp() {
rootMenu = new Menu("Root");
Menu menu1 = new Menu("Menu1");
Menu menu2 = new Menu("Menu2");
Menu menu1_1 = new Menu("Menu1.1");
menu1.addMenu(menu1_1);
rootMenu.addMenu(menu1);
rootMenu.addMenu(menu2);
}
@AfterEach
@ -20,19 +30,17 @@ class MenuTest {
@Test
void testCorrectName() {
Menu menu = new Menu("Item 1");
assertTrue(menu.getName().equals("Item 1"));
assertEquals("Root", rootMenu.getName());
}
@Test
void testAddMenuFunction() {
ArrayList<Menu> menus = new ArrayList<>();
Menu menu1 = new Menu("Menu 1");
Menu menu2 = new Menu("Menu 2");
Menu menu1_1 = new Menu("Menu 1.1");
menu1.addMenu(menu1_1);
menus.add(menu1);
menus.add(menu2);
assertTrue(menus.get(0).equals(menu1) && menus.get(1).equals(menu2) && menus.get(0).getSubMenuList().get(0).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
void testPreviousMenuFunction(){
assertTrue(rootMenu.getPreviousMenu() == null && rootMenu.getMenu(0).getPreviousMenu().getName().equals("Root") && rootMenu.getMenu(0).getMenu(0).getPreviousMenu().getName().equals("Menu1"));
}
}
Loading…
Cancel
Save