OLD | NEW |
| (Empty) |
1 #!/usr/bin/python | |
2 # Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | |
3 # for details. All rights reserved. Use of this source code is governed by a | |
4 # BSD-style license that can be found in the LICENSE file. | |
5 | |
6 import idlparser | |
7 import logging.config | |
8 import sys | |
9 import unittest | |
10 | |
11 | |
12 class IDLParserTestCase(unittest.TestCase): | |
13 | |
14 def _run_test(self, syntax, content, expected): | |
15 """Utility for running a IDL parsing tests and comparing results. | |
16 | |
17 Program exits (sys.exit) if expected does not match actual. | |
18 | |
19 Args: | |
20 syntax -- IDL grammar to use (either idlparser.WEBKIT_SYNTAX, | |
21 WEBIDL_SYNTAX or FREMONTCUT_SYNTAX). If None, will run | |
22 multiple tests, each with a different syntax. | |
23 content -- input text for the parser. | |
24 expected -- expected parse result. | |
25 """ | |
26 | |
27 all_syntaxes = {idlparser.WEBIDL_SYNTAX: 'Web IDL', | |
28 idlparser.WEBKIT_SYNTAX: 'WebKit', | |
29 idlparser.FREMONTCUT_SYNTAX: 'FremontCut'} | |
30 | |
31 if syntax is None: | |
32 for syntax in all_syntaxes: | |
33 self._run_test(syntax, content, expected) | |
34 return | |
35 | |
36 if syntax not in all_syntaxes: | |
37 raise RuntimeError('Unexpected syntax %s' % syntax) | |
38 | |
39 actual = None | |
40 error = None | |
41 try: | |
42 parser = idlparser.IDLParser(syntax) | |
43 actual = parser.parse(content) | |
44 except SyntaxError, e: | |
45 error = e | |
46 pass | |
47 if actual != expected: | |
48 self.fail(''' | |
49 SYNTAX : %s | |
50 CONTENT : | |
51 %s | |
52 EXPECTED: | |
53 %s | |
54 ACTUAL : | |
55 %s | |
56 ERROR : %s''' % (all_syntaxes[syntax], content, expected, actual, error)) | |
57 | |
58 def test_empty_module(self): | |
59 self._run_test( | |
60 None, | |
61 'module M {};', | |
62 [('Module', [('Id', 'M')])]) | |
63 | |
64 def test_empty_interface(self): | |
65 self._run_test( | |
66 None, | |
67 'interface I {};', | |
68 [('Interface', [('Id', 'I')])]) | |
69 | |
70 def test_module_with_empty_interface(self): | |
71 self._run_test( | |
72 None, | |
73 'module M { interface I {}; };', | |
74 [('Module', [('Id', 'M'), ('Interface', [('Id', 'I')])])]) | |
75 | |
76 # testing the gcc pre-processing | |
77 def test_gcc_preprocessing(self): | |
78 self._run_test( | |
79 idlparser.WEBKIT_SYNTAX, | |
80 ''' | |
81 #if 1 | |
82 module M1 {}; | |
83 #endif | |
84 #if 0 | |
85 module M2 {}; | |
86 #endif | |
87 ''', | |
88 [('Module', [('Id', 'M1')])]) | |
89 | |
90 def test_attribute_with_exceptoins(self): | |
91 self._run_test( | |
92 idlparser.WEBKIT_SYNTAX, | |
93 '''interface I { | |
94 attribute boolean A setter raises (E), getter raises (E); | |
95 };''', | |
96 [('Interface', [('Id', 'I'), ('Attribute', [('Type', [('BooleanType', None
)]), ('Id', 'A'), ('SetRaises', [('ScopedName', 'E')]), ('GetRaises', [('ScopedN
ame', 'E')])])])]) | |
97 | |
98 def test_interface_with_extended_attributes(self): | |
99 self._run_test( | |
100 idlparser.WEBKIT_SYNTAX, | |
101 'interface [ExAt1, ExAt2] I {};', | |
102 [('Interface', [('ExtAttrs', [('ExtAttr', [('Id', 'ExAt1')]), ('ExtAttr',
[('Id', 'ExAt2')])]), ('Id', 'I')])]) | |
103 | |
104 def test_implements_statement(self): | |
105 self._run_test( | |
106 idlparser.WEBIDL_SYNTAX, | |
107 'X implements Y;', | |
108 [('ImplStmt', [('ImplStmtImplementor', ('ScopedName', 'X')), ('ImplStmtImp
lemented', ('ScopedName', 'Y'))])]) | |
109 | |
110 def test_operation(self): | |
111 self._run_test( | |
112 None, | |
113 'interface I { boolean func(); };', | |
114 [('Interface', [('Id', 'I'), ('Operation', [('ReturnType', [('BooleanType'
, None)]), ('Id', 'func')])])]) | |
115 | |
116 def test_attribute_types(self): | |
117 self._run_test( | |
118 None, | |
119 '''interface I { | |
120 attribute boolean boolAttr; | |
121 attribute DOMString strAttr; | |
122 attribute SomeType someAttr; | |
123 };''', | |
124 [('Interface', [('Id', 'I'), ('Attribute', [('Type', [('BooleanType', None
)]), ('Id', 'boolAttr')]), ('Attribute', [('Type', [('ScopedName', 'DOMString')]
), ('Id', 'strAttr')]), ('Attribute', [('Type', [('ScopedName', 'SomeType')]), (
'Id', 'someAttr')])])]) | |
125 | |
126 def test_constants(self): | |
127 self._run_test( | |
128 None, | |
129 '''interface I { | |
130 const long c1 = 0; | |
131 const long c2 = 1; | |
132 const long c3 = 0x01; | |
133 const long c4 = 10; | |
134 const boolean b1 = true; | |
135 const boolean b1 = false; | |
136 };''', | |
137 [('Interface', [('Id', 'I'), ('Const', [('Type', [('LongType', None)]), ('
Id', 'c1'), ('ConstExpr', '0')]), ('Const', [('Type', [('LongType', None)]), ('I
d', 'c2'), ('ConstExpr', '1')]), ('Const', [('Type', [('LongType', None)]), ('Id
', 'c3'), ('ConstExpr', '0x01')]), ('Const', [('Type', [('LongType', None)]), ('
Id', 'c4'), ('ConstExpr', '10')]), ('Const', [('Type', [('BooleanType', None)]),
('Id', 'b1'), ('ConstExpr', 'true')]), ('Const', [('Type', [('BooleanType', Non
e)]), ('Id', 'b1'), ('ConstExpr', 'false')])])]) | |
138 | |
139 def test_inheritance(self): | |
140 self._run_test( | |
141 None, | |
142 ''' | |
143 interface Shape {}; | |
144 interface Rectangle : Shape {}; | |
145 interface Square : Rectangle, Shape {}; | |
146 ''', | |
147 [('Interface', [('Id', 'Shape')]), ('Interface', [('Id', 'Rectangle'), ('P
arentInterface', [('InterfaceType', ('ScopedName', 'Shape'))])]), ('Interface',
[('Id', 'Square'), ('ParentInterface', [('InterfaceType', ('ScopedName', 'Rectan
gle'))]), ('ParentInterface', [('InterfaceType', ('ScopedName', 'Shape'))])])]) | |
148 | |
149 def test_annotations(self): | |
150 self._run_test( | |
151 idlparser.FREMONTCUT_SYNTAX, | |
152 '@Ano1 @Ano2() @Ano3(x) @Ano4(x=1,y=2) interface I {};', | |
153 [('Interface', [('Annotation', [('Id', 'Ano1')]), ('Annotation', [('Id', '
Ano2')]), ('Annotation', [('Id', 'Ano3'), ('AnnotationArg', [('Id', 'x')])]), ('
Annotation', [('Id', 'Ano4'), ('AnnotationArg', [('Id', 'x'), ('AnnotationArgVal
ue', '1')]), ('AnnotationArg', [('Id', 'y'), ('AnnotationArgValue', '2')])]), ('
Id', 'I')])]) | |
154 | |
155 | |
156 if __name__ == "__main__": | |
157 logging.config.fileConfig("logging.conf") | |
158 if __name__ == '__main__': | |
159 unittest.main() | |
OLD | NEW |