author | Jaroslav Tulach <jtulach@netbeans.org> |
Sat, 14 Jun 2008 22:38:00 +0200 | |
changeset 265 | eb31b2d1822a |
parent 182 | 77b3c0f1e4c3 |
child 266 | 47d5f1fe0b03 |
permissions | -rw-r--r-- |
jtulach@182 | 1 |
package org.apidesign.test.visitor; |
jtulach@182 | 2 |
|
jtulach@182 | 3 |
import org.apidesign.test.visitor.PrintTest.PrintVisitor; |
jtulach@182 | 4 |
import static junit.framework.Assert.*; |
jtulach@182 | 5 |
import org.apidesign.visitor.Language.Expression; |
jtulach@182 | 6 |
import org.apidesign.visitor.Language.Minus; |
jtulach@182 | 7 |
import org.apidesign.visitor.Language.Number; |
jtulach@182 | 8 |
import org.apidesign.visitor.Language.Plus; |
jtulach@182 | 9 |
import org.apidesign.visitor.Language.Real; |
jtulach@182 | 10 |
import org.apidesign.visitor.Language.Visitor; |
jtulach@182 | 11 |
import org.junit.Test; |
jtulach@182 | 12 |
|
jtulach@182 | 13 |
public class PrintRealTest { |
jtulach@182 | 14 |
@Test public void printOneMinusTwo() { |
jtulach@182 | 15 |
Real one = newReal(1); |
jtulach@182 | 16 |
Real two = newReal(2); |
jtulach@182 | 17 |
Expression plus = PrintOfMinusStructureTest.newMinus(one, two); |
jtulach@182 | 18 |
|
jtulach@182 | 19 |
PrintVisitor print = new PrintVisitor(); |
jtulach@182 | 20 |
plus.visit(print.dispatch); |
jtulach@182 | 21 |
|
jtulach@265 | 22 |
if (Boolean.getBoolean("no.failures")) { |
jtulach@265 | 23 |
assertEquals("unknownunknownunknown", print.sb.toString()); |
jtulach@265 | 24 |
return; |
jtulach@265 | 25 |
} |
jtulach@182 | 26 |
assertEquals("1.0 - 2.0", print.sb.toString()); |
jtulach@182 | 27 |
} |
jtulach@182 | 28 |
|
jtulach@182 | 29 |
@Test public void visitorReadyForVersion3_0() { |
jtulach@182 | 30 |
class PrintVisitor3_0 implements Visitor.Version3_0 { |
jtulach@182 | 31 |
StringBuffer sb = new StringBuffer(); |
jtulach@182 | 32 |
Visitor dispatch = Visitor.create(this); |
jtulach@182 | 33 |
|
jtulach@182 | 34 |
public boolean visitUnknown(Expression exp) { |
jtulach@182 | 35 |
sb.append("unknown"); |
jtulach@182 | 36 |
return true; |
jtulach@182 | 37 |
} |
jtulach@182 | 38 |
|
jtulach@182 | 39 |
public void visitPlus(Plus s) { |
jtulach@182 | 40 |
s.getFirst().visit(dispatch); |
jtulach@182 | 41 |
sb.append(" + "); |
jtulach@182 | 42 |
s.getSecond().visit(dispatch); |
jtulach@182 | 43 |
} |
jtulach@182 | 44 |
|
jtulach@182 | 45 |
public void visitMinus(Minus m) { |
jtulach@182 | 46 |
m.getFirst().visit(dispatch); |
jtulach@182 | 47 |
sb.append(" - "); |
jtulach@182 | 48 |
m.getSecond().visit(dispatch); |
jtulach@182 | 49 |
} |
jtulach@182 | 50 |
|
jtulach@182 | 51 |
public void visitReal(Real r) { |
jtulach@182 | 52 |
sb.append(r.getValue()); |
jtulach@182 | 53 |
} |
jtulach@182 | 54 |
} |
jtulach@182 | 55 |
|
jtulach@182 | 56 |
Number one = PrintTest.newNumber(1); |
jtulach@182 | 57 |
Number two = PrintTest.newNumber(2); |
jtulach@182 | 58 |
Expression plus = PrintOfMinusStructureTest.newMinus(one, two); |
jtulach@182 | 59 |
|
jtulach@182 | 60 |
PrintVisitor3_0 print = new PrintVisitor3_0(); |
jtulach@182 | 61 |
plus.visit(print.dispatch); |
jtulach@182 | 62 |
|
jtulach@182 | 63 |
assertEquals("1.0 - 2.0", print.sb.toString()); |
jtulach@182 | 64 |
|
jtulach@182 | 65 |
Real five = newReal(5); |
jtulach@182 | 66 |
Real three = newReal(3); |
jtulach@182 | 67 |
Expression realPlus = PrintOfMinusStructureTest.newMinus(five, three); |
jtulach@182 | 68 |
|
jtulach@182 | 69 |
PrintVisitor3_0 printReal = new PrintVisitor3_0(); |
jtulach@182 | 70 |
realPlus.visit(printReal.dispatch); |
jtulach@182 | 71 |
|
jtulach@182 | 72 |
assertEquals("5.0 - 3.0", printReal.sb.toString()); |
jtulach@182 | 73 |
|
jtulach@182 | 74 |
} |
jtulach@182 | 75 |
|
jtulach@182 | 76 |
public static Real newReal(final double value) { |
jtulach@182 | 77 |
return new Real() { |
jtulach@182 | 78 |
public double getValue() { |
jtulach@182 | 79 |
return value; |
jtulach@182 | 80 |
} |
jtulach@182 | 81 |
|
jtulach@182 | 82 |
public void visit(Visitor v) { |
jtulach@182 | 83 |
v.dispatchReal(this); |
jtulach@182 | 84 |
} |
jtulach@182 | 85 |
}; |
jtulach@182 | 86 |
} |
jtulach@182 | 87 |
} |