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