File: test_operators.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 (339 lines) | stat: -rw-r--r-- 8,998 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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#------------------------------------------------------------------------------
# Copyright (c) 2019-2024, Nucleic Development Team.
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file COPYING.txt, distributed with this software.
#------------------------------------------------------------------------------
import sys
from textwrap import dedent

import pytest

from enaml.core.alias import Alias
from utils import compile_source


SUBSCRIPTION_BLOCK_TESTS = {
    # What should not work
    'leftshift newline in func': (SyntaxError, """
        enamldef Main(Window):
            Container:
                Label:
                    func test():
                        x = 1 <<
                            8
                        return str(x)
                    text = test()
        """),
    'leftshfit newline in attr': (SyntaxError, """
        enamldef Main(Window):
            Container:
                Label:
                    attr x = 1 <<
                                8
                    text = str(x)
        """),
    'leftshift newline in if': (SyntaxError, """
        enamldef Main(Window):
            Container:
                Label:
                    func test():
                        if 1 <<
                            8:
                            return 'this is bad'
                    text = str(test())
        """),
    'yield in sub block': (SyntaxError, """
        enamldef Main(Window):
            Container:
                Label:
                    text <<
                        for i in range(10):
                            yield i
        """),
    # What should still work
    'leftshift newline in parenthesis': (None, """
        enamldef Main(Window):
            Container:
                Label:
                    text = str(1 <<
                            8)
        """),
    'leftshift token in func': (None, """
        enamldef Main(Window):
            Container:
                Label:
                    func test():
                        x = 1 << 8
                        return str(x)
                    text = test()
        """),
    'leftshift token in attr': (None, """
        enamldef Main(Window):
            Container:
                Label:
                    attr x = 1 << 8
                    text = str(x)
        """),
    'leftshift newline update attr': (None, """
        enamldef Main(Window):
            Container:
                Label:
                    text <<
                        return str(1 << 8)
        """),
    'leftshift update attr': (None, """
        enamldef Main(Window):
            Container:
                Label:
                    text << str(1 << 8)
        """),
    'sub block var': (None, """
        enamldef Main(Window):
            Container:
                Label:
                    text <<
                        x = 10
                        if x:
                            return 'ten'
                        return 'not ten'
        """),
}


@pytest.mark.parametrize('name', SUBSCRIPTION_BLOCK_TESTS.keys())
def test_subscription_block_syntax(name):
    """Test a subscription block operator does not interfere with the leftshift
    operator.

    """
    error, code = SUBSCRIPTION_BLOCK_TESTS[name]
    source = dedent("""
    from enaml.widgets.api import *
    %s
    """ % code.lstrip())
    if error is not None:
        with pytest.raises(error):
            Main = compile_source(source, 'Main')
            window = Main()
    else:
        Main = compile_source(source, 'Main')
        window = Main()

# The following tests need to ensure that assignment in subscription block
# (either through a direct assignment or through the use of the walrus
# operator) do not cause issue with the tracer.
# Assignments can target the following scopes:
# - local to the block (only scope that the walrus operator target)
# - global module scope
# - widget and dynamic scope that can only be accessed qualified names (self.)

def test_subscription_block_observers1():
    """Test a subscription block operator.

    """
    source = dedent("""\
    from enaml.widgets.api import *

    enamldef Main(Window):
        alias label
        attr a = 1
        attr b = 2
        Label: label:
            text <<
                if a & 1:
                    return 'a'
                elif b & 1:
                    return 'b'
                return 'none'

    """)
    Main = compile_source(source, 'Main')
    window = Main()
    label = window.label
    assert label.text == 'a'
    window.a = 2
    assert label.text == 'none'  # 2 and 2
    window.b = 1
    assert label.text == 'b'
    window.a = 1
    assert label.text == 'a'


def test_subscription_block_observers2():
    """Test that intermediate assignments (local) do not confuse the tracer.

    """
    source = dedent("""\
    from enaml.widgets.api import *

    enamldef Main(Window):
        alias label
        attr a = 1
        attr b = 2
        Label: label:
            text <<
                b = a
                if b & 1:
                    return 'a'
                return 'none'

    """)
    Main = compile_source(source, 'Main')
    window = Main()
    label = window.label
    assert label.text == 'a'
    window.a = 2
    assert label.text == 'none'
    window.b = 1
    assert label.text == 'none'
    window.a = 1
    assert label.text == 'a'


def test_subscription_block_observers3():
    """Test that handling recursive behavior works.

    """
    source = dedent("""\
    from enaml.widgets.api import *

    enamldef Main(Window): main:
        alias label
        attr a = 1
        attr b = 2
        Label: label:
            text <<
                value = 'none'
                if a & 1:
                    value = 'a'
                elif b & 1:
                    value = 'b'
                main.b = 2
                return value

    """)
    Main = compile_source(source, 'Main')
    window = Main()
    label = window.label
    assert label.text == 'a'
    window.a = 2
    assert label.text == 'none'  # 2 and 2
    window.b = 1
    assert label.text == 'b'
    assert window.b == 2
    window.a = 1
    assert label.text == 'a'


def test_subscription_block_observers4():
    """Test that global variable shadowed by local can be accessed.

    """
    source = dedent("""\
    from enaml.widgets.api import *

    b = 2

    enamldef Main(Window): main:
        alias label
        attr a = 1
        attr b = 2
        attr counter = 0
        Label: label:
            text <<
                global b
                main.counter += 1
                if a & 1:
                    return 'a'
                elif b & 1:
                    return 'b'
                return 'none'

    """)
    Main = compile_source(source, 'Main')
    window = Main()
    label = window.label
    assert label.text == 'a'
    assert window.counter == 1
    window.a = 2
    assert label.text == 'none'  # 2 and 2
    assert window.counter == 2
    window.b = 1
    assert label.text == 'none'
    assert window.counter == 2
    window.a = 1
    assert label.text == 'a'
    assert window.counter == 3


def test_subscription_block_observers5():
    """Test that global variable shadowed by local do not block notification
    on qualified name access.

    """
    source = dedent("""\
    from enaml.widgets.api import *

    b = 2

    enamldef Main(Window): main:
        alias label
        attr a = 1
        attr b = 2
        attr counter = 0
        Label: label:
            text <<
                global b
                main.counter += 1
                if a & 1:
                    return 'a'
                elif main.b & 1:
                    return 'b'
                return 'none'

    """)
    Main = compile_source(source, 'Main')
    window = Main()
    label = window.label
    assert label.text == 'a'
    assert window.counter == 1
    window.a = 2
    assert label.text == 'none'  # 2 and 2
    assert window.counter == 2
    window.b = 1
    assert label.text == 'b'
    assert window.counter == 3
    window.a = 1
    assert label.text == 'a'
    assert window.counter == 4


@pytest.mark.skipif(sys.version_info < (3, 8), reason="Require Python 3.8+")
def test_subscription_operator_walrus():
    """Test handling the walrus operator inside a subscription.

    """
    source = dedent("""\
    from enaml.widgets.api import *

    enamldef Main(Window):
        alias label
        alias label2
        attr a = 1
        attr b = 2
        attr c = 5
        Label: label:
            text << f"{a + (c := b + 1)}"
        Label: label2:
            text << f"{c}"

    """)
    Main = compile_source(source, 'Main')
    window = Main()
    label = window.label
    assert label.text == '4'
    # This is the same behavior as everywhere else where access can use a bare name
    # but assignment needs a qualified name.
    assert window.label2.text == '5'