Revision: 5522
          http://jnode.svn.sourceforge.net/jnode/?rev=5522&view=rev
Author:   crawley
Date:     2009-05-25 11:12:34 +0000 (Mon, 25 May 2009)

Log Message:
-----------
Starting to build unit tests for bjorne completion

Modified Paths:
--------------
    trunk/shell/src/test/org/jnode/test/shell/Cassowary.java

Added Paths:
-----------
    trunk/shell/src/test/org/jnode/test/shell/MyEchoCommand.java
    trunk/shell/src/test/org/jnode/test/shell/bjorne/BjorneCompletionTests.java

Modified: trunk/shell/src/test/org/jnode/test/shell/Cassowary.java
===================================================================
--- trunk/shell/src/test/org/jnode/test/shell/Cassowary.java    2009-05-25 
10:57:49 UTC (rev 5521)
+++ trunk/shell/src/test/org/jnode/test/shell/Cassowary.java    2009-05-25 
11:12:34 UTC (rev 5522)
@@ -44,7 +44,7 @@
 public class Cassowary {
     private static boolean initialized;
 
-    protected static void initEnv() throws NamingException {
+    public static void initEnv() throws NamingException {
         if (initialized) {
             return;
         }

Added: trunk/shell/src/test/org/jnode/test/shell/MyEchoCommand.java
===================================================================
--- trunk/shell/src/test/org/jnode/test/shell/MyEchoCommand.java                
                (rev 0)
+++ trunk/shell/src/test/org/jnode/test/shell/MyEchoCommand.java        
2009-05-25 11:12:34 UTC (rev 5522)
@@ -0,0 +1,66 @@
+/*
+ * $Id: EchoCommand.java 5380 2009-05-01 15:30:29Z crawley $
+ *
+ * Copyright (C) 2003-2009 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;
+
+import java.io.PrintWriter;
+
+import org.jnode.shell.AbstractCommand;
+import org.jnode.shell.syntax.Argument;
+import org.jnode.shell.syntax.StringArgument;
+
+/**
+ * Echo the command's arguments to its output. 
+ * 
+ * @author epr
+ * @author craw...@jnode.org
+ */
+public class MyEchoCommand extends AbstractCommand {
+
+    private static final String help_words = "the text to be printed";
+    private static final String help_super = "Print text to standard output";
+    
+    private final StringArgument argWords;
+
+    public MyEchoCommand() {
+        super(help_super);
+        argWords = new StringArgument("text", Argument.MULTIPLE, help_words);
+        registerArguments(argWords);
+    }
+
+    public static void main(String[] args) throws Exception {
+        new MyEchoCommand().execute(args);
+    }
+
+    /**
+     * Execute the command
+     */
+    public void execute() throws Exception {
+        PrintWriter out = getOutput().getPrintWriter();
+        String[] words = argWords.getValues();
+        for (int i = 0; i < words.length; i++) {
+            if (i > 0) {
+                out.print(' ');
+            }
+            out.print(words[i]);
+        }
+        out.println();
+    }
+}

Added: 
trunk/shell/src/test/org/jnode/test/shell/bjorne/BjorneCompletionTests.java
===================================================================
--- trunk/shell/src/test/org/jnode/test/shell/bjorne/BjorneCompletionTests.java 
                        (rev 0)
+++ trunk/shell/src/test/org/jnode/test/shell/bjorne/BjorneCompletionTests.java 
2009-05-25 11:12:34 UTC (rev 5522)
@@ -0,0 +1,136 @@
+/*
+ * $Id$
+ *
+ * Copyright (C) 2003-2009 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.bjorne;
+
+import java.util.Set;
+
+import javax.naming.NamingException;
+
+import junit.framework.TestCase;
+
+import org.jnode.shell.CommandCompletions;
+import org.jnode.shell.Completable;
+import org.jnode.shell.ShellManager;
+import org.jnode.shell.ShellSyntaxException;
+import org.jnode.shell.ShellUtils;
+import org.jnode.shell.alias.AliasManager;
+import org.jnode.shell.bjorne.BjorneInterpreter;
+import org.jnode.shell.help.CompletionException;
+import org.jnode.shell.syntax.ArgumentSyntax;
+import org.jnode.shell.syntax.EmptySyntax;
+import org.jnode.shell.syntax.OptionSyntax;
+import org.jnode.shell.syntax.RepeatSyntax;
+import org.jnode.shell.syntax.SequenceSyntax;
+import org.jnode.shell.syntax.SyntaxBundle;
+import org.jnode.shell.syntax.SyntaxManager;
+import org.jnode.test.shell.Cassowary;
+import org.jnode.test.shell.syntax.TestShell;
+
+public class BjorneCompletionTests extends TestCase {
+    
+    static TestShell shell;
+    static {
+        try {
+            Cassowary.initEnv();
+            shell = new TestShell();
+            ShellUtils.getShellManager().registerShell(shell);
+            
+            AliasManager am = shell.getAliasManager();
+            am.add("gc", "org.jnode.command.system.GcCommand");
+            am.add("cpuid", "org.jnode.command.system.CpuIDCommand");
+            am.add("set", "org.jnode.command.system.SetCommand");
+            am.add("dir", "org.jnode.test.shell.MyDirCommand");
+            am.add("duh", "org.jnode.test.shell.MyDuhCommand");
+            am.add("cat", "org.jnode.test.shell.MyCatCommand");
+            am.add("echo", "org.jnode.test.shell.MyEchoCommand");
+            am.add("alias", "org.jnode.test.shell.MyAliasCommand");
+
+            SyntaxManager sm = shell.getSyntaxManager();
+            sm.add(new SyntaxBundle("set",
+                new SequenceSyntax(new ArgumentSyntax("key"), new 
ArgumentSyntax("value"))));
+            sm.add(new SyntaxBundle("duh", new ArgumentSyntax("path")));
+            sm.add(new SyntaxBundle("echo", new RepeatSyntax(new 
ArgumentSyntax("text"))));
+            sm.add(new SyntaxBundle("cpuid", new SequenceSyntax()));
+            sm.add(new SyntaxBundle("alias",
+                new EmptySyntax(null, "Print all available aliases and 
corresponding classnames"),
+                new SequenceSyntax(null, "Set an aliases for given classnames",
+                    new ArgumentSyntax("alias"), new 
ArgumentSyntax("classname")),
+                new OptionSyntax("remove", 'r', null, "Remove an alias")));
+            
+        } catch (NamingException ex) {
+            throw new RuntimeException(ex);
+        }
+    }
+    
+    
+    private static boolean DEBUG = true;
+
+    public void testSimpleCommand() throws ShellSyntaxException, 
CompletionException {
+        doCompletionTest("echo hi", "TE");
+    }
+
+    private void doCompletionTest(String input, String flags) 
+        throws ShellSyntaxException, CompletionException {
+        BjorneInterpreter interpreter = new BjorneInterpreter();
+        for (int i = 0; i <= input.length(); i++) {
+            String partial = input.substring(0, i);
+            int inWord = 0;
+            int wordStart = 0;
+            for (int j = 0; j < i; j++) {
+                if (partial.charAt(j) == ' ') {
+                    inWord++;
+                    wordStart = j + 1;
+                }
+            }
+            String lastWord = partial.substring(wordStart);
+            Completable completable = interpreter.parsePartial(shell, partial);
+            CommandCompletions completions = new CommandCompletions();
+            completable.complete(completions, shell);
+            Set<String> completionWords = completions.getCompletions();
+            switch (flags.charAt(inWord)) {
+                case 'T':
+                    // Expect completions
+                    assertTrue("got no completions: " + diag(partial, 
completions), completionWords.size() > 0);
+                    break;
+                case 'F':
+                    // Expect no completions
+                    assertTrue("got unexpected completions: " + diag(partial, 
completions), completionWords.size() == 0);
+                    break;
+                case 'E':
+                    // Expect completions if the last char is ' ', otherwise 
not
+                    if (wordStart >= partial.length()) {
+                        assertTrue("got no completions: " + diag(partial, 
completions), completionWords.size() > 0);
+                    } else {
+                        assertTrue("got unexpected completions: " + 
diag(partial, completions), completionWords.size() == 0);
+                    }
+                case '?':
+                    // Maybe completions, maybe not
+            }
+            for (String completionWord : completionWords) {
+                assertTrue(completionWord.startsWith(lastWord));
+            }
+        }
+    }
+
+    private String diag(String partial, CommandCompletions completions) {
+        return "partial = '" + partial + "', completions = " + completions; 
+    }
+}


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.

------------------------------------------------------------------------------
Register Now for Creativity and Technology (CaT), June 3rd, NYC. CaT
is a gathering of tech-side developers & brand creativity professionals. Meet
the minds behind Google Creative Lab, Visual Complexity, Processing, & 
iPhoneDevCamp asthey present alongside digital heavyweights like Barbarian
Group, R/GA, & Big Spaceship. http://www.creativitycat.com 
_______________________________________________
Jnode-svn-commits mailing list
Jnode-svn-commits@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/jnode-svn-commits

Reply via email to