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. 34
      src/test/java/Application/MenuTest.java

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

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

34
src/test/java/Application/MenuTest.java

@ -6,12 +6,22 @@ import org.junit.jupiter.api.Test;
import java.util.ArrayList; 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 { class MenuTest {
Menu rootMenu;
@BeforeEach @BeforeEach
void setUp() { 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 @AfterEach
@ -19,20 +29,18 @@ class MenuTest {
} }
@Test @Test
void testCorrectName(){
Menu menu = new Menu("Item 1");
assertTrue(menu.getName().equals("Item 1"));
void testCorrectName() {
assertEquals("Root", rootMenu.getName());
} }
@Test @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));
void testAddMenuFunction() {
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