/*
* $Id$
*
* Copyright (C) 2003-2014 JNode.org
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; If not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
package org.jnode.test.shell.syntax;
import org.jnode.shell.AbstractCommand;
import org.jnode.shell.Command;
import org.jnode.shell.CommandInfo;
import org.jnode.shell.CommandLine;
import org.jnode.shell.CommandLine.Token;
import org.jnode.shell.syntax.Argument;
import org.jnode.shell.syntax.CommandSyntaxException;
import org.jnode.shell.syntax.FileArgument;
import org.jnode.shell.syntax.FlagArgument;
import org.jnode.shell.syntax.IntegerArgument;
import org.jnode.shell.syntax.OptionSetSyntax;
import org.jnode.shell.syntax.OptionSyntax;
import org.junit.Assert;
public class OptionSetSyntaxTest {
public static class Test extends AbstractCommand {
private final FileArgument fileArg = new FileArgument("fileArg", Argument.OPTIONAL +
Argument.MULTIPLE);
private final IntegerArgument intArg = new IntegerArgument("intArg", Argument.OPTIONAL +
Argument.MULTIPLE);
private final FlagArgument flagArg1 = new FlagArgument("flagArg1", Argument.OPTIONAL +
Argument.SINGLE);
private final FlagArgument flagArg2 = new FlagArgument("flagArg2", Argument.OPTIONAL +
Argument.SINGLE);
private final FlagArgument flagArg3 = new FlagArgument("flagArg3", Argument.OPTIONAL +
Argument.SINGLE);
private final FlagArgument flagArg4 = new FlagArgument("flagArg4", Argument.OPTIONAL +
Argument.SINGLE);
public Test() {
registerArguments(fileArg, intArg, flagArg1, flagArg2, flagArg3, flagArg4);
}
public void execute() throws Exception {
}
}
@org.junit.Test
public void testConstructor() {
new OptionSetSyntax(new OptionSyntax("intArg", 'i'), new OptionSyntax("fileArg", 'f'),
new OptionSyntax("flagArg1", 'x'), new OptionSyntax("flagArg2", 'y'),
new OptionSyntax("flagArg3", 'z'), new OptionSyntax("flagArg4", "boring"));
}
@org.junit.Test
public void testOne() throws Exception {
TestShell shell = new TestShell();
shell.addAlias("cmd", "org.jnode.test.shell.syntax.OptionSetSyntaxTest$Test");
shell.addSyntax("cmd", new OptionSetSyntax(new OptionSyntax("intArg", 'i'),
new OptionSyntax("fileArg", 'f'), new OptionSyntax("flagArg1", 'x'),
new OptionSyntax("flagArg2", 'y'), new OptionSyntax("flagArg3", 'z'),
new OptionSyntax("flagArg4", "boring")));
CommandLine cl;
CommandInfo cmdInfo;
Command cmd;
cl = new CommandLine(new Token("cmd"), new Token[] {}, null);
cmdInfo = cl.parseCommandLine(shell);
cmd = cmdInfo.createCommandInstance();
Assert.assertEquals(0, cmd.getArgumentBundle().getArgument("fileArg").getValues().length);
Assert.assertEquals(0, cmd.getArgumentBundle().getArgument("intArg").getValues().length);
cl =
new CommandLine(new Token("cmd"), new Token[] {new Token("-f"), new Token("F1")},
null);
cmdInfo = cl.parseCommandLine(shell);
cmd = cmdInfo.createCommandInstance();
Assert.assertEquals(1, cmd.getArgumentBundle().getArgument("fileArg").getValues().length);
Assert.assertEquals(0, cmd.getArgumentBundle().getArgument("intArg").getValues().length);
Assert.assertEquals("F1", cmd.getArgumentBundle().getArgument("fileArg").getValue()
.toString());
cl =
new CommandLine(new Token("cmd"), new Token[] {new Token("-f"), new Token("F1"),
new Token("-x"), new Token("-yz")}, null);
cmdInfo = cl.parseCommandLine(shell);
cmd = cmdInfo.createCommandInstance();
Assert.assertEquals(1, cmd.getArgumentBundle().getArgument("fileArg").getValues().length);
Assert.assertEquals(0, cmd.getArgumentBundle().getArgument("intArg").getValues().length);
Assert.assertEquals(1, cmd.getArgumentBundle().getArgument("flagArg1").getValues().length);
Assert.assertEquals(1, cmd.getArgumentBundle().getArgument("flagArg2").getValues().length);
Assert.assertEquals(1, cmd.getArgumentBundle().getArgument("flagArg3").getValues().length);
cl = new CommandLine(new Token("cmd"), new Token[] {new Token("-yz")}, null);
cmdInfo = cl.parseCommandLine(shell);
cmd = cmdInfo.createCommandInstance();
Assert.assertEquals(0, cmd.getArgumentBundle().getArgument("fileArg").getValues().length);
Assert.assertEquals(0, cmd.getArgumentBundle().getArgument("intArg").getValues().length);
Assert.assertEquals(0, cmd.getArgumentBundle().getArgument("flagArg1").getValues().length);
Assert.assertEquals(1, cmd.getArgumentBundle().getArgument("flagArg2").getValues().length);
Assert.assertEquals(1, cmd.getArgumentBundle().getArgument("flagArg3").getValues().length);
try {
cl = new CommandLine(new Token("cmd"), new Token[] {new Token("-xya")}, null);
cl.parseCommandLine(shell);
Assert.fail("no exception");
} catch (CommandSyntaxException ex) {
// expected
}
try {
cl = new CommandLine(new Token("cmd"), new Token[] {new Token("-")}, null);
cl.parseCommandLine(shell);
Assert.fail("no exception");
} catch (CommandSyntaxException ex) {
// expected
}
}
}