File: test_inner_functions.py

package info (click to toggle)
python-enaml 0.19.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,284 kB
  • sloc: python: 31,443; cpp: 4,499; makefile: 140; javascript: 68; lisp: 53; sh: 20
file content (316 lines) | stat: -rw-r--r-- 8,811 bytes parent folder | download
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#------------------------------------------------------------------------------
# Copyright (c) 2013-2025, Nucleic Development Team.
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file LICENSE, distributed with this software.
#------------------------------------------------------------------------------
"""Test the handling of nested functions such as lambda functions and
implicit functions used by comprehensions.

"""
import pytest

from utils import compile_source


OPERATOR_TEMPLATES = [
"""from enaml.widgets.window import Window

enamldef Main(Window):
    event ev
    attr called : bool = False
    ev ::
        test = 1
        a = {}
        try:
            b = i
        except NameError:
            pass
        else:
            raise AssertionError('i leaked out of the comprehension')
        self.called = True

""",
"""from enaml.widgets.window import Window, Container

template Temp():

    Container:
        event ev
        attr called : bool = False

enamldef Main(Window):

    alias ev: t.ev
    alias called: t.called

    Temp(): t:
        t.ev ::
            test = 1
            a = {}
            try:
                b = i
            except NameError:
                pass
            else:
                raise AssertionError('i leaked out of the comprehension')
            t.called = True

"""
]


SYNCHRONIZATION_TEMPLATES = [
"""from enaml.widgets.api import Window, Container, Field, Label

enamldef Main(Window):

    attr colors = ['red', 'blue', 'yellow', 'green']
    alias search_txt : search.text
    alias formatted_comp : lab.text

    Container: container:

        Field: search:
            placeholder = "Search..."
        Label: lab:
            text << '{{}}'.format({})

""",
"""from enaml.widgets.api import Window, Container, Field, Label

template Temp():

    Container: container:

        alias fd: search.text
        alias lb: lab.text

        Field: search:
            placeholder = "Search..."
        Label: lab:
            text << '{{}}'.format({})


enamldef Main(Window):

    attr colors = ['red', 'blue', 'yellow', 'green']
    alias search_txt : t.fd
    alias formatted_comp : t.lb

    Temp(): t:
        pass

"""
]


FUNCTION_TEMPLATES =[
"""from enaml.widgets.window import Window

enamldef Main(Window):
    func call():
        test = 1
        a = {}
        try:
            b = i
        except NameError:
            pass
        else:
            raise AssertionError('i leaked out of the comprehension')
        return True

""",
"""from enaml.widgets.window import Window, Container

template Temp():

    Container:
        func call():
            test = 1
            a = {}
            try:
                b = i
            except NameError:
                pass
            else:
                raise AssertionError('i leaked out of the comprehension')
            return True

enamldef Main(Window):

    func call():
        return t.call()

    Temp(): t:
        pass

"""
]


@pytest.mark.parametrize("template", SYNCHRONIZATION_TEMPLATES)
def test_lambda(template):
    """Test that lambda work when used in the context of tracing.

    """
    source = template.format(
        'sorted([1, 2, 3], key=lambda x: -x)')
    win = compile_source(source, 'Main')()
    assert win.formatted_comp == '[3, 2, 1]'


@pytest.mark.parametrize("template", SYNCHRONIZATION_TEMPLATES)
def test_tracing_lambda(template):
    """Test that lambda can be traced.

    """
    source = template.format(
        'sorted([1, 2, 3], key=lambda x: colors[x-1][0])')
    win = compile_source(source, 'Main')()
    assert win.formatted_comp == '[2, 1, 3]'
    win.colors = ['yellow', 'red', 'blue', 'green']
    assert win.formatted_comp == '[3, 2, 1]'


# Test that we can access the non local values and the closure variables
@pytest.mark.parametrize("template", OPERATOR_TEMPLATES)
@pytest.mark.parametrize('value', ['self', 'test'])
def test_list_comprehension_operator(template, value):
    """Test running a list comprehension in an operator handler.

    """
    source = template.format('[%s for i in range(10)]' % value)
    win = compile_source(source, 'Main')()
    win.ev = True
    assert win.called


