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

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