From: <cr...@us...> - 2009-01-12 14:22:22
|
Revision: 4851 http://jnode.svn.sourceforge.net/jnode/?rev=4851&view=rev Author: crawley Date: 2009-01-12 14:22:05 +0000 (Mon, 12 Jan 2009) Log Message: ----------- Tweaking some Bjorne unit tests Modified Paths: -------------- trunk/shell/src/shell/org/jnode/shell/bjorne/BjorneContext.java trunk/shell/src/shell/org/jnode/shell/bjorne/BjorneToken.java Added Paths: ----------- trunk/shell/src/test/org/jnode/test/shell/bjorne/ 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 Removed Paths: ------------- trunk/shell/src/shell/org/jnode/shell/bjorne/BjorneContextTests.java trunk/shell/src/shell/org/jnode/shell/bjorne/BjorneParserTests.java trunk/shell/src/shell/org/jnode/shell/bjorne/BjorneTokenizerTests.java Modified: trunk/shell/src/shell/org/jnode/shell/bjorne/BjorneContext.java =================================================================== --- trunk/shell/src/shell/org/jnode/shell/bjorne/BjorneContext.java 2009-01-12 08:03:50 UTC (rev 4850) +++ trunk/shell/src/shell/org/jnode/shell/bjorne/BjorneContext.java 2009-01-12 14:22:05 UTC (rev 4851) @@ -251,18 +251,16 @@ } /** - * Perform expand-and-split processing on a sequence of characters. The resulting - * wordTokens are assembled into a CommandLine. This method is only used in tests - * at the moment, and probably should be removed. (It does not set token attributes - * properly ...) + * Perform expand-and-split processing on a sequence of characters. This method is only + * used in tests at the moment, and probably should be removed. (It does not set token + * attributes properly ...) * * @param text the characters to be split * @return the command line * @throws ShellException */ - public CommandLine expandAndSplit(CharSequence text) throws ShellException { - LinkedList<BjorneToken> words = split(expand(text)); - return makeCommandLine(words); + public List<BjorneToken> expandAndSplit(CharSequence text) throws ShellException { + return split(expand(text)); } private CommandLine makeCommandLine(LinkedList<BjorneToken> wordTokens) { @@ -337,7 +335,7 @@ */ public LinkedList<BjorneToken> split(CharSequence text) throws ShellException { LinkedList<BjorneToken> wordTokens = new LinkedList<BjorneToken>(); - splitAndAppend(new BjorneToken(-1, text.toString(), -1, -1), wordTokens); + splitAndAppend(new BjorneToken(BjorneToken.TOK_WORD, text.toString(), -1, -1), wordTokens); return wordTokens; } Deleted: trunk/shell/src/shell/org/jnode/shell/bjorne/BjorneContextTests.java =================================================================== --- trunk/shell/src/shell/org/jnode/shell/bjorne/BjorneContextTests.java 2009-01-12 08:03:50 UTC (rev 4850) +++ trunk/shell/src/shell/org/jnode/shell/bjorne/BjorneContextTests.java 2009-01-12 14:22:05 UTC (rev 4851) @@ -1,133 +0,0 @@ -/* - * $Id: Command.java 3772 2008-02-10 15:02:53Z lsantha $ - * - * JNode.org - * Copyright (C) 2007-2008 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.shell.bjorne; - -import java.util.Iterator; - -import junit.framework.TestCase; - -import org.jnode.shell.CommandLine; -import org.jnode.shell.ShellException; - -public class BjorneContextTests extends TestCase { - - public void testContext() { - new BjorneContext(null, null); - } - - public void testExpand1() throws ShellException { - BjorneContext context = new BjorneContext(null, null); - CommandLine expansion = context.expandAndSplit(""); - checkExpansion(expansion, new String[] {}); - } - - public void testExpand2() throws ShellException { - BjorneContext context = new BjorneContext(null, null); - CommandLine expansion = context.expandAndSplit(" "); - checkExpansion(expansion, new String[] {}); - } - - public void testExpand3() throws ShellException { - BjorneContext context = new BjorneContext(null, null); - CommandLine expansion = context.expandAndSplit("hi"); - checkExpansion(expansion, new String[] {"hi"}); - } - - public void testExpand4() throws ShellException { - BjorneContext context = new BjorneContext(null, null); - CommandLine expansion = context.expandAndSplit("hi there "); - checkExpansion(expansion, new String[] {"hi", "there"}); - } - - public void testExpand5() throws ShellException { - BjorneContext context = new BjorneContext(null, null); - CommandLine expansion = context.expandAndSplit("'hi there '"); - checkExpansion(expansion, new String[] {"hi there "}); - } - - public void testExpand6() throws ShellException { - BjorneContext context = new BjorneContext(null, null); - CommandLine expansion = context.expandAndSplit("\"hi there \" "); - checkExpansion(expansion, new String[] {"hi there "}); - } - - public void testExpand7() throws ShellException { - BjorneContext context = new BjorneContext(null, null); - CommandLine expansion = context.expandAndSplit("hi\\ there"); - checkExpansion(expansion, new String[] {"hi there"}); - } - - public void testExpand8() throws ShellException { - BjorneContext context = new BjorneContext(null, null); - CommandLine expansion = context.expandAndSplit("\\\"hi\\ there\\\""); - checkExpansion(expansion, new String[] {"\"hi there\""}); - } - - public void testExpand9() throws ShellException { - BjorneContext context = new BjorneContext(null, null); - CommandLine expansion = context.expandAndSplit("$?"); - checkExpansion(expansion, new String[] {"0"}); - } - - public void testExpand10() throws ShellException { - BjorneContext context = new BjorneContext(null, null); - context.setVariable("A", "A"); - CommandLine expansion = context.expandAndSplit("$A"); - checkExpansion(expansion, new String[] {"A"}); - } - - public void testExpand11() throws ShellException { - BjorneContext context = new BjorneContext(null, null); - context.setVariable("A", "A"); - CommandLine expansion = context.expandAndSplit("\\$A"); - checkExpansion(expansion, new String[] {"$A"}); - } - - public void testExpand12() throws ShellException { - BjorneContext context = new BjorneContext(null, null); - context.setVariable("A", "A"); - CommandLine expansion = context.expandAndSplit("\"$A\""); - checkExpansion(expansion, new String[] {"A"}); - } - - public void testExpand13() throws ShellException { - BjorneContext context = new BjorneContext(null, null); - context.setVariable("A", "A"); - CommandLine expansion = context.expandAndSplit("'$A'"); - checkExpansion(expansion, new String[] {"$A"}); - } - - @SuppressWarnings("deprecation") - private void checkExpansion(CommandLine expansion, String[] expected) { - int i; - Iterator<String> it = expansion.iterator(); - for (i = 0; i < expected.length; i++) { - if (it.hasNext()) { - assertEquals("incorrect expansion at word " + i, expected[i], it.next()); - } else { - fail("Too few words in expansion at word " + i + ": expecting '" + expected[i] + "'"); - } - } - if (it.hasNext()) { - fail("Too many words in expansion at word " + i + ": '" + it.next() + "'"); - } - } -} Deleted: trunk/shell/src/shell/org/jnode/shell/bjorne/BjorneParserTests.java =================================================================== --- trunk/shell/src/shell/org/jnode/shell/bjorne/BjorneParserTests.java 2009-01-12 08:03:50 UTC (rev 4850) +++ trunk/shell/src/shell/org/jnode/shell/bjorne/BjorneParserTests.java 2009-01-12 14:22:05 UTC (rev 4851) @@ -1,138 +0,0 @@ -/* - * $Id: Command.java 3772 2008-02-10 15:02:53Z lsantha $ - * - * JNode.org - * Copyright (C) 2007-2008 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.shell.bjorne; - -import junit.framework.TestCase; - -import org.jnode.shell.ShellException; - -public class BjorneParserTests extends TestCase { - - private static final boolean DEBUG = true; - - public void testParser() { - new BjorneParser(new BjorneTokenizer("")); - } - - public void test1() throws ShellException { - assertEquals( - "SimpleCommand{nodeType=1,command=WORD{foo},arguments=[]}", - doTest("foo")); - } - - public void test2() throws ShellException { - assertEquals( - "SimpleCommand{nodeType=1,command=WORD{ls},arguments=[WORD{-l}]}", - doTest("ls -l")); - } - - public void test3() throws ShellException { - assertEquals( - "SimpleCommand{nodeType=1," - + "redirects=[Redirect{redirectionType=60,io=IO_NUMBER{1},arg=WORD{/tmp/foo}}," - + "Redirect{redirectionType=62,arg=WORD{/tmp/bar}}]," - + "command=WORD{ls},arguments=[WORD{-l}]}", - doTest("ls -l 1< /tmp/foo > /tmp/bar")); - } - - public void test4() throws ShellException { - assertEquals( - "ListCommand{nodeType=2,flags=0x10," - + "commands=[SimpleCommand{nodeType=1,assignments=[ASSIGNMENT{FOO=BAR}]," - + "command=WORD{ls},arguments=[WORD{-l}]}," - + "SimpleCommand{nodeType=1,command=WORD{less},arguments=[]}]}", - doTest("FOO=BAR ls -l | less")); - } - - public void test5() throws ShellException { - assertEquals( - "ListCommand{nodeType=2,flags=0x10,commands=[" - + "SimpleCommand{nodeType=1,command=WORD{cat},arguments=[WORD{foo}]}," - + "ListCommand{nodeType=10,commands=[" - + "ListCommand{nodeType=2,commands=[" - + "SimpleCommand{nodeType=1,command=WORD{wc},arguments=[WORD{1}]}," - + "SimpleCommand{nodeType=1,flags=0x2,command=WORD{wc},arguments=[WORD{2}]}]}," - + "ListCommand{nodeType=2,commands=[" - + "SimpleCommand{nodeType=1,command=WORD{wc},arguments=[WORD{3}]}," - + "SimpleCommand{nodeType=1,flags=0x4,command=WORD{wc},arguments=[WORD{4}]}]}]}]}", - doTest("cat foo | ( wc 1 && wc 2 ; wc 3 || wc 4 )")); - } - - public void test6() throws ShellException { - assertEquals( - "ListCommand{nodeType=2,commands=[" - + "SimpleCommand{nodeType=1,flags=0x1,command=WORD{cat},arguments=[WORD{foo}]}," - + "SimpleCommand{nodeType=1,command=WORD{cat},arguments=[WORD{bar}]}," - + "SimpleCommand{nodeType=1,command=WORD{cat},arguments=[WORD{baz}]}]}", - doTest("cat foo & cat bar ; cat baz ;")); - } - - public void test7() throws ShellException { - assertEquals( - "LoopCommand{nodeType=3,var=NAME{i}," - + "words=[WORD{1},WORD{2},WORD{3},WORD{4},WORD{5}]," - + "body=SimpleCommand{nodeType=1,command=WORD{echo},arguments=[WORD{$i}]}}", - doTest("for i in 1 2 3 4 5 ; do echo $i ; done")); - } - - public void test7a() throws ShellException { - assertEquals( - "LoopCommand{nodeType=3,var=NAME{i}," - + "words=[WORD{1},WORD{2},WORD{3},WORD{4},WORD{5}]," - + "body=SimpleCommand{nodeType=1,command=WORD{echo},arguments=[WORD{$i}]}}", - doTest("for i in 1 2 3 4 5 ; do \n echo $i ; done")); - } - - public void test8() throws ShellException { - assertEquals( - "LoopCommand{nodeType=4," - + "cond=SimpleCommand{nodeType=1,command=WORD{true},arguments=[]}," - + "body=SimpleCommand{nodeType=1,command=WORD{echo},arguments=[WORD{$i}]}}", - doTest("while true ; do echo $i ; done")); - } - - public void test9() throws ShellException { - assertEquals( - "LoopCommand{nodeType=5," - + "cond=SimpleCommand{nodeType=1,command=WORD{true},arguments=[]}," - + "body=SimpleCommand{nodeType=1,command=WORD{echo},arguments=[WORD{$i}]}}", - doTest("until true ; do echo $i ; done")); - } - - public void test10() throws ShellException { - assertEquals( - "CaseCommand{nodeType=9,word=WORD{$1},caseItems=[" - + "CaseItem{,pattern=[],body=" - + "SimpleCommand{nodeType=1,command=WORD{ls},arguments=[WORD{-l}]}}," - + "CaseItem{,pattern=[],body=" - + "SimpleCommand{nodeType=1,command=WORD{ls},arguments=[WORD{-a}]}}]}", - doTest("case $1 in ( a ) ls -l ;; b ) ls -a ; esac")); - } - - private String doTest(String input) throws ShellException { - BjorneParser p = new BjorneParser(new BjorneTokenizer(input, DEBUG)); - String res = p.parse().toString(); - if (DEBUG) { - System.err.println(res); - } - return res; - } -} Modified: trunk/shell/src/shell/org/jnode/shell/bjorne/BjorneToken.java =================================================================== --- trunk/shell/src/shell/org/jnode/shell/bjorne/BjorneToken.java 2009-01-12 08:03:50 UTC (rev 4850) +++ trunk/shell/src/shell/org/jnode/shell/bjorne/BjorneToken.java 2009-01-12 14:22:05 UTC (rev 4851) @@ -138,21 +138,21 @@ } private void validate() { - switch (tokenType) { - case TOK_WORD: - case TOK_IO_NUMBER: - case TOK_NAME: - case TOK_ASSIGNMENT: - if (text == null || text.length() == 0) { - throw new IllegalArgumentException("null or empty text"); - } - break; - - default: - if (text != null && text.length() > 0) { - throw new IllegalArgumentException("non-empty text"); - } - } +// switch (tokenType) { +// case TOK_WORD: +// case TOK_IO_NUMBER: +// case TOK_NAME: +// case TOK_ASSIGNMENT: +// if (text == null || text.length() == 0) { +// throw new IllegalArgumentException("null or empty text"); +// } +// break; +// +// default: +// if (text != null && text.length() > 0) { +// throw new IllegalArgumentException("non-empty text"); +// } +// } } public String getText() { Deleted: trunk/shell/src/shell/org/jnode/shell/bjorne/BjorneTokenizerTests.java =================================================================== --- trunk/shell/src/shell/org/jnode/shell/bjorne/BjorneTokenizerTests.java 2009-01-12 08:03:50 UTC (rev 4850) +++ trunk/shell/src/shell/org/jnode/shell/bjorne/BjorneTokenizerTests.java 2009-01-12 14:22:05 UTC (rev 4851) @@ -1,553 +0,0 @@ -/* - * $Id: Command.java 3772 2008-02-10 15:02:53Z lsantha $ - * - * JNode.org - * Copyright (C) 2007-2008 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.shell.bjorne; - -import static org.jnode.shell.bjorne.BjorneToken.RULE_1_CONTEXT; -import static org.jnode.shell.bjorne.BjorneToken.RULE_5_CONTEXT; -import static org.jnode.shell.bjorne.BjorneToken.RULE_6_CONTEXT; -import static org.jnode.shell.bjorne.BjorneToken.RULE_7a_CONTEXT; -import static org.jnode.shell.bjorne.BjorneToken.RULE_7b_CONTEXT; -import static org.jnode.shell.bjorne.BjorneToken.RULE_8_CONTEXT; -import static org.jnode.shell.bjorne.BjorneToken.TOK_AMP; -import static org.jnode.shell.bjorne.BjorneToken.TOK_AND_IF; -import static org.jnode.shell.bjorne.BjorneToken.TOK_ASSIGNMENT; -import static org.jnode.shell.bjorne.BjorneToken.TOK_BANG; -import static org.jnode.shell.bjorne.BjorneToken.TOK_BAR; -import static org.jnode.shell.bjorne.BjorneToken.TOK_CASE; -import static org.jnode.shell.bjorne.BjorneToken.TOK_CLOBBER; -import static org.jnode.shell.bjorne.BjorneToken.TOK_DGREAT; -import static org.jnode.shell.bjorne.BjorneToken.TOK_DLESS; -import static org.jnode.shell.bjorne.BjorneToken.TOK_DLESSDASH; -import static org.jnode.shell.bjorne.BjorneToken.TOK_DO; -import static org.jnode.shell.bjorne.BjorneToken.TOK_DONE; -import static org.jnode.shell.bjorne.BjorneToken.TOK_DSEMI; -import static org.jnode.shell.bjorne.BjorneToken.TOK_ELIF; -import static org.jnode.shell.bjorne.BjorneToken.TOK_ELSE; -import static org.jnode.shell.bjorne.BjorneToken.TOK_END_OF_LINE; -import static org.jnode.shell.bjorne.BjorneToken.TOK_END_OF_STREAM; -import static org.jnode.shell.bjorne.BjorneToken.TOK_ESAC; -import static org.jnode.shell.bjorne.BjorneToken.TOK_FI; -import static org.jnode.shell.bjorne.BjorneToken.TOK_FOR; -import static org.jnode.shell.bjorne.BjorneToken.TOK_GREAT; -import static org.jnode.shell.bjorne.BjorneToken.TOK_GREATAND; -import static org.jnode.shell.bjorne.BjorneToken.TOK_IF; -import static org.jnode.shell.bjorne.BjorneToken.TOK_IN; -import static org.jnode.shell.bjorne.BjorneToken.TOK_IO_NUMBER; -import static org.jnode.shell.bjorne.BjorneToken.TOK_LBRACE; -import static org.jnode.shell.bjorne.BjorneToken.TOK_LESS; -import static org.jnode.shell.bjorne.BjorneToken.TOK_LESSAND; -import static org.jnode.shell.bjorne.BjorneToken.TOK_LESSGREAT; -import static org.jnode.shell.bjorne.BjorneToken.TOK_LPAREN; -import static org.jnode.shell.bjorne.BjorneToken.TOK_NAME; -import static org.jnode.shell.bjorne.BjorneToken.TOK_OR_IF; -import static org.jnode.shell.bjorne.BjorneToken.TOK_RBRACE; -import static org.jnode.shell.bjorne.BjorneToken.TOK_RPAREN; -import static org.jnode.shell.bjorne.BjorneToken.TOK_SEMI; -import static org.jnode.shell.bjorne.BjorneToken.TOK_THEN; -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; - -public class BjorneTokenizerTests extends TestCase { - - public void testBjorneTokenizer() { - new BjorneTokenizer("hello"); - } - - public void testEmpty() { - BjorneTokenizer tokenizer = new BjorneTokenizer(""); - BjorneToken token = tokenizer.peek(); - assertEquals(TOK_END_OF_STREAM, token.getTokenType()); - token = tokenizer.peek(RULE_1_CONTEXT); - assertEquals(TOK_END_OF_STREAM, token.getTokenType()); - token = tokenizer.next(); - assertEquals(TOK_END_OF_STREAM, token.getTokenType()); - token = tokenizer.next(RULE_1_CONTEXT); - assertEquals(TOK_END_OF_STREAM, token.getTokenType()); - } - - public void testNewline() { - BjorneTokenizer tokenizer = new BjorneTokenizer("\n"); - BjorneToken token = tokenizer.next(); - assertEquals(TOK_END_OF_LINE, token.getTokenType()); - token = tokenizer.next(); - assertEquals(TOK_END_OF_STREAM, token.getTokenType()); - } - - public void testBlanksAndNewlines() { - BjorneTokenizer tokenizer = new BjorneTokenizer(" \n\t\n "); - BjorneToken token = tokenizer.next(); - assertEquals(TOK_END_OF_LINE, token.getTokenType()); - token = tokenizer.next(); - assertEquals(TOK_END_OF_LINE, token.getTokenType()); - token = tokenizer.next(); - assertEquals(TOK_END_OF_STREAM, token.getTokenType()); - } - - public void testComments() { - BjorneTokenizer tokenizer = new BjorneTokenizer( - "# comment\n #comment 2\n # comment # 3"); - BjorneToken token = tokenizer.next(); - assertEquals(TOK_END_OF_LINE, token.getTokenType()); - token = tokenizer.next(); - assertEquals(TOK_END_OF_LINE, token.getTokenType()); - token = tokenizer.next(); - assertEquals(TOK_END_OF_STREAM, token.getTokenType()); - } - - public void testSymbols() { - BjorneTokenizer tokenizer = new BjorneTokenizer("; | & < > ( )"); - BjorneToken token = tokenizer.next(); - assertEquals(TOK_SEMI, token.getTokenType()); - token = tokenizer.next(); - assertEquals(TOK_BAR, token.getTokenType()); - token = tokenizer.next(); - assertEquals(TOK_AMP, token.getTokenType()); - token = tokenizer.next(); - assertEquals(TOK_LESS, token.getTokenType()); - token = tokenizer.next(); - assertEquals(TOK_GREAT, token.getTokenType()); - token = tokenizer.next(); - assertEquals(TOK_LPAREN, token.getTokenType()); - token = tokenizer.next(); - assertEquals(TOK_RPAREN, token.getTokenType()); - token = tokenizer.next(); - assertEquals(TOK_END_OF_STREAM, token.getTokenType()); - } - - public void testSymbols2() { - BjorneTokenizer tokenizer = new BjorneTokenizer( - "; ;; | || & && < << > >>"); - BjorneToken token = tokenizer.next(); - assertEquals(TOK_SEMI, token.getTokenType()); - token = tokenizer.next(); - assertEquals(TOK_DSEMI, token.getTokenType()); - token = tokenizer.next(); - assertEquals(TOK_BAR, token.getTokenType()); - token = tokenizer.next(); - assertEquals(TOK_OR_IF, token.getTokenType()); - token = tokenizer.next(); - assertEquals(TOK_AMP, token.getTokenType()); - token = tokenizer.next(); - assertEquals(TOK_AND_IF, token.getTokenType()); - token = tokenizer.next(); - assertEquals(TOK_LESS, token.getTokenType()); - token = tokenizer.next(); - assertEquals(TOK_DLESS, token.getTokenType()); - token = tokenizer.next(); - assertEquals(TOK_GREAT, token.getTokenType()); - token = tokenizer.next(); - assertEquals(TOK_DGREAT, token.getTokenType()); - token = tokenizer.next(); - assertEquals(TOK_END_OF_STREAM, token.getTokenType()); - } - - public void testSymbols3() { - BjorneTokenizer tokenizer = new BjorneTokenizer(";;;|||&&&<<<>>>"); - BjorneToken token = tokenizer.next(); - assertEquals(TOK_DSEMI, token.getTokenType()); - token = tokenizer.next(); - assertEquals(TOK_SEMI, token.getTokenType()); - token = tokenizer.next(); - assertEquals(TOK_OR_IF, token.getTokenType()); - token = tokenizer.next(); - assertEquals(TOK_BAR, token.getTokenType()); - token = tokenizer.next(); - assertEquals(TOK_AND_IF, token.getTokenType()); - token = tokenizer.next(); - assertEquals(TOK_AMP, token.getTokenType()); - token = tokenizer.next(); - assertEquals(TOK_DLESS, token.getTokenType()); - token = tokenizer.next(); - assertEquals(TOK_LESSGREAT, token.getTokenType()); - token = tokenizer.next(); - assertEquals(TOK_DGREAT, token.getTokenType()); - token = tokenizer.next(); - assertEquals(TOK_END_OF_STREAM, token.getTokenType()); - } - - public void testSymbols4() { - BjorneTokenizer tokenizer = new BjorneTokenizer( - "< << <<- <& <> > >> >| >&"); - BjorneToken token = tokenizer.next(); - assertEquals(TOK_LESS, token.getTokenType()); - token = tokenizer.next(); - assertEquals(TOK_DLESS, token.getTokenType()); - token = tokenizer.next(); - assertEquals(TOK_DLESSDASH, token.getTokenType()); - token = tokenizer.next(); - assertEquals(TOK_LESSAND, token.getTokenType()); - token = tokenizer.next(); - assertEquals(TOK_LESSGREAT, token.getTokenType()); - token = tokenizer.next(); - assertEquals(TOK_GREAT, token.getTokenType()); - token = tokenizer.next(); - assertEquals(TOK_DGREAT, token.getTokenType()); - token = tokenizer.next(); - assertEquals(TOK_CLOBBER, token.getTokenType()); - token = tokenizer.next(); - assertEquals(TOK_GREATAND, token.getTokenType()); - token = tokenizer.next(); - assertEquals(TOK_END_OF_STREAM, token.getTokenType()); - } - - public void testWords() { - BjorneTokenizer tokenizer = new BjorneTokenizer("hello there"); - BjorneToken token = tokenizer.next(); - assertEquals(TOK_WORD, token.getTokenType()); - assertEquals("hello", token.getText()); - token = tokenizer.next(); - assertEquals(TOK_WORD, token.getTokenType()); - assertEquals("there", token.getText()); - token = tokenizer.next(); - assertEquals(TOK_END_OF_STREAM, token.getTokenType()); - } - - public void testWords2() { - BjorneTokenizer tokenizer = new BjorneTokenizer( - "hello\\ there\\\n friend"); - BjorneToken token = tokenizer.next(); - assertEquals(TOK_WORD, token.getTokenType()); - assertEquals("hello\\ there", token.getText()); - token = tokenizer.next(); - assertEquals(TOK_WORD, token.getTokenType()); - assertEquals("friend", token.getText()); - token = tokenizer.next(); - assertEquals(TOK_END_OF_STREAM, token.getTokenType()); - - tokenizer = new BjorneTokenizer("hello\\ there\\\n\\ friend"); - token = tokenizer.next(); - assertEquals(TOK_WORD, token.getTokenType()); - assertEquals("hello\\ there\\ friend", token.getText()); - token = tokenizer.next(); - assertEquals(TOK_END_OF_STREAM, token.getTokenType()); - } - - public void testWords3() { - BjorneTokenizer tokenizer = new BjorneTokenizer("'1 2' \"3 4\" `5 6`"); - BjorneToken token = tokenizer.next(); - assertEquals(TOK_WORD, token.getTokenType()); - assertEquals("'1 2'", token.getText()); - token = tokenizer.next(); - assertEquals(TOK_WORD, token.getTokenType()); - assertEquals("\"3 4\"", token.getText()); - token = tokenizer.next(); - assertEquals(TOK_WORD, token.getTokenType()); - assertEquals("`5 6`", token.getText()); - token = tokenizer.next(); - assertEquals(TOK_END_OF_STREAM, token.getTokenType()); - } - - public void testWords4() { - BjorneTokenizer tokenizer = new BjorneTokenizer("'1 \"2\"' \"3\\\"4\""); - BjorneToken token = tokenizer.next(); - assertEquals(TOK_WORD, token.getTokenType()); - assertEquals("'1 \"2\"'", token.getText()); - token = tokenizer.next(); - assertEquals(TOK_WORD, token.getTokenType()); - assertEquals("\"3\\\"4\"", token.getText()); - token = tokenizer.next(); - assertEquals(TOK_END_OF_STREAM, token.getTokenType()); - } - - public void testWords5() { - BjorneTokenizer tokenizer = new BjorneTokenizer("1<2>3&4;5|6)7"); - BjorneToken token = tokenizer.next(); - assertEquals(TOK_IO_NUMBER, token.getTokenType()); - assertEquals("1", token.getText()); - token = tokenizer.next(); - assertEquals(TOK_LESS, token.getTokenType()); - token = tokenizer.next(); - assertEquals(TOK_IO_NUMBER, token.getTokenType()); - assertEquals("2", token.getText()); - token = tokenizer.next(); - assertEquals(TOK_GREAT, token.getTokenType()); - token = tokenizer.next(); - assertEquals(TOK_WORD, token.getTokenType()); - assertEquals("3", token.getText()); - token = tokenizer.next(); - assertEquals(TOK_AMP, token.getTokenType()); - token = tokenizer.next(); - assertEquals(TOK_WORD, token.getTokenType()); - assertEquals("4", token.getText()); - token = tokenizer.next(); - assertEquals(TOK_SEMI, token.getTokenType()); - token = tokenizer.next(); - assertEquals(TOK_WORD, token.getTokenType()); - assertEquals("5", token.getText()); - token = tokenizer.next(); - assertEquals(TOK_BAR, token.getTokenType()); - token = tokenizer.next(); - assertEquals(TOK_WORD, token.getTokenType()); - assertEquals("6", token.getText()); - token = tokenizer.next(); - assertEquals(TOK_RPAREN, token.getTokenType()); - token = tokenizer.next(); - assertEquals(TOK_WORD, token.getTokenType()); - assertEquals("7", token.getText()); - token = tokenizer.next(); - assertEquals(TOK_END_OF_STREAM, token.getTokenType()); - } - - public void testRule1() { - BjorneTokenizer tokenizer = new BjorneTokenizer( - "if then else elif fi for done while until " - + "case { } ! do in esac"); - BjorneToken token = tokenizer.next(RULE_1_CONTEXT); - assertEquals(TOK_IF, token.getTokenType()); - token = tokenizer.next(RULE_1_CONTEXT); - assertEquals(TOK_THEN, token.getTokenType()); - token = tokenizer.next(RULE_1_CONTEXT); - assertEquals(TOK_ELSE, token.getTokenType()); - token = tokenizer.next(RULE_1_CONTEXT); - assertEquals(TOK_ELIF, token.getTokenType()); - token = tokenizer.next(RULE_1_CONTEXT); - assertEquals(TOK_FI, token.getTokenType()); - token = tokenizer.next(RULE_1_CONTEXT); - assertEquals(TOK_FOR, token.getTokenType()); - token = tokenizer.next(RULE_1_CONTEXT); - assertEquals(TOK_DONE, token.getTokenType()); - token = tokenizer.next(RULE_1_CONTEXT); - assertEquals(TOK_WHILE, token.getTokenType()); - token = tokenizer.next(RULE_1_CONTEXT); - assertEquals(TOK_UNTIL, token.getTokenType()); - token = tokenizer.next(RULE_1_CONTEXT); - assertEquals(TOK_CASE, token.getTokenType()); - token = tokenizer.next(RULE_1_CONTEXT); - assertEquals(TOK_LBRACE, token.getTokenType()); - token = tokenizer.next(RULE_1_CONTEXT); - assertEquals(TOK_RBRACE, token.getTokenType()); - token = tokenizer.next(RULE_1_CONTEXT); - assertEquals(TOK_BANG, token.getTokenType()); - token = tokenizer.next(RULE_1_CONTEXT); - assertEquals(TOK_DO, token.getTokenType()); - token = tokenizer.next(RULE_1_CONTEXT); - assertEquals(TOK_WORD, token.getTokenType()); - token = tokenizer.next(RULE_1_CONTEXT); - assertEquals(TOK_ESAC, token.getTokenType()); - token = tokenizer.next(RULE_1_CONTEXT); - assertEquals(TOK_END_OF_STREAM, token.getTokenType()); - } - - public void testRule5() { - BjorneTokenizer tokenizer = new BjorneTokenizer( - "if a a1 9a a_b a,b AB A=b"); - BjorneToken token = tokenizer.next(RULE_5_CONTEXT); - assertEquals(TOK_NAME, token.getTokenType()); - token = tokenizer.next(RULE_5_CONTEXT); - assertEquals(TOK_NAME, token.getTokenType()); - token = tokenizer.next(RULE_5_CONTEXT); - assertEquals(TOK_NAME, token.getTokenType()); - token = tokenizer.next(RULE_5_CONTEXT); - assertEquals(TOK_WORD, token.getTokenType()); - token = tokenizer.next(RULE_5_CONTEXT); - assertEquals(TOK_NAME, token.getTokenType()); - token = tokenizer.next(RULE_5_CONTEXT); - assertEquals(TOK_WORD, token.getTokenType()); - token = tokenizer.next(RULE_5_CONTEXT); - assertEquals(TOK_NAME, token.getTokenType()); - token = tokenizer.next(RULE_5_CONTEXT); - assertEquals(TOK_WORD, token.getTokenType()); - token = tokenizer.next(RULE_5_CONTEXT); - assertEquals(TOK_END_OF_STREAM, token.getTokenType()); - } - - public void testRule6() { - BjorneTokenizer tokenizer = new BjorneTokenizer("if in do"); - BjorneToken token = tokenizer.next(RULE_6_CONTEXT); - assertEquals(TOK_WORD, token.getTokenType()); - token = tokenizer.next(RULE_6_CONTEXT); - assertEquals(TOK_IN, token.getTokenType()); - token = tokenizer.next(RULE_6_CONTEXT); - assertEquals(TOK_WORD, token.getTokenType()); - token = tokenizer.next(RULE_6_CONTEXT); - assertEquals(TOK_END_OF_STREAM, token.getTokenType()); - } - - public void testRule7a() { - BjorneTokenizer tokenizer = new BjorneTokenizer( - "if then else elif fi for done while until " - + "case { } ! do in esac a= a=b 1a=b =c"); - BjorneToken token = tokenizer.next(RULE_7a_CONTEXT); - assertEquals(TOK_IF, token.getTokenType()); - token = tokenizer.next(RULE_7a_CONTEXT); - assertEquals(TOK_THEN, token.getTokenType()); - token = tokenizer.next(RULE_7a_CONTEXT); - assertEquals(TOK_ELSE, token.getTokenType()); - token = tokenizer.next(RULE_7a_CONTEXT); - assertEquals(TOK_ELIF, token.getTokenType()); - token = tokenizer.next(RULE_7a_CONTEXT); - assertEquals(TOK_FI, token.getTokenType()); - token = tokenizer.next(RULE_7a_CONTEXT); - assertEquals(TOK_FOR, token.getTokenType()); - token = tokenizer.next(RULE_7a_CONTEXT); - assertEquals(TOK_DONE, token.getTokenType()); - token = tokenizer.next(RULE_7a_CONTEXT); - assertEquals(TOK_WHILE, token.getTokenType()); - token = tokenizer.next(RULE_7a_CONTEXT); - assertEquals(TOK_UNTIL, token.getTokenType()); - token = tokenizer.next(RULE_7a_CONTEXT); - assertEquals(TOK_CASE, token.getTokenType()); - token = tokenizer.next(RULE_7a_CONTEXT); - assertEquals(TOK_LBRACE, token.getTokenType()); - token = tokenizer.next(RULE_7a_CONTEXT); - assertEquals(TOK_RBRACE, token.getTokenType()); - token = tokenizer.next(RULE_7a_CONTEXT); - assertEquals(TOK_BANG, token.getTokenType()); - token = tokenizer.next(RULE_7a_CONTEXT); - assertEquals(TOK_DO, token.getTokenType()); - token = tokenizer.next(RULE_7a_CONTEXT); - assertEquals(TOK_WORD, token.getTokenType()); - token = tokenizer.next(RULE_7a_CONTEXT); - assertEquals(TOK_ESAC, token.getTokenType()); - token = tokenizer.next(RULE_7a_CONTEXT); - assertEquals(TOK_ASSIGNMENT, token.getTokenType()); - token = tokenizer.next(RULE_7a_CONTEXT); - assertEquals(TOK_ASSIGNMENT, token.getTokenType()); - token = tokenizer.next(RULE_7a_CONTEXT); - assertEquals(TOK_WORD, token.getTokenType()); - token = tokenizer.next(RULE_7a_CONTEXT); - assertEquals(TOK_WORD, token.getTokenType()); - token = tokenizer.next(RULE_7a_CONTEXT); - assertEquals(TOK_END_OF_STREAM, token.getTokenType()); - } - - public void testRule7b() { - BjorneTokenizer tokenizer = new BjorneTokenizer( - "if then else elif fi for done while until " - + "case { } ! do in esac a= a=b 1a=b =c"); - BjorneToken token = tokenizer.next(RULE_7b_CONTEXT); - assertEquals(TOK_WORD, token.getTokenType()); - token = tokenizer.next(RULE_7b_CONTEXT); - assertEquals(TOK_WORD, token.getTokenType()); - token = tokenizer.next(RULE_7b_CONTEXT); - assertEquals(TOK_WORD, token.getTokenType()); - token = tokenizer.next(RULE_7b_CONTEXT); - assertEquals(TOK_WORD, token.getTokenType()); - token = tokenizer.next(RULE_7b_CONTEXT); - assertEquals(TOK_WORD, token.getTokenType()); - token = tokenizer.next(RULE_7b_CONTEXT); - assertEquals(TOK_WORD, token.getTokenType()); - token = tokenizer.next(RULE_7b_CONTEXT); - assertEquals(TOK_WORD, token.getTokenType()); - token = tokenizer.next(RULE_7b_CONTEXT); - assertEquals(TOK_WORD, token.getTokenType()); - token = tokenizer.next(RULE_7b_CONTEXT); - assertEquals(TOK_WORD, token.getTokenType()); - token = tokenizer.next(RULE_7b_CONTEXT); - assertEquals(TOK_WORD, token.getTokenType()); - token = tokenizer.next(RULE_7b_CONTEXT); - assertEquals(TOK_WORD, token.getTokenType()); - token = tokenizer.next(RULE_7b_CONTEXT); - assertEquals(TOK_WORD, token.getTokenType()); - token = tokenizer.next(RULE_7b_CONTEXT); - assertEquals(TOK_WORD, token.getTokenType()); - token = tokenizer.next(RULE_7b_CONTEXT); - assertEquals(TOK_WORD, token.getTokenType()); - token = tokenizer.next(RULE_7b_CONTEXT); - assertEquals(TOK_WORD, token.getTokenType()); - token = tokenizer.next(RULE_7b_CONTEXT); - assertEquals(TOK_WORD, token.getTokenType()); - token = tokenizer.next(RULE_7b_CONTEXT); - assertEquals(TOK_ASSIGNMENT, token.getTokenType()); - token = tokenizer.next(RULE_7b_CONTEXT); - assertEquals(TOK_ASSIGNMENT, token.getTokenType()); - token = tokenizer.next(RULE_7b_CONTEXT); - assertEquals(TOK_WORD, token.getTokenType()); - token = tokenizer.next(RULE_7b_CONTEXT); - assertEquals(TOK_WORD, token.getTokenType()); - token = tokenizer.next(RULE_7b_CONTEXT); - assertEquals(TOK_END_OF_STREAM, token.getTokenType()); - } - - public void testRule8() { - BjorneTokenizer tokenizer = new BjorneTokenizer( - "if then else elif fi for done while until " - + "case { } ! do in esac a a_b a= a=b 1a=b =c"); - BjorneToken token = tokenizer.next(RULE_8_CONTEXT); - assertEquals(TOK_IF, token.getTokenType()); - token = tokenizer.next(RULE_8_CONTEXT); - assertEquals(TOK_THEN, token.getTokenType()); - token = tokenizer.next(RULE_8_CONTEXT); - assertEquals(TOK_ELSE, token.getTokenType()); - token = tokenizer.next(RULE_8_CONTEXT); - assertEquals(TOK_ELIF, token.getTokenType()); - token = tokenizer.next(RULE_8_CONTEXT); - assertEquals(TOK_FI, token.getTokenType()); - token = tokenizer.next(RULE_8_CONTEXT); - assertEquals(TOK_FOR, token.getTokenType()); - token = tokenizer.next(RULE_8_CONTEXT); - assertEquals(TOK_DONE, token.getTokenType()); - token = tokenizer.next(RULE_8_CONTEXT); - assertEquals(TOK_WHILE, token.getTokenType()); - token = tokenizer.next(RULE_8_CONTEXT); - assertEquals(TOK_UNTIL, token.getTokenType()); - token = tokenizer.next(RULE_8_CONTEXT); - assertEquals(TOK_CASE, token.getTokenType()); - token = tokenizer.next(RULE_8_CONTEXT); - assertEquals(TOK_LBRACE, token.getTokenType()); - token = tokenizer.next(RULE_8_CONTEXT); - assertEquals(TOK_RBRACE, token.getTokenType()); - token = tokenizer.next(RULE_8_CONTEXT); - assertEquals(TOK_BANG, token.getTokenType()); - token = tokenizer.next(RULE_8_CONTEXT); - assertEquals(TOK_DO, token.getTokenType()); - token = tokenizer.next(RULE_8_CONTEXT); - assertEquals(TOK_NAME, token.getTokenType()); // yes: in -> NAME - token = tokenizer.next(RULE_8_CONTEXT); - assertEquals(TOK_ESAC, token.getTokenType()); - token = tokenizer.next(RULE_8_CONTEXT); - assertEquals(TOK_NAME, token.getTokenType()); - token = tokenizer.next(RULE_8_CONTEXT); - assertEquals(TOK_NAME, token.getTokenType()); - token = tokenizer.next(RULE_8_CONTEXT); - assertEquals(TOK_ASSIGNMENT, token.getTokenType()); - token = tokenizer.next(RULE_8_CONTEXT); - assertEquals(TOK_ASSIGNMENT, token.getTokenType()); - token = tokenizer.next(RULE_8_CONTEXT); - assertEquals(TOK_WORD, token.getTokenType()); - token = tokenizer.next(RULE_8_CONTEXT); - assertEquals(TOK_WORD, token.getTokenType()); - token = tokenizer.next(RULE_8_CONTEXT); - assertEquals(TOK_END_OF_STREAM, token.getTokenType()); - } - - public void testRegress() { - BjorneTokenizer tokenizer = new BjorneTokenizer("ls -l"); - BjorneToken token = tokenizer.peek(RULE_7a_CONTEXT); - assertEquals(TOK_WORD, token.getTokenType()); - assertEquals("ls", token.getText()); - token = tokenizer.next(); - assertEquals(TOK_WORD, token.getTokenType()); - assertEquals("ls", token.getText()); - token = tokenizer.peek(); - assertEquals(TOK_WORD, token.getTokenType()); - assertEquals("-l", token.getText()); - token = tokenizer.next(); - assertEquals(TOK_WORD, token.getTokenType()); - assertEquals("-l", token.getText()); - token = tokenizer.peek(); - assertEquals(TOK_END_OF_STREAM, token.getTokenType()); - token = tokenizer.next(); - assertEquals(TOK_END_OF_STREAM, token.getTokenType()); - } -} Added: trunk/shell/src/test/org/jnode/test/shell/bjorne/BjorneContextTests.java =================================================================== --- trunk/shell/src/test/org/jnode/test/shell/bjorne/BjorneContextTests.java (rev 0) +++ trunk/shell/src/test/org/jnode/test/shell/bjorne/BjorneContextTests.java 2009-01-12 14:22:05 UTC (rev 4851) @@ -0,0 +1,135 @@ +/* + * $Id: Command.java 3772 2008-02-10 15:02:53Z lsantha $ + * + * JNode.org + * Copyright (C) 2007-2008 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.Iterator; +import java.util.List; + +import junit.framework.TestCase; + +import org.jnode.shell.ShellException; +import org.jnode.shell.bjorne.BjorneContext; +import org.jnode.shell.bjorne.BjorneToken; + +public class BjorneContextTests extends TestCase { + + public void testContext() { + new BjorneContext(null, null); + } + + public void testExpand1() throws ShellException { + BjorneContext context = new BjorneContext(null, null); + List<BjorneToken> expansion = context.expandAndSplit(""); + checkExpansion(expansion, new String[] {}); + } + + public void testExpand2() throws ShellException { + BjorneContext context = new BjorneContext(null, null); + List<BjorneToken> expansion = context.expandAndSplit(" "); + checkExpansion(expansion, new String[] {}); + } + + public void testExpand3() throws ShellException { + BjorneContext context = new BjorneContext(null, null); + List<BjorneToken> expansion = context.expandAndSplit("hi"); + checkExpansion(expansion, new String[] {"hi"}); + } + + public void testExpand4() throws ShellException { + BjorneContext context = new BjorneContext(null, null); + List<BjorneToken> expansion = context.expandAndSplit("hi there "); + checkExpansion(expansion, new String[] {"hi", "there"}); + } + + public void testExpand5() throws ShellException { + BjorneContext context = new BjorneContext(null, null); + List<BjorneToken> expansion = context.expandAndSplit("'hi there '"); + checkExpansion(expansion, new String[] {"hi there "}); + } + + public void testExpand6() throws ShellException { + BjorneContext context = new BjorneContext(null, null); + List<BjorneToken> expansion = context.expandAndSplit("\"hi there \" "); + checkExpansion(expansion, new String[] {"hi there "}); + } + + public void testExpand7() throws ShellException { + BjorneContext context = new BjorneContext(null, null); + List<BjorneToken> expansion = context.expandAndSplit("hi\\ there"); + checkExpansion(expansion, new String[] {"hi there"}); + } + + public void testExpand8() throws ShellException { + BjorneContext context = new BjorneContext(null, null); + List<BjorneToken> expansion = context.expandAndSplit("\\\"hi\\ there\\\""); + checkExpansion(expansion, new String[] {"\"hi there\""}); + } + + public void testExpand9() throws ShellException { + BjorneContext context = new BjorneContext(null, null); + List<BjorneToken> expansion = context.expandAndSplit("$?"); + checkExpansion(expansion, new String[] {"0"}); + } + + public void testExpand10() throws ShellException { + BjorneContext context = new BjorneContext(null, null); + context.setVariable("A", "A"); + List<BjorneToken> expansion = context.expandAndSplit("$A"); + checkExpansion(expansion, new String[] {"A"}); + } + + public void testExpand11() throws ShellException { + BjorneContext context = new BjorneContext(null, null); + context.setVariable("A", "A"); + List<BjorneToken> expansion = context.expandAndSplit("\\$A"); + checkExpansion(expansion, new String[] {"$A"}); + } + + public void testExpand12() throws ShellException { + BjorneContext context = new BjorneContext(null, null); + context.setVariable("A", "A"); + List<BjorneToken> expansion = context.expandAndSplit("\"$A\""); + checkExpansion(expansion, new String[] {"A"}); + } + + public void testExpand13() throws ShellException { + BjorneContext context = new BjorneContext(null, null); + context.setVariable("A", "A"); + List<BjorneToken> expansion = context.expandAndSplit("'$A'"); + checkExpansion(expansion, new String[] {"$A"}); + } + + @SuppressWarnings("deprecation") + private void checkExpansion(List<BjorneToken> expansion, String[] expected) { + int i; + Iterator<BjorneToken> it = expansion.iterator(); + for (i = 0; i < expected.length; i++) { + if (it.hasNext()) { + assertEquals("incorrect expansion at word " + i, expected[i], it.next().getText()); + } else { + fail("Too few words in expansion at word " + i + ": expecting '" + expected[i] + "'"); + } + } + if (it.hasNext()) { + fail("Too many words in expansion at word " + i + ": '" + it.next() + "'"); + } + } +} Added: trunk/shell/src/test/org/jnode/test/shell/bjorne/BjorneParserTests.java =================================================================== --- trunk/shell/src/test/org/jnode/test/shell/bjorne/BjorneParserTests.java (rev 0) +++ trunk/shell/src/test/org/jnode/test/shell/bjorne/BjorneParserTests.java 2009-01-12 14:22:05 UTC (rev 4851) @@ -0,0 +1,140 @@ +/* + * $Id: Command.java 3772 2008-02-10 15:02:53Z lsantha $ + * + * JNode.org + * Copyright (C) 2007-2008 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 junit.framework.TestCase; + +import org.jnode.shell.ShellException; +import org.jnode.shell.bjorne.BjorneParser; +import org.jnode.shell.bjorne.BjorneTokenizer; + +public class BjorneParserTests extends TestCase { + + private static final boolean DEBUG = true; + + public void testParser() { + new BjorneParser(new BjorneTokenizer("")); + } + + public void test1() throws ShellException { + assertEquals( + "SimpleCommand{nodeType=1,words=[WORD{foo}]}", + doTest("foo")); + } + + public void test2() throws ShellException { + assertEquals( + "SimpleCommand{nodeType=1,words=[WORD{ls},WORD{-l}]}", + doTest("ls -l")); + } + + public void test3() throws ShellException { + assertEquals( + "SimpleCommand{nodeType=1," + + "redirects=[Redirect{redirectionType=60,io=IO_NUMBER{1},arg=WORD{/tmp/foo}}," + + "Redirect{redirectionType=62,arg=WORD{/tmp/bar}}]," + + "words=[WORD{ls},WORD{-l}]}", + doTest("ls -l 1< /tmp/foo > /tmp/bar")); + } + + public void test4() throws ShellException { + assertEquals( + "ListCommand{nodeType=2,flags=0x10," + + "commands=[SimpleCommand{nodeType=1,assignments=[ASSIGNMENT{FOO=BAR}]," + + "words=[WORD{ls},WORD{-l}]}," + + "SimpleCommand{nodeType=1,words=[WORD{less}]}]}", + doTest("FOO=BAR ls -l | less")); + } + + public void test5() throws ShellException { + assertEquals( + "ListCommand{nodeType=2,flags=0x10,commands=[" + + "SimpleCommand{nodeType=1,words=[WORD{cat},WORD{foo}]}," + + "ListCommand{nodeType=10,commands=[" + + "ListCommand{nodeType=2,commands=[" + + "SimpleCommand{nodeType=1,words=[WORD{wc},WORD{1}]}," + + "SimpleCommand{nodeType=1,flags=0x2,words=[WORD{wc},WORD{2}]}]}," + + "ListCommand{nodeType=2,commands=[" + + "SimpleCommand{nodeType=1,words=[WORD{wc},WORD{3}]}," + + "SimpleCommand{nodeType=1,flags=0x4,words=[WORD{wc},WORD{4}]}]}]}]}", + doTest("cat foo | ( wc 1 && wc 2 ; wc 3 || wc 4 )")); + } + + public void test6() throws ShellException { + assertEquals( + "ListCommand{nodeType=2,commands=[" + + "SimpleCommand{nodeType=1,flags=0x1,words=[WORD{cat},WORD{foo}]}," + + "SimpleCommand{nodeType=1,words=[WORD{cat},WORD{bar}]}," + + "SimpleCommand{nodeType=1,words=[WORD{cat},WORD{baz}]}]}", + doTest("cat foo & cat bar ; cat baz ;")); + } + + public void test7() throws ShellException { + assertEquals( + "LoopCommand{nodeType=3,var=NAME{i}," + + "words=[WORD{1},WORD{2},WORD{3},WORD{4},WORD{5}]," + + "body=SimpleCommand{nodeType=1,words=[WORD{echo},WORD{$i}]}}", + doTest("for i in 1 2 3 4 5 ; do echo $i ; done")); + } + + public void test7a() throws ShellException { + assertEquals( + "LoopCommand{nodeType=3,var=NAME{i}," + + "words=[WORD{1},WORD{2},WORD{3},WORD{4},WORD{5}]," + + "body=SimpleCommand{nodeType=1,words=[WORD{echo},WORD{$i}]}}", + doTest("for i in 1 2 3 4 5 ; do \n echo $i ; done")); + } + + public void test8() throws ShellException { + assertEquals( + "LoopCommand{nodeType=4," + + "cond=SimpleCommand{nodeType=1,words=[WORD{true}]}," + + "body=SimpleCommand{nodeType=1,words=[WORD{echo},WORD{$i}]}}", + doTest("while true ; do echo $i ; done")); + } + + public void test9() throws ShellException { + assertEquals( + "LoopCommand{nodeType=5," + + "cond=SimpleCommand{nodeType=1,words=[WORD{true}]}," + + "body=SimpleCommand{nodeType=1,words=[WORD{echo},WORD{$i}]}}", + doTest("until true ; do echo $i ; done")); + } + + public void test10() throws ShellException { + assertEquals( + "CaseCommand{nodeType=9,word=WORD{$1},caseItems=[" + + "CaseItem{,pattern=[],body=" + + "SimpleCommand{nodeType=1,words=[WORD{ls},WORD{-l}]}}," + + "CaseItem{,pattern=[],body=" + + "SimpleCommand{nodeType=1,words=[WORD{ls},WORD{-a}]}}]}", + doTest("case $1 in ( a ) ls -l ;; b ) ls -a ; esac")); + } + + private String doTest(String input) throws ShellException { + BjorneParser p = new BjorneParser(new BjorneTokenizer(input, DEBUG)); + String res = p.parse().toString(); + if (DEBUG) { + System.err.println(res); + } + return res; + } +} Added: trunk/shell/src/test/org/jnode/test/shell/bjorne/BjorneTokenizerTests.java =================================================================== --- trunk/shell/src/test/org/jnode/test/shell/bjorne/BjorneTokenizerTests.java (rev 0) +++ trunk/shell/src/test/org/jnode/test/shell/bjorne/BjorneTokenizerTests.java 2009-01-12 14:22:05 UTC (rev 4851) @@ -0,0 +1,557 @@ +/* + * $Id: Command.java 3772 2008-02-10 15:02:53Z lsantha $ + * + * JNode.org + * Copyright (C) 2007-2008 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 static org.jnode.shell.bjorne.BjorneToken.RULE_1_CONTEXT; +import static org.jnode.shell.bjorne.BjorneToken.RULE_5_CONTEXT; +import static org.jnode.shell.bjorne.BjorneToken.RULE_6_CONTEXT; +import static org.jnode.shell.bjorne.BjorneToken.RULE_7a_CONTEXT; +import static org.jnode.shell.bjorne.BjorneToken.RULE_7b_CONTEXT; +import static org.jnode.shell.bjorne.BjorneToken.RULE_8_CONTEXT; +import static org.jnode.shell.bjorne.BjorneToken.TOK_AMP; +import static org.jnode.shell.bjorne.BjorneToken.TOK_AND_IF; +import static org.jnode.shell.bjorne.BjorneToken.TOK_ASSIGNMENT; +import static org.jnode.shell.bjorne.BjorneToken.TOK_BANG; +import static org.jnode.shell.bjorne.BjorneToken.TOK_BAR; +import static org.jnode.shell.bjorne.BjorneToken.TOK_CASE; +import static org.jnode.shell.bjorne.BjorneToken.TOK_CLOBBER; +import static org.jnode.shell.bjorne.BjorneToken.TOK_DGREAT; +import static org.jnode.shell.bjorne.BjorneToken.TOK_DLESS; +import static org.jnode.shell.bjorne.BjorneToken.TOK_DLESSDASH; +import static org.jnode.shell.bjorne.BjorneToken.TOK_DO; +import static org.jnode.shell.bjorne.BjorneToken.TOK_DONE; +import static org.jnode.shell.bjorne.BjorneToken.TOK_DSEMI; +import static org.jnode.shell.bjorne.BjorneToken.TOK_ELIF; +import static org.jnode.shell.bjorne.BjorneToken.TOK_ELSE; +import static org.jnode.shell.bjorne.BjorneToken.TOK_END_OF_LINE; +import static org.jnode.shell.bjorne.BjorneToken.TOK_END_OF_STREAM; +import static org.jnode.shell.bjorne.BjorneToken.TOK_ESAC; +import static org.jnode.shell.bjorne.BjorneToken.TOK_FI; +import static org.jnode.shell.bjorne.BjorneToken.TOK_FOR; +import static org.jnode.shell.bjorne.BjorneToken.TOK_GREAT; +import static org.jnode.shell.bjorne.BjorneToken.TOK_GREATAND; +import static org.jnode.shell.bjorne.BjorneToken.TOK_IF; +import static org.jnode.shell.bjorne.BjorneToken.TOK_IN; +import static org.jnode.shell.bjorne.BjorneToken.TOK_IO_NUMBER; +import static org.jnode.shell.bjorne.BjorneToken.TOK_LBRACE; +import static org.jnode.shell.bjorne.BjorneToken.TOK_LESS; +import static org.jnode.shell.bjorne.BjorneToken.TOK_LESSAND; +import static org.jnode.shell.bjorne.BjorneToken.TOK_LESSGREAT; +import static org.jnode.shell.bjorne.BjorneToken.TOK_LPAREN; +import static org.jnode.shell.bjorne.BjorneToken.TOK_NAME; +import static org.jnode.shell.bjorne.BjorneToken.TOK_OR_IF; +import static org.jnode.shell.bjorne.BjorneToken.TOK_RBRACE; +import static org.jnode.shell.bjorne.BjorneToken.TOK_RPAREN; +import static org.jnode.shell.bjorne.BjorneToken.TOK_SEMI; +import static org.jnode.shell.bjorne.BjorneToken.TOK_THEN; +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 org.jnode.shell.bjorne.BjorneToken; +import org.jnode.shell.bjorne.BjorneTokenizer; + +import junit.framework.TestCase; + +public class BjorneTokenizerTests extends TestCase { + + public void testBjorneTokenizer() { + new BjorneTokenizer("hello"); + } + + public void testEmpty() { + BjorneTokenizer tokenizer = new BjorneTokenizer(""); + BjorneToken token = tokenizer.peek(); + assertEquals(TOK_END_OF_STREAM, token.getTokenType()); + token = tokenizer.peek(RULE_1_CONTEXT); + assertEquals(TOK_END_OF_STREAM, token.getTokenType()); + token = tokenizer.next(); + assertEquals(TOK_END_OF_STREAM, token.getTokenType()); + token = tokenizer.next(RULE_1_CONTEXT); + assertEquals(TOK_END_OF_STREAM, token.getTokenType()); + } + + public void testNewline() { + BjorneTokenizer tokenizer = new BjorneTokenizer("\n"); + BjorneToken token = tokenizer.next(); + assertEquals(TOK_END_OF_LINE, token.getTokenType()); + token = tokenizer.next(); + assertEquals(TOK_END_OF_STREAM, token.getTok... [truncated message content] |