Package org.jnode.test.shell.syntax

Source Code of org.jnode.test.shell.syntax.PowersetSyntaxTest$Test

/*
* $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.ArgumentBundle;
import org.jnode.shell.syntax.CommandSyntaxException;
import org.jnode.shell.syntax.FileArgument;
import org.jnode.shell.syntax.IntegerArgument;
import org.jnode.shell.syntax.OptionSyntax;
import org.jnode.shell.syntax.PowersetSyntax;
import org.jnode.shell.syntax.RepeatSyntax;
import org.jnode.shell.syntax.SequenceSyntax;
import org.jnode.shell.syntax.StringArgument;
import org.jnode.shell.syntax.Syntax;
import org.junit.Assert;

public class PowersetSyntaxTest {

    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 StringArgument otherArg = new StringArgument("otherArg", Argument.OPTIONAL +
                Argument.MULTIPLE);

        public Test() {
            registerArguments(fileArg, intArg, otherArg);
        }

        public void execute() throws Exception {
        }
    }

    @org.junit.Test
    public void testConstructor() {
        new PowersetSyntax(new OptionSyntax("intArg", 'i'), new OptionSyntax("fileArg", 'f'));
    }

    @org.junit.Test
    public void testFormat() {
        ArgumentBundle bundle = new Test().getArgumentBundle();
        Syntax syntax1 =
                new PowersetSyntax(new OptionSyntax("intArg", 'i'),
                        new OptionSyntax("fileArg", 'f'));
        Assert.assertEquals("[ ( -i <intArg> ) | ( -f <fileArg> ) ] ...", syntax1.format(bundle));
    }

    @org.junit.Test
    public void testOne() throws Exception {
        TestShell shell = new TestShell();
        shell.addAlias("cmd", "org.jnode.test.shell.syntax.PowersetSyntaxTest$Test");
        shell.addSyntax("cmd", new PowersetSyntax(new OptionSyntax("intArg", 'i'),
                new OptionSyntax("fileArg", 'f')));

        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("-i"), new Token("1")}, null);
        cmdInfo = cl.parseCommandLine(shell);
        cmd = cmdInfo.createCommandInstance();
        Assert.assertEquals(1, cmd.getArgumentBundle().getArgument("fileArg").getValues().length);
        Assert.assertEquals(1, cmd.getArgumentBundle().getArgument("intArg").getValues().length);
        Assert.assertEquals("F1", cmd.getArgumentBundle().getArgument("fileArg").getValue()
                .toString());
        Assert.assertEquals("1", cmd.getArgumentBundle().getArgument("intArg").getValue()
                .toString());

        try {
            cl = new CommandLine(new Token("cmd"), new Token[] {new Token("-f")}, null);
            cl.parseCommandLine(shell);
            Assert.fail("no exception");
        } catch (CommandSyntaxException ex) {
            // expected
        }

        try {
            cl =
                    new CommandLine(new Token("cmd"),
                            new Token[] {new Token("-i"), new Token("F1")}, null);
            cl.parseCommandLine(shell);
            Assert.fail("no exception");
        } catch (CommandSyntaxException ex) {
            // expected
        }
    }

    @org.junit.Test
    public void testLazy() throws Exception {
        TestShell shell = new TestShell();
        shell.addAlias("cmd", "org.jnode.test.shell.syntax.PowersetSyntaxTest$Test");
        shell.addSyntax("cmd", new SequenceSyntax(new PowersetSyntax(
                new OptionSyntax("intArg", 'i'), new OptionSyntax("fileArg", 'f')),
                new RepeatSyntax(new OptionSyntax("otherArg", 'i'))));

        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);
        Assert.assertEquals(0, cmd.getArgumentBundle().getArgument("otherArg").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(0, cmd.getArgumentBundle().getArgument("otherArg").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("-i"), new Token("1")}, 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("otherArg").getValues().length);
        Assert.assertEquals("F1", cmd.getArgumentBundle().getArgument("fileArg").getValue()
                .toString());
        Assert.assertEquals("1", cmd.getArgumentBundle().getArgument("otherArg").getValue()
                .toString());

        cl =
                new CommandLine(new Token("cmd"), new Token[] {new Token("-i"), new Token("1"),
                    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(1, cmd.getArgumentBundle().getArgument("intArg").getValues().length);
        Assert.assertEquals(0, cmd.getArgumentBundle().getArgument("otherArg").getValues().length);
        Assert.assertEquals("F1", cmd.getArgumentBundle().getArgument("fileArg").getValue()
                .toString());
        Assert.assertEquals("1", cmd.getArgumentBundle().getArgument("intArg").getValue()
                .toString());

        cl =
                new CommandLine(new Token("cmd"), new Token[] {new Token("-i"), new Token("1"),
                    new Token("-f"), new Token("F1"), new Token("-i"), new Token("2")}, null);
        cmdInfo = cl.parseCommandLine(shell);
        cmd = cmdInfo.createCommandInstance();
        Assert.assertEquals(1, cmd.getArgumentBundle().getArgument("fileArg").getValues().length);
        Assert.assertEquals(1, cmd.getArgumentBundle().getArgument("intArg").getValues().length);
        Assert.assertEquals(1, cmd.getArgumentBundle().getArgument("otherArg").getValues().length);
        Assert.assertEquals("F1", cmd.getArgumentBundle().getArgument("fileArg").getValue()
                .toString());
        Assert.assertEquals("1", cmd.getArgumentBundle().getArgument("intArg").getValue()
                .toString());
        Assert.assertEquals("2", cmd.getArgumentBundle().getArgument("otherArg").getValue()
                .toString());
    }

    @org.junit.Test
    public void testEager() throws Exception {
        TestShell shell = new TestShell();
        shell.addAlias("cmd", "org.jnode.test.shell.syntax.PowersetSyntaxTest$Test");
        shell.addSyntax("cmd", new SequenceSyntax(new PowersetSyntax(null, true, null,
                new OptionSyntax("intArg", 'i'), new OptionSyntax("fileArg", 'f')),
                new RepeatSyntax(new OptionSyntax("otherArg", 'i'))));

        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);
        Assert.assertEquals(0, cmd.getArgumentBundle().getArgument("otherArg").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(0, cmd.getArgumentBundle().getArgument("otherArg").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("-i"), new Token("1")}, null);
        cmdInfo = cl.parseCommandLine(shell);
        cmd = cmdInfo.createCommandInstance();
        Assert.assertEquals(1, cmd.getArgumentBundle().getArgument("fileArg").getValues().length);
        Assert.assertEquals(1, cmd.getArgumentBundle().getArgument("intArg").getValues().length);
        Assert.assertEquals(0, cmd.getArgumentBundle().getArgument("otherArg").getValues().length);
        Assert.assertEquals("F1", cmd.getArgumentBundle().getArgument("fileArg").getValue()
                .toString());
        Assert.assertEquals("1", cmd.getArgumentBundle().getArgument("intArg").getValue()
                .toString());

        cl =
                new CommandLine(new Token("cmd"), new Token[] {new Token("-i"), new Token("1"),
                    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(1, cmd.getArgumentBundle().getArgument("intArg").getValues().length);
        Assert.assertEquals(0, cmd.getArgumentBundle().getArgument("otherArg").getValues().length);
        Assert.assertEquals("F1", cmd.getArgumentBundle().getArgument("fileArg").getValue()
                .toString());
        Assert.assertEquals("1", cmd.getArgumentBundle().getArgument("intArg").getValue()
                .toString());

        cl =
                new CommandLine(new Token("cmd"), new Token[] {new Token("-i"), new Token("1"),
                    new Token("-f"), new Token("F1"), new Token("-i"), new Token("2")}, null);
        cmdInfo = cl.parseCommandLine(shell);
        cmd = cmdInfo.createCommandInstance();
        Assert.assertEquals(1, cmd.getArgumentBundle().getArgument("fileArg").getValues().length);
        Assert.assertEquals(2, cmd.getArgumentBundle().getArgument("intArg").getValues().length);
        Assert.assertEquals(0, cmd.getArgumentBundle().getArgument("otherArg").getValues().length);
    }
}
TOP

Related Classes of org.jnode.test.shell.syntax.PowersetSyntaxTest$Test

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.