From: <cr...@us...> - 2009-01-14 13:58:20
|
Revision: 4862 http://jnode.svn.sourceforge.net/jnode/?rev=4862&view=rev Author: crawley Date: 2009-01-14 13:58:05 +0000 (Wed, 14 Jan 2009) Log Message: ----------- Minor bjorne internal interface changes + extra unit tests Modified Paths: -------------- trunk/shell/src/shell/org/jnode/shell/CommandLine.java trunk/shell/src/shell/org/jnode/shell/bjorne/BjorneContext.java trunk/shell/src/test/org/jnode/test/shell/bjorne/BjorneContextTests.java trunk/shell/src/test/org/jnode/test/shell/bjorne/BjorneParserTests.java trunk/shell/src/test/org/jnode/test/shell/bjorne/BjorneTokenizerTests.java Modified: trunk/shell/src/shell/org/jnode/shell/CommandLine.java =================================================================== --- trunk/shell/src/shell/org/jnode/shell/CommandLine.java 2009-01-14 13:56:47 UTC (rev 4861) +++ trunk/shell/src/shell/org/jnode/shell/CommandLine.java 2009-01-14 13:58:05 UTC (rev 4862) @@ -335,6 +335,10 @@ } return arguments; } + + public Token[] getArgumentTokens() { + return argumentTokens; + } /** * Get the arguments as String[]. Modified: trunk/shell/src/shell/org/jnode/shell/bjorne/BjorneContext.java =================================================================== --- trunk/shell/src/shell/org/jnode/shell/bjorne/BjorneContext.java 2009-01-14 13:56:47 UTC (rev 4861) +++ trunk/shell/src/shell/org/jnode/shell/bjorne/BjorneContext.java 2009-01-14 13:58:05 UTC (rev 4862) @@ -38,6 +38,7 @@ import java.io.InputStream; import java.io.PrintStream; import java.util.ArrayList; +import java.util.Arrays; import java.util.HashMap; import java.util.LinkedList; import java.util.List; @@ -111,7 +112,7 @@ private int lastAsyncPid; - private boolean tildes = true; + private boolean tildeExpansion = true; private boolean globbing = true; @@ -143,8 +144,26 @@ this.interpreter = parent.interpreter; this.holders = copyStreamHolders(parent.holders); this.variables = copyVariables(parent.variables); + this.globbing = parent.globbing; + this.tildeExpansion = parent.tildeExpansion; } + + public boolean isTildeExpansion() { + return tildeExpansion; + } + public void setTildeExpansion(boolean tildeExpansion) { + this.tildeExpansion = tildeExpansion; + } + + public boolean isGlobbing() { + return globbing; + } + + public void setGlobbing(boolean globbing) { + this.globbing = globbing; + } + /** * Create a deep copy of some variable bindings */ @@ -233,8 +252,8 @@ * @return the command line * @throws ShellException */ - public CommandLine expandAndSplit(BjorneToken[] tokens) throws ShellException { - LinkedList<BjorneToken> wordTokens = new LinkedList<BjorneToken>(); + public CommandLine expandAndSplit(Iterable<BjorneToken> tokens) throws ShellException { + List<BjorneToken> wordTokens = new LinkedList<BjorneToken>(); for (BjorneToken token : tokens) { String word = token.getText(); CharSequence expanded = expand(word); @@ -249,6 +268,10 @@ } return makeCommandLine(wordTokens); } + + public CommandLine expandAndSplit(BjorneToken[] tokens) throws ShellException { + return expandAndSplit(Arrays.asList(tokens)); + } /** * Perform expand-and-split processing on a sequence of characters. This method is only @@ -263,11 +286,11 @@ return split(expand(text)); } - private CommandLine makeCommandLine(LinkedList<BjorneToken> wordTokens) { - if (globbing || tildes) { - LinkedList<BjorneToken> globbedWordTokens = new LinkedList<BjorneToken>(); + private CommandLine makeCommandLine(List<BjorneToken> wordTokens) { + if (globbing || tildeExpansion) { + List<BjorneToken> globbedWordTokens = new LinkedList<BjorneToken>(); for (BjorneToken wordToken : wordTokens) { - if (tildes) { + if (tildeExpansion) { wordToken = tildeExpand(wordToken); } if (globbing) { @@ -282,7 +305,7 @@ if (nosWords == 0) { return new CommandLine(null, null); } else { - BjorneToken alias = wordTokens.removeFirst(); + BjorneToken alias = wordTokens.remove(0); BjorneToken[] args = wordTokens.toArray(new BjorneToken[nosWords - 1]); return new CommandLine(alias, args, null); } @@ -306,7 +329,7 @@ } } - private void globAndAppend(BjorneToken wordToken, LinkedList<BjorneToken> globbedWordTokens) { + private void globAndAppend(BjorneToken wordToken, List<BjorneToken> globbedWordTokens) { // Try to deal with the 'not-a-pattern' case quickly and cheaply. String word = wordToken.getText(); if (!PathnamePattern.isPattern(word)) { @@ -333,8 +356,8 @@ * @return the destination for the tokens. * @throws ShellException */ - public LinkedList<BjorneToken> split(CharSequence text) throws ShellException { - LinkedList<BjorneToken> wordTokens = new LinkedList<BjorneToken>(); + public List<BjorneToken> split(CharSequence text) throws ShellException { + List<BjorneToken> wordTokens = new LinkedList<BjorneToken>(); splitAndAppend(new BjorneToken(BjorneToken.TOK_WORD, text.toString(), -1, -1), wordTokens); return wordTokens; } @@ -347,7 +370,7 @@ * @param wordTokens the destination for the tokens. * @throws ShellException */ - private void splitAndAppend(BjorneToken token, LinkedList<BjorneToken> wordTokens) + private void splitAndAppend(BjorneToken token, List<BjorneToken> wordTokens) throws ShellException { String text = token.getText(); StringBuffer sb = null; @@ -941,6 +964,8 @@ // TODO Auto-generated method stub return false; } + + private static class VariableSlot { @@ -962,7 +987,7 @@ } } - static class StreamHolder { + public static class StreamHolder { public final CommandIO stream; private boolean isMine; Modified: trunk/shell/src/test/org/jnode/test/shell/bjorne/BjorneContextTests.java =================================================================== --- trunk/shell/src/test/org/jnode/test/shell/bjorne/BjorneContextTests.java 2009-01-14 13:56:47 UTC (rev 4861) +++ trunk/shell/src/test/org/jnode/test/shell/bjorne/BjorneContextTests.java 2009-01-14 13:58:05 UTC (rev 4862) @@ -20,11 +20,14 @@ */ package org.jnode.test.shell.bjorne; +import java.io.File; import java.util.Iterator; +import java.util.LinkedList; import java.util.List; import junit.framework.TestCase; +import org.jnode.shell.CommandLine; import org.jnode.shell.ShellException; import org.jnode.shell.bjorne.BjorneContext; import org.jnode.shell.bjorne.BjorneToken; @@ -117,6 +120,38 @@ checkExpansion(expansion, new String[] {"$A"}); } + public void testExpand14() throws ShellException { + BjorneContext parentContext = new BjorneContext(null, new BjorneContext.StreamHolder[0]); + parentContext.setVariable("A", "A"); + BjorneContext context = new BjorneContext(parentContext); + List<BjorneToken> expansion = context.expandAndSplit("'$A'"); + checkExpansion(expansion, new String[] {"$A"}); + } + + public void testExpand15() throws Exception { + BjorneContext context = new BjorneContext(null, null); + assertEquals(true, context.isGlobbing()); + assertEquals(true, context.isTildeExpansion()); + if (new File("../README.txt").exists()) { + CommandLine expansion = context.expandAndSplit(context.split("../README.*")); + checkExpansion(expansion, new String[] {"../README.txt"}); + } + context.setGlobbing(false); + CommandLine expansion = context.expandAndSplit(context.split("../README.*")); + checkExpansion(expansion, new String[] {"../README.*"}); + } + + public void testExpand16() throws Exception { + BjorneContext context = new BjorneContext(null, null); + assertEquals(true, context.isGlobbing()); + assertEquals(true, context.isTildeExpansion()); + CommandLine expansion = context.expandAndSplit(context.split("~")); + checkExpansion(expansion, new String[] {System.getProperty("user.home")}); + context.setTildeExpansion(false); + expansion = context.expandAndSplit(context.split("~")); + checkExpansion(expansion, new String[] {"~"}); + } + @SuppressWarnings("deprecation") private void checkExpansion(List<BjorneToken> expansion, String[] expected) { int i; @@ -132,4 +167,13 @@ fail("Too many words in expansion at word " + i + ": '" + it.next() + "'"); } } + + private void checkExpansion(CommandLine expansion, String[] expected) { + List<BjorneToken> words = new LinkedList<BjorneToken>(); + words.add((BjorneToken) expansion.getCommandToken()); + for (CommandLine.Token word : expansion.getArgumentTokens()) { + words.add((BjorneToken) word); + } + checkExpansion(words, expected); + } } Modified: trunk/shell/src/test/org/jnode/test/shell/bjorne/BjorneParserTests.java =================================================================== --- trunk/shell/src/test/org/jnode/test/shell/bjorne/BjorneParserTests.java 2009-01-14 13:56:47 UTC (rev 4861) +++ trunk/shell/src/test/org/jnode/test/shell/bjorne/BjorneParserTests.java 2009-01-14 13:58:05 UTC (rev 4862) @@ -128,6 +128,23 @@ + "SimpleCommand{nodeType=1,words=[WORD{ls},WORD{-a}]}}]}", doTest("case $1 in ( a ) ls -l ;; b ) ls -a ; esac")); } + + public void test11() throws ShellException { + assertEquals( + "IfCommand{nodeType=6,cond=" + + "SimpleCommand{nodeType=1,words=[WORD{true}]},thenPart=" + + "SimpleCommand{nodeType=1,words=[WORD{echo},WORD{yes}]}}", + doTest("if true ; then echo yes ; fi")); + } + + public void test12() throws ShellException { + assertEquals( + "IfCommand{nodeType=6,cond=" + + "SimpleCommand{nodeType=1,words=[WORD{true}]},thenPart=" + + "SimpleCommand{nodeType=1,words=[WORD{echo},WORD{yes}]},elsePart=" + + "SimpleCommand{nodeType=1,words=[WORD{echo},WORD{false}]}}", + doTest("if true ; then echo yes ; else echo false ; fi")); + } private String doTest(String input) throws ShellException { BjorneParser p = new BjorneParser(new BjorneTokenizer(input, DEBUG)); Modified: trunk/shell/src/test/org/jnode/test/shell/bjorne/BjorneTokenizerTests.java =================================================================== --- trunk/shell/src/test/org/jnode/test/shell/bjorne/BjorneTokenizerTests.java 2009-01-14 13:56:47 UTC (rev 4861) +++ trunk/shell/src/test/org/jnode/test/shell/bjorne/BjorneTokenizerTests.java 2009-01-14 13:58:05 UTC (rev 4862) @@ -65,12 +65,11 @@ import static org.jnode.shell.bjorne.BjorneToken.TOK_UNTIL; import static org.jnode.shell.bjorne.BjorneToken.TOK_WHILE; import static org.jnode.shell.bjorne.BjorneToken.TOK_WORD; +import junit.framework.TestCase; import org.jnode.shell.bjorne.BjorneToken; import org.jnode.shell.bjorne.BjorneTokenizer; -import junit.framework.TestCase; - public class BjorneTokenizerTests extends TestCase { public void testBjorneTokenizer() { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |