001 /*
002 * PrettyTranslatorTests.java
003 * Copyright (c) 1998-2008, The University of Sheffield.
004 *
005 * This code is from the GATE project (http://gate.ac.uk/) and is free
006 * software licenced under the GNU General Public License version 3. It is
007 * distributed without any warranty. For more details see COPYING.txt in the
008 * top level directory (or at http://gatewiki.sf.net/COPYING.txt).
009 *
010 * Hamish Cunningham, 3rd May 2006
011 */
012
013 package gate.yam.translate;
014
015 import java.io.*;
016 import junit.framework.*;
017 import org.apache.log4j.Logger;
018 import gate.*;
019 import gate.util.*;
020 import gate.yam.*;
021 import gate.yam.parse.*;
022 import static gate.yam.translate.NodeKind.*;
023
024
025 /**
026 * Unit test for PrettyTranslator.
027 */
028 public class PrettyTranslatorTests extends AbstractTranslatorTest
029 {
030 /** Logger */
031 static Logger log =
032 Logger.getLogger("gate.yam.translate.PrettyTranslatorTests");
033
034 /** Initialise GATE if it hasn't already been done. */
035 static {
036 if(! Gate.isInitialised()) {
037 File cowGate =
038 new File(System.getProperty("user.dir"), "web-app/WEB-INF/gate");
039 System.setProperty(
040 "gate.user.session",
041 new File(cowGate, "user-gate.session").getAbsolutePath()
042 );
043 System.setProperty(
044 "gate.user.config",
045 new File(cowGate, "user-gate.xml").getAbsolutePath()
046 );
047 System.setProperty(
048 "gate.site.config",
049 new File(cowGate, "site-gate.xml").getAbsolutePath()
050 );
051 Gate.setGateHome(cowGate);
052 try {
053 Gate.init();
054 } catch(GateException e) {
055 log.error("couldn't init Gate");
056 log.error(e);
057 }
058 }
059 } // static block
060
061 /** Create the test case. */
062 public PrettyTranslatorTests(String testName) { super(testName); }
063
064 /** Paths of example test files. */
065 public String[] getTestFilePaths() {
066 String testFilePaths[] = {
067 /* TODO
068 "/yam-errors", // unterminated comment
069 */
070 "/yam-tables",
071 "/yam-small-tables",
072 "/yam-tables-bug",
073 "/yam-errors-min",
074 "/yam-small-error",
075 "/yam-minimal",
076 "/yam-lists",
077 "/yam-includes",
078 "/yam-pretty",
079 "/yam-anchor-links",
080 "/yam-context",
081 "/yam-includes2",
082 "/yam-lists-bug",
083 "/yam-lists-bug-simple",
084 "/yam-lists-bug2",
085 "/yam-lists-bug2-simple",
086 "/yam-scratch",
087 "/yam-images",
088 "/yam-zero",
089 "/yam-first",
090 "/yam-urls",
091 "/yam-predicates",
092 "/yam-heading-increments",
093 "/yam-comprehensive",
094 "/yam-large",
095 "/yam-backlog-bug",
096 "/yam-wierd",
097 "/yam-headings",
098 "/yam-huge",
099 };
100 return testFilePaths;
101 }
102
103 /** Suffix of input files. */
104 public String getInputSuffix() { return "yam"; }
105
106 /** Suffix of output files. */
107 public String[] getOutputSuffixes() {
108 String outputSuffixes[] = {
109 "yam"
110 };
111 return outputSuffixes;
112 }
113
114 /** Run the translator and get the response */
115 public Writer doTranslation(
116 Reader testReader, Writer responseWriter, String outputType,
117 String testName
118 ) throws Exception {
119 log.debug("testName = " + testName);
120
121 // find the source directory
122 String testFilePath =
123 this.getClass().getResource(testName + ".yam").getPath();
124 File testFileDir = new File(testFilePath).getParentFile();
125
126 YamParseTree tree = null;
127 if(outputType.equals("yam")) {
128 YamCommand yam = new YamCommand();
129 tree = yam.translate(
130 testReader, responseWriter, YamFile.FileType.YAM, testFileDir
131 );
132 } else {
133 throw new GateException("unknown output type " + outputType);
134 }
135
136 if(testName.equals("/gate/yam/resources/yam-pretty")) {
137 log.debug("doing extra tests...");
138 doSuperCleverMysteriousTests(tree);
139 }
140
141 return responseWriter;
142 } // doTranslation(...)
143
144 public void doSuperCleverMysteriousTests(YamParseTree tree) {
145 // tree has a first Sep node
146 SimpleNode rootNode = tree.getRootNode();
147 SimpleNode child = null;
148 Class childClass = null;
149 int childNumber = 0;
150 int numChildren = rootNode.jjtGetNumChildren();
151 do {
152 child = (SimpleNode) rootNode.jjtGetChild(childNumber++);
153 } while(
154 childNumber < numChildren && child != null &&
155 ( (childClass = child.getClass()) == ASTSep.class )
156 );
157 log.debug("childClass=" + childClass.getName());
158 assertTrue(child != null && childClass != ASTSep.class);
159
160 // Cursor finds the first Sep node, etc.
161 Cursor here = new Cursor(rootNode, 1, 3);
162 log.debug(
163 "previousKind=" + here.getPreviousKind() + "; kind=" + here.getKind() +
164 "; nextKind=" + here.getNextKind()
165 );
166 assertTrue(child == here.getHereNode());
167 assertTrue(here.getKind() == NodeKind.UNIT_PARA);
168 assertTrue(here.getPreviousKind() == NodeKind.NULL);
169
170 // all children of the root are Sep, Unit or Word
171 childNumber = 0;
172 for( ; childNumber < numChildren; childNumber++) {
173 SimpleNode n = (SimpleNode) rootNode.jjtGetChild(childNumber);
174 if(n == null) continue;
175 Class nClass = n.getClass();
176 log.debug("nClass=" + nClass.getName());
177 assertTrue(
178 nClass == ASTSep.class || nClass == ASTWord.class ||
179 nClass == ASTUnit.class
180 );
181 }
182
183 log.debug(
184 "previousKind=" + here.getPreviousKind() + "; kind=" + here.getKind() +
185 "; nextKind=" + here.getNextKind()
186 );
187 checkHere(here, NULL, UNIT_PARA, UNIT_PARA);
188 here.advance();
189 checkHere(here, UNIT_PARA, UNIT_PARA, UNIT_INCLUDE);
190 here.advance();
191 checkHere(here, UNIT_PARA, UNIT_INCLUDE, UNIT_PARA);
192 here.advance();
193 checkHere(here, UNIT_INCLUDE, UNIT_PARA, UNIT_PARA);
194 here.advance();
195 checkHere(here, UNIT_PARA, UNIT_PARA, UNIT_SECTION);
196 here.advance();
197 checkHere(here, UNIT_PARA, UNIT_SECTION, UNIT_PARA);
198 here.advance();
199 checkHere(here, UNIT_SECTION, UNIT_PARA, NULL);
200 here.advance();
201 assertTrue(here.atEnd());
202 checkHere(here, UNIT_PARA, NULL, NULL);
203 here.advance();
204 assertTrue(here.atEnd());
205 checkHere(here, NULL, NULL, NULL);
206 here.advance();
207 assertTrue(here.atEnd());
208 checkHere(here, NULL, NULL, NULL);
209 } // doSuperClever...
210
211 /** check the types at the current cursor position */
212 void checkHere(Cursor c, NodeKind prevK, NodeKind hereK, NodeKind nextK) {
213 log.debug(
214 "previousKind=" + c.getPreviousKind() + "; kind=" + c.getKind() +
215 "; nextKind=" + c.getNextKind()
216 );
217 assertTrue(c.getPreviousKind() == prevK);
218 assertTrue(c.getKind() == hereK);
219 assertTrue(c.getNextKind() == nextK);
220 } // checkHere(Cursor, NodeKind, NodeKind, NodeKind)
221
222 } // PrettyTranslatorTests
|