• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 Shenzhen Kaihong Digital.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 package parse;
17 
18 import grammar.*;
19 import org.antlr.v4.runtime.CharStreams;
20 import org.antlr.v4.runtime.CodePointCharStream;
21 import org.junit.jupiter.api.Test;
22 
23 import java.util.List;
24 
25 import static org.junit.jupiter.api.Assertions.*;
26 
27 /**
28  * <h3>类名:该类用于xxx</h3>
29  * description
30  *
31  * @author Administrator
32  *         date 2025-02-28
33  * @version 1.0
34  * @since 2025-02-28
35  */
36 class ParseTsTest {
37 
38     String testClass2 = "abstract class Person {\n" +
39             "    name: string;\n" +
40             "\n" +
41             "    constructor(name: string) {\n" +
42             "        this.name = name;\n" +
43             "    }\n" +
44             "\n" +
45             "    abstract find(target: string): Person;\n" +
46             "    abstract nameAbs: string;\n" +
47             "}";
48 
49     String testClass3 = "abstract class Person {\n" +
50             "    name: string;\n" +
51             "    value: int;\n" +
52             "\n" +
53             "    constructor(name: string, value: int) {\n" +
54             "        this.name = name;\n" +
55             "        this.value = value;\n" +
56             "    }\n" +
57             "\n" +
58             "    abstract find(target: string, cnt: int): Person;\n" +
59             "    abstract add(int, string): Person;\n" +
60             "    abstract nameAbs: string;\n" +
61             "}";
62 
63     String testClass4 = "abstract class Person {\n" +
64             "    name: string;\n" +
65             "    value: int;\n" +
66             "\n" +
67             "    constructor(string, int) {\n" +
68             "        this.name = name;\n" +
69             "        this.value = value;\n" +
70             "    }\n" +
71             "\n" +
72             "    abstract find(string): Person;\n" +
73             "    abstract add(int, string): Person;\n" +
74             "    abstract nameAbs: string;\n" +
75             "}";
76 
77     @Test
parseFile()78     void parseFile() {
79     }
80 
81     @Test
parseContent()82     void parseContent() {
83     }
84 
85     @Test
parseCStreamEnum()86     void parseCStreamEnum() {
87         ParseBase parser = ParseFactory.getParser("ts");
88         String testEnum = "enum Colors {\n" +
89                 "  Red = \"RED\",\n" +
90                 "  Green = \"GREEN\",\n" +
91                 "  Blue = \"BLUE\"\n" +
92                 "}";
93         CodePointCharStream cStream = CharStreams.fromString(testEnum);
94         ParseObj po = parser.parseCStream(cStream);
95         List<EnumObj> eol = po.getEnumList();
96         assertEquals(1, eol.size());
97         EnumObj eo = eol.get(0);
98         assertEquals("Colors", eo.getName());
99         List<String> ml = eo.getMemberList();
100         assertEquals(3, ml.size());
101         assertEquals("Red", ml.get(0));
102         assertEquals("Green", ml.get(1));
103         assertEquals("Blue", ml.get(2));
104         List<String> vl = eo.getValueList();
105         assertEquals(3, vl.size());
106         assertEquals("\"RED\"", vl.get(0));
107         assertEquals("\"GREEN\"", vl.get(1));
108         assertEquals("\"BLUE\"", vl.get(2));
109     }
110 
111     @Test
parseCStreamClass_1()112     void parseCStreamClass_1() {
113         String testClass = "export class Box {\n" +
114                 "\tlines: number;\n" +
115                 "\tsrcType: TestEnum;\n" +
116                 "\tdestType1: TestSt3;\n" +
117                 "\tdesType2: TestShap_t;\n" +
118                 "\tdesType3: TestUnion3_t;\n" +
119                 "\tcalcArea: number;\n" +
120                 "\theith_: number;\n" +
121                 "\ttransform2D(calcCB: Calculate): boolean;\n" +
122                 "\ttransform3D(ctCB: CallbackTest): boolean;\n" +
123                 "};";
124         ParseBase parser = ParseFactory.getParser("ts");
125 
126         CodePointCharStream cStream = CharStreams.fromString(testClass);
127         ParseObj po = parser.parseCStream(cStream);
128         List<ClassObj> eol = po.getClassList();
129         assertEquals(1, eol.size());
130         ClassObj co = eol.get(0);
131         assertEquals("Box", co.getName());
132         List<ParamObj> pl = co.getParamList();
133         assertEquals(7, pl.size());
134         ParamObj poItem = pl.get(0);
135         assertEquals("lines", poItem.getName());
136         assertEquals("number", poItem.getType());
137         poItem = pl.get(1);
138         assertEquals("srcType", poItem.getName());
139         assertEquals("TestEnum", poItem.getType());
140         poItem = pl.get(2);
141         assertEquals("destType1", poItem.getName());
142         assertEquals("TestSt3", poItem.getType());
143         poItem = pl.get(3);
144         assertEquals("desType2", poItem.getName());
145         assertEquals("TestShap_t", poItem.getType());
146         poItem = pl.get(4);
147         assertEquals("desType3", poItem.getName());
148         assertEquals("TestUnion3_t", poItem.getType());
149         poItem = pl.get(5);
150         assertEquals("calcArea", poItem.getName());
151         assertEquals("number", poItem.getType());
152         poItem = pl.get(6);
153         assertEquals("heith_", poItem.getName());
154         assertEquals("number", poItem.getType());
155     }
156 
157     @Test
parseCStreamClass_2()158     void parseCStreamClass_2() {
159         String testClass = testClass2;
160         CodePointCharStream cStream = CharStreams.fromString(testClass);
161         ParseBase parser = ParseFactory.getParser("ts");
162         ParseObj po = parser.parseCStream(cStream);
163         List<ClassObj> eol = po.getClassList();
164         assertEquals(1, eol.size());
165         ClassObj co = eol.get(0);
166         assertEquals("Person", co.getName());
167         List<ParamObj> pl = co.getParamList();
168         assertEquals(2, pl.size());
169         ParamObj poItem = pl.get(0);
170         assertEquals("name", poItem.getName());
171         assertEquals("string", poItem.getType());
172         poItem = pl.get(1);
173         assertEquals("nameAbs", poItem.getName());
174         assertEquals("string", poItem.getType());
175         List<FuncObj> fol = co.getFuncList();
176         assertEquals(2, fol.size());
177         FuncObj foItem = fol.get(0);
178         assertEquals("constructor", foItem.getName());
179         List<ParamObj> pol = foItem.getParamList();
180         assertEquals(1, pol.size());
181         poItem = pol.get(0);
182         assertEquals("name", poItem.getName());
183         assertEquals("string", poItem.getType());
184         foItem = fol.get(1);
185         assertEquals("find", foItem.getName());
186         assertEquals("Person", foItem.getRetValue());
187         pol = foItem.getParamList();
188         assertEquals(1, pol.size());
189         poItem = pol.get(0);
190         assertEquals("string", poItem.getType());
191 
192     }
193 
194     @Test
parseCStreamClass_3()195     void parseCStreamClass_3() {
196         String testClass = this.testClass3;
197         CodePointCharStream cStream = CharStreams.fromString(testClass);
198         ParseBase parser = ParseFactory.getParser("ts");
199         ParseObj po = parser.parseCStream(cStream);
200         List<ClassObj> eol = po.getClassList();
201         assertEquals(1, eol.size());
202         ClassObj co = eol.get(0);
203         List<ParamObj> pl = co.getParamList();
204         assertEquals(3, pl.size());
205         ParamObj poItem = pl.get(1);
206         assertEquals("value", poItem.getName());
207         assertEquals("int", poItem.getType());
208         poItem = pl.get(2);
209         assertEquals("nameAbs", poItem.getName());
210         assertEquals("string", poItem.getType());
211         List<FuncObj> fol = co.getFuncList();
212         assertEquals(3, fol.size());
213         FuncObj foItem = fol.get(0);
214         assertEquals("constructor", foItem.getName());
215         List<ParamObj> pol = foItem.getParamList();
216         assertEquals(2, pol.size());
217         poItem = pol.get(0);
218         assertEquals("name", poItem.getName());
219         assertEquals("string", poItem.getType());
220         poItem = pol.get(1);
221         assertEquals("value", poItem.getName());
222         assertEquals("int", poItem.getType());
223 
224         foItem = fol.get(1);
225         assertEquals("find", foItem.getName());
226         assertEquals("Person", foItem.getRetValue());
227         pol = foItem.getParamList();
228         assertEquals(2, pol.size());
229         poItem = pol.get(0);
230         assertEquals("target", poItem.getName());
231         assertEquals("string", poItem.getType());
232         poItem = pol.get(1);
233         assertEquals("cnt", poItem.getName());
234         assertEquals("int", poItem.getType());
235 
236         foItem = fol.get(2);
237         assertEquals("add", foItem.getName());
238         assertEquals("Person", foItem.getRetValue());
239         pol = foItem.getParamList();
240         assertEquals(2, pol.size());
241         poItem = pol.get(0);
242         assertEquals("int", poItem.getName());
243         assertEquals("int", poItem.getType());
244         poItem = pol.get(1);
245         assertEquals("string", poItem.getType());
246     }
247 
248     @Test
parseCStreamClass_4()249     void parseCStreamClass_4() {
250         String testClass = testClass4;
251         CodePointCharStream cStream = CharStreams.fromString(testClass);
252         ParseBase parser = ParseFactory.getParser("ts");
253         ParseObj po = parser.parseCStream(cStream);
254         List<ClassObj> eol = po.getClassList();
255         assertEquals(1, eol.size());
256         ClassObj co = eol.get(0);
257         List<ParamObj> pl = co.getParamList();
258         assertEquals(3, pl.size());
259         ParamObj poItem = pl.get(0);
260         assertEquals("name", poItem.getName());
261         assertEquals("string", poItem.getType());
262         poItem = pl.get(1);
263         assertEquals("value", poItem.getName());
264         assertEquals("int", poItem.getType());
265         poItem = pl.get(2);
266         assertEquals("nameAbs", poItem.getName());
267         assertEquals("string", poItem.getType());
268         List<FuncObj> fol = co.getFuncList();
269         assertEquals(3, fol.size());
270         FuncObj foItem = fol.get(0);
271         assertEquals("constructor", foItem.getName());
272         List<ParamObj> pol = foItem.getParamList();
273         assertEquals(2, pol.size());
274         poItem = pol.get(0);
275         assertEquals("string", poItem.getName());
276         assertEquals("string", poItem.getType());
277         poItem = pol.get(1);
278         assertEquals("int", poItem.getType());
279 
280         foItem = fol.get(1);
281         assertEquals("find", foItem.getName());
282         assertEquals("Person", foItem.getRetValue());
283         pol = foItem.getParamList();
284         assertEquals(1, pol.size());
285         poItem = pol.get(0);
286         assertEquals("string", poItem.getName());
287         assertEquals("string", poItem.getType());
288 
289         foItem = fol.get(2);
290         assertEquals("add", foItem.getName());
291         assertEquals("Person", foItem.getRetValue());
292         pol = foItem.getParamList();
293         assertEquals(2, pol.size());
294         poItem = pol.get(0);
295         assertEquals("int", poItem.getName());
296         assertEquals("int", poItem.getType());
297         poItem = pol.get(1);
298         assertEquals("string", poItem.getName());
299         assertEquals("string", poItem.getType());
300     }
301 
302     @Test
parseCStreamFunc()303     void parseCStreamFunc() {
304         ParseBase parser = ParseFactory.getParser("ts");
305         String testFunc = "export function transform2D(\n" +
306                 "\tdirection: number,\n" +
307                 "\tangle: number,\n" +
308                 "\tcalcCB: Calculate): boolean;";
309         CodePointCharStream cStream = CharStreams.fromString(testFunc);
310         ParseObj po = parser.parseCStream(cStream);
311         List<FuncObj> fol = po.getFuncList();
312         assertEquals(1, fol.size());
313         FuncObj fo = fol.get(0);
314         assertEquals("transform2D", fo.getName());
315         assertEquals("boolean", fo.getRetValue());
316         List<ParamObj> pol = fo.getParamList();
317         assertEquals(3, pol.size());
318         ParamObj poItem = pol.get(0);
319         assertEquals("direction", poItem.getName());
320         assertEquals("number", poItem.getType());
321         poItem = pol.get(1);
322         assertEquals("angle", poItem.getName());
323         assertEquals("number", poItem.getType());
324         poItem = pol.get(2);
325         assertEquals("calcCB", poItem.getName());
326         assertEquals("Calculate", poItem.getType());
327     }
328 
329     @Test
parseCStreamInterface()330     void parseCStreamInterface() {
331         ParseBase parser = ParseFactory.getParser("ts");
332         String testInterface = "export interface CallbackTest {\n" +
333                 "\t(msg: string): void;\n" +
334                 "};";
335         CodePointCharStream cStream = CharStreams.fromString(testInterface);
336         ParseObj po = parser.parseCStream(cStream);
337         List<InterfaceObject> iol = po.getInterfaceList();
338         assertEquals(1, iol.size());
339         InterfaceObject ioItem = iol.get(0);
340         List<FuncObj> fol = ioItem.getFuncList();
341         assertEquals(1, fol.size());
342         FuncObj foItem = fol.get(0);
343         assertEquals("", foItem.getName());
344         assertEquals("void", foItem.getRetValue());
345         List<ParamObj> pol = foItem.getParamList();
346         assertEquals(1, pol.size());
347         ParamObj poItem = pol.get(0);
348         assertEquals("msg", poItem.getName());
349         assertEquals("string", poItem.getType());
350     }
351 
352     @Test
parseCStreamType()353     void parseCStreamType() {
354         ParseBase parser = ParseFactory.getParser("ts");
355         String testType = "export type TestShap_t = TestShape;";
356         CodePointCharStream cStream = CharStreams.fromString(testType);
357         ParseObj po = parser.parseCStream(cStream);
358         List<TypeObj> tol = po.getTypeList();
359         assertEquals(1, tol.size());
360         TypeObj toItem = tol.get(0);
361         assertEquals("TestShap_t", toItem.getName());
362         List<String> tl = toItem.getTypeList();
363         assertEquals(1, tl.size());
364         assertEquals("TestShape", tl.get(0));
365     }
366 
367     @Test
receive()368     void receive() {
369     }
370 
371     @Test
parseEnum()372     void parseEnum() {
373     }
374 
375     @Test
parseUnion()376     void parseUnion() {
377     }
378 
379     @Test
parseStruct()380     void parseStruct() {
381     }
382 
383     @Test
parseClass()384     void parseClass() {
385     }
386 
387     @Test
parseFunc()388     void parseFunc() {
389     }
390 
391     @Test
parseType()392     void parseType() {
393     }
394 
395     @Test
handleEvent()396     void handleEvent() {
397     }
398 }