You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CommandFlagHandlerTest.java 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * Copyright (c) 2006-2015 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.commandparser.commands.flags;
  23. import com.dmdirc.events.eventbus.MBassadorEventBus;
  24. import com.dmdirc.commandparser.CommandArguments;
  25. import com.dmdirc.interfaces.CommandController;
  26. import com.dmdirc.events.eventbus.EventBus;
  27. import com.dmdirc.interfaces.WindowModel;
  28. import java.util.Arrays;
  29. import java.util.List;
  30. import java.util.Map;
  31. import org.junit.Test;
  32. import org.junit.runner.RunWith;
  33. import org.junit.runners.Parameterized;
  34. import static org.junit.Assert.assertEquals;
  35. import static org.junit.Assert.assertNotNull;
  36. import static org.junit.Assert.assertNull;
  37. import static org.junit.Assert.assertTrue;
  38. import static org.mockito.Mockito.mock;
  39. import static org.mockito.Mockito.when;
  40. @RunWith(Parameterized.class)
  41. public class CommandFlagHandlerTest {
  42. private static final CommandFlag NO_ARGS_FLAG_1 = new CommandFlag("noargs1");
  43. private static final CommandFlag NO_ARGS_FLAG_2 = new CommandFlag("noargs2", false);
  44. private static final CommandFlag NO_ARGS_FLAG_3 = new CommandFlag("noargs3");
  45. private static final CommandFlag NO_ARGS_FLAG_4 = new CommandFlag("noargs4", false);
  46. private static final CommandFlag IMM_ARGS_FLAG_1 = new CommandFlag("immargs1", true, 1, 0);
  47. private static final CommandFlag IMM_ARGS_FLAG_2 = new CommandFlag("immargs2", true, 2, 0);
  48. private static final CommandFlag DEF_ARGS_FLAG_1 = new CommandFlag("defargs1", true, 0, 1);
  49. private static final CommandFlag DEF_ARGS_FLAG_2 = new CommandFlag("defargs2", true, 0, 2);
  50. private final CommandFlagHandler handler;
  51. private final String input;
  52. private final CommandFlag[] flags;
  53. private final int[] offsets;
  54. public CommandFlagHandlerTest(final String input, final CommandFlag[] flags, final int[] offsets) {
  55. NO_ARGS_FLAG_3.addEnabled(NO_ARGS_FLAG_4);
  56. NO_ARGS_FLAG_3.addDisabled(NO_ARGS_FLAG_1);
  57. handler = new CommandFlagHandler(NO_ARGS_FLAG_1, NO_ARGS_FLAG_2, NO_ARGS_FLAG_3,
  58. NO_ARGS_FLAG_4, IMM_ARGS_FLAG_1, IMM_ARGS_FLAG_2, DEF_ARGS_FLAG_1, DEF_ARGS_FLAG_2);
  59. this.input = input;
  60. this.flags = flags;
  61. this.offsets = offsets;
  62. }
  63. @Test
  64. public void testParse() {
  65. final WindowModel container = mock(WindowModel.class);
  66. final EventBus eventBus = mock(MBassadorEventBus.class);
  67. final CommandController controller = mock(CommandController.class);
  68. when(controller.getCommandChar()).thenReturn('/');
  69. when(controller.getSilenceChar()).thenReturn('.');
  70. when(container.getEventBus()).thenReturn(eventBus);
  71. final Map<CommandFlag, Integer> results = handler.parse(container,
  72. new CommandArguments(controller, input));
  73. if (flags == null) {
  74. assertNull("Command should fail: " + input, results);
  75. } else {
  76. assertNotNull("Command should NOT fail: " + input, results);
  77. assertTrue("Result must contain a null element: " + input,
  78. results.containsKey(null));
  79. int i = 0;
  80. for (CommandFlag flag : flags) {
  81. assertTrue("Result must contain flag: " + flag.getName()
  82. + ": " + input, results.containsKey(flag));
  83. assertEquals("Offset for flag " + flag.getName()
  84. + " should be " + offsets[i] + ": " + input,
  85. offsets[i++], (int) results.get(flag));
  86. }
  87. assertEquals("Global offset should be " + offsets[i],
  88. offsets[i], (int) results.get(null));
  89. assertEquals("Result should contain " + (flags.length + 1)
  90. + " elements: " + input, flags.length + 1, results.size());
  91. }
  92. }
  93. @Parameterized.Parameters
  94. public static List<Object[]> data() {
  95. final Object[][] tests = {
  96. {"/foo", new CommandFlag[0], new int[]{0}},
  97. {"/foo --noargs1", new CommandFlag[]{NO_ARGS_FLAG_1}, new int[]{1, 1}},
  98. {"/foo --noargs2", null, null},
  99. {"/foo --noargs3", new CommandFlag[]{NO_ARGS_FLAG_3}, new int[]{1, 1}},
  100. {"/foo --noargs4", null, null},
  101. {"/foo --noargs5", new CommandFlag[0], new int[]{0}},
  102. {"/foo --noargs3 --noargs4", new CommandFlag[]{NO_ARGS_FLAG_3, NO_ARGS_FLAG_4}, new int[]{1, 2, 2}},
  103. {"/foo --noargs3 --noargs1", null, null},
  104. {"/foo --noargs1 --noargs3", new CommandFlag[]{NO_ARGS_FLAG_1, NO_ARGS_FLAG_3}, new int[]{1, 2, 2}},
  105. {"/foo --noargs3 --noargs4 --noargs1", null, null},
  106. {"/foo --noargs3 --noargs4 --noargs2", null, null},
  107. {"/foo --immargs1", null, null},
  108. {"/foo --noargs1 --immargs1", null, null},
  109. {"/foo --immargs2", null, null},
  110. {"/foo --immargs2 xxx", null, null},
  111. {"/foo --immargs1 arg1", new CommandFlag[]{IMM_ARGS_FLAG_1}, new int[]{1, 2}},
  112. {"/foo --noargs1 --immargs1 arg1", new CommandFlag[]{IMM_ARGS_FLAG_1, NO_ARGS_FLAG_1},
  113. new int[]{2, 1, 3}},
  114. {"/foo --immargs1 arg1 --noargs1", new CommandFlag[]{IMM_ARGS_FLAG_1, NO_ARGS_FLAG_1},
  115. new int[]{1, 3, 3}},
  116. {"/foo --defargs1", null, null},
  117. {"/foo --noargs1 --defargs1", null, null},
  118. {"/foo --defargs2", null, null},
  119. {"/foo --defargs2 xxx", null, null},
  120. {"/foo --defargs1 arg1", new CommandFlag[]{DEF_ARGS_FLAG_1}, new int[]{1, 2}},
  121. {"/foo --noargs1 --defargs1 arg1", new CommandFlag[]{DEF_ARGS_FLAG_1, NO_ARGS_FLAG_1},
  122. new int[]{2, 1, 3}},
  123. {"/foo --defargs1 --noargs1 arg1", new CommandFlag[]{DEF_ARGS_FLAG_1, NO_ARGS_FLAG_1},
  124. new int[]{2, 2, 3}},
  125. };
  126. return Arrays.asList(tests);
  127. }
  128. }