source: trunk/org.modelica.mdt.test/src/org/modelica/mdt/test/TestModelicaParser.java

Last change on this file was 437, checked in by remar, 19 years ago
  • parser now understands strings and doesn't try to parse contents of 'em
  • elmir was really tired when he did the test for the parser, so it's been fixed
File size: 20.0 KB
Line 
1/*
2 * This file is part of Modelica Development Tooling.
3 *
4 * Copyright (c) 2005, Link�pings universitet, Department of
5 * Computer and Information Science, PELAB
6 *
7 * All rights reserved.
8 *
9 * (The new BSD license, see also
10 * http://www.opensource.org/licenses/bsd-license.php)
11 *
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions are
15 * met:
16 *
17 * * Redistributions of source code must retain the above copyright
18 *   notice, this list of conditions and the following disclaimer.
19 *
20 * * Redistributions in binary form must reproduce the above copyright
21 *   notice, this list of conditions and the following disclaimer in
22 *   the documentation and/or other materials provided with the
23 *   distribution.
24 *
25 * * Neither the name of Link�pings universitet nor the names of its
26 *   contributors may be used to endorse or promote products derived from
27 *   this software without specific prior written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
32 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
33 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
34 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
39 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 */
41
42package org.modelica.mdt.test;
43
44import java.util.Iterator;
45
46import org.modelica.mdt.core.Element;
47import org.modelica.mdt.core.List;
48import org.modelica.mdt.core.ListElement;
49import org.modelica.mdt.core.ModelicaParserException;
50import org.modelica.mdt.core.compiler.ModelicaParser;
51
52import junit.framework.TestCase;
53
54/**
55 * test org.modelica.mdt.core.compiler.ModelicaParser class' code
56 */
57public class TestModelicaParser extends TestCase
58{
59    /*
60     * the output of the getElements() function to test parsing
61     */
62    private static final String GET_ELEMENTS_OUTPUT =
63       
64    "{ { elementvisibility=public, " +
65     "elementfile=\"/home/x05andre/ex/Modelica Library/Modelica/package.mo\", "+
66     "elementline=2, elementcolumn=1, final=false, replaceable=false, "+
67     "inout=\"none\", elementtype=extends, path=Icons.Library },\n"+
68    "{ elementvisibility=public, elementtype=annotation },\n"+
69    "{ elementvisibility=public, " +
70     "elementfile=\"/home/x05andre/ex/Modelica Library/Modelica/package.mo\", " +
71     "elementline=127, elementcolumn=1, final=false, replaceable=false, " +
72     "inout=\"none\", elementtype=classdef, classname=UsersGuide, " +
73     "classrestriction=PACKAGE, " +
74     "classfile=\"/home/x05andre/ex/Modelica Library/Modelica/package.mo\", " +
75     "classline=127, classcolumn=1 },\n"+
76    "{ elementvisibility=public, elementfile=\"\", elementline=0, " +
77     "elementcolumn=0, final=false, replaceable=false, inout=\"none\", " +
78     "elementtype=classdef, classname=Media, classrestriction=PACKAGE, " +
79     "classfile=\"/home/x05andre/ex/Modelica" +
80                " Library/Modelica/Media/package.mo\", " +
81     "classline=1, classcolumn=1 },\n"+
82    "{ elementvisibility=public, elementfile=\"\", elementline=0, " +
83     "elementcolumn=0, final=false, replaceable=false, inout=\"none\", " +
84     "elementtype=classdef, classname=Utilities, classrestriction=PACKAGE, " +
85     "classfile=\"/home/x05andre/ex/Modelica " +
86                "Library/Modelica/Utilities/package.mo\", " +
87                "classline=1, classcolumn=1 },\n"+
88    "{ elementvisibility=public, elementfile=\"\", elementline=0, " +
89     "elementcolumn=0, final=false, replaceable=false, inout=\"none\", " +
90     "elementtype=classdef, classname=Mechanics, classrestriction=PACKAGE, " +
91     "classfile=\"/home/x05andre/ex/Modelica Library/" +
92                 "Modelica/Mechanics/package.mo\", " +
93     "classline=1, classcolumn=1 },\n"+
94    "{ elementvisibility=public, elementfile=\"\", elementline=0, " +
95     "elementcolumn=0, final=false, replaceable=false, inout=\"none\", " +
96     "elementtype=classdef, classname=Electrical, classrestriction=PACKAGE, " +
97     "classfile=\"/home/x05andre/ex/Modelica Library/" +
98                "Modelica/Electrical/package.mo\"," +
99     " classline=1, classcolumn=1 },\n"+
100    "{ elementvisibility=public, elementfile=\"\", elementline=0, " +
101     "elementcolumn=0, final=false, replaceable=false, inout=\"none\", " +
102     "elementtype=classdef, classname=Math, classrestriction=PACKAGE, " +
103     "classfile=\"/home/x05andre/ex/Modelica Library/Modelica/Math/package.mo\","+
104     " classline=1, classcolumn=1 },\n"+
105    "{ elementvisibility=public, elementfile=\"\", elementline=0, " +
106     "elementcolumn=0, final=false, replaceable=false, inout=\"none\", " +
107     "elementtype=classdef, classname=Blocks, classrestriction=PACKAGE, " +
108     "classfile=\"/home/x05andre/ex/Modelica " +
109                "Library/Modelica/Blocks/package.mo\", " +
110                "classline=1, classcolumn=1 },\n"+
111    "{ elementvisibility=public, elementfile=\"\", elementline=0, " +
112     "elementcolumn=0, final=false, replaceable=false, inout=\"none\", " +
113     "elementtype=classdef, classname=Thermal, classrestriction=PACKAGE, " +
114     "classfile=\"/home/x05andre/ex/Modelica " +
115                "Library/Modelica/Thermal/package.mo\", classline=1, " +
116                "classcolumn=1 },\n"+
117    "{ elementvisibility=public, elementfile=\"\", elementline=0, " +
118     "elementcolumn=0, final=false, replaceable=false, inout=\"none\", " +
119     "elementtype=classdef, classname=Icons, classrestriction=PACKAGE, " +
120     "classfile=\"/home/x05andre/ex/Modelica Library/Modelica/Icons.mo\", " +
121     "classline=1, classcolumn=1 },\n"+
122    "{ elementvisibility=public, elementfile=\"\", elementline=0, " +
123     "elementcolumn=0, final=false, replaceable=false, inout=\"none\", " +
124     "elementtype=classdef, classname=Constants, classrestriction=PACKAGE, " +
125     "classfile=\"/home/x05andre/ex/Modelica Library/Modelica/Constants.mo\", " +
126     "classline=1, classcolumn=1 },\n"+
127    "{ elementvisibility=public, elementfile=\"\", elementline=0, " +
128     "elementcolumn=0, final=false, replaceable=false, inout=\"none\", " +
129     "elementtype=classdef, classname=SIunits, classrestriction=PACKAGE, " +
130     "classfile=\"/home/x05andre/ex/Modelica Library/Modelica/SIunits.mo\", " +
131     "classline=1, classcolumn=1 },\n"+
132    "{ elementvisibility=public, elementfile=\"\", elementline=0, " +
133     "elementcolumn=0, final=false, replaceable=false, inout=\"none\", " +
134     "elementtype=classdef, classname=StateGraph, classrestriction=PACKAGE, " +
135     "classfile=\"/home/x05andre/ex/Modelica Library/Modelica/StateGraph.mo\", " +
136     "classline=1, classcolumn=1 }\n"+
137    "}\n";
138   
139    /*
140     * the output of the getElements() function to test parsing
141     */
142
143    private static final String GET_CLASS_INFO1 = 
144    "{\"model\",\"Chua's circuit, ns, V, A\",\"/home/x05andre/ex/Modelica " +
145    "Library/Modelica/Electrical/Analog/Examples/ChuaCircuit.mo\",{false,f" +
146    "alse,true},{\"writable\",1,1,65,16}}";
147    /*
148     * The expected results when getElements() output is parsed
149     */
150    private static final String[][] GET_ELEMENTS_RES =
151    {
152      {"elementvisibility=public",
153       "elementfile=\"/home/x05andre/ex/Modelica Library/Modelica/package.mo\"",
154       "elementline=2", "elementcolumn=1", "final=false", "replaceable=false",
155       "inout=\"none\"", "elementtype=extends", "path=Icons.Library"},
156       
157      {"elementvisibility=public", "elementtype=annotation"},
158     
159      {"elementvisibility=public",
160       "elementfile=\"/home/x05andre/ex/Modelica Library/Modelica/package.mo\"",
161       "elementline=127","elementcolumn=1", "final=false", "replaceable=false",
162       "inout=\"none\"", "elementtype=classdef", "classname=UsersGuide",
163       "classrestriction=PACKAGE",
164       "classfile=\"/home/x05andre/ex/Modelica Library/Modelica/package.mo\"",
165       "classline=127", "classcolumn=1"},
166
167      {"elementvisibility=public", "elementfile=\"\"", "elementline=0",
168       "elementcolumn=0", "final=false", "replaceable=false", "inout=\"none\"",
169       "elementtype=classdef", "classname=Media", "classrestriction=PACKAGE",
170       "classfile=\"/home/x05andre/ex/Modelica Library/Modelica/Media/package.mo\"",
171       "classline=1", "classcolumn=1"},
172       
173      {"elementvisibility=public", "elementfile=\"\"", "elementline=0",
174       "elementcolumn=0", "final=false", "replaceable=false", "inout=\"none\"",
175       "elementtype=classdef", "classname=Utilities", 
176       "classrestriction=PACKAGE",
177       "classfile=\"/home/x05andre/ex/Modelica " +
178                    "Library/Modelica/Utilities/package.mo\"",
179       "classline=1", "classcolumn=1"},
180       
181      {"elementvisibility=public", "elementfile=\"\"", "elementline=0",
182       "elementcolumn=0", "final=false", "replaceable=false", "inout=\"none\"",
183       "elementtype=classdef", "classname=Mechanics", "classrestriction=PACKAGE",
184       "classfile=\"/home/x05andre/ex/Modelica " +
185                 "Library/Modelica/Mechanics/package.mo\"",
186       "classline=1", "classcolumn=1"},
187       
188      {"elementvisibility=public", "elementfile=\"\"", "elementline=0",
189       "elementcolumn=0", "final=false", "replaceable=false", "inout=\"none\"",
190       "elementtype=classdef", "classname=Electrical", 
191       "classrestriction=PACKAGE",
192       "classfile=\"/home/x05andre/ex/Modelica Library/" +
193                    "Modelica/Electrical/package.mo\"",
194       "classline=1", "classcolumn=1" },
195       
196      {"elementvisibility=public", "elementfile=\"\"", "elementline=0",
197       "elementcolumn=0", "final=false", "replaceable=false", "inout=\"none\"",
198       "elementtype=classdef", "classname=Math", "classrestriction=PACKAGE",
199       "classfile=\"/home/x05andre/ex/Modelica "+
200                   "Library/Modelica/Math/package.mo\"",
201       "classline=1", "classcolumn=1" },
202       
203      {"elementvisibility=public", "elementfile=\"\"", "elementline=0",
204       "elementcolumn=0", "final=false", "replaceable=false", "inout=\"none\"",
205       "elementtype=classdef", "classname=Blocks", "classrestriction=PACKAGE",
206       "classfile=\"/home/x05andre/ex/Modelica " +
207                    "Library/Modelica/Blocks/package.mo\"",
208       "classline=1", "classcolumn=1" },
209
210      {"elementvisibility=public", "elementfile=\"\"", "elementline=0",
211       "elementcolumn=0", "final=false", "replaceable=false", "inout=\"none\"",
212       "elementtype=classdef", "classname=Thermal", "classrestriction=PACKAGE",
213       "classfile=\"/home/x05andre/ex/Modelica " +
214                    "Library/Modelica/Thermal/package.mo\"", "classline=1",
215       "classcolumn=1"},
216       
217      {"elementvisibility=public", "elementfile=\"\"", "elementline=0",
218       "elementcolumn=0", "final=false", "replaceable=false", "inout=\"none\"",
219       "elementtype=classdef", "classname=Icons", "classrestriction=PACKAGE",
220       "classfile=\"/home/x05andre/ex/Modelica Library/Modelica/Icons.mo\"",
221       "classline=1", "classcolumn=1" },
222
223      {"elementvisibility=public", "elementfile=\"\"", "elementline=0",
224       "elementcolumn=0", "final=false", "replaceable=false", "inout=\"none\"",
225       "elementtype=classdef","classname=Constants", "classrestriction=PACKAGE",
226       "classfile=\"/home/x05andre/ex/Modelica Library/Modelica/Constants.mo\"",
227       "classline=1", "classcolumn=1" },
228
229      {"elementvisibility=public", "elementfile=\"\"", "elementline=0",
230       "elementcolumn=0", "final=false", "replaceable=false", "inout=\"none\"",
231       "elementtype=classdef", "classname=SIunits", "classrestriction=PACKAGE",
232       "classfile=\"/home/x05andre/ex/Modelica Library/Modelica/SIunits.mo\"",
233       "classline=1", "classcolumn=1" },
234
235      {"elementvisibility=public", "elementfile=\"\"", "elementline=0",
236       "elementcolumn=0", "final=false", "replaceable=false", "inout=\"none\"",
237       "elementtype=classdef", "classname=StateGraph",
238       "classrestriction=PACKAGE",
239       "classfile=\"/home/x05andre/ex/Modelica " +
240                   "Library/Modelica/StateGraph.mo\"",
241       "classline=1", "classcolumn=1" }
242    };
243
244    /**
245     * test parsing some simple unnested lists with ModelicaParser.parseList()
246     * @throws ModelicaParserException
247     */
248    public void testParseSimpleList() throws ModelicaParserException
249    {
250        /*
251         * some combinations of empty lists
252         */
253        List res;
254       
255        res = ModelicaParser.parseList("{}");
256        assertEquals(0, res.size());
257   
258        res = ModelicaParser.parseList("{ }");
259        assertEquals(0, res.size());
260       
261        res = ModelicaParser.parseList("{    }");
262        assertEquals(0, res.size());
263
264       
265        res = ModelicaParser.parseList("{ \n}");
266        assertEquals(0, res.size());
267       
268        res = ModelicaParser.parseList("{   \n    }");
269        assertEquals(0, res.size());
270
271        /*
272         * single element lists
273         */
274        res = ModelicaParser.parseList("{hej}");
275        assertEquals(1, res.size());
276        assertEquals("hej", res.elementAt(0).toString());
277       
278        res = ModelicaParser.parseList("{  muu}");
279        assertEquals(1,res.size());
280        assertEquals("muu", res.elementAt(0).toString());
281       
282        res = ModelicaParser.parseList("{  muu   }");
283        assertEquals(1, res.size());
284        assertEquals("muu", res.elementAt(0).toString());
285
286        res = ModelicaParser.parseList("{muu   }");
287        assertEquals(1, res.size());
288        assertEquals("muu", res.elementAt(0).toString());
289
290
291        /*
292         * two element lists
293         */
294        res = ModelicaParser.parseList("{hej,peter}");
295        assertEquals(2, res.size());
296        assertEquals("hej", res.elementAt(0).toString());
297        assertEquals("peter", res.elementAt(1).toString());
298       
299        res = ModelicaParser.parseList("{peter,  labb}");
300        assertEquals("peter", res.elementAt(0).toString());
301        assertEquals("labb", res.elementAt(1).toString());
302       
303        res = ModelicaParser.parseList("{peter ,labb}");
304        assertEquals("peter", res.elementAt(0).toString());
305        assertEquals("labb", res.elementAt(1).toString());
306       
307        res = ModelicaParser.parseList("{peter   ,labb}");
308        assertEquals("peter", res.elementAt(0).toString());
309        assertEquals("labb", res.elementAt(1).toString());
310       
311        /*
312         * tree elements list
313         */
314        res = ModelicaParser.parseList("{alan, l, cox}");
315        assertEquals(3, res.size());
316        assertEquals("alan", res.elementAt(0).toString());
317        assertEquals("l", res.elementAt(1).toString());
318        assertEquals("cox", res.elementAt(2).toString());
319       
320        res = ModelicaParser.parseList("{alan,l,cox}");
321        assertEquals(3, res.size());
322        assertEquals("alan", res.elementAt(0).toString());
323        assertEquals("l", res.elementAt(1).toString());
324        assertEquals("cox",res.elementAt(2).toString());
325
326    }
327   
328    /**
329     * test parsing some nested lists with ModelicaParser.parseList()
330     * @throws ModelicaParserException
331     */
332    public void testParseList() throws ModelicaParserException
333    {
334        List v = ModelicaParser.parseList("{{a    ,b   }  ,   c   }");
335        assertTrue(v.elementAt(0) instanceof List);
336        assertTrue(v.size() == 2);
337
338        assertEquals("a", ((List)v.elementAt(0)).elementAt(0).toString());
339        assertEquals("b", ((List)v.elementAt(0)).elementAt(1).toString());
340        assertEquals("c", v.elementAt(1).toString());
341       
342        v = ModelicaParser.parseList("{a, b, c}");
343        assertEquals(3, v.size());
344        assertEquals("a", v.elementAt(0).toString());
345        assertEquals("b", v.elementAt(1).toString());
346        assertEquals("c", v.elementAt(2).toString());
347       
348        v = ModelicaParser.parseList("{a, b, c={a, b, c}}");
349        assertEquals(3, v.size());
350        assertEquals("a", v.elementAt(0).toString());
351        assertEquals("b", v.elementAt(1).toString());
352        assertEquals("c={a, b, c}", v.elementAt(2).toString());
353       
354        boolean failedParse = false;
355        try
356        {
357            v = ModelicaParser.parseList("{,,}");
358        }
359        catch(ModelicaParserException e)
360        {
361            failedParse = true;
362        }
363        assertTrue("Parsing didn't fail on {,,}", failedParse);
364       
365        v = ModelicaParser.parseList("{foo={bar, gzonk}}");
366        assertEquals(1, v.size());
367        assertEquals("foo={bar, gzonk}", v.elementAt(0).toString());
368    }
369   
370    /**
371     * Test actual input that omc have returned via getElementsInfo()
372     * function.
373     */
374    public void testParseGetElementsOutput() throws ModelicaParserException
375    {
376        List parsedList = ModelicaParser.parseList(GET_ELEMENTS_OUTPUT);
377       
378        assertEquals(14, parsedList.size());
379       
380        /*
381         * each element in the res should be a list
382         */
383        Iterator<ListElement> it = parsedList.iterator();
384        for (String[] expectedList : GET_ELEMENTS_RES)
385        {
386            ListElement parsedElements = it.next();
387            assertTrue("expected to find a list but found an element",
388                     (parsedElements instanceof List));
389           
390            Iterator<ListElement> elementsIterator = ((List)parsedElements).iterator(); 
391            for (String expectedElement : expectedList)
392            {
393                assertEquals("parsed list contains unexpected element",
394                        expectedElement, elementsIterator.next().toString()); 
395            }
396            assertFalse("more elements then expected in the parse results",
397                    elementsIterator.hasNext());
398        }
399    }
400   
401    /**
402     * Test actual input that omc have returned via getClassInformation()
403     * function.
404     */
405    public void testParseGetClassInformationOutput() throws ModelicaParserException
406    {
407        List parsedList;
408        ListElement listElement;
409        List list;
410
411        parsedList = ModelicaParser.parseList(GET_CLASS_INFO1);
412
413        assertEquals("Parse resuls is of wrong length", 5, parsedList.size());
414       
415        listElement = parsedList.elementAt(0);
416        assertTrue("Expected simple element found list",
417                listElement instanceof Element);
418        assertEquals("Wrong string", "\"model\"",
419                ((Element)listElement).toString());
420
421        listElement = parsedList.elementAt(1);
422        assertTrue("expected simple element found list",
423                listElement instanceof Element);
424        assertEquals("Wrong string",
425                "\"Chua's circuit, ns, V, A\"",
426                ((Element)listElement).toString());
427       
428        listElement = parsedList.elementAt(2);
429        assertTrue("Expected simple element found list",
430                listElement instanceof Element);
431        assertEquals("Wrong string",
432                "\"/home/x05andre/ex/Modelica Library/Modelica/Electrical/Anal" +
433                "og/Examples/ChuaCircuit.mo\"",
434                ((Element)listElement).toString());
435       
436        listElement = parsedList.elementAt(3);
437        assertTrue("expected list found simple element",
438                listElement instanceof List);
439        list = (List)listElement;
440       
441        assertEquals("parse resuls if of wrong length", 3, list.size());
442       
443        listElement = list.elementAt(0);
444        assertTrue("expected simple element found list",
445                listElement instanceof Element);
446        assertEquals("wrong string", "false",
447                ((Element)listElement).toString());
448       
449        listElement = list.elementAt(1);
450        assertTrue("expected simple element found list",
451                listElement instanceof Element);
452        assertEquals("wrong string", "false",
453                ((Element)listElement).toString());
454       
455        listElement = list.elementAt(2);
456        assertTrue("expected simple element found list",
457                listElement instanceof Element);
458        assertEquals("wrong string", "true",
459                ((Element)listElement).toString());
460
461
462        listElement = parsedList.elementAt(4);
463        assertTrue("expected list found simple element",
464                listElement instanceof List);
465        list = (List)listElement;
466       
467        assertEquals("parse resuls if of wrong length", 5, list.size());
468       
469        listElement = list.elementAt(0);
470        assertTrue("expected simple element found list",
471                listElement instanceof Element);
472        assertEquals("wrong string", "\"writable\"",
473                ((Element)listElement).toString());
474       
475        listElement = list.elementAt(1);
476        assertTrue("expected simple element found list",
477                listElement instanceof Element);
478        assertEquals("wrong string", "1",
479                ((Element)listElement).toString());
480       
481        listElement = list.elementAt(2);
482        assertTrue("expected simple element found list",
483                listElement instanceof Element);
484        assertEquals("wrong string", "1",
485                ((Element)listElement).toString());
486       
487        listElement = list.elementAt(3);
488        assertTrue("expected simple element found list",
489                listElement instanceof Element);
490        assertEquals("wrong string", "65",
491                ((Element)listElement).toString());
492
493        listElement = list.elementAt(4);
494        assertTrue("expected simple element found list",
495                listElement instanceof Element);
496        assertEquals("wrong string", "16",
497                ((Element)listElement).toString());
498    }
499}
Note: See TracBrowser for help on using the repository browser.