KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > freemarker > eclipse > test > AbstractRuleTestCase


1 package freemarker.eclipse.test;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.List JavaDoc;
5
6 import junit.framework.TestCase;
7
8 import org.eclipse.jface.text.rules.IRule;
9 import org.eclipse.jface.text.rules.IToken;
10 import org.eclipse.jface.text.rules.Token;
11
12 import freemarker.eclipse.test.util.MockCharacterScanner;
13
14 /**
15  * Superclass for all rule tests. A subclass can add tests by invoking
16  * {@link #addMatchingTest addMatchingTest} or
17  * {@link #addNonMatchingTest addNonMatchingTest}.
18  *
19  * Matching tests evaluate to a token (which is checked) and a position
20  * after the analysis (which is checked as well).
21  *
22  * Non-matching tests should evaluate to Token.UNDEFINED and the
23  * position after the analysis should be 0.
24  *
25  * @author <a HREF="mailto:stephan&#64;chaquotay.net">Stephan Mueller</a>
26  * @version $Id: AbstractRuleTestCase.java,v 1.1 2004/02/05 00:17:52 stephanmueller Exp $
27  */

28 public abstract class AbstractRuleTestCase extends TestCase {
29
30     protected List JavaDoc tests = new ArrayList JavaDoc();
31     protected IRule rule = null;
32
33     public static final IToken NO_MATCH = new Token(null) {
34         public boolean isUndefined() {
35             return (true);
36         }
37         public String JavaDoc toString() {
38             return "no match";
39         }
40     };
41
42     public static final IToken MATCH = new Token(null) {
43         public String JavaDoc toString() {
44             return "match";
45         }
46     };
47
48     public AbstractRuleTestCase(String JavaDoc name) {
49         super(name);
50     }
51     
52     public void runTest() {
53         if (null == rule) {
54             throw new RuntimeException JavaDoc("rule == null");
55         }
56         for (int i = 0; i < tests.size(); i++) {
57             RuleTestInfo info = (RuleTestInfo) tests.get(i);
58             MockCharacterScanner scanner =
59                 new MockCharacterScanner(info.text);
60             IToken result = rule.evaluate(scanner);
61
62             // we use our own 'UNDEFINED' token which looks nicer
63
if (result.equals(Token.UNDEFINED))
64                 result = NO_MATCH;
65
66             assertEquals(info.message, info.expected, result);
67             assertEquals(info.message, info.pos, scanner.getPos());
68         }
69     }
70
71     public void addMatchingTest(String JavaDoc text, int pos, String JavaDoc message) {
72         tests.add(new RuleTestInfo(text, MATCH, pos, message));
73     }
74
75     public void addNonMatchingTest(String JavaDoc text, String JavaDoc message) {
76         tests.add(new RuleTestInfo(text, NO_MATCH, 0, message));
77     }
78
79     private class RuleTestInfo {
80
81         public int pos;
82         public IToken expected;
83         public String JavaDoc text;
84         public String JavaDoc message;
85
86         public RuleTestInfo(String JavaDoc text, IToken exp, int pos, String JavaDoc msg) {
87             this.pos = pos;
88             this.expected = exp;
89             this.message = msg;
90             this.text = text;
91         }
92     }
93
94 }
95
Popular Tags