source: trunk/org.modelica.mdt.test/src/org/modelica/mdt/test/TestProxyParser.java @ 206

Last change on this file since 206 was 206, checked in by remar, 19 years ago
  • added tests for parsing of lists where the elements contain lists: {a, b, c={a, b}} -> [a, b, c={a, b}]
File size: 7.5 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.Vector;
45
46import org.modelica.mdt.internal.omcproxy.CompileError;
47import org.modelica.mdt.internal.omcproxy.CompilerException;
48import org.modelica.mdt.internal.omcproxy.ProxyParser;
49
50import junit.framework.TestCase;
51
52/**
53 * Test various methods in org.modelica.mdt.internal.omcproxy.ProxyParser
54 * @author Elmir Jagudin
55 */
56public class TestProxyParser extends TestCase
57{
58
59    /**
60     * Test if ProxyParser.parseSimpleList() works as prescribed.
61     */
62    public void testParseSimpleList()
63    {
64        /*
65         * some combinations of empty lists
66         */
67        String[] res;
68       
69        try
70        {
71            res = ProxyParser.parseSimpleList("{}");
72            assertEquals(0, res.length);
73       
74            res = ProxyParser.parseSimpleList("{ }");
75            assertEquals(0, res.length);
76           
77            res = ProxyParser.parseSimpleList("{    }");
78            assertEquals(0, res.length);
79   
80           
81            res = ProxyParser.parseSimpleList("{ \n}");
82            assertEquals(0, res.length);
83           
84            res = ProxyParser.parseSimpleList("{   \n    }");
85            assertEquals(0, res.length);
86   
87            /*
88             * single element lists
89             */
90            res = ProxyParser.parseSimpleList("{hej}");
91            assertEquals(1, res.length);
92            assertEquals("hej", res[0]);
93           
94            res = ProxyParser.parseSimpleList("{  muu}");
95            assertEquals(1, res.length);
96            assertEquals("muu", res[0]);
97           
98            res = ProxyParser.parseSimpleList("{  muu   }");
99            assertEquals(1, res.length);
100            assertEquals("muu", res[0]);
101   
102            res = ProxyParser.parseSimpleList("{muu   }");
103            assertEquals(1, res.length);
104            assertEquals("muu", res[0]);
105   
106   
107            /*
108             * two element lists
109             */
110            res = ProxyParser.parseSimpleList("{hej,peter}");
111            assertEquals(2, res.length);
112            assertEquals("hej", res[0]);
113            assertEquals("peter", res[1]);
114           
115            res = ProxyParser.parseSimpleList("{peter,  labb}");
116            assertEquals("peter", res[0]);
117            assertEquals("labb", res[1]);
118           
119            res = ProxyParser.parseSimpleList("{peter ,labb}");
120            assertEquals("peter", res[0]);
121            assertEquals("labb", res[1]);
122           
123            res = ProxyParser.parseSimpleList("{peter   ,labb}");
124            assertEquals("peter", res[0]);
125            assertEquals("labb", res[1]);
126           
127            /*
128             * tree elements list
129             */
130            res = ProxyParser.parseSimpleList("{alan, l, cox}");
131            assertEquals(3, res.length);
132            assertEquals("alan", res[0]);
133            assertEquals("l", res[1]);
134            assertEquals("cox", res[2]);
135           
136            res = ProxyParser.parseSimpleList("{alan,l,cox}");
137            assertEquals(3, res.length);
138            assertEquals("alan", res[0]);
139            assertEquals("l", res[1]);
140            assertEquals("cox", res[2]);
141
142        }
143        catch(CompilerException e)
144        {
145            fail(e.getMessage());
146        }
147    }
148   
149    public void testParseList()
150    {
151        Vector v = ProxyParser.parseList("{{a    ,b   }  ,   c   }");
152        assertTrue(v.get(0) instanceof Vector);
153        assertTrue(v.size() == 2);
154        assertTrue(((Vector)v.get(0)).get(0).equals("a"));
155        assertTrue(((Vector)v.get(0)).get(1).equals("b"));
156        assertTrue(v.get(1).equals("c"));
157       
158        v = ProxyParser.parseList("{a, b, c}");
159        assertTrue(v.size() == 3);
160        assertTrue(v.get(0).equals("a"));
161        assertTrue(v.get(1).equals("b"));
162        assertTrue(v.get(2).equals("c"));
163       
164        v = ProxyParser.parseList("{a, b, c={a, b, c}}");
165        assertTrue(v.size() == 3);
166        assertTrue(v.get(0).equals("a"));
167        assertTrue(v.get(1).equals("b"));
168        assertTrue(v.get(2).equals("c={a, b, c}"));
169       
170        v = ProxyParser.parseList("{,,}");
171        assertTrue(v.size() == 0);
172       
173        v = ProxyParser.parseList("{foo={bar, gzonk}}");
174        assertTrue(v.size() == 1);
175        assertTrue(v.get(0).equals("foo={bar, gzonk}"));
176    }
177               
178
179
180    /**
181     * Test if ProxyParser.parseErrorString() works as prescribed.
182     */ 
183    public void testParseErrorString()
184    {
185        try
186        {
187            /* single error windows style */
188            String errorString = 
189                "[c:/folde/hej.mo:2:1]: error: unexpected tooken: muu\n";
190            CompileError[] errors = ProxyParser.parseErrorString(errorString);
191   
192            assertEquals(1, errors.length);     
193            CompileError error = errors[0];
194            assertEquals(2, error.getLine());
195            assertEquals("unexpected tooken: muu", error.getErrorDescription());
196   
197            /* two errors windows style */
198            errorString = 
199                "[c:/folder he/hej.mo:4:1]: error: unexpected end of file\n" + 
200                "[c:/folder he/hej.mo:15:1]: error: hej remar\n";
201            errors = ProxyParser.parseErrorString(errorString);
202            assertEquals(2, errors.length);
203           
204            error = errors[0];
205            assertEquals(4, error.getLine());
206            assertEquals("unexpected end of file", error.getErrorDescription());
207   
208            error = errors[1];
209            assertEquals(15, error.getLine());
210            assertEquals("hej remar", error.getErrorDescription());
211           
212            /* single error unix style */
213            errorString = 
214                "[/usr/folde/hej.mo:12:1]: error: unexpected tooken: muu\n";
215            errors = ProxyParser.parseErrorString(errorString);
216   
217            assertEquals(1, errors.length);     
218            error = errors[0];
219            assertEquals(12, error.getLine());
220            assertEquals("unexpected tooken: muu", error.getErrorDescription());
221   
222            /* two errors unix style */
223            errorString = 
224                "[/hej/hop/hej.mo:453:1]: error: unexpected end of file\n" + 
225                "[/usr/local/modelica/hej.mo:715:1]: error: hej remar\n";
226            errors = ProxyParser.parseErrorString(errorString);
227            assertEquals(2, errors.length);
228           
229            error = errors[0];
230            assertEquals(453, error.getLine());
231            assertEquals("unexpected end of file", error.getErrorDescription());
232   
233            error = errors[1];
234            assertEquals(715, error.getLine());
235            assertEquals("hej remar", error.getErrorDescription());
236   
237            /* no errors aka empty error string */
238            errors = ProxyParser.parseErrorString("");
239            assertEquals(0, errors.length);
240        }
241        catch(Exception e)
242        {
243            /* On any exception, fail. */
244            fail(e.getMessage());
245        }
246    }
247}
Note: See TracBrowser for help on using the repository browser.