author | Jaroslav Tulach <jtulach@netbeans.org> |
Sat, 14 Jun 2008 22:38:00 +0200 | |
changeset 265 | eb31b2d1822a |
parent 178 | f3f2f73686d8 |
child 266 | 47d5f1fe0b03 |
permissions | -rw-r--r-- |
jtulach@169 | 1 |
package org.apidesign.test.visitor; |
jtulach@169 | 2 |
|
jtulach@169 | 3 |
import static junit.framework.Assert.*; |
jtulach@169 | 4 |
import org.apidesign.visitor.Language.Expression; |
jtulach@169 | 5 |
import org.apidesign.visitor.Language.Number; |
jtulach@169 | 6 |
import org.apidesign.visitor.Language.Plus; |
jtulach@170 | 7 |
import org.apidesign.visitor.Language.Visitor1_0; |
jtulach@169 | 8 |
import org.junit.Test; |
jtulach@169 | 9 |
|
jtulach@169 | 10 |
public class PrintTest { |
jtulach@169 | 11 |
|
jtulach@170 | 12 |
public static class PrintVisitor implements Visitor1_0 { |
jtulach@169 | 13 |
StringBuffer sb = new StringBuffer(); |
jtulach@169 | 14 |
|
jtulach@169 | 15 |
public void visitUnknown(Expression exp) { |
jtulach@169 | 16 |
sb.append("unknown"); |
jtulach@169 | 17 |
} |
jtulach@169 | 18 |
|
jtulach@169 | 19 |
public void visitPlus(Plus s) { |
jtulach@169 | 20 |
s.getFirst().visit(this); |
jtulach@169 | 21 |
sb.append(" + "); |
jtulach@169 | 22 |
s.getSecond().visit(this); |
jtulach@169 | 23 |
} |
jtulach@169 | 24 |
|
jtulach@169 | 25 |
public void visitNumber(Number n) { |
jtulach@169 | 26 |
sb.append (n.getValue()); |
jtulach@169 | 27 |
} |
jtulach@169 | 28 |
} |
jtulach@169 | 29 |
|
jtulach@169 | 30 |
@Test public void printOnePlusOne() { |
jtulach@169 | 31 |
Number one = new Number(1); |
jtulach@169 | 32 |
Expression plus = new Plus(one, one); |
jtulach@169 | 33 |
|
jtulach@169 | 34 |
PrintVisitor print = new PrintVisitor(); |
jtulach@169 | 35 |
plus.visit(print); |
jtulach@169 | 36 |
|
jtulach@169 | 37 |
assertEquals("1 + 1", print.sb.toString()); |
jtulach@169 | 38 |
} |
jtulach@169 | 39 |
|
jtulach@169 | 40 |
@Test public void printOnePlusTwoPlusThree() { |
jtulach@169 | 41 |
Number one = new Number(1); |
jtulach@169 | 42 |
Number two = new Number(2); |
jtulach@169 | 43 |
Number three = new Number(3); |
jtulach@169 | 44 |
Expression plus = new Plus(one, new Plus(two, three)); |
jtulach@169 | 45 |
|
jtulach@169 | 46 |
PrintVisitor print = new PrintVisitor(); |
jtulach@169 | 47 |
plus.visit(print); |
jtulach@169 | 48 |
|
jtulach@169 | 49 |
assertEquals("1 + 2 + 3", print.sb.toString()); |
jtulach@169 | 50 |
} |
jtulach@169 | 51 |
} |