001 /*
002 * YamParserTests.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, 12th May 2006
011 */
012
013 package gate.yam.parse;
014
015 import java.io.*;
016 import java.util.*;
017 import junit.framework.*;
018 import org.apache.log4j.Logger;
019 import gate.util.*;
020 import gate.yam.*;
021
022
023 /**
024 * Unit test for YAM parse trees.
025 */
026 public class YamParserTests extends TestCase
027 {
028
029 /** Create the test case */
030 public YamParserTests(String testName) {
031 super(testName);
032 }
033
034 /** Logger */
035 static Logger lgr = Logger.getLogger("gate.yam");
036
037 /** SInS (plain) logger */
038 static Logger slgr = Logger.getLogger("gate.sins");
039
040 /** Test errors and warnings. */
041 public void testErrorsAndWarnings() throws Exception {
042 String testName = "/gate/yam/resources/yam-errors.yam";
043 lgr.info("testing errors generated when parsing " + testName);
044
045 // find the source directory
046 String testFilePath =
047 this.getClass().getResource(testName).getPath();
048 File testFileDir = new File(testFilePath).getParentFile();
049
050 // a reader for the test file
051 InputStream testFileStream =
052 this.getClass().getResourceAsStream(testName);
053 if(testFileStream == null) fail("couldn't get resource " + testName);
054 InputStreamReader testReader =
055 new InputStreamReader(testFileStream, "UTF-8");
056
057 // response writer
058 StringWriter responseWriter = new StringWriter();
059
060 // run the translator and check the errors
061 YamCommand yam = new YamCommand();
062 YamParseTree parseTree = yam.translate(
063 testReader, responseWriter, YamFile.FileType.HTML, testFileDir
064 );
065 List errors = parseTree.getErrors();
066 List warnings = parseTree.getWarnings();
067
068 int e = errors.size();
069 if(e != 3) lgr.info(yam.printErrors(parseTree));
070 //lgr.info(yam.printErrors(parseTree));
071 assertEquals("wrong number of errors", 3, e);
072
073 ParsingProblem p = (ParsingProblem) errors.get(1);
074 //lgr.info(p.getMessage());
075 assertEquals("begin line wrong", p.beginLine, 17);
076 assertEquals("begin column wrong", p.beginColumn, 19);
077 assertEquals("end line wrong", p.endLine, 24);
078 assertEquals("end column wrong", p.endColumn, 2);
079 assertEquals("included flag wrong", p.included, false);
080
081 p = (ParsingProblem) errors.get(2);
082 //lgr.info(p.getMessage());
083 assertEquals("begin line wrong", p.beginLine, 1);
084 assertEquals("begin column wrong", p.beginColumn, 1);
085 assertEquals("end line wrong", p.endLine, 1);
086 assertEquals("end column wrong", p.endColumn, 9);
087 assertEquals("included flag wrong", p.included, true);
088 }
089
090 /**
091 * Test error handling utility stuff.
092 */
093 public void testErrorStuff() throws Exception
094 {
095 String eStrings[] = {
096 // empty strings
097 "", "", "",
098
099 // empty lines
100 "\n", "", "\n",
101 "\n\n", "", "\n\n",
102 "\n\n\n", "", "\n\n\n",
103 "\n\n\n\n", "\n\n\n", "\n",
104
105 // unix lines
106 "123\n0", "", "123\n0",
107 "123\n456\n7", "123\n", "456\n7",
108 "123\n456\n789\n0", "123\n456\n", "789\n0",
109 "123\n", "", "123\n",
110 "123\n456\n", "123\n", "456\n",
111 "123\n456\n789\n", "123\n456\n", "789\n",
112
113 // mac lines
114 "123\r0", "", "123\r0",
115 "123\r456\r7", "123\r", "456\r7",
116 "123\r456\r789\r0", "123\r456\r", "789\r0",
117 "123\r", "", "123\r",
118 "123\r456\r", "123\r", "456\r",
119 "123\r456\r789\r", "123\r456\r", "789\r",
120
121 // dos lines
122 "123\r\n0", "", "123\r\n0",
123 "123\r\n456\r\n7", "123\r\n", "456\r\n7",
124 "123\r\n456\r\n789\r\n0", "123\r\n456\r\n", "789\r\n0",
125 "123\r\n", "", "123\r\n",
126 "123\r\n456\r\n", "123\r\n", "456\r\n",
127 "123\r\n456\r\n789\r\n", "123\r\n456\r\n", "789\r\n",
128
129 // messed-up lines
130 "123\n\r456\n\r789\n\r", "123\n\r456\n\r789\n", "\r",
131 };
132 StringReader reader = new StringReader("");
133 YamParser parser = new YamParser(reader);
134
135 for(int i=0; i<eStrings.length; i += 3) {
136 String eString = eStrings[i];
137 String start = eStrings[i+1];
138 String end = eStrings[i+2];
139
140 // this was superseded by the getNextToken-based error code in
141 // YamParser.parse
142 // String[] split = parser.splitErrorInput(eString);
143 /*
144 lgr.info("i=" + i + ", start(" + start + "); end(" + end + ")");
145 lgr.info(
146 "i=" + i + ", split[0](" + split[0] + "); split[1](" + split[1] + ")"
147 );
148 lgr.info();
149 */
150 // assertTrue(
151 // "i=" + i + ", start(" + start + ") wrong", start.equals(split[0])
152 // );
153 // assertTrue(
154 // "i=" + i + ", end(" + end + ") wrong", end.equals(split[1])
155 // );
156 }
157 } // testErrorStuff()
158
159 /** Test the getSepSpacing function. */
160 public void testGetSepSpacing() {
161 String[] cases = {
162 "\n",
163 "\n ",
164 "\n ",
165 "\n\t",
166 "\n \t ",
167 "\n \t\t \t",
168
169 "\r",
170 "\r ",
171 "\r ",
172 "\r\t",
173 "\r \t ",
174 "\r \t\t \t",
175
176 "\r\n",
177 "\r\n ",
178 "\r\n ",
179 "\r\n\t",
180 "\r\n \t ",
181 "\r\n \t\t \t",
182
183 "\n\n\r\n",
184 "\n\n\r\n ",
185 "\n\n\r\n ",
186 "\n\n\r\n\t",
187 "\n\n\r\n \t ",
188 "\n\n\r\n \t\t \t",
189 };
190 int[] expectedSpacings = {
191 0, 1, 2, 8, 10, 24,
192 0, 1, 2, 8, 10, 24,
193 0, 1, 2, 8, 10, 24,
194 0, 1, 2, 8, 10, 24,
195 };
196 StringReader reader = new StringReader("");
197 YamParser p = new YamParser(reader);
198 Token t = new Token();
199
200 for(int caseNumber = 0; caseNumber < cases.length; caseNumber++) {
201 t.image = cases[caseNumber];
202 int spacing = p.getSepSpacing(t);
203 assertEquals("wrong spacing: ", expectedSpacings[caseNumber], spacing);
204 }
205 } // testGetSepSpacing()
206
207 } // YamParserTests
|