1 3 package test.jmock.examples.calculator; 4 5 import junit.framework.TestCase; 6 import org.jmock.examples.calculator.Lexer; 7 import org.jmock.examples.calculator.Token; 8 9 10 public class LexerTest 11 extends TestCase 12 { 13 public void testHasAStackOfPushBackTokens() throws Exception { 14 Lexer lexer = new Lexer(""); 15 lexer.pushBack(new Token(1, "1")); 16 lexer.pushBack(new Token(2, "2")); 17 18 assertNextToken(lexer, 2, "2"); 19 assertNextToken(lexer, 1, "1"); 20 assertNextToken(lexer, Token.END, null); 21 } 22 23 public void testTokenisesLiteralIntegerValues() throws Exception { 24 String input = "23"; 25 Lexer lexer = new Lexer(input); 26 27 assertNextToken(lexer, Token.NUMBER, "23"); 28 assertNextToken(lexer, Token.END, null); 29 } 30 31 public void testTokenisesLiteralRealValues() throws Exception { 32 String input = "123.45"; 33 Lexer lexer = new Lexer(input); 34 35 assertNextToken(lexer, Token.NUMBER, "123.45"); 36 assertNextToken(lexer, Token.END, null); 37 } 38 39 public void testTokenisesAddOperator() throws Exception { 40 Lexer lexer = new Lexer("1+2"); 41 42 assertNextToken(lexer, Token.NUMBER, "1"); 43 assertNextToken(lexer, Token.ADD, "+"); 44 assertNextToken(lexer, Token.NUMBER, "2"); 45 assertNextToken(lexer, Token.END, null); 46 } 47 48 public void testTokenisesSubtractOperator() throws Exception { 49 Lexer lexer = new Lexer("1-2"); 50 51 assertNextToken(lexer, Token.NUMBER, "1"); 52 assertNextToken(lexer, Token.SUBTRACT, "-"); 53 assertNextToken(lexer, Token.NUMBER, "2"); 54 assertNextToken(lexer, Token.END, null); 55 } 56 57 public void testTokenisesMultiplyOperator() throws Exception { 58 Lexer lexer = new Lexer("1*2"); 59 60 assertNextToken(lexer, Token.NUMBER, "1"); 61 assertNextToken(lexer, Token.MULTIPLY, "*"); 62 assertNextToken(lexer, Token.NUMBER, "2"); 63 assertNextToken(lexer, Token.END, null); 64 } 65 66 public void testTokenisesDivideOperator() throws Exception { 67 Lexer lexer = new Lexer("1/2"); 68 69 assertNextToken(lexer, Token.NUMBER, "1"); 70 assertNextToken(lexer, Token.DIVIDE, "/"); 71 assertNextToken(lexer, Token.NUMBER, "2"); 72 assertNextToken(lexer, Token.END, null); 73 } 74 75 public void testTokenisesPowerOperator() throws Exception { 76 Lexer lexer = new Lexer("1^2"); 77 78 assertNextToken(lexer, Token.NUMBER, "1"); 79 assertNextToken(lexer, Token.POWER, "^"); 80 assertNextToken(lexer, Token.NUMBER, "2"); 81 assertNextToken(lexer, Token.END, null); 82 } 83 84 public void testTokenisesIdentifiers() throws Exception { 85 Lexer lexer = new Lexer("identifier anotherIdentifier-identifier3"); 86 87 assertNextToken(lexer, Token.IDENTIFIER, "identifier"); 88 assertNextToken(lexer, Token.IDENTIFIER, "anotherIdentifier"); 89 assertNextToken(lexer, Token.SUBTRACT, "-"); 90 assertNextToken(lexer, Token.IDENTIFIER, "identifier3"); 91 assertNextToken(lexer, Token.END, null); 92 } 93 94 public void testSkipsWhitespaceBetweenTokens() throws Exception { 95 String input = " 2 \t\n\r + 3"; 96 Lexer lexer = new Lexer(input); 97 98 assertNextToken(lexer, Token.NUMBER, "2"); 99 assertNextToken(lexer, Token.ADD, "+"); 100 assertNextToken(lexer, Token.NUMBER, "3"); 101 assertNextToken(lexer, Token.END, null); 102 } 103 104 public void testTokenisesParentheses() throws Exception { 105 Lexer lexer = new Lexer("(x+y)*z"); 106 107 assertNextToken(lexer, Token.PAREN_OPEN, "("); 108 assertNextToken(lexer, Token.IDENTIFIER, "x"); 109 assertNextToken(lexer, Token.ADD, "+"); 110 assertNextToken(lexer, Token.IDENTIFIER, "y"); 111 assertNextToken(lexer, Token.PAREN_CLOSE, ")"); 112 assertNextToken(lexer, Token.MULTIPLY, "*"); 113 assertNextToken(lexer, Token.IDENTIFIER, "z"); 114 assertNextToken(lexer, Token.END, null); 115 } 116 117 private void assertNextToken( Lexer lexer, int tokenType, String tokenValue ) 118 throws Exception { 119 Token token = lexer.nextToken(); 120 121 assertEquals("token type", tokenType, token.getType()); 122 assertEquals("token value", tokenValue, token.getValue()); 123 } 124 } 125 | Popular Tags |