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.

CommandArgumentsTest.java 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * Copyright (c) 2006-2012 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;
  23. import com.dmdirc.interfaces.CommandController;
  24. import java.util.Arrays;
  25. import org.junit.Before;
  26. import org.junit.Test;
  27. import static org.junit.Assert.*;
  28. import static org.mockito.Mockito.*;
  29. public class CommandArgumentsTest {
  30. private CommandController controller;
  31. @Before
  32. public void setUp() {
  33. this.controller = mock(CommandController.class);
  34. when(this.controller.getCommandChar()).thenReturn('/');
  35. when(this.controller.getSilenceChar()).thenReturn('.');
  36. }
  37. /** Ensures the ctor which takes a collection builds the 'line' property. */
  38. @Test
  39. public void testCollectionCtorCreatesLine() {
  40. final CommandArguments args = new CommandArguments(controller,
  41. Arrays.asList("/command", "arg1", "arg2"));
  42. assertEquals("/command arg1 arg2", args.getLine());
  43. }
  44. /** Ensures the ctor works with an empty collection. */
  45. @Test
  46. public void testCollectionCtorWithEmpty() {
  47. final CommandArguments args = new CommandArguments(
  48. controller, Arrays.asList(new String[0]));
  49. assertEquals("", args.getLine());
  50. }
  51. /** Ensures that getStrippedLine returns non-command lines as-is. */
  52. @Test
  53. public void testGetStrippedLineNormal() {
  54. final CommandArguments args = new CommandArguments(controller, "blah blah");
  55. assertEquals("blah blah", args.getStrippedLine());
  56. }
  57. /**
  58. * Ensures that getStrippedLine returns command lines without the command
  59. * char.
  60. */
  61. @Test
  62. public void testGetStrippedLineCommand() {
  63. final CommandArguments args = new CommandArguments(controller, "/blah blah");
  64. assertEquals("blah blah", args.getStrippedLine());
  65. }
  66. /**
  67. * Ensures that getStrippedLine returns silenced lines without the
  68. * command or silence chars.
  69. */
  70. @Test
  71. public void testGetStrippedLineSilenced() {
  72. final CommandArguments args = new CommandArguments(controller, "/.blah blah");
  73. assertEquals("blah blah", args.getStrippedLine());
  74. }
  75. @Test
  76. public void testIsCommand() {
  77. assertTrue(new CommandArguments(controller, "/").isCommand());
  78. assertTrue(new CommandArguments(controller, "/foo bar").isCommand());
  79. assertFalse(new CommandArguments(controller, " /").isCommand());
  80. assertFalse(new CommandArguments(controller, "").isCommand());
  81. assertFalse(new CommandArguments(controller, "foo").isCommand());
  82. }
  83. @Test
  84. public void testIsSilent() {
  85. final char c = '/';
  86. final char s = '.';
  87. assertTrue(new CommandArguments(controller, c + "" + s).isSilent());
  88. assertFalse(new CommandArguments(controller, "f" + s).isSilent());
  89. assertTrue(new CommandArguments(controller, c + "" + s + "foo").isSilent());
  90. assertFalse(new CommandArguments(controller, "").isSilent());
  91. assertFalse(new CommandArguments(controller, "foo").isSilent());
  92. }
  93. @Test
  94. public void testGetLine() {
  95. assertEquals("foo", new CommandArguments(controller, "foo").getLine());
  96. assertEquals("foo bar", new CommandArguments(controller, "foo bar").getLine());
  97. assertEquals("", new CommandArguments(controller, "").getLine());
  98. }
  99. @Test
  100. public void testGetWords() {
  101. final CommandArguments args = new CommandArguments(controller, "a\tb c d e");
  102. assertEquals(5, args.getWords().length);
  103. assertEquals("a", args.getWords()[0]);
  104. assertEquals("b", args.getWords()[1]);
  105. assertEquals("c", args.getWords()[2]);
  106. assertEquals("d", args.getWords()[3]);
  107. assertEquals("e", args.getWords()[4]);
  108. }
  109. @Test
  110. public void testGetArguments() {
  111. final CommandArguments args = new CommandArguments(controller, "a\tb c d e");
  112. assertEquals(4, args.getArguments().length);
  113. assertEquals("b", args.getArguments()[0]);
  114. assertEquals("c", args.getArguments()[1]);
  115. assertEquals("d", args.getArguments()[2]);
  116. assertEquals("e", args.getArguments()[3]);
  117. }
  118. @Test
  119. public void testGetArgumentsAsString() {
  120. assertEquals("b\tc d", new CommandArguments(controller, "a b\tc d").getArgumentsAsString());
  121. assertEquals("", new CommandArguments(controller, "a").getArgumentsAsString());
  122. assertEquals("", new CommandArguments(controller, "a\t \t \t").getArgumentsAsString());
  123. assertEquals("b", new CommandArguments(controller, "a\t \t \tb").getArgumentsAsString());
  124. }
  125. }