OLD | NEW |
1 # Copyright 2017 The Chromium Authors. All rights reserved. | 1 # Copyright 2017 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 # pylint: disable=import-error,print-statement,relative-import,protected-access | 5 # pylint: disable=import-error,print-statement,relative-import,protected-access |
6 | 6 |
7 """Unit tests for overload_set_algorithm.py.""" | 7 """Unit tests for overload_set_algorithm.py.""" |
8 | 8 |
9 import unittest | 9 import unittest |
10 | |
11 from overload_set_algorithm import effective_overload_set | 10 from overload_set_algorithm import effective_overload_set |
12 from overload_set_algorithm import MethodContextAdapter | |
13 | |
14 | |
15 class MethodContextAdapterTest(unittest.TestCase): | |
16 def test_simple(self): | |
17 adapter = MethodContextAdapter() | |
18 self.assertEqual(adapter.arguments({'arguments': 'foo'}), 'foo') | |
19 self.assertEqual(adapter.type({'idl_type_object': 'bar'}), 'bar') | |
20 self.assertEqual(adapter.is_optional({'is_optional': 'baz'}), 'baz') | |
21 self.assertEqual(adapter.is_variadic({'is_variadic': 'qux'}), 'qux') | |
22 | 11 |
23 | 12 |
24 class EffectiveOverloadSetTest(unittest.TestCase): | 13 class EffectiveOverloadSetTest(unittest.TestCase): |
25 def test_example_in_comments(self): | 14 def test_example_in_comments(self): |
26 operation_list = [ | 15 operation_list = [ |
27 {'arguments': [{'idl_type_object': 'long', # f1(optional long x) | 16 {'arguments': [{'idl_type_object': 'long', # f1(optional long x) |
28 'is_optional': True, | 17 'is_optional': True, |
29 'is_variadic': False}]}, | 18 'is_variadic': False}]}, |
30 {'arguments': [{'idl_type_object': 'DOMString', # f2(DOMString s) | 19 {'arguments': [{'idl_type_object': 'DOMString', # f2(DOMString s) |
31 'is_optional': False, | 20 'is_optional': False, |
32 'is_variadic': False}]}, | 21 'is_variadic': False}]}] |
33 ] | |
34 | 22 |
35 overload_set = [ | 23 overload_set = [ |
36 ({'arguments': [{'idl_type_object': 'long', # f1(long) | 24 ({'arguments': [{'idl_type_object': 'long', # f1(long) |
37 'is_optional': True, | 25 'is_optional': True, |
38 'is_variadic': False}]}, | 26 'is_variadic': False}]}, |
39 ('long',), | 27 ('long',), |
40 (True,)), | 28 (True,)), |
41 ({'arguments': [{'idl_type_object': 'long', # f1() | 29 ({'arguments': [{'idl_type_object': 'long', # f1() |
42 'is_optional': True, | 30 'is_optional': True, |
43 'is_variadic': False}]}, | 31 'is_variadic': False}]}, |
44 (), | 32 (), |
45 ()), | 33 ()), |
46 ({'arguments': [{'idl_type_object': 'DOMString', # f2(DOMString) | 34 ({'arguments': [{'idl_type_object': 'DOMString', # f2(DOMString) |
47 'is_optional': False, | 35 'is_optional': False, |
48 'is_variadic': False}]}, | 36 'is_variadic': False}]}, |
49 ('DOMString',), | 37 ('DOMString',), |
50 (False,))] | 38 (False,))] |
51 | 39 |
52 self.assertEqual( | 40 self.assertEqual(effective_overload_set(operation_list), overload_set) |
53 effective_overload_set(operation_list, MethodContextAdapter()), | 41 |
54 overload_set) | 42 def test_example_in_spec(self): |
| 43 """Tests the example provided in Web IDL spec: |
| 44 https://heycam.github.io/webidl/#dfn-effective-overload-set, |
| 45 look for example right after the algorithm. |
| 46 |
| 47 The output differs from spec because we don't implement the part |
| 48 of the algorithm that handles variadic arguments.""" |
| 49 operation_list = [ |
| 50 # f1: f(DOMString a) |
| 51 {'arguments': [{'idl_type_object': 'DOMString', |
| 52 'is_optional': False, |
| 53 'is_variadic': False}]}, |
| 54 # f2: f(Node a, DOMString b, double... c) |
| 55 {'arguments': [{'idl_type_object': 'Node', |
| 56 'is_optional': False, |
| 57 'is_variadic': False}, |
| 58 {'idl_type_object': 'DOMString', |
| 59 'is_optional': False, |
| 60 'is_variadic': False}, |
| 61 {'idl_type_object': 'double', |
| 62 'is_optional': False, |
| 63 'is_variadic': True}]}, |
| 64 # f3: f() |
| 65 {'arguments': []}, |
| 66 # f4: f(Event a, DOMString b, optional DOMString c, double... d) |
| 67 {'arguments': [{'idl_type_object': 'Event', |
| 68 'is_optional': False, |
| 69 'is_variadic': False}, |
| 70 {'idl_type_object': 'DOMString', |
| 71 'is_optional': False, |
| 72 'is_variadic': False}, |
| 73 {'idl_type_object': 'DOMString', |
| 74 'is_optional': True, |
| 75 'is_variadic': False}, |
| 76 {'idl_type_object': 'double', |
| 77 'is_optional': False, |
| 78 'is_variadic': True}]}] |
| 79 overload_set = [ |
| 80 # <f1, (DOMString), (required)> |
| 81 ({'arguments': [{'idl_type_object': 'DOMString', |
| 82 'is_optional': False, |
| 83 'is_variadic': False}]}, |
| 84 ('DOMString',), |
| 85 (False,)), |
| 86 # <f2, (Node, DOMString, double), (required, required, variadic)> |
| 87 ({'arguments': [{'idl_type_object': 'Node', |
| 88 'is_optional': False, |
| 89 'is_variadic': False}, |
| 90 {'idl_type_object': 'DOMString', |
| 91 'is_optional': False, |
| 92 'is_variadic': False}, |
| 93 {'idl_type_object': 'double', |
| 94 'is_optional': False, |
| 95 'is_variadic': True}]}, |
| 96 ('Node', 'DOMString', 'double'), |
| 97 (False, False, True)), |
| 98 # <f2, (Node, DOMString), (required, required)> |
| 99 ({'arguments': [{'idl_type_object': 'Node', |
| 100 'is_optional': False, |
| 101 'is_variadic': False}, |
| 102 {'idl_type_object': 'DOMString', |
| 103 'is_optional': False, |
| 104 'is_variadic': False}, |
| 105 {'idl_type_object': 'double', |
| 106 'is_optional': False, |
| 107 'is_variadic': True}]}, |
| 108 ('Node', 'DOMString'), |
| 109 (False, False)), |
| 110 # Missing from the output: |
| 111 # <f2, (Node, DOMString, double, double), |
| 112 # (required, required, variadic, variadic)>, |
| 113 # <f3, (), ()> |
| 114 ({'arguments': []}, (), ()), |
| 115 # <f4, (Event, DOMString, DOMString, double), |
| 116 # (required, required, optional, variadic)> |
| 117 ({'arguments': [{'idl_type_object': 'Event', |
| 118 'is_optional': False, |
| 119 'is_variadic': False}, |
| 120 {'idl_type_object': 'DOMString', |
| 121 'is_optional': False, |
| 122 'is_variadic': False}, |
| 123 {'idl_type_object': 'DOMString', |
| 124 'is_optional': True, |
| 125 'is_variadic': False}, |
| 126 {'idl_type_object': 'double', |
| 127 'is_optional': False, |
| 128 'is_variadic': True}]}, |
| 129 ('Event', 'DOMString', 'DOMString', 'double'), |
| 130 (False, False, True, True)), |
| 131 # <f4, (Event, DOMString, DOMString), |
| 132 # (required, required, optional)> |
| 133 ({'arguments': [{'idl_type_object': 'Event', |
| 134 'is_optional': False, |
| 135 'is_variadic': False}, |
| 136 {'idl_type_object': 'DOMString', |
| 137 'is_optional': False, |
| 138 'is_variadic': False}, |
| 139 {'idl_type_object': 'DOMString', |
| 140 'is_optional': True, |
| 141 'is_variadic': False}, |
| 142 {'idl_type_object': 'double', |
| 143 'is_optional': False, |
| 144 'is_variadic': True}]}, |
| 145 ('Event', 'DOMString', 'DOMString'), |
| 146 (False, False, True)), |
| 147 # <f4, (Event, DOMString), (required, required)> |
| 148 ({'arguments': [{'idl_type_object': 'Event', |
| 149 'is_optional': False, |
| 150 'is_variadic': False}, |
| 151 {'idl_type_object': 'DOMString', |
| 152 'is_optional': False, |
| 153 'is_variadic': False}, |
| 154 {'idl_type_object': 'DOMString', |
| 155 'is_optional': True, |
| 156 'is_variadic': False}, |
| 157 {'idl_type_object': 'double', |
| 158 'is_optional': False, |
| 159 'is_variadic': True}]}, |
| 160 ('Event', 'DOMString'), |
| 161 (False, False))] |
| 162 |
| 163 self.assertEqual(effective_overload_set(operation_list), overload_set) |
OLD | NEW |