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
|
#------------------------------------------------------------------------------
# Copyright (c) 2022-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.
#------------------------------------------------------------------------------
from textwrap import dedent
from utils import compile_source
def test_tracer_load_attr():
source = dedent("""\
from atom.api import Atom, Str
from enaml.widgets.api import Window, Container, Label
class Model(Atom):
value = Str("foo")
enamldef Main(Window):
alias lbl
attr model = Model()
Container:
Label: lbl:
text << model.value
""")
Main = compile_source(source, 'Main')
window = Main()
assert window.lbl.text == "foo"
window.model.value = "bar"
assert window.lbl.text == "bar"
def test_tracer_call_func():
source = dedent("""\
from atom.api import Atom, Int
from enaml.widgets.api import Window, Container, Label
class Model(Atom):
value = Int(1)
enamldef Main(Window):
alias lbl
attr model = Model()
Container:
Label: lbl:
text << str(model.value)
""")
Main = compile_source(source, 'Main')
window = Main()
assert window.lbl.text == "1"
window.model.value = 2
assert window.lbl.text == "2"
def test_tracer_binary_subscr():
source = dedent("""\
from atom.api import Atom, ContainerList
from enaml.widgets.api import Window, Container, SpinBox
class Model(Atom):
items = ContainerList(default=[1])
enamldef Main(Window):
alias sb
attr model = Model()
Container:
SpinBox: sb:
value << model.items[0]
""")
Main = compile_source(source, 'Main')
window = Main()
window.model.items[0] = 3
assert window.sb.value == 3
def test_inverter_load_attr():
source = dedent("""\
from atom.api import Atom, Int
from enaml.widgets.api import Window, Container, Label, SpinBox
class Model(Atom):
value = Int(0)
enamldef Main(Window):
alias sb
attr model = Model()
Container:
SpinBox: sb:
value := model.value
""")
Main = compile_source(source, 'Main')
window = Main()
assert window.sb.value == 0
window.sb.value = 2
assert window.model.value == 2
window.model.value = 3
assert window.sb.value == 3
def test_inverter_call_func():
source = dedent("""\
from atom.api import Atom, Int
from enaml.widgets.api import Window, Container, Label, SpinBox
class Model(Atom):
value = Int(0)
enamldef Main(Window):
alias sb
attr model = Model()
Container:
SpinBox: sb:
value := getattr(model, "value")
""")
Main = compile_source(source, 'Main')
window = Main()
assert window.sb.value == 0
window.sb.value = 2
assert window.model.value == 2
window.model.value = 3
assert window.sb.value == 3
def test_inverter_binary_subscr():
source = dedent("""\
from atom.api import Atom, ContainerList
from enaml.widgets.api import Window, Container, SpinBox
class Model(Atom):
items = ContainerList(default=[1])
enamldef Main(Window):
alias sb
attr model = Model()
Container:
SpinBox: sb:
value := model.items[0]
""")
Main = compile_source(source, 'Main')
window = Main()
assert window.sb.value == 1
window.sb.value = 2
assert window.model.items[0] == 2
window.model.items[0] = 3
assert window.sb.value == 3
|