1 module dli.menu_items.nested_menu_menu_item;
2 
3 import dli.text_menu : MenuItem;
4 import dli.i_text_menu;
5 
6 /// Shortcut class to ease nesting menus
7 public class NestedMenuMenuItem : MenuItem
8 {
9     /// Create a new MenuItem that will run nestedMenu when executed
10     public this(ITextMenu nestedMenu, string displayString, bool enabled = true)
11     in
12     {
13         assert(nestedMenu !is null);
14         assert(displayString !is null);
15     }
16     do
17     {
18         super(displayString, {nestedMenu.run();}, enabled);
19     }
20 }
21 
22 // TESTS
23 version(unittest)
24 {
25     import test.dli.mock_menu;
26     import test.dli.mock_menu_item;
27 
28     @("Test NestedMenuMenuItem properly stops and starts nested menus")
29     unittest
30     {
31         auto parentMenu = new MockMenu();
32         /*
33             We employ this constructor here so that
34             nestedMenu uses parentMenu's input and 
35             output streams.
36         */
37         auto nestedMenu = new MockMenu(parentMenu);
38         auto nestedMenuMenuItem = new NestedMenuMenuItem(nestedMenu, "");
39         auto itemInNestedMenu = new MockMenuItem();
40 
41         parentMenu.addItem(nestedMenuMenuItem, 1);
42         nestedMenu.addItem(itemInNestedMenu, 1);
43 
44         // First, enter the nested menu and exit it, without selecting the inner item
45         parentMenu.mock_writeln("1");
46         parentMenu.mock_writeExitRequest(); // This should exit the nested menu
47         parentMenu.mock_writeExitRequest(); // This should the parent menu
48         parentMenu.run();
49 
50         assert(!itemInNestedMenu.executed);
51 
52         // Now try to execute it
53         parentMenu.mock_writeln("1");
54         parentMenu.mock_writeln("1");
55         parentMenu.mock_writeExitRequest(); 
56         parentMenu.mock_writeExitRequest();
57         parentMenu.run();
58 
59         assert(itemInNestedMenu.executed);
60     }
61 }