Crossfire JXClient, Trunk  R20561
CommandExpanderTest.java
Go to the documentation of this file.
1 /*
2  * This file is part of JXClient, the Fullscreen Java Crossfire Client.
3  *
4  * JXClient is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * JXClient is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with JXClient; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17  *
18  * Copyright (C) 2005-2008 Yann Chachkoff.
19  * Copyright (C) 2006-2011 Andreas Kirschbaum.
20  */
21 
22 package com.realtime.crossfire.jxclient.commands;
23 
26 import java.util.Arrays;
27 import java.util.Collection;
28 import org.jetbrains.annotations.NotNull;
29 import org.junit.Assert;
30 import org.junit.Test;
31 
36 public class CommandExpanderTest {
37 
42  @Test
43  public void testExpandSingle() {
44  final Commands commands = new Commands();
45  check("xyz", commands, new CommandExec(null, "xyz"));
46  }
47 
52  @Test
53  public void testExpandMultiple() {
54  final Commands commands = new Commands();
55  check("xyz;abc;abc", commands, new CommandExec(null, "xyz"), new CommandExec(null, "abc"), new CommandExec(null, "abc"));
56  }
57 
62  @Test
64  final Commands commands = new Commands();
65  check("xyz;abc", commands, new CommandExec(null, "xyz"), new CommandExec(null, "abc"));
66  check("xyz ;abc", commands, new CommandExec(null, "xyz"), new CommandExec(null, "abc"));
67  check("xyz; abc", commands, new CommandExec(null, "xyz"), new CommandExec(null, "abc"));
68  check("xyz ; abc", commands, new CommandExec(null, "xyz"), new CommandExec(null, "abc"));
69  }
70 
75  @Test
76  public void testExpandIgnoreEmpty() {
77  final Commands commands = new Commands();
78  check("", commands);
79  check(";;;", commands);
80  check(" ; xyz ; abc ; ", commands, new CommandExec(null, "xyz"), new CommandExec(null, "abc"));
81  }
82 
87  @Test
88  public void testExpandPredefined() {
89  final CrossfireServerConnection crossfireServerConnection = new TestCrossfireServerConnection();
90  final Commands commands = new Commands();
91  final TestCommand cmd = new TestCommand("cmd", false, crossfireServerConnection);
92  commands.addCommand(cmd);
93  check("cmd", commands, new CommandExec(cmd, ""));
94  check("cmd abc", commands, new CommandExec(cmd, "abc"));
95  check("cm", commands, new CommandExec(null, "cm"));
96  check("cmd2", commands, new CommandExec(null, "cmd2"));
97  check("cmd;cmd;cm;cmd;cmd2", commands, new CommandExec(cmd, ""), new CommandExec(cmd, ""), new CommandExec(null, "cm"), new CommandExec(cmd, ""), new CommandExec(null, "cmd2"));
98  }
99 
104  @Test
105  public void testExpandArguments() {
106  final CrossfireServerConnection crossfireServerConnection = new TestCrossfireServerConnection();
107  final Commands commands = new Commands();
108  final TestCommand cmd = new TestCommand("cmd", false, crossfireServerConnection);
109  commands.addCommand(cmd);
110  check("cmd", commands, new CommandExec(cmd, ""));
111  check("cmd abc", commands, new CommandExec(cmd, "abc"));
112  check("cmd abc", commands, new CommandExec(cmd, "abc"));
113  check("cm", commands, new CommandExec(null, "cm"));
114  check("cm abc", commands, new CommandExec(null, "cm abc"));
115  check("cm abc", commands, new CommandExec(null, "cm abc"));
116  }
117 
123  @Test
125  final CrossfireServerConnection crossfireServerConnection = new TestCrossfireServerConnection();
126  final Commands commands = new Commands();
127  final TestCommand cmd = new TestCommand("cmd", false, crossfireServerConnection);
128  commands.addCommand(cmd);
129  check("cmd", commands, new CommandExec(cmd, ""));
130  check("Cmd", commands, new CommandExec(cmd, ""));
131  check("cmD", commands, new CommandExec(cmd, ""));
132  check("CMD", commands, new CommandExec(cmd, ""));
133  }
134 
139  @Test
140  public void testExpandAllArguments() {
141  final CrossfireServerConnection crossfireServerConnection = new TestCrossfireServerConnection();
142  final Commands commands = new Commands();
143  final TestCommand cmd = new TestCommand("cmd", false, crossfireServerConnection);
144  final TestCommand bind = new TestCommand("bind", true, crossfireServerConnection);
145  commands.addCommand(cmd);
146  commands.addCommand(bind);
147  check("cmd bind", commands, new CommandExec(cmd, "bind"));
148  check("bind cmd", commands, new CommandExec(bind, "cmd"));
149  check("cmd bind;bind cmd", commands, new CommandExec(cmd, "bind"), new CommandExec(bind, "cmd"));
150  check("bind cmd;cmd bind", commands, new CommandExec(bind, "cmd;cmd bind"));
151  check("cmd bind;bind cmd;cmd bind", commands, new CommandExec(cmd, "bind"), new CommandExec(bind, "cmd;cmd bind"));
152  check("bind cmd;cmd bind;bind cmd", commands, new CommandExec(bind, "cmd;cmd bind;bind cmd"));
153  check("cmd bind ; bind cmd ; cmd bind", commands, new CommandExec(cmd, "bind"), new CommandExec(bind, "cmd ; cmd bind"));
154  check("bind cmd ; cmd bind ; bind cmd", commands, new CommandExec(bind, "cmd ; cmd bind ; bind cmd"));
155  }
156 
164  private static void check(@NotNull final CharSequence command, @NotNull final Commands commands, @NotNull final CommandExec... commandList) {
165  final Collection<CommandExec> expandedCommands = CommandExpander.expand(command, commands);
166  Assert.assertEquals(Arrays.asList(commandList), expandedCommands);
167  }
168 
169 }
Expands a command (or list of commands) into a sequence of Commands to execute.
A Command instance and its arguments.
static Collection< CommandExec > expand(@NotNull final CharSequence commandList, @NotNull final Commands commands)
Expands a command list into a sequence of Commands to execute.
void testExpandAllArguments()
Checks that CommandExpander#expand(CharSequence, Commands) correctly parses commands that consume all...
void testExpandCaseInsensitiveCommandNames()
Checks that CommandExpander#expand(CharSequence, Commands) matches command names case-insensitive.
void addCommand(@NotNull final Command command)
Adds an executable Command.
Definition: Commands.java:45
void testExpandMultiple()
Checks that CommandExpander#expand(CharSequence, Commands) works as expected.
void testExpandArguments()
Checks that CommandExpander#expand(CharSequence, Commands) correctly parses command arguments...
void testExpandSingle()
Checks that CommandExpander#expand(CharSequence, Commands) works as expected.
void testExpandIgnoreEmpty()
Checks that CommandExpander#expand(CharSequence, Commands) ignores empty commands.
Adds encoding/decoding of crossfire protocol packets to a ServerConnection.
void testExpandPredefined()
Checks that CommandExpander#expand(CharSequence, Commands) detects predefined commands.
void testExpandIgnoreWhitespace()
Checks that CommandExpander#expand(CharSequence, Commands) allows whitespace before or after the comm...
static void check(@NotNull final CharSequence command, @NotNull final Commands commands, @NotNull final CommandExec... commandList)
Checks that CommandExpander#expand(CharSequence, Commands) works as expected.
Parses and executes client-side commands.
Definition: Commands.java:33