@pytest.mark.parametrize("template", SYNCHRONIZATION_TEMPLATES)
def test_list_comprehension_synchronization(template):
    """Test running a list comprehension in a traced read handler.

    """
    source = template.format(
        '[c for c in colors if not search.text or search.text in c]')
    win = compile_source(source, 'Main')()
    assert win.formatted_comp == "['red', 'blue', 'yellow', 'green']"
    win.search_txt = 'red'
    assert win.formatted_comp == "['red']"
    win.search_txt = ''
    win.colors = ['red', 'blue', 'yellow']
    assert win.formatted_comp == "['red', 'blue', 'yellow']"


# Test that we can access the non local values and the closure variables
@pytest.mark.parametrize("template", FUNCTION_TEMPLATES)
@pytest.mark.parametrize('value', ['self', 'test'])
def test_list_comprehension_func(template, value):
    """Test running a list comprehension in a declarative function.

    """
    source = template.format('[%s for i in range(10)]' % value)
    win = compile_source(source, 'Main')()
    assert win.call()


# Test that we can access the non local values and the closure variables
@pytest.mark.parametrize("template", OPERATOR_TEMPLATES)
@pytest.mark.parametrize('value', ['self', 'test'])
def test_dict_comprehension_operator(template, value):
    """Test running a dict comprehension in an operator handler.

    """
    source = template.format('{i: %s for i in range(10)}' % value)
    win = compile_source(source, 'Main')()
    win.ev = True
    assert win.called


@pytest.mark.parametrize("template", SYNCHRONIZATION_TEMPLATES)
def test_dict_comprehension_synchronisation(template):
    """Test running a dict comprehension in a traced read handler.

    """
    source = template.format(
        '{i: search.text for i in range(3)}')
    win = compile_source(source, 'Main')()
    assert eval(win.formatted_comp) == {0: '', 1: '', 2: ''}
    win.search_txt = 'red'
    assert eval(win.formatted_comp) == {0: 'red', 1: 'red', 2: 'red'}


# Test that we can access the non local values and the closure variables
@pytest.mark.parametrize("template", FUNCTION_TEMPLATES)
@pytest.mark.parametrize('value', ['self', 'test'])
def test_dict_comprehension_func(template, value):
    """Test running a dict comprehension in a declarative function.

    """
    source = template.format('{i: %s for i in range(10)}' % value)
    win = compile_source(source, 'Main')()
    assert win.call()


# Test that we can access the non local values and the closure variables
@pytest.mark.parametrize("template", OPERATOR_TEMPLATES)
@pytest.mark.parametrize('value', ['self', 'test'])
def test_set_comprehension_operator(template, value):
    """Test running a set comprehension in an operator handler.

    """
    source = template.format('{%s for i in range(10)}' % value)
    win = compile_source(source, 'Main')()
    win.ev = True
    assert win.called


@pytest.mark.parametrize("template", SYNCHRONIZATION_TEMPLATES)
def test_set_comprehension_synchronization(template):
    """Test running a set comprehension in a traced read handler.

    """
    source = template.format(
        '{search.text for i in range(3)}')
    win = compile_source(source, 'Main')()
    assert eval(win.formatted_comp) == {''}
    win.search_txt = 'red'
    assert eval(win.formatted_comp) == {'red'}


# Test that we can access the non local values and the closure variables
@pytest.mark.parametrize("template", FUNCTION_TEMPLATES)
@pytest.mark.parametrize('value', ['self', 'test'])
def test_set_comprehension_func(template, value):
    """Test running a set comprehension in a declarative function.

    """
    source = template.format('{%s for i in range(10)}' % value)
    win = compile_source(source, 'Main')()
    assert win.call()


@pytest.mark.parametrize("template", FUNCTION_TEMPLATES)
def test_handling_nested_comprehension(template):
    """Test handling nested comprehensions.

    """
    source = template.format('{self for i in {j for j in range(10)}}')
    win = compile_source(source, 'Main')()
    assert win.call()


@pytest.mark.parametrize("template", SYNCHRONIZATION_TEMPLATES)
def test_handling_nested_comprehension_synchronization(template):
    """Test handling nested comprehensions in a traced read.

    """
    source = template.format(
        '{search.text for i in {j for j in colors}}')
    win = compile_source(source, 'Main')()
    assert eval(win.formatted_comp) == {''}
    win.search_txt = 'red'
    assert eval(win.formatted_comp) == {'red'}
    win.colors = []
    assert eval(win.formatted_comp) == set()