author | Jaroslav Tulach <jtulach@netbeans.org> |
Sat, 14 Jun 2008 09:59:20 +0200 | |
changeset 177 | 67d6dceb1002 |
parent 176 | 0f658628beac |
child 179 | b94c14f52c31 |
permissions | -rw-r--r-- |
jtulach@176 | 1 |
package org.apidesign.test.visitor; |
jtulach@176 | 2 |
|
jtulach@177 | 3 |
import org.apidesign.visitor.Language.Expression; |
jtulach@177 | 4 |
import org.apidesign.visitor.Language.Number; |
jtulach@177 | 5 |
import org.apidesign.visitor.Language.Plus; |
jtulach@177 | 6 |
import org.apidesign.visitor.Language.Visitor; |
jtulach@177 | 7 |
import org.apidesign.visitor.Language.Visitor; |
jtulach@176 | 8 |
import static junit.framework.Assert.*; |
jtulach@176 | 9 |
import org.junit.Test; |
jtulach@176 | 10 |
|
jtulach@176 | 11 |
public class PrintTest { |
jtulach@177 | 12 |
|
jtulach@177 | 13 |
public static Number newNumber(final int value) { |
jtulach@177 | 14 |
return new Number() { |
jtulach@177 | 15 |
public int getValue() { |
jtulach@177 | 16 |
return value; |
jtulach@177 | 17 |
} |
jtulach@176 | 18 |
|
jtulach@177 | 19 |
public void visit(Visitor v) { |
jtulach@177 | 20 |
v.dispatchNumber(this); |
jtulach@177 | 21 |
} |
jtulach@177 | 22 |
}; |
jtulach@177 | 23 |
} |
jtulach@177 | 24 |
public static Plus newPlus( |
jtulach@177 | 25 |
final Expression first, final Expression second |
jtulach@177 | 26 |
) { |
jtulach@177 | 27 |
return new Plus() { |
jtulach@177 | 28 |
public Expression getFirst() { |
jtulach@177 | 29 |
return first; |
jtulach@177 | 30 |
} |
jtulach@177 | 31 |
|
jtulach@177 | 32 |
public Expression getSecond() { |
jtulach@177 | 33 |
return second; |
jtulach@177 | 34 |
} |
jtulach@177 | 35 |
|
jtulach@177 | 36 |
public void visit(Visitor v) { |
jtulach@177 | 37 |
v.dispatchPlus(this); |
jtulach@177 | 38 |
} |
jtulach@177 | 39 |
}; |
jtulach@177 | 40 |
} |
jtulach@177 | 41 |
|
jtulach@177 | 42 |
public static class PrintVisitor implements Visitor.Version1_0 { |
jtulach@176 | 43 |
StringBuffer sb = new StringBuffer(); |
jtulach@176 | 44 |
|
jtulach@177 | 45 |
final Visitor dispatch = Visitor.create(this); |
jtulach@177 | 46 |
|
jtulach@176 | 47 |
public void visitPlus(Plus s) { |
jtulach@177 | 48 |
s.getFirst().visit(dispatch); |
jtulach@176 | 49 |
sb.append(" + "); |
jtulach@177 | 50 |
s.getSecond().visit(dispatch); |
jtulach@176 | 51 |
} |
jtulach@176 | 52 |
|
jtulach@176 | 53 |
public void visitNumber(Number n) { |
jtulach@176 | 54 |
sb.append (n.getValue()); |
jtulach@176 | 55 |
} |
jtulach@177 | 56 |
|
jtulach@177 | 57 |
public boolean visitUnknown(Expression e) { |
jtulach@177 | 58 |
sb.append("unknown"); |
jtulach@177 | 59 |
return true; |
jtulach@177 | 60 |
} |
jtulach@176 | 61 |
} |
jtulach@176 | 62 |
|
jtulach@176 | 63 |
@Test public void printOnePlusOne() { |
jtulach@177 | 64 |
Number one = newNumber(1); |
jtulach@177 | 65 |
Expression plus = newPlus(one, one); |
jtulach@176 | 66 |
|
jtulach@176 | 67 |
PrintVisitor print = new PrintVisitor(); |
jtulach@177 | 68 |
plus.visit(print.dispatch); |
jtulach@176 | 69 |
|
jtulach@176 | 70 |
assertEquals("1 + 1", print.sb.toString()); |
jtulach@176 | 71 |
} |
jtulach@176 | 72 |
|
jtulach@176 | 73 |
@Test public void printOnePlusTwoPlusThree() { |
jtulach@177 | 74 |
Number one = newNumber(1); |
jtulach@177 | 75 |
Number two = newNumber(2); |
jtulach@177 | 76 |
Number three = newNumber(3); |
jtulach@177 | 77 |
Expression plus = newPlus(one, newPlus(two, three)); |
jtulach@176 | 78 |
|
jtulach@176 | 79 |
PrintVisitor print = new PrintVisitor(); |
jtulach@177 | 80 |
plus.visit(print.dispatch); |
jtulach@176 | 81 |
|
jtulach@176 | 82 |
assertEquals("1 + 2 + 3", print.sb.toString()); |
jtulach@176 | 83 |
} |
jtulach@176 | 84 |
} |