Debian Codegraph @

test/test_menu_rules.py

raw
1 import xml.etree.cElementTree as etree
2 import unittest
3
4 from xdg.Menu import XMLMenuBuilder, Rule
5
6
7 _tests = [
8 {
9 'doc': """
10 <Include>
11 <And>
12 <Category>Accessibility</Category>
13 <Not>
14 <Category>Settings</Category>
15 </Not>
16 </And>
17 <Filename>screenreader.desktop</Filename>
18 </Include>
19 """,
20 'data': [
21 ('app1.desktop', ['Accessibility'], True),
22 ('app2.desktop', ['Accessibility', 'Settings'], False),
23 ('app3.desktop', ['Accessibility', 'Preferences'], True),
24 ('app4.desktop', ['Graphics', 'Settings'], False),
25 ('screenreader.desktop', ['Utility', 'Other'], True)
26 ]
27 },
28 {
29 'doc': """
30 <Include>
31 <And>
32 <Category>Settings</Category>
33 <Not>
34 <Or>
35 <Category>System</Category>
36 <Category>X-GNOME-Settings-Panel</Category>
37 <Filename>foobar.desktop</Filename>
38 </Or>
39 </Not>
40 </And>
41 </Include>
42 """,
43 'data': [
44 ('app0.desktop', [], False),
45 ('app1.desktop', ['Settings'], True),
46 ('app2.desktop', ['System', 'Settings'], False),
47 ('app3.desktop', ['Games', 'Preferences'], False),
48 ('app4.desktop', ['Graphics', 'Settings'], True),
49 ('app5.desktop', ['X-GNOME-Settings-Panel', 'Settings'], False),
50 ('foobar.desktop', ['Settings', 'Other'], False)
51 ]
52 },
53 # Empty conditions
54 {
55 'doc': "<Include></Include>",
56 'data': [
57 ('app0.desktop', ['Graphics', 'Settings'], False),
58 ('screenreader.desktop', [], False)
59 ]
60 },
61 {
62 'doc': "<Include><Or></Or></Include>",
63 'data': [
64 ('app0.desktop', ['Graphics', 'Settings'], False),
65 ('screenreader.desktop', [], False)
66 ]
67 },
68 {
69 'doc': "<Include><And></And></Include>",
70 'data': [
71 ('app0.desktop', ['Graphics', 'Settings'], False),
72 ('screenreader.desktop', [], False)
73 ]
74 },
75 {
76 'doc': "<Include><Not></Not></Include>",
77 'data': [
78 ('app0.desktop', ['Graphics', 'Settings'], False),
79 ('screenreader.desktop', [], False)
80 ]
81 },
82 {
83 'doc': """
84 <Include>
85 <Filename>screenreader.desktop</Filename>
86 <Not></Not>
87 </Include>
88 """,
89 'data': [
90 ('app0.desktop', ['Graphics', 'Settings'], False),
91 ('screenreader.desktop', [], True)
92 ]
93 },
94 {
95 'doc': """
96 <Include>
97 <And>
98 <Filename>screenreader.desktop</Filename>
99 <Not></Not>
100 </And>
101 </Include>
102 """,
103 'data': [
104 ('app0.desktop', ['Graphics', 'Settings'], False),
105 ('screenreader.desktop', [], True)
106 ]
107 },
108 # Single condition
109 {
110 'doc': "<Include><Or><Filename>foobar.desktop</Filename></Or></Include>",
111 'data': [
112 ('app0.desktop', ['Graphics', 'Settings'], False),
113 ('foobar.desktop', [], True)
114 ]
115 },
116 # All
117 {
118 'doc': "<Include><Or><All/></Or></Include>",
119 'data': [
120 ('app0.desktop', ['Graphics', 'Settings'], True),
121 ('foobar.desktop', [], True)
122 ]
123 },
124 {
125 'doc': "<Include><Filename>foobar.desktop</Filename><All/></Include>",
126 'data': [
127 ('app0.desktop', ['Graphics', 'Settings'], True),
128 ('foobar.desktop', [], True)
129 ]
130 },
131 {
132 'doc': """
133 <Include>
134 <Filename>foobar.desktop</Filename>
135 <Category>Graphics</Category>
136 <Not><All/></Not>
137 </Include>
138 """,
139 'data': [
140 ('app0.desktop', ['Graphics', 'Settings'], True),
141 ('app1.desktop', ['Accessibility'], False),
142 ('app2.desktop', ['Accessibility', 'Settings'], False),
143 ('foobar.desktop', [], True),
144 ]
145 }
146 ]
147
148
149 class MockMenuEntry(object):
150
151 def __init__(self, id, categories):
152 self.DesktopFileID = id
153 self.Categories = categories
154
155 def __str__(self):
156 return "<%s: %s>" % (self.DesktopFileID, self.Categories)
157
158
159 class RulesTest(unittest.TestCase):
160 """Basic rule matching tests"""
161
162 def test_rule_from_node(self):
163 parser = XMLMenuBuilder(debug=True)
164 for i, test in enumerate(_tests):
165 root = etree.fromstring(test['doc'])
166 rule = parser.parse_rule(root)
167 for j, data in enumerate(test['data']):
168 menuentry = MockMenuEntry(data[0], data[1])
169 result = eval(rule.code)
170 message = "Error in test %s with result set %s: got %s, expected %s"
171 assert result == data[2], message % (i, j, result, data[2])
172
173 def test_rule_from_filename(self):
174 tests = [
175 ('foobar.desktop', 'foobar.desktop', True),
176 ('barfoo.desktop', 'foobar.desktop', False)
177 ]
178 for i, test in enumerate(tests):
179 rule = Rule.fromFilename(Rule.TYPE_INCLUDE, test[0])
180 menuentry = MockMenuEntry(test[1], [])
181 result = eval(rule.code)
182 message = "Error with result set %s: got %s, expected %s"
183 assert result == test[2], message % (i, result, test[2])