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.7KB

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