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 | |
---|
42 | package org.modelica.mdt.test; |
---|
43 | |
---|
44 | import java.util.Vector; |
---|
45 | |
---|
46 | import org.modelica.mdt.core.compiler.ModelicaParser; |
---|
47 | |
---|
48 | import junit.framework.TestCase; |
---|
49 | |
---|
50 | /** |
---|
51 | * test org.modelica.mdt.core.compiler.ModelicaParser class' code |
---|
52 | */ |
---|
53 | public class TestModelicaParser extends TestCase |
---|
54 | { |
---|
55 | /** |
---|
56 | * test parsing some simple unnested lists with ModelicaParser.parseList() |
---|
57 | */ |
---|
58 | public void testParseSimpleList() |
---|
59 | { |
---|
60 | /* |
---|
61 | * some combinations of empty lists |
---|
62 | */ |
---|
63 | Vector<Object> res; |
---|
64 | |
---|
65 | res = ModelicaParser.parseList("{}"); |
---|
66 | assertEquals(0, res.size()); |
---|
67 | |
---|
68 | res = ModelicaParser.parseList("{ }"); |
---|
69 | assertEquals(0, res.size()); |
---|
70 | |
---|
71 | res = ModelicaParser.parseList("{ }"); |
---|
72 | assertEquals(0, res.size()); |
---|
73 | |
---|
74 | |
---|
75 | res = ModelicaParser.parseList("{ \n}"); |
---|
76 | assertEquals(0, res.size()); |
---|
77 | |
---|
78 | res = ModelicaParser.parseList("{ \n }"); |
---|
79 | assertEquals(0, res.size()); |
---|
80 | |
---|
81 | /* |
---|
82 | * single element lists |
---|
83 | */ |
---|
84 | res = ModelicaParser.parseList("{hej}"); |
---|
85 | assertEquals(1, res.size()); |
---|
86 | assertEquals("hej", (String)res.elementAt(0)); |
---|
87 | |
---|
88 | res = ModelicaParser.parseList("{ muu}"); |
---|
89 | assertEquals(1,res.size()); |
---|
90 | assertEquals("muu", (String)res.elementAt(0)); |
---|
91 | |
---|
92 | res = ModelicaParser.parseList("{ muu }"); |
---|
93 | assertEquals(1, res.size()); |
---|
94 | assertEquals("muu", (String)res.elementAt(0)); |
---|
95 | |
---|
96 | res = ModelicaParser.parseList("{muu }"); |
---|
97 | assertEquals(1, res.size()); |
---|
98 | assertEquals("muu", (String)res.elementAt(0)); |
---|
99 | |
---|
100 | |
---|
101 | /* |
---|
102 | * two element lists |
---|
103 | */ |
---|
104 | res = ModelicaParser.parseList("{hej,peter}"); |
---|
105 | assertEquals(2, res.size()); |
---|
106 | assertEquals("hej", (String)res.elementAt(0)); |
---|
107 | assertEquals("peter", (String)res.elementAt(1)); |
---|
108 | |
---|
109 | res = ModelicaParser.parseList("{peter, labb}"); |
---|
110 | assertEquals("peter", (String)res.elementAt(0)); |
---|
111 | assertEquals("labb", (String)res.elementAt(1)); |
---|
112 | |
---|
113 | res = ModelicaParser.parseList("{peter ,labb}"); |
---|
114 | assertEquals("peter", (String)res.elementAt(0)); |
---|
115 | assertEquals("labb", (String)res.elementAt(1)); |
---|
116 | |
---|
117 | res = ModelicaParser.parseList("{peter ,labb}"); |
---|
118 | assertEquals("peter", (String)res.elementAt(0)); |
---|
119 | assertEquals("labb", (String)res.elementAt(1)); |
---|
120 | |
---|
121 | /* |
---|
122 | * tree elements list |
---|
123 | */ |
---|
124 | res = ModelicaParser.parseList("{alan, l, cox}"); |
---|
125 | assertEquals(3, res.size()); |
---|
126 | assertEquals("alan", (String)res.elementAt(0)); |
---|
127 | assertEquals("l", (String)res.elementAt(1)); |
---|
128 | assertEquals("cox", (String)res.elementAt(2)); |
---|
129 | |
---|
130 | res = ModelicaParser.parseList("{alan,l,cox}"); |
---|
131 | assertEquals(3, res.size()); |
---|
132 | assertEquals("alan", (String)res.elementAt(0)); |
---|
133 | assertEquals("l", (String)res.elementAt(1)); |
---|
134 | assertEquals("cox", (String)res.elementAt(2)); |
---|
135 | |
---|
136 | } |
---|
137 | |
---|
138 | /** |
---|
139 | * test parsing some nested lists with ModelicaParser.parseList() |
---|
140 | */ |
---|
141 | public void testParseList() |
---|
142 | { |
---|
143 | Vector v = ModelicaParser.parseList("{{a ,b } , c }"); |
---|
144 | assertTrue(v.get(0) instanceof Vector); |
---|
145 | assertTrue(v.size() == 2); |
---|
146 | assertTrue(((Vector)v.get(0)).get(0).equals("a")); |
---|
147 | assertTrue(((Vector)v.get(0)).get(1).equals("b")); |
---|
148 | assertTrue(v.get(1).equals("c")); |
---|
149 | |
---|
150 | v = ModelicaParser.parseList("{a, b, c}"); |
---|
151 | assertTrue(v.size() == 3); |
---|
152 | assertTrue(v.get(0).equals("a")); |
---|
153 | assertTrue(v.get(1).equals("b")); |
---|
154 | assertTrue(v.get(2).equals("c")); |
---|
155 | |
---|
156 | v = ModelicaParser.parseList("{a, b, c={a, b, c}}"); |
---|
157 | assertTrue(v.size() == 3); |
---|
158 | assertTrue(v.get(0).equals("a")); |
---|
159 | assertTrue(v.get(1).equals("b")); |
---|
160 | assertTrue(v.get(2).equals("c={a, b, c}")); |
---|
161 | |
---|
162 | v = ModelicaParser.parseList("{,,}"); |
---|
163 | assertTrue(v.size() == 0); |
---|
164 | |
---|
165 | v = ModelicaParser.parseList("{foo={bar, gzonk}}"); |
---|
166 | assertTrue(v.size() == 1); |
---|
167 | assertTrue(v.get(0).equals("foo={bar, gzonk}")); |
---|
168 | } |
---|
169 | } |
---|