| 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 idlnode | |
| 7 import idlparser | |
| 8 import logging.config | |
| 9 import sys | |
| 10 import unittest | |
| 11 | |
| 12 | |
| 13 class IDLNodeTestCase(unittest.TestCase): | |
| 14 | |
| 15 def _run_test(self, syntax, content, expected): | |
| 16 """Utility run tests and prints extra contextual information. | |
| 17 | |
| 18 Args: | |
| 19 syntax -- IDL grammar to use (either idlparser.WEBKIT_SYNTAX, | |
| 20 WEBIDL_SYNTAX or FREMONTCUT_SYNTAX). If None, will run | |
| 21 multiple tests, each with a different syntax. | |
| 22 content -- input text for the parser. | |
| 23 expected -- expected parse result. | |
| 24 """ | |
| 25 if syntax is None: | |
| 26 self._run_test(idlparser.WEBIDL_SYNTAX, content, expected) | |
| 27 self._run_test(idlparser.WEBKIT_SYNTAX, content, expected) | |
| 28 self._run_test(idlparser.FREMONTCUT_SYNTAX, content, expected) | |
| 29 return | |
| 30 | |
| 31 actual = None | |
| 32 error = None | |
| 33 ast = None | |
| 34 parseResult = None | |
| 35 try: | |
| 36 parser = idlparser.IDLParser(syntax) | |
| 37 ast = parser.parse(content) | |
| 38 node = idlnode.IDLFile(ast) | |
| 39 actual = node.to_dict() if node else None | |
| 40 except SyntaxError, e: | |
| 41 error = e | |
| 42 pass | |
| 43 if actual == expected: | |
| 44 return | |
| 45 else: | |
| 46 msg = ''' | |
| 47 SYNTAX : %s | |
| 48 CONTENT : | |
| 49 %s | |
| 50 EXPECTED: | |
| 51 %s | |
| 52 ACTUAL : | |
| 53 %s | |
| 54 ERROR : %s | |
| 55 AST : | |
| 56 %s | |
| 57 ''' % (syntax, content, expected, actual, error, ast) | |
| 58 self.fail(msg) | |
| 59 | |
| 60 def test_empty_module(self): | |
| 61 self._run_test( | |
| 62 None, | |
| 63 'module TestModule {};', | |
| 64 {'modules': [{'id': 'TestModule'}]}) | |
| 65 | |
| 66 def test_empty_interface(self): | |
| 67 self._run_test( | |
| 68 None, | |
| 69 'module TestModule { interface Interface1 {}; };', | |
| 70 {'modules': [{'interfaces': [{'javascript_binding_name': 'Interface1', 'id
': 'Interface1'}], 'id': 'TestModule'}]}) | |
| 71 | |
| 72 def test_gcc_preprocessor(self): | |
| 73 self._run_test( | |
| 74 idlparser.WEBKIT_SYNTAX, | |
| 75 '#if 1\nmodule TestModule {};\n#endif\n', | |
| 76 {'modules': [{'id': 'TestModule'}]}) | |
| 77 | |
| 78 def test_extended_attributes(self): | |
| 79 self._run_test( | |
| 80 idlparser.WEBKIT_SYNTAX, | |
| 81 'module M { interface [ExAt1, ExAt2] I {};};', | |
| 82 {'modules': [{'interfaces': [{'javascript_binding_name': 'I', 'ext_attrs':
{'ExAt1': None, 'ExAt2': None}, 'id': 'I'}], 'id': 'M'}]}) | |
| 83 | |
| 84 def test_implements_statement(self): | |
| 85 self._run_test( | |
| 86 idlparser.WEBIDL_SYNTAX, | |
| 87 'module M { X implements Y; };', | |
| 88 {'modules': [{'implementsStatements': [{'implementor': {'id': 'X'}, 'imple
mented': {'id': 'Y'}}], 'id': 'M'}]}) | |
| 89 | |
| 90 def test_attributes(self): | |
| 91 self._run_test( | |
| 92 idlparser.WEBIDL_SYNTAX, | |
| 93 '''interface I { | |
| 94 attribute long a1; | |
| 95 readonly attribute DOMString a2; | |
| 96 attribute any a3; | |
| 97 };''', | |
| 98 {'interfaces': [{'javascript_binding_name': 'I', 'attributes': [{'type': {
'id': 'long'}, 'id': 'a1'}, {'type': {'id': 'DOMString'}, 'is_read_only': True,
'id': 'a2'}, {'type': {'id': 'any'}, 'id': 'a3'}], 'id': 'I'}]}) | |
| 99 | |
| 100 def test_operations(self): | |
| 101 self._run_test( | |
| 102 idlparser.WEBIDL_SYNTAX, | |
| 103 '''interface I { | |
| 104 [ExAttr] t1 op1(); | |
| 105 t2 op2(in int arg1, in long arg2); | |
| 106 getter any item(in long index); | |
| 107 stringifier name(); | |
| 108 };''', | |
| 109 {'interfaces': [{'operations': [{'type': {'id': 't1'}, 'ext_attrs': {'ExAt
tr': None}, 'id': 'op1'}, {'type': {'id': 't2'}, 'id': 'op2', 'arguments': [{'ty
pe': {'id': 'int'}, 'id': 'arg1'}, {'type': {'id': 'long'}, 'id': 'arg2'}]}, {'s
pecials': ['getter'], 'type': {'id': 'any'}, 'id': 'item', 'arguments': [{'type'
: {'id': 'long'}, 'id': 'index'}]}, {'is_stringifier': True, 'type': {'id': 'nam
e'}}], 'javascript_binding_name': 'I', 'id': 'I'}]}) | |
| 110 | |
| 111 def test_constants(self): | |
| 112 self._run_test( | |
| 113 None, | |
| 114 '''interface I { | |
| 115 const long c1 = 0; | |
| 116 const long c2 = 1; | |
| 117 const long c3 = 0x01; | |
| 118 const long c4 = 10; | |
| 119 const boolean b1 = false; | |
| 120 const boolean b2 = true; | |
| 121 };''', | |
| 122 {'interfaces': [{'javascript_binding_name': 'I', 'id': 'I', 'constants': [
{'type': {'id': 'long'}, 'id': 'c1', 'value': '0'}, {'type': {'id': 'long'}, 'id
': 'c2', 'value': '1'}, {'type': {'id': 'long'}, 'id': 'c3', 'value': '0x01'}, {
'type': {'id': 'long'}, 'id': 'c4', 'value': '10'}, {'type': {'id': 'boolean'},
'id': 'b1', 'value': 'false'}, {'type': {'id': 'boolean'}, 'id': 'b2', 'value':
'true'}]}]}) | |
| 123 | |
| 124 def test_annotations(self): | |
| 125 self._run_test( | |
| 126 idlparser.FREMONTCUT_SYNTAX, | |
| 127 '@Ano1 @Ano2() @Ano3(x=1) @Ano4(x,y=2) interface I {};', | |
| 128 {'interfaces': [{'javascript_binding_name': 'I', 'id': 'I', 'annotations':
{'Ano4': {'y': '2', 'x': None}, 'Ano1': {}, 'Ano2': {}, 'Ano3': {'x': '1'}}}]}) | |
| 129 self._run_test( | |
| 130 idlparser.FREMONTCUT_SYNTAX, | |
| 131 '''interface I : @Ano1 J { | |
| 132 @Ano2 attribute int someAttr; | |
| 133 @Ano3 void someOp(); | |
| 134 @Ano3 const int someConst = 0; | |
| 135 };''', | |
| 136 {'interfaces': [{'operations': [{'type': {'id': 'void'}, 'id': 'someOp', '
annotations': {'Ano3': {}}}], 'javascript_binding_name': 'I', 'parents': [{'type
': {'id': 'J'}, 'annotations': {'Ano1': {}}}], 'attributes': [{'type': {'id': 'i
nt'}, 'id': 'someAttr', 'annotations': {'Ano2': {}}}], 'id': 'I', 'constants': [
{'type': {'id': 'int'}, 'id': 'someConst', 'value': '0', 'annotations': {'Ano3':
{}}}]}]}) | |
| 137 | |
| 138 def test_inheritance(self): | |
| 139 self._run_test( | |
| 140 None, | |
| 141 'interface Shape {}; interface Rectangle : Shape {}; interface Square : Re
ctangle, Shape {};', | |
| 142 {'interfaces': [{'javascript_binding_name': 'Shape', 'id': 'Shape'}, {'jav
ascript_binding_name': 'Rectangle', 'parents': [{'type': {'id': 'Shape'}}], 'id'
: 'Rectangle'}, {'javascript_binding_name': 'Square', 'parents': [{'type': {'id'
: 'Rectangle'}}, {'type': {'id': 'Shape'}}], 'id': 'Square'}]}) | |
| 143 | |
| 144 if __name__ == "__main__": | |
| 145 logging.config.fileConfig("logging.conf") | |
| 146 if __name__ == '__main__': | |
| 147 unittest.main() | |
| OLD | NEW |