1/*
2 [The "BSD license"]
3 Copyright (c) 2007 Kenny MacDermid
4 All rights reserved.
5
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
8 are met:
9 1. Redistributions of source code must retain the above copyright
10    notice, this list of conditions and the following disclaimer.
11 2. Redistributions in binary form must reproduce the above copyright
12    notice, this list of conditions and the following disclaimer in the
13    documentation and/or other materials provided with the distribution.
14 3. The name of the author may not be used to endorse or promote products
15    derived from this software without specific prior written permission.
16
17 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27*/
28package org.antlr.gunit;
29
30public abstract class AbstractTest implements ITestCase {
31	// store essential individual test result for string template
32	protected String header;
33	protected String actual;
34
35	protected boolean hasErrorMsg;
36
37	private String testedRuleName;
38	private int testCaseIndex;
39
40	// TODO: remove these. They're only used as part of a refactor to keep the
41	//       code cleaner. It is a mock-instanceOf() replacement.
42	public abstract int getType();
43	public abstract String getText();
44
45	public abstract String getExpected();
46	// return an escaped string of the expected result
47	public String getExpectedResult() {
48		String expected = getExpected();
49		if ( expected!=null ) expected = JUnitCodeGen.escapeForJava(expected);
50		return expected;
51	}
52	public abstract String getResult(gUnitTestResult testResult);
53	public String getHeader() { return this.header; }
54	public String getActual() { return this.actual; }
55	// return an escaped string of the actual result
56	public String getActualResult() {
57		String actual = getActual();
58		// there is no need to escape the error message from ANTLR
59		if ( actual!=null && !hasErrorMsg ) actual = JUnitCodeGen.escapeForJava(actual);
60		return actual;
61	}
62
63	public String getTestedRuleName() { return this.testedRuleName; }
64	public int getTestCaseIndex() { return this.testCaseIndex; }
65
66	public void setHeader(String rule, String lexicalRule, String treeRule, int numOfTest, int line) {
67		StringBuffer buf = new StringBuffer();
68		buf.append("test" + numOfTest + " (");
69		if ( treeRule!=null ) {
70			buf.append(treeRule+" walks ");
71		}
72		if ( lexicalRule!=null ) {
73			buf.append(lexicalRule + ", line"+line+")" + " - ");
74		}
75		else buf.append(rule + ", line"+line+")" + " - ");
76		this.header = buf.toString();
77	}
78	public void setActual(String actual) { this.actual = actual; }
79
80	public void setTestedRuleName(String testedRuleName) { this.testedRuleName = testedRuleName; }
81	public void setTestCaseIndex(int testCaseIndex) { this.testCaseIndex = testCaseIndex; }
82
83}
84