1
package junit.test.parser.svg.parsers;
3
import java.text.ParseException;
5
import junit.framework.TestCase;
6
import latexDraw.parsers.svg.SVGLength;
7
import latexDraw.parsers.svg.parsers.SVGLengthParser;
8
import latexDraw.parsers.svg.parsers.UnitProcessor;
10
import org.junit.Test;
13
* Tests the class SVGLengthParser.<br>
15
* This file is part of LaTeXDraw<br>
16
* Copyright (c) 2005-2008 Arnaud BLOUIN<br>
18
* LaTeXDraw is free software; you can redistribute it and/or modify
19
* it under the terms of the GNU General Public License as published by
20
* the Free Software Foundation; either version 2 of the License, or
21
* any later version.<br>
23
* LaTeXDraw is distributed without any warranty; without even the
24
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
25
* PURPOSE. See the GNU General Public License for more details.<br>
28
* @author Arnaud BLOUIN<br>
32
@SuppressWarnings("nls")
33
public class TestSVGLengthParser extends TestCase
36
public void testParseLength()
43
new SVGLengthParser(null);
46
catch(IllegalArgumentException e){ /* */ }
50
p = new SVGLengthParser("");
54
catch(ParseException e){ /* */ }
58
p = new SVGLengthParser("1m");
62
catch(ParseException e){ /* */ }
66
p = new SVGLengthParser("m");
70
catch(ParseException e){ /* */ }
74
p = new SVGLengthParser("1p");
78
catch(ParseException e){ /* */ }
82
p = new SVGLengthParser("1e1i");
86
catch(ParseException e){ /* */ }
90
p = new SVGLengthParser("1ci");
94
catch(ParseException e){ /* */ }
98
p = new SVGLengthParser("1 c ");
102
catch(ParseException e){ /* */ }
106
p = new SVGLengthParser("1mm");
109
assertEquals(l.getValueAsString(), "1");
110
assertEquals(l.getLengthType(), SVGLength.LengthType.MM);
111
assertEquals(UnitProcessor.toUserUnit(1, SVGLength.LengthType.MM), l.getValue());
113
catch(ParseException e){ fail(); }
117
p = new SVGLengthParser("0.65 cm");
120
assertEquals(l.getValueAsString(), "0.65");
121
assertEquals(l.getLengthType(), SVGLength.LengthType.CM);
122
assertEquals(UnitProcessor.toUserUnit(0.65, SVGLength.LengthType.CM), l.getValue());
124
catch(ParseException e){ fail(); }
128
p = new SVGLengthParser("\t -10.65 \t \n pc");
131
assertEquals(l.getValueAsString(), "-10.65");
132
assertEquals(l.getLengthType(), SVGLength.LengthType.PC);
133
assertEquals(UnitProcessor.toUserUnit(-10.65, SVGLength.LengthType.PC), l.getValue());
135
catch(ParseException e){ fail(); }
139
p = new SVGLengthParser("\t -10.65e2 \t \n px \t ");
142
assertEquals(l.getValueAsString(), "-10.65e2");
143
assertEquals(l.getLengthType(), SVGLength.LengthType.PX);
144
assertEquals(UnitProcessor.toUserUnit(-10.65e2, SVGLength.LengthType.PX), l.getValue());
146
catch(ParseException e){ fail(); }
150
p = new SVGLengthParser("\t -10.65e0pt \t ");
153
assertEquals(l.getValueAsString(), "-10.65e0");
154
assertEquals(l.getLengthType(), SVGLength.LengthType.PT);
155
assertEquals(UnitProcessor.toUserUnit(-10.65e0, SVGLength.LengthType.PT), l.getValue());
157
catch(ParseException e){ fail(); }
161
p = new SVGLengthParser("\t -1.in \t ");
164
assertEquals(l.getValueAsString(), "-1.");
165
assertEquals(l.getLengthType(), SVGLength.LengthType.IN);
166
assertEquals(UnitProcessor.toUserUnit(-1., SVGLength.LengthType.IN), l.getValue());
168
catch(ParseException e){ fail(); }
172
p = new SVGLengthParser("-1.");
175
assertEquals(l.getValueAsString(), "-1.");
176
assertEquals(l.getLengthType(), SVGLength.LengthType.NUMBER);
177
assertEquals(UnitProcessor.toUserUnit(-1., SVGLength.LengthType.NUMBER), l.getValue());
179
catch(ParseException e){ fail(); }
185
public void testParseNumberOrPercent()
192
p = new SVGLengthParser("1mm");
193
l = p.parseNumberOrPercent();
195
assertEquals(l.getValueAsString(), "1");
196
assertEquals(l.getLengthType(), SVGLength.LengthType.NUMBER);
197
assertEquals(UnitProcessor.toUserUnit(1, SVGLength.LengthType.NUMBER), l.getValue());
199
catch(ParseException e){ fail(); }
203
p = new SVGLengthParser("0.876");
204
l = p.parseNumberOrPercent();
206
assertEquals(l.getValueAsString(), "0.876");
207
assertEquals(l.getLengthType(), SVGLength.LengthType.NUMBER);
208
assertEquals(UnitProcessor.toUserUnit(0.876, SVGLength.LengthType.NUMBER), l.getValue());
210
catch(ParseException e){ fail(); }
216
public void testParseCoordinate()
223
p = new SVGLengthParser("");
224
l = p.parseCoordinate();
227
catch(ParseException e){ /* */ }
231
p = new SVGLengthParser("1m");
232
l = p.parseCoordinate();
235
catch(ParseException e){ /* */ }
239
p = new SVGLengthParser("m");
240
l = p.parseCoordinate();
243
catch(ParseException e){ /* */ }
247
p = new SVGLengthParser("1p");
248
l = p.parseCoordinate();
251
catch(ParseException e){ /* */ }
255
p = new SVGLengthParser("1e1i");
256
l = p.parseCoordinate();
259
catch(ParseException e){ /* */ }
263
p = new SVGLengthParser("1ci");
264
l = p.parseCoordinate();
267
catch(ParseException e){ /* */ }
271
p = new SVGLengthParser("1 c ");
272
l = p.parseCoordinate();
275
catch(ParseException e){ /* */ }
279
p = new SVGLengthParser("1mm");
280
l = p.parseCoordinate();
282
assertEquals(l.getValueAsString(), "1");
283
assertEquals(l.getLengthType(), SVGLength.LengthType.MM);
284
assertEquals(UnitProcessor.toUserUnit(1, SVGLength.LengthType.MM), l.getValue());
286
catch(ParseException e){ fail(); }
290
p = new SVGLengthParser("0.65 cm");
291
l = p.parseCoordinate();
293
assertEquals(l.getValueAsString(), "0.65");
294
assertEquals(l.getLengthType(), SVGLength.LengthType.CM);
295
assertEquals(UnitProcessor.toUserUnit(0.65, SVGLength.LengthType.CM), l.getValue());
297
catch(ParseException e){ fail(); }
301
p = new SVGLengthParser("\t -10.65 \t \n pc");
302
l = p.parseCoordinate();
304
assertEquals(l.getValueAsString(), "-10.65");
305
assertEquals(l.getLengthType(), SVGLength.LengthType.PC);
306
assertEquals(UnitProcessor.toUserUnit(-10.65, SVGLength.LengthType.PC), l.getValue());
308
catch(ParseException e){ fail(); }
312
p = new SVGLengthParser("\t -10.65e2 \t \n px \t ");
313
l = p.parseCoordinate();
315
assertEquals(l.getValueAsString(), "-10.65e2");
316
assertEquals(l.getLengthType(), SVGLength.LengthType.PX);
317
assertEquals(UnitProcessor.toUserUnit(-10.65e2, SVGLength.LengthType.PX), l.getValue());
319
catch(ParseException e){ fail(); }
323
p = new SVGLengthParser("\t -10.65e0pt \t ");
324
l = p.parseCoordinate();
326
assertEquals(l.getValueAsString(), "-10.65e0");
327
assertEquals(l.getLengthType(), SVGLength.LengthType.PT);
328
assertEquals(UnitProcessor.toUserUnit(-10.65e0, SVGLength.LengthType.PT), l.getValue());
330
catch(ParseException e){ fail(); }
334
p = new SVGLengthParser("\t -1.in \t ");
335
l = p.parseCoordinate();
337
assertEquals(l.getValueAsString(), "-1.");
338
assertEquals(l.getLengthType(), SVGLength.LengthType.IN);
339
assertEquals(UnitProcessor.toUserUnit(-1., SVGLength.LengthType.IN), l.getValue());
341
catch(ParseException e){ fail(); }
345
p = new SVGLengthParser("-1.");
346
l = p.parseCoordinate();
348
assertEquals(l.getValueAsString(), "-1.");
349
assertEquals(l.getLengthType(), SVGLength.LengthType.NUMBER);
350
assertEquals(UnitProcessor.toUserUnit(-1., SVGLength.LengthType.NUMBER), l.getValue());
352
catch(ParseException e){ fail(); }