/* * Copyright (c) 2006-2011 DMDirc Developers * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ package com.dmdirc.commandparser; import java.util.Arrays; import com.dmdirc.config.IdentityManager; import com.dmdirc.config.InvalidIdentityFileException; import org.junit.BeforeClass; import org.junit.Test; import static org.junit.Assert.*; public class CommandArgumentsTest { @BeforeClass public static void beforeClass() throws InvalidIdentityFileException { IdentityManager.load(); } /** Ensures the ctor which takes a collection builds the 'line' property. */ @Test public void testCollectionCtorCreatesLine() { final CommandArguments args = new CommandArguments(Arrays.asList( "/command", "arg1", "arg2")); assertEquals("/command arg1 arg2", args.getLine()); } /** Ensures the ctor works with an empty collection. */ @Test public void testCollectionCtorWithEmpty() { final CommandArguments args = new CommandArguments(Arrays.asList( new String[0])); assertEquals("", args.getLine()); } /** Ensures that getStrippedLine returns non-command lines as-is. */ @Test public void testGetStrippedLineNormal() { final CommandArguments args = new CommandArguments("blah blah"); assertEquals("blah blah", args.getStrippedLine()); } /** * Ensures that getStrippedLine returns command lines without the command * char. */ @Test public void testGetStrippedLineCommand() { final CommandArguments args = new CommandArguments("/blah blah"); assertEquals("blah blah", args.getStrippedLine()); } /** * Ensures that getStrippedLine returns silenced lines without the * command or silence chars. */ @Test public void testGetStrippedLineSilenced() { final CommandArguments args = new CommandArguments("/.blah blah"); assertEquals("blah blah", args.getStrippedLine()); } @Test public void testIsCommand() { assertTrue(new CommandArguments(CommandManager.getCommandManager().getCommandChar() + "").isCommand()); assertTrue(new CommandArguments(CommandManager.getCommandManager().getCommandChar() + "foo bar").isCommand()); assertFalse(new CommandArguments(" " + CommandManager.getCommandManager().getCommandChar()).isCommand()); assertFalse(new CommandArguments("").isCommand()); assertFalse(new CommandArguments("foo").isCommand()); } @Test public void testIsSilent() { final char c = CommandManager.getCommandManager().getCommandChar(); final char s = CommandManager.getCommandManager().getSilenceChar(); assertTrue(new CommandArguments(c + "" + s).isSilent()); assertFalse(new CommandArguments("f" + s).isSilent()); assertTrue(new CommandArguments(c + "" + s + "foo").isSilent()); assertFalse(new CommandArguments("").isSilent()); assertFalse(new CommandArguments("foo").isSilent()); } @Test public void testGetLine() { assertEquals("foo", new CommandArguments("foo").getLine()); assertEquals("foo bar", new CommandArguments("foo bar").getLine()); assertEquals("", new CommandArguments("").getLine()); } @Test public void testGetWords() { final CommandArguments args = new CommandArguments("a\tb c d e"); assertEquals(5, args.getWords().length); assertEquals("a", args.getWords()[0]); assertEquals("b", args.getWords()[1]); assertEquals("c", args.getWords()[2]); assertEquals("d", args.getWords()[3]); assertEquals("e", args.getWords()[4]); } @Test public void testGetArguments() { final CommandArguments args = new CommandArguments("a\tb c d e"); assertEquals(4, args.getArguments().length); assertEquals("b", args.getArguments()[0]); assertEquals("c", args.getArguments()[1]); assertEquals("d", args.getArguments()[2]); assertEquals("e", args.getArguments()[3]); } @Test public void testGetArgumentsAsString() { assertEquals("b\tc d", new CommandArguments("a b\tc d").getArgumentsAsString()); assertEquals("", new CommandArguments("a").getArgumentsAsString()); assertEquals("", new CommandArguments("a\t \t \t").getArgumentsAsString()); assertEquals("b", new CommandArguments("a\t \t \tb").getArgumentsAsString()); } }