Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

CommandFlagHandlerTest.java 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * Copyright (c) 2006-2014 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.FrameContainer;
  24. import com.dmdirc.commandparser.CommandArguments;
  25. import com.dmdirc.interfaces.CommandController;
  26. import java.util.Arrays;
  27. import java.util.List;
  28. import java.util.Map;
  29. import org.junit.Test;
  30. import org.junit.runner.RunWith;
  31. import org.junit.runners.Parameterized;
  32. import static org.junit.Assert.*;
  33. import static org.mockito.Mockito.*;
  34. @RunWith(Parameterized.class)
  35. public class CommandFlagHandlerTest {
  36. private static final CommandFlag NO_ARGS_FLAG_1 = new CommandFlag("noargs1");
  37. private static final CommandFlag NO_ARGS_FLAG_2 = new CommandFlag("noargs2", false);
  38. private static final CommandFlag NO_ARGS_FLAG_3 = new CommandFlag("noargs3");
  39. private static final CommandFlag NO_ARGS_FLAG_4 = new CommandFlag("noargs4", false);
  40. private static final CommandFlag IMM_ARGS_FLAG_1 = new CommandFlag("immargs1", true, 1, 0);
  41. private static final CommandFlag IMM_ARGS_FLAG_2 = new CommandFlag("immargs2", true, 2, 0);
  42. private static final CommandFlag DEF_ARGS_FLAG_1 = new CommandFlag("defargs1", true, 0, 1);
  43. private static final CommandFlag DEF_ARGS_FLAG_2 = new CommandFlag("defargs2", true, 0, 2);
  44. private final CommandFlagHandler handler;
  45. private final String input;
  46. private final CommandFlag[] flags;
  47. private final int[] offsets;
  48. public CommandFlagHandlerTest(final String input, final CommandFlag[] flags, final int[] offsets) {
  49. NO_ARGS_FLAG_3.addEnabled(NO_ARGS_FLAG_4);
  50. NO_ARGS_FLAG_3.addDisabled(NO_ARGS_FLAG_1);
  51. handler = new CommandFlagHandler(NO_ARGS_FLAG_1, NO_ARGS_FLAG_2, NO_ARGS_FLAG_3,
  52. NO_ARGS_FLAG_4, IMM_ARGS_FLAG_1, IMM_ARGS_FLAG_2, DEF_ARGS_FLAG_1, DEF_ARGS_FLAG_2);
  53. this.input = input;
  54. this.flags = flags;
  55. this.offsets = offsets;
  56. }
  57. @Test
  58. public void testParse() {
  59. final FrameContainer container = mock(FrameContainer.class);
  60. final CommandController controller = mock(CommandController.class);
  61. when(controller.getCommandChar()).thenReturn('/');
  62. when(controller.getSilenceChar()).thenReturn('.');
  63. final Map<CommandFlag, Integer> results = handler.parse(container,
  64. new CommandArguments(controller, input));
  65. if (flags == null) {
  66. assertNull("Command should fail: " + input, results);
  67. } else {
  68. assertNotNull("Command should NOT fail: " + input, results);
  69. assertTrue("Result must contain a null element: " + input,
  70. results.containsKey(null));
  71. int i = 0;
  72. for (CommandFlag flag : flags) {
  73. assertTrue("Result must contain flag: " + flag.getName()
  74. + ": " + input, results.containsKey(flag));
  75. assertEquals("Offset for flag " + flag.getName()
  76. + " should be " + offsets[i] + ": " + input,
  77. offsets[i++], (int) results.get(flag));
  78. }
  79. assertEquals("Global offset should be " + offsets[i],
  80. offsets[i], (int) results.get(null));
  81. assertEquals("Result should contain " + (flags.length + 1)
  82. + " elements: " + input, flags.length + 1, results.size());
  83. }
  84. }
  85. @Parameterized.Parameters
  86. public static List<Object[]> data() {
  87. final Object[][] tests = {
  88. {"/foo", new CommandFlag[0], new int[]{0}},
  89. {"/foo --noargs1", new CommandFlag[]{NO_ARGS_FLAG_1}, new int[]{1, 1}},
  90. {"/foo --noargs2", null, null},
  91. {"/foo --noargs3", new CommandFlag[]{NO_ARGS_FLAG_3}, new int[]{1, 1}},
  92. {"/foo --noargs4", null, null},
  93. {"/foo --noargs5", new CommandFlag[0], new int[]{0}},
  94. {"/foo --noargs3 --noargs4", new CommandFlag[]{NO_ARGS_FLAG_3, NO_ARGS_FLAG_4}, new int[]{1, 2, 2}},
  95. {"/foo --noargs3 --noargs1", null, null},
  96. {"/foo --noargs1 --noargs3", new CommandFlag[]{NO_ARGS_FLAG_1, NO_ARGS_FLAG_3}, new int[]{1, 2, 2}},
  97. {"/foo --noargs3 --noargs4 --noargs1", null, null},
  98. {"/foo --noargs3 --noargs4 --noargs2", null, null},
  99. {"/foo --immargs1", null, null},
  100. {"/foo --noargs1 --immargs1", null, null},
  101. {"/foo --immargs2", null, null},
  102. {"/foo --immargs2 xxx", null, null},
  103. {"/foo --immargs1 arg1", new CommandFlag[]{IMM_ARGS_FLAG_1}, new int[]{1, 2}},
  104. {"/foo --noargs1 --immargs1 arg1", new CommandFlag[]{IMM_ARGS_FLAG_1, NO_ARGS_FLAG_1},
  105. new int[]{2, 1, 3}},
  106. {"/foo --immargs1 arg1 --noargs1", new CommandFlag[]{IMM_ARGS_FLAG_1, NO_ARGS_FLAG_1},
  107. new int[]{1, 3, 3}},
  108. {"/foo --defargs1", null, null},
  109. {"/foo --noargs1 --defargs1", null, null},
  110. {"/foo --defargs2", null, null},
  111. {"/foo --defargs2 xxx", null, null},
  112. {"/foo --defargs1 arg1", new CommandFlag[]{DEF_ARGS_FLAG_1}, new int[]{1, 2}},
  113. {"/foo --noargs1 --defargs1 arg1", new CommandFlag[]{DEF_ARGS_FLAG_1, NO_ARGS_FLAG_1},
  114. new int[]{2, 1, 3}},
  115. {"/foo --defargs1 --noargs1 arg1", new CommandFlag[]{DEF_ARGS_FLAG_1, NO_ARGS_FLAG_1},
  116. new int[]{2, 2, 3}},
  117. };
  118. return Arrays.asList(tests);
  119. }
  120. }