author | Jaroslav Tulach <jtulach@netbeans.org> |
Sat, 14 Jun 2008 10:08:12 +0200 | |
changeset 244 | 2b19d24a755f |
parent 167 | ab8c04922abe |
permissions | -rw-r--r-- |
jtulach@167 | 1 |
package org.apidesign.test.visitor; |
jtulach@167 | 2 |
|
jtulach@167 | 3 |
import static junit.framework.Assert.*; |
jtulach@167 | 4 |
import org.apidesign.visitor.Language.Expression; |
jtulach@167 | 5 |
import org.apidesign.visitor.Language.Number; |
jtulach@167 | 6 |
import org.apidesign.visitor.Language.Plus; |
jtulach@167 | 7 |
import org.apidesign.visitor.Language.Visitor; |
jtulach@167 | 8 |
import org.junit.Test; |
jtulach@167 | 9 |
|
jtulach@167 | 10 |
public class CountNumbersTest { |
jtulach@167 | 11 |
|
jtulach@167 | 12 |
// BEGIN: visitor.count.numbers.traversal |
jtulach@167 | 13 |
private static class CountNumbers extends Visitor/*version1.0*/ { |
jtulach@167 | 14 |
int cnt; |
jtulach@167 | 15 |
|
jtulach@167 | 16 |
@Override |
jtulach@167 | 17 |
public boolean visitUnknown(Expression exp) { |
jtulach@167 | 18 |
return true; |
jtulach@167 | 19 |
} |
jtulach@244 | 20 |
@Override |
jtulach@167 | 21 |
public void visitNumber(Number n) { |
jtulach@167 | 22 |
cnt++; |
jtulach@167 | 23 |
} |
jtulach@167 | 24 |
} |
jtulach@167 | 25 |
|
jtulach@167 | 26 |
public static int countNumbers(Expression expression) { |
jtulach@167 | 27 |
CountNumbers counter = new CountNumbers(); |
jtulach@167 | 28 |
expression.visit(counter); |
jtulach@167 | 29 |
return counter.cnt; |
jtulach@167 | 30 |
} |
jtulach@167 | 31 |
// END: visitor.count.numbers.traversal |
jtulach@167 | 32 |
|
jtulach@167 | 33 |
@Test public void printOnePlusOne() { |
jtulach@167 | 34 |
Number one = new Number(1); |
jtulach@167 | 35 |
Expression expression = new Plus(one, one); |
jtulach@167 | 36 |
|
jtulach@167 | 37 |
assertEquals("Two 1's", 2, countNumbers(expression)); |
jtulach@167 | 38 |
} |
jtulach@167 | 39 |
|
jtulach@167 | 40 |
@Test public void printOnePlusTwoPlusThree() { |
jtulach@167 | 41 |
Number one = new Number(1); |
jtulach@167 | 42 |
Number two = new Number(2); |
jtulach@167 | 43 |
Number three = new Number(3); |
jtulach@167 | 44 |
Expression plus = new Plus(one, new Plus(two, three)); |
jtulach@167 | 45 |
|
jtulach@167 | 46 |
assertEquals("Three", 3, countNumbers(plus)); |
jtulach@167 | 47 |
} |
jtulach@167 | 48 |
} |