summaryrefslogtreecommitdiff
path: root/test/test-menu-rules.py
blob: 1bfafb1960f8de1cfd2b43bbb287528e3b5dc94a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import xml.etree.cElementTree as etree
import unittest

from xdg.Menu import Parser, Rule


_tests = [
    {
        'doc': """
<Include>
    <And>
        <Category>Accessibility</Category>
        <Not>
            <Category>Settings</Category>
        </Not>
    </And>
    <Filename>screenreader.desktop</Filename>
</Include>
        """,
        'data': [
            ('app1.desktop', ['Accessibility'], True),
            ('app2.desktop', ['Accessibility', 'Settings'], False),
            ('app3.desktop', ['Accessibility', 'Preferences'], True),
            ('app4.desktop', ['Graphics', 'Settings'], False),
            ('screenreader.desktop', ['Utility', 'Other'], True)
        ]
    },
    {
        'doc': """
<Include>
    <And>
        <Category>Settings</Category>
        <Not>
            <Or>
                <Category>System</Category>
                <Category>X-GNOME-Settings-Panel</Category>
                <Filename>foobar.desktop</Filename>
            </Or>
        </Not>
    </And>
</Include>
        """,
        'data': [
            ('app0.desktop', [], False),
            ('app1.desktop', ['Settings'], True),
            ('app2.desktop', ['System', 'Settings'], False),
            ('app3.desktop', ['Games', 'Preferences'], False),
            ('app4.desktop', ['Graphics', 'Settings'], True),
            ('app5.desktop', ['X-GNOME-Settings-Panel', 'Settings'], False),
            ('foobar.desktop', ['Settings', 'Other'], False)
        ]
    },
    # Empty conditions
    {
        'doc': "<Include></Include>",
        'data': [
            ('app0.desktop', ['Graphics', 'Settings'], False),
            ('screenreader.desktop', [], False)
        ]
    },
    {
        'doc': "<Include><Or></Or></Include>",
        'data': [
            ('app0.desktop', ['Graphics', 'Settings'], False),
            ('screenreader.desktop', [], False)
        ]
    },
    {
        'doc': "<Include><And></And></Include>",
        'data': [
            ('app0.desktop', ['Graphics', 'Settings'], False),
            ('screenreader.desktop', [], False)
        ]
    },
    {
        'doc': "<Include><Not></Not></Include>",
        'data': [
            ('app0.desktop', ['Graphics', 'Settings'], False),
            ('screenreader.desktop', [], False)
        ]
    },
    {
        'doc': """
<Include>
    <Filename>screenreader.desktop</Filename>
    <Not></Not>
</Include>
        """,
        'data': [
            ('app0.desktop', ['Graphics', 'Settings'], False),
            ('screenreader.desktop', [], True)
        ]
    },
    {
        'doc': """
<Include>
    <And>
        <Filename>screenreader.desktop</Filename>
        <Not></Not>
    </And>
</Include>
        """,
        'data': [
            ('app0.desktop', ['Graphics', 'Settings'], False),
            ('screenreader.desktop', [], True)
        ]
    },
    # Single condition
    {
        'doc': "<Include><Or><Filename>foobar.desktop</Filename></Or></Include>",
        'data': [
            ('app0.desktop', ['Graphics', 'Settings'], False),
            ('foobar.desktop', [], True)
        ]
    },
    # All
    {
        'doc': "<Include><Or><All/></Or></Include>",
        'data': [
            ('app0.desktop', ['Graphics', 'Settings'], True),
            ('foobar.desktop', [], True)
        ]
    },
    {
        'doc': "<Include><Filename>foobar.desktop</Filename><All/></Include>",
        'data': [
            ('app0.desktop', ['Graphics', 'Settings'], True),
            ('foobar.desktop', [], True)
        ]
    },
    {
        'doc': """
<Include>
    <Filename>foobar.desktop</Filename>
    <Category>Graphics</Category>
    <Not><All/></Not>
</Include>
        """,
        'data': [
            ('app0.desktop', ['Graphics', 'Settings'], True),
            ('app1.desktop', ['Accessibility'], False),
            ('app2.desktop', ['Accessibility', 'Settings'], False),
            ('foobar.desktop', [], True),
        ]
    }
]


class MockMenuEntry(object):

    def __init__(self, id, categories):
        self.DesktopFileID = id
        self.Categories = categories

    def __str__(self):
        return "<%s: %s>" % (self.DesktopFileID, self.Categories)


class RulesTest(unittest.TestCase):
    """Basic rule matching tests"""

    def test_rule_from_node(self):
        parser = Parser(debug=True)
        for i, test in enumerate(_tests):
            root = etree.fromstring(test['doc'])
            rule = parser.parse_rule(root)
            for j, data in enumerate(test['data']):
                menuentry = MockMenuEntry(data[0], data[1])
                result = eval(rule.code)
                message = "Error in test %s with result set %s: got %s, expected %s"
                assert result == data[2], message % (i, j, result, data[2])

    def test_rule_from_filename(self):
        tests = [
            ('foobar.desktop', 'foobar.desktop', True),
            ('barfoo.desktop', 'foobar.desktop', False)
        ]
        for i, test in enumerate(tests):
            rule = Rule.fromFilename(Rule.TYPE_INCLUDE, test[0])
            menuentry = MockMenuEntry(test[1], [])
            result = eval(rule.code)
            message = "Error with result set %s: got %s, expected %s"
            assert result == test[2], message % (i, result, test[2])