author | Jaroslav Tulach <jtulach@netbeans.org> |
Sat, 14 Jun 2008 22:38:00 +0200 | |
changeset 265 | eb31b2d1822a |
parent 174 | 3f3e772ccd52 |
child 266 | 47d5f1fe0b03 |
permissions | -rw-r--r-- |
jtulach@172 | 1 |
package org.apidesign.test.visitor; |
jtulach@172 | 2 |
|
jtulach@172 | 3 |
import org.apidesign.test.visitor.PrintTest.PrintVisitor; |
jtulach@172 | 4 |
import static junit.framework.Assert.*; |
jtulach@172 | 5 |
import org.apidesign.visitor.Language.Expression; |
jtulach@172 | 6 |
import org.apidesign.visitor.Language.Minus; |
jtulach@172 | 7 |
import org.apidesign.visitor.Language.Number; |
jtulach@172 | 8 |
import org.apidesign.visitor.Language.Plus; |
jtulach@172 | 9 |
import org.apidesign.visitor.Language.Real; |
jtulach@172 | 10 |
import org.apidesign.visitor.Language.Visitor3_0; |
jtulach@172 | 11 |
import org.junit.Test; |
jtulach@172 | 12 |
|
jtulach@172 | 13 |
public class PrintOfMinusStructureTest { |
jtulach@172 | 14 |
@Test public void printOneMinusTwo() { |
jtulach@172 | 15 |
Real one = new Real(1); |
jtulach@172 | 16 |
Real two = new Real(2); |
jtulach@172 | 17 |
Expression plus = new Minus(one, two); |
jtulach@172 | 18 |
|
jtulach@172 | 19 |
PrintVisitor print = new PrintVisitor(); |
jtulach@172 | 20 |
plus.visit(print); |
jtulach@172 | 21 |
|
jtulach@265 | 22 |
if (Boolean.getBoolean("no.failures")) { |
jtulach@265 | 23 |
assertEquals( |
jtulach@265 | 24 |
"Not defined how to handle Minus. We'll get wrong result", |
jtulach@265 | 25 |
"unknown", print.sb.toString() |
jtulach@265 | 26 |
); |
jtulach@265 | 27 |
return; |
jtulach@265 | 28 |
} |
jtulach@174 | 29 |
assertEquals("1.0 - 2.0", print.sb.toString()); |
jtulach@172 | 30 |
} |
jtulach@172 | 31 |
|
jtulach@172 | 32 |
@Test public void visitorReadyForVersion3_0() { |
jtulach@172 | 33 |
// BEGIN: visitor.nonmonotonic.print3 |
jtulach@172 | 34 |
class PrintVisitor3_0 implements Visitor3_0 { |
jtulach@172 | 35 |
StringBuffer sb = new StringBuffer(); |
jtulach@172 | 36 |
|
jtulach@172 | 37 |
public void visitUnknown(Expression exp) { |
jtulach@172 | 38 |
sb.append("unknown"); |
jtulach@172 | 39 |
} |
jtulach@172 | 40 |
|
jtulach@172 | 41 |
public void visitPlus(Plus s) { |
jtulach@172 | 42 |
s.getFirst().visit(this); |
jtulach@172 | 43 |
sb.append(" + "); |
jtulach@172 | 44 |
s.getSecond().visit(this); |
jtulach@172 | 45 |
} |
jtulach@172 | 46 |
|
jtulach@172 | 47 |
public void visitNumber(Number n) { |
jtulach@172 | 48 |
sb.append (n.getValue()); |
jtulach@172 | 49 |
} |
jtulach@172 | 50 |
public void visitMinus(Minus m) { |
jtulach@172 | 51 |
m.getFirst().visit(this); |
jtulach@172 | 52 |
sb.append(" - "); |
jtulach@172 | 53 |
m.getSecond().visit(this); |
jtulach@172 | 54 |
} |
jtulach@172 | 55 |
|
jtulach@172 | 56 |
public void visitReal(Real r) { |
jtulach@172 | 57 |
sb.append(r.getValue()); |
jtulach@172 | 58 |
} |
jtulach@172 | 59 |
} |
jtulach@172 | 60 |
|
jtulach@172 | 61 |
Number one = new Number(1); |
jtulach@172 | 62 |
Number two = new Number(2); |
jtulach@172 | 63 |
Expression plus = new Minus(one, two); |
jtulach@172 | 64 |
|
jtulach@172 | 65 |
PrintVisitor3_0 print = new PrintVisitor3_0(); |
jtulach@172 | 66 |
plus.visit(print); |
jtulach@172 | 67 |
|
jtulach@172 | 68 |
assertEquals("1.0 - 2.0", print.sb.toString()); |
jtulach@172 | 69 |
// END: visitor.nonmonotonic.print3 |
jtulach@174 | 70 |
|
jtulach@174 | 71 |
Real five = new Real(5); |
jtulach@174 | 72 |
Real three = new Real(3); |
jtulach@174 | 73 |
Expression realPlus = new Minus(five, three); |
jtulach@174 | 74 |
|
jtulach@174 | 75 |
PrintVisitor3_0 printReal = new PrintVisitor3_0(); |
jtulach@174 | 76 |
realPlus.visit(printReal); |
jtulach@174 | 77 |
|
jtulach@174 | 78 |
assertEquals("5.0 - 3.0", printReal.sb.toString()); |
jtulach@172 | 79 |
|
jtulach@172 | 80 |
} |
jtulach@172 | 81 |
} |