예제 #1
0
    def test_deferred_events_debug(self): # XXX Subsumed by next test
        'Deferred events -- DEBUG'

        c1, c2, c3 = self.factory(), self.factory(), self.factory()
        dm = DictModifiedEventMonitor(c1)
        cm = ContextModifiedEventMonitor(c1)

        #print 'one' # XXX
        #def p(s): print s # XXX
        #c1.on_trait_change(lambda *args: p('A'), 'defer_events') # XXX
        #c1.context_data.on_trait_change(lambda *args:p('B'), 'defer_events')#XXX
        c1.defer_events = True
        #print 'two' # XXX
        c1.update(a=1, x=arange(3), c2=c2)
        c2.update(a=2, x=-arange(3))
        c3.update(a=3, x=1+arange(3))
        c1.update(c3=c3)
        dm.assert_events()
        cm.assert_events()

        c1.defer_events = False
        dm.assert_event(added={'a':1, 'x':arange(3), 'c2':c2, 'c3':c3})
        cm.assert_event(added=['x', 'c2.x', 'c3.x'], changed=['a', 'c2', 'c3'])

        c1.defer_events = True
        c1['x'] = -arange(3)
        c2['x'] = arange(3)
        c1.set_dotted('c3.x', 1-arange(3))
        dm.assert_events()
        cm.assert_events()

        c1.defer_events = False
        dm.assert_event(changed={'x':arange(3)})
        cm.assert_event(modified=['x', 'c2.x', 'c3.x'])
    def test_deferred_events_debug(self):  # XXX Subsumed by next test
        "Deferred events -- DEBUG"

        c1, c2, c3 = self.factory(), self.factory(), self.factory()
        dm = DictModifiedEventMonitor(c1)
        cm = ContextModifiedEventMonitor(c1)

        # print 'one' # XXX
        # def p(s): print s # XXX
        # c1.on_trait_change(lambda *args: p('A'), 'defer_events') # XXX
        # c1.context_data.on_trait_change(lambda *args:p('B'), 'defer_events')#XXX
        c1.defer_events = True
        # print 'two' # XXX
        c1.update(a=1, x=arange(3), c2=c2)
        c2.update(a=2, x=-arange(3))
        c3.update(a=3, x=1 + arange(3))
        c1.update(c3=c3)
        dm.assert_events()
        cm.assert_events()

        c1.defer_events = False
        dm.assert_event(added={"a": 1, "x": arange(3), "c2": c2, "c3": c3})
        cm.assert_event(added=["x", "c2.x", "c3.x"], changed=["a", "c2", "c3"])

        c1.defer_events = True
        c1["x"] = -arange(3)
        c2["x"] = arange(3)
        c1.set_dotted("c3.x", 1 - arange(3))
        dm.assert_events()
        cm.assert_events()

        c1.defer_events = False
        dm.assert_event(changed={"x": arange(3)})
        cm.assert_event(modified=["x", "c2.x", "c3.x"])
    def test_pickling_dynamic_binding(self):
        "Pickling: dynamic binding"

        p = self.factory()
        c = self.factory(context_name="c")

        p.bind_dynamic(c, "context_name")

        # Pickle 'p'. Also pickle the new 'p' out to nowhere to ensure that
        # '__getstate__' doesn't have side effects.
        p = loads(dumps(p))
        dumps(p)
        c = p["c"]

        dm = DictModifiedEventMonitor(p)
        cm = ContextModifiedEventMonitor(p)

        c.context_name = "d"
        dm.assert_events(dict(removed={"c": c}), dict(added={"d": c}))
        cm.assert_events(dict(changed="c"), dict(changed="d"))

        p["c"] = c
        dm.assert_events(dict(removed={"d": c}), dict(added={"c": c}))
        cm.assert_events(dict(changed="d"), dict(changed="c"))

        del p["c"]
        dm.assert_event(removed={"c": c})
        cm.assert_event(changed="c")

        c.context_name = "d"
        dm.assert_events()
        cm.assert_events()
    def test_deferred_events_debug(self): # XXX Subsumed by next test
        'Deferred events -- DEBUG'

        c1, c2, c3 = self.factory(), self.factory(), self.factory()
        dm = DictModifiedEventMonitor(c1)
        cm = ContextModifiedEventMonitor(c1)

        #print 'one' # XXX
        #def p(s): print s # XXX
        #c1.on_trait_change(lambda *args: p('A'), 'defer_events') # XXX
        #c1.context_data.on_trait_change(lambda *args:p('B'), 'defer_events')#XXX
        c1.defer_events = True
        #print 'two' # XXX
        c1.update(a=1, x=arange(3), c2=c2)
        c2.update(a=2, x=-arange(3))
        c3.update(a=3, x=1+arange(3))
        c1.update(c3=c3)
        dm.assert_events()
        cm.assert_events()

        c1.defer_events = False
        dm.assert_event(added={'a':1, 'x':arange(3), 'c2':c2, 'c3':c3})
        cm.assert_event(added=['x', 'c2.x', 'c3.x'], changed=['a', 'c2', 'c3'])

        c1.defer_events = True
        c1['x'] = -arange(3)
        c2['x'] = arange(3)
        c1.set_dotted('c3.x', 1-arange(3))
        dm.assert_events()
        cm.assert_events()

        c1.defer_events = False
        dm.assert_event(changed={'x':arange(3)})
        cm.assert_event(modified=['x', 'c2.x', 'c3.x'])
    def test_regression_context_modified_incomplete_when_array_replaces_none(self):
        "Regression: 'context_modified' incomplete when array replaces None"

        c = self.factory()
        dm = DictModifiedEventMonitor(c)
        cm = ContextModifiedEventMonitor(c)

        c["x"] = None
        dm.assert_event(added={"x": None})
        cm.assert_event(changed="x")

        c["x"] = arange(3)
        dm.assert_event(changed={"x": None})
        cm.assert_event(changed="x", added="x")
예제 #6
0
    def test_pickling_dynamic_binding(self):
        'Pickling: dynamic binding'

        p = self.factory()
        c = self.factory(context_name='c')

        p.bind_dynamic(c, 'context_name')

        # Pickle 'p'. Also pickle the new 'p' out to nowhere to ensure that
        # '__getstate__' doesn't have side effects.
        p = loads(dumps(p))
        dumps(p)
        c = p['c']

        dm = DictModifiedEventMonitor(p)
        cm = ContextModifiedEventMonitor(p)

        c.context_name = 'd'
        dm.assert_events(dict(removed={'c':c}),
                         dict(added={'d':c}))
        cm.assert_events(dict(changed='c'),
                         dict(changed='d'))

        p['c'] = c
        dm.assert_events(dict(removed={'d':c}),
                         dict(added={'c':c}))
        cm.assert_events(dict(changed='d'),
                         dict(changed='c'))

        del p['c']
        dm.assert_event(removed={'c':c})
        cm.assert_event(changed='c')

        c.context_name = 'd'
        dm.assert_events()
        cm.assert_events()
    def test_pickling_dynamic_binding(self):
        'Pickling: dynamic binding'

        p = self.factory()
        c = self.factory(context_name='c')

        p.bind_dynamic(c, 'context_name')

        # Pickle 'p'. Also pickle the new 'p' out to nowhere to ensure that
        # '__getstate__' doesn't have side effects.
        p = loads(dumps(p))
        dumps(p)
        c = p['c']

        dm = DictModifiedEventMonitor(p)
        cm = ContextModifiedEventMonitor(p)

        c.context_name = 'd'
        dm.assert_events(dict(removed={'c':c}),
                         dict(added={'d':c}))
        cm.assert_events(dict(changed='c'),
                         dict(changed='d'))

        p['c'] = c
        dm.assert_events(dict(removed={'d':c}),
                         dict(added={'c':c}))
        cm.assert_events(dict(changed='d'),
                         dict(changed='c'))

        del p['c']
        dm.assert_event(removed={'c':c})
        cm.assert_event(changed='c')

        c.context_name = 'd'
        dm.assert_events()
        cm.assert_events()
예제 #8
0
    def test_regression_context_modified_incomplete_when_array_replaces_none(self):
        "Regression: 'context_modified' incomplete when array replaces None"

        c = self.factory()
        dm = DictModifiedEventMonitor(c)
        cm = ContextModifiedEventMonitor(c)

        c['x'] = None
        dm.assert_event(added={'x':None})
        cm.assert_event(changed='x')

        c['x'] = arange(3)
        dm.assert_event(changed={'x':None})
        cm.assert_event(changed='x', added='x')
    def test_events_dynamic_binding(self):
        "Events: dynamic binding"

        # TODO Consolidate events

        p = self.factory()
        c = self.factory(context_name="c")
        c["x"] = arange(3)
        c["a"] = 8

        dm = DictModifiedEventMonitor(p)
        cm = ContextModifiedEventMonitor(p)

        p.bind_dynamic(c, "context_name")
        dm.assert_event(added={"c": c})
        cm.assert_event(changed="c", added=["c.x"])

        c.context_name = "bar"
        dm.assert_events(dict(removed={"c": c}), dict(added={"bar": c}))
        cm.assert_events(dict(changed="c", removed=["c.x"]), dict(changed=["bar"], added=["bar.x"]))

        p["foo"] = c
        dm.assert_events(dict(removed={"bar": c}), dict(added={"foo": c}))
        cm.assert_events(dict(changed=["bar"], removed=["bar.x"]), dict(changed=["foo"], added=["foo.x"]))

        p["c"] = 3
        dm.assert_event(added={"c": 3})
        cm.assert_event(changed="c")

        p["c"] = c
        dm.assert_events(dict(removed={"foo": c}), dict(changed={"c": 3}))
        cm.assert_events(dict(changed=["foo"], removed=["foo.x"]), dict(changed="c", added=["c.x"]))

        p["x"] = arange(3)
        dm.assert_event(added={"x": arange(3)})
        cm.assert_event(added="x")

        p["x"] = c
        dm.assert_events(dict(removed={"c": c}), dict(changed={"x": arange(3)}))
        cm.assert_events(dict(changed="c", removed=["c.x"]), dict(removed="x", changed="x", added=["x.x"]))

        ### 'del' a dynamic binding:

        del p["x"]
        dm.assert_event(removed={"x": c})
        cm.assert_event(changed="x", removed=["x.x"])

        c.context_name = "c"
        assert "c" not in p
        dm.assert_events()
        cm.assert_events()

        p.update(c=3, x=arange(3), foo="foo")
        dm.assert_event(added={"c": 3, "x": arange(3), "foo": "foo"})
        cm.assert_event(added="x", changed=["c", "foo"])

        ### Clobber dynamic bindings

        c.context_name = "b"
        p.bind_dynamic(c, "context_name")
        dm.assert_event(added={"b": c})
        cm.assert_event(changed="b", added=["b.x"])

        # If a dynamic binding tries to indirectly clobber a dictionary
        # mapping, then we drop the dynamic binding entirely
        c.context_name = "c"
        dm.assert_event(removed={"b": c})
        cm.assert_event(changed="b", removed=["b.x"])
        assert_equal(c.context_name, "c")

        # But new dictionary mappings are allowed to clobber dynamic bindings
        c.context_name = "b"
        p.bind_dynamic(c, "context_name")
        dm.assert_event(added={"b": c})
        cm.assert_event(changed="b", added=["b.x"])
        p["b"] = 2
        dm.assert_event(changed={"b": c})
        cm.assert_event(changed="b", removed=["b.x"])
    def test_deferred_events(self):
        "Deferred events"

        # Possible event combinations:
        #    1. none     + added
        #    2. none     + modified
        #    3. none     + removed
        #    4. removed  + added
        #    5. added    + modified
        #    6. modified + modified
        #    7. added    + removed
        #    8. modified + removed
        #    9. none     + changed
        #   10. changed  + changed

        c = self.factory()
        dm = DictModifiedEventMonitor(c)
        cm = ContextModifiedEventMonitor(c)

        c.update(a=1, x=arange(3), t=True)
        dm.assert_event(added={"a": 1, "x": arange(3), "t": True})

        c.defer_events = True
        #  3. none    + removed
        c.clear()
        #  4. removed + added
        c.update(a=1, x=arange(3), t=True)
        dm.assert_events()
        cm.assert_events()

        c.defer_events = False
        dm.assert_event(changed={"x": arange(3)})  # Because `[1,2] is not [1,2]`
        cm.assert_event(modified=["x"])

        c.defer_events = True
        #  1. none     + added
        c["y"] = -arange(3)
        #  5. added    + modified
        c["y"] = 1 + arange(3)
        #  2. none     + modified
        c["x"] = 1 + arange(3)
        #  6. modified + modified
        c["x"] = -arange(3)
        #  9. none     + changed
        c["foo"] = "foo"
        # 10. changed  + changed
        c["foo"] = "bar"
        dm.assert_events()
        cm.assert_events()

        c.defer_events = False
        dm.assert_event(added={"y": 1 + arange(3), "foo": "bar"}, changed={"x": arange(3)})
        cm.assert_event(added=["y"], modified=["x"], changed=["foo"])

        c.defer_events = True
        #  1. none     + added
        c["z"] = arange(3)
        #  7. added    + removed
        del c["z"]
        #  2. none     + modified
        c["y"] = arange(3)
        #  8. modified + removed
        del c["y"]
        dm.assert_events()
        cm.assert_events()

        c.defer_events = False
        dm.assert_event(removed={"y": 1 + arange(3)})
        cm.assert_event(removed=["y"])

        # Something more normal looking
        c.defer_events = True
        c["n"] = array([-1, 0, 1])
        for c["i"] in range(10):
            c["n"] *= 2
        del c["i"]
        dm.assert_events()
        cm.assert_events()

        c.defer_events = False
        dm.assert_event(added={"n": array([-1024, 0, 1024])})
        cm.assert_event(added=["n"])
예제 #11
0
    def test_deferred_events(self):
        'Deferred events'

        # Possible event combinations:
        #    1. none     + added
        #    2. none     + modified
        #    3. none     + removed
        #    4. removed  + added
        #    5. added    + modified
        #    6. modified + modified
        #    7. added    + removed
        #    8. modified + removed
        #    9. none     + changed
        #   10. changed  + changed

        c = self.factory()
        dm = DictModifiedEventMonitor(c)
        cm = ContextModifiedEventMonitor(c)

        c.update(a=1, x=arange(3), t=True)
        dm.assert_event(added={'a':1, 'x':arange(3), 't':True})

        c.defer_events = True
        #  3. none    + removed
        c.clear()
        #  4. removed + added
        c.update(a=1, x=arange(3), t=True)
        dm.assert_events()
        cm.assert_events()

        c.defer_events = False
        dm.assert_event(changed={'x':arange(3)}) # Because `[1,2] is not [1,2]`
        cm.assert_event(modified=['x'])

        c.defer_events = True
        #  1. none     + added
        c['y'] = -arange(3)
        #  5. added    + modified
        c['y'] = 1+arange(3)
        #  2. none     + modified
        c['x'] = 1+arange(3)
        #  6. modified + modified
        c['x'] = -arange(3)
        #  9. none     + changed
        c['foo'] = 'foo'
        # 10. changed  + changed
        c['foo'] = 'bar'
        dm.assert_events()
        cm.assert_events()

        c.defer_events = False
        dm.assert_event(added={'y':1+arange(3), 'foo':'bar'},
                        changed={'x':arange(3)})
        cm.assert_event(added=['y'], modified=['x'],
                        changed=['foo'])

        c.defer_events = True
        #  1. none     + added
        c['z'] = arange(3)
        #  7. added    + removed
        del c['z']
        #  2. none     + modified
        c['y'] = arange(3)
        #  8. modified + removed
        del c['y']
        dm.assert_events()
        cm.assert_events()

        c.defer_events = False
        dm.assert_event(removed={'y':1+arange(3)})
        cm.assert_event(removed=['y'])

        # Something more normal looking
        c.defer_events = True
        c['n'] = array([-1, 0, 1])
        for c['i'] in range(10):
            c['n'] *= 2
        del c['i']
        dm.assert_events()
        cm.assert_events()

        c.defer_events = False
        dm.assert_event(added={'n':array([-1024, 0, 1024])})
        cm.assert_event(added=['n'])
    def test_events_nested_contexts(self):
        "Events: nested contexts"

        ### setitem (inside-out)

        c1, c2, c3 = self.factory(), self.factory(), self.factory()
        dm = DictModifiedEventMonitor(c1)
        cm = ContextModifiedEventMonitor(c1)

        c3["a"] = 1
        c3["x"] = arange(3)
        dm.assert_events()
        cm.assert_events()

        c2["a"] = 2
        c2["x"] = arange(3)
        c2["c3"] = c3
        dm.assert_events()
        cm.assert_events()

        c1["a"] = 3
        c1["x"] = 1 + arange(3)
        c1["c2"] = c2
        dm.assert_events(dict(added={"a": 3}), dict(added={"x": 1 + arange(3)}), dict(added={"c2": c2}))
        cm.assert_events(dict(changed="a"), dict(added="x"), dict(changed=["c2"], added=["c2.x", "c2.c3.x"]))

        ### setitem (outside-in)

        c1, c2, c3 = self.factory(), self.factory(), self.factory()
        dm = DictModifiedEventMonitor(c1)
        cm = ContextModifiedEventMonitor(c1)

        c1["a"] = 3
        c1["x"] = 1 + arange(3)
        c1["c2"] = c2
        dm.assert_events(dict(added={"a": 3}), dict(added={"x": 1 + arange(3)}), dict(added={"c2": c2}))
        cm.assert_events(dict(changed="a"), dict(added="x"), dict(changed=["c2"]))

        c2["a"] = 2
        c2["x"] = arange(3)
        c2["c3"] = c3
        dm.assert_events()
        cm.assert_event(added=["c2.x"])

        c3["a"] = 1
        c3["x"] = arange(3)
        dm.assert_events()
        cm.assert_event(added=["c2.c3.x"])

        ### update (inside-out)

        c1, c2, c3 = self.factory(), self.factory(), self.factory()
        dm = DictModifiedEventMonitor(c1)
        cm = ContextModifiedEventMonitor(c1)

        c3.update(a=1, x=arange(3))
        dm.assert_events()
        cm.assert_events()

        c2.update(a=2, x=-arange(3), c3=c3)
        dm.assert_events()
        cm.assert_events()

        c1.update(a=3, x=1 + arange(3), c2=c2)
        dm.assert_event(added={"a": 3, "x": 1 + arange(3), "c2": c2})
        cm.assert_event(added=["x", "c2.x", "c2.c3.x"], changed=["a", "c2"])

        ### update (outside-in)

        c1, c2, c3 = self.factory(), self.factory(), self.factory()
        dm = DictModifiedEventMonitor(c1)
        cm = ContextModifiedEventMonitor(c1)

        c1.update(a=3, x=1 + arange(3), c2=c2)
        dm.assert_event(added={"a": 3, "x": 1 + arange(3), "c2": c2})
        cm.assert_event(added="x", changed=["a", "c2"])

        c2.update(a=2, x=-arange(3), c3=c3)
        dm.assert_events()
        cm.assert_event(added=["c2.x"])

        c3.update(a=1, x=arange(3))
        dm.assert_events()
        cm.assert_event(added=["c2.c3.x"])

        ### Mutate

        c1, c2, c3 = self.factory(), self.factory(), self.factory()
        c3.update(a=1, l=[1], x=arange(3))
        c2.update(a=2, l=[2], x=-arange(3), c3=c3)
        c1.update(a=3, l=[3], x=1 + arange(3), c2=c2)
        dm = DictModifiedEventMonitor(c1)
        cm = ContextModifiedEventMonitor(c1)

        c1["a"] += 3
        dm.assert_event(changed={"a": 3})
        cm.assert_event(changed="a")

        c1["l"].append(0)
        dm.assert_events()
        cm.assert_events()

        c1["x"] += 3
        dm.assert_events()
        cm.assert_events()

        c1["x"] = c1["x"] + 3
        dm.assert_event(changed={"x": 4 + arange(3)})
        cm.assert_event(modified="x")

        c2["a"] += 3
        dm.assert_events()
        cm.assert_events()

        c2["l"].append(0)
        dm.assert_events()
        cm.assert_events()

        c2["x"] += 3
        dm.assert_events()
        cm.assert_event(modified=["c2.x"])

        c3["a"] += 3
        dm.assert_events()
        cm.assert_events()

        c3["l"].append(0)
        dm.assert_events()
        cm.assert_events()

        c3["x"] += 3
        dm.assert_events()
        cm.assert_event(modified=["c2.c3.x"])

        ### delitem (outside-in)

        c1, c2, c3 = self.factory(), self.factory(), self.factory()
        c3.update(a=1, x=arange(3))
        c2.update(a=2, x=-arange(3), c3=c3)
        c1.update(a=3, x=1 + arange(3), c2=c2)
        dm = DictModifiedEventMonitor(c1)
        cm = ContextModifiedEventMonitor(c1)

        del c1["a"]
        del c1["x"]
        del c1["c2"]
        dm.assert_events(dict(removed={"a": 3}), dict(removed={"x": 1 + arange(3)}), dict(removed={"c2": c2}))
        cm.assert_events(dict(changed="a"), dict(removed="x"), dict(changed=["c2"], removed=["c2.x", "c2.c3.x"]))

        del c2["a"]
        del c2["x"]
        del c2["c3"]
        dm.assert_events()
        cm.assert_events()

        del c3["a"]
        del c3["x"]
        dm.assert_events()
        cm.assert_events()

        ### delitem (inside-out)

        c1, c2, c3 = self.factory(), self.factory(), self.factory()
        c3.update(a=1, x=arange(3))
        c2.update(a=2, x=-arange(3), c3=c3)
        c1.update(a=3, x=1 + arange(3), c2=c2)
        dm = DictModifiedEventMonitor(c1)
        cm = ContextModifiedEventMonitor(c1)

        del c3["a"]
        del c3["x"]
        dm.assert_events()
        cm.assert_event(removed=["c2.c3.x"])

        del c2["a"]
        del c2["x"]
        del c2["c3"]
        dm.assert_events()
        cm.assert_event(removed=["c2.x"])

        del c1["a"]
        del c1["x"]
        del c1["c2"]
        dm.assert_events(dict(removed={"a": 3}), dict(removed={"x": 1 + arange(3)}), dict(removed={"c2": c2}))
        cm.assert_events(dict(changed="a"), dict(removed="x"), dict(changed=["c2"]))

        ### clear (outside-in)

        c1, c2, c3 = self.factory(), self.factory(), self.factory()
        c3.update(a=1, x=arange(3))
        c2.update(a=2, x=-arange(3), c3=c3)
        c1.update(a=3, x=1 + arange(3), c2=c2)
        dm = DictModifiedEventMonitor(c1)
        cm = ContextModifiedEventMonitor(c1)

        c1.clear()
        dm.assert_event(removed={"a": 3, "x": 1 + arange(3), "c2": c2})
        cm.assert_event(changed=["a", "c2"], removed=["x", "c2.x", "c2.c3.x"])

        c2.clear()
        dm.assert_events()
        cm.assert_events()

        c3.clear()
        dm.assert_events()
        cm.assert_events()

        ### clear (inside-out)

        c1, c2, c3 = self.factory(), self.factory(), self.factory()
        c3.update(a=1, x=arange(3))
        c2.update(a=2, x=-arange(3), c3=c3)
        c1.update(a=3, x=1 + arange(3), c2=c2)
        dm = DictModifiedEventMonitor(c1)
        cm = ContextModifiedEventMonitor(c1)

        c3.clear()
        dm.assert_events()
        cm.assert_event(removed=["c2.c3.x"])

        c2.clear()
        dm.assert_events()
        cm.assert_event(removed=["c2.x"])

        c1.clear()
        dm.assert_event(removed={"a": 3, "x": 1 + arange(3), "c2": c2})
        cm.assert_event(changed=["a", "c2"], removed="x")
    def test_events_multiple_names(self):
        "Events: multiple names"

        c = self.factory()
        dm = DictModifiedEventMonitor(c)
        cm = ContextModifiedEventMonitor(c)

        c.update(a=1, x=arange(3), t=True)
        dm.assert_event(added={"a": 1, "x": arange(3), "t": True})
        cm.assert_event(changed="at", added="x")

        c.update(a=2, x=-arange(3), y=arange(3))
        dm.assert_event(changed={"a": 1, "x": arange(3)}, added={"y": arange(3)})
        cm.assert_events(dict(changed="a", modified="x", added="y"))

        del c["t"]
        dm.assert_event(removed={"t": True})
        cm.assert_event(changed="t")

        del c["x"]
        dm.assert_event(removed={"x": -arange(3)})
        cm.assert_event(removed="x")

        c.clear()
        dm.assert_event(removed={"a": 2, "y": arange(3)})
        cm.assert_event(changed="a", removed="y")
    def test_events_single_name(self):
        "Events: single name"

        c = self.factory()
        dm = DictModifiedEventMonitor(c)
        cm = ContextModifiedEventMonitor(c)

        c["x"] = 1
        dm.assert_event(added={"x": 1})
        cm.assert_event(changed="x")

        c["x"] = 1
        dm.assert_events()
        cm.assert_events()

        c["x"] = "foo"
        dm.assert_event(changed={"x": 1})
        cm.assert_event(changed="x")

        c["x"] = "foo"
        dm.assert_events()
        cm.assert_events()

        del c["x"]
        dm.assert_event(removed={"x": "foo"})
        cm.assert_event(changed="x")

        c["x"] = "foo"
        dm.flush()
        cm.flush()

        c["x"] = arange(3)
        dm.assert_event(changed={"x": "foo"})
        cm.assert_event(added="x", changed="x")

        c["x"] = arange(3)  # This fires because 'arange(3) is not arange(3)'
        dm.assert_event(changed={"x": arange(3)})
        cm.assert_event(modified="x")

        c["x"] = -arange(4)
        dm.assert_event(changed={"x": arange(3)})
        cm.assert_event(modified="x")

        del c["x"]
        dm.assert_event(removed={"x": -arange(4)})
        cm.assert_event(removed="x")

        c["x"] = arange(3)
        dm.assert_event(added={"x": arange(3)})
        cm.assert_event(added="x")

        c["x"] = 3
        dm.assert_event(changed={"x": arange(3)})
        cm.assert_event(removed="x", changed="x")
    def test_events_dynamic_binding(self):
        'Events: dynamic binding'

        # TODO Consolidate events

        p = self.factory()
        c = self.factory(context_name='c')
        c['x'] = arange(3)
        c['a'] = 8

        dm = DictModifiedEventMonitor(p)
        cm = ContextModifiedEventMonitor(p)

        p.bind_dynamic(c, 'context_name')
        dm.assert_event(added={'c':c})
        cm.assert_event(changed='c', added=['c.x'])

        c.context_name = 'bar'
        dm.assert_events(dict(removed={'c':c}),
                         dict(added={'bar':c}))
        cm.assert_events(dict(changed='c', removed=['c.x']),
                         dict(changed=['bar'], added=['bar.x']))

        p['foo'] = c
        dm.assert_events(dict(removed={'bar':c}),
                         dict(added={'foo':c}))
        cm.assert_events(dict(changed=['bar'], removed=['bar.x']),
                         dict(changed=['foo'], added=['foo.x']))

        p['c'] = 3
        dm.assert_event(added={'c':3})
        cm.assert_event(changed='c')

        p['c'] = c
        dm.assert_events(dict(removed={'foo':c}),
                         dict(changed={'c':3}))
        cm.assert_events(dict(changed=['foo'], removed=['foo.x']),
                         dict(changed='c', added=['c.x']))

        p['x'] = arange(3)
        dm.assert_event(added={'x':arange(3)})
        cm.assert_event(added='x')

        p['x'] = c
        dm.assert_events(dict(removed={'c':c}),
                         dict(changed={'x':arange(3)}))
        cm.assert_events(dict(changed='c', removed=['c.x']),
                         dict(removed='x', changed='x', added=['x.x']))

        ### 'del' a dynamic binding:

        del p['x']
        dm.assert_event(removed={'x':c})
        cm.assert_event(changed='x', removed=['x.x'])

        c.context_name = 'c'
        assert 'c' not in p
        dm.assert_events()
        cm.assert_events()

        p.update(c=3, x=arange(3), foo='foo')
        dm.assert_event(added={'c':3, 'x':arange(3), 'foo':'foo'})
        cm.assert_event(added='x', changed=['c', 'foo'])

        ### Clobber dynamic bindings

        c.context_name = 'b'
        p.bind_dynamic(c, 'context_name')
        dm.assert_event(added={'b':c})
        cm.assert_event(changed='b', added=['b.x'])

        # If a dynamic binding tries to indirectly clobber a dictionary
        # mapping, then we drop the dynamic binding entirely
        c.context_name = 'c'
        dm.assert_event(removed={'b':c})
        cm.assert_event(changed='b', removed=['b.x'])
        assert_equal(c.context_name, 'c')

        # But new dictionary mappings are allowed to clobber dynamic bindings
        c.context_name = 'b'
        p.bind_dynamic(c, 'context_name')
        dm.assert_event(added={'b':c})
        cm.assert_event(changed='b', added=['b.x'])
        p['b'] = 2
        dm.assert_event(changed={'b':c})
        cm.assert_event(changed='b', removed=['b.x'])
    def test_deferred_events(self):
        'Deferred events'

        # Possible event combinations:
        #    1. none     + added
        #    2. none     + modified
        #    3. none     + removed
        #    4. removed  + added
        #    5. added    + modified
        #    6. modified + modified
        #    7. added    + removed
        #    8. modified + removed
        #    9. none     + changed
        #   10. changed  + changed

        c = self.factory()
        dm = DictModifiedEventMonitor(c)
        cm = ContextModifiedEventMonitor(c)

        c.update(a=1, x=arange(3), t=True)
        dm.assert_event(added={'a':1, 'x':arange(3), 't':True})

        c.defer_events = True
        #  3. none    + removed
        c.clear()
        #  4. removed + added
        c.update(a=1, x=arange(3), t=True)
        dm.assert_events()
        cm.assert_events()

        c.defer_events = False
        dm.assert_event(changed={'x':arange(3)}) # Because `[1,2] is not [1,2]`
        cm.assert_event(modified=['x'])

        c.defer_events = True
        #  1. none     + added
        c['y'] = -arange(3)
        #  5. added    + modified
        c['y'] = 1+arange(3)
        #  2. none     + modified
        c['x'] = 1+arange(3)
        #  6. modified + modified
        c['x'] = -arange(3)
        #  9. none     + changed
        c['foo'] = 'foo'
        # 10. changed  + changed
        c['foo'] = 'bar'
        dm.assert_events()
        cm.assert_events()

        c.defer_events = False
        dm.assert_event(added={'y':1+arange(3), 'foo':'bar'},
                        changed={'x':arange(3)})
        cm.assert_event(added=['y'], modified=['x'],
                        changed=['foo'])

        c.defer_events = True
        #  1. none     + added
        c['z'] = arange(3)
        #  7. added    + removed
        del c['z']
        #  2. none     + modified
        c['y'] = arange(3)
        #  8. modified + removed
        del c['y']
        dm.assert_events()
        cm.assert_events()

        c.defer_events = False
        dm.assert_event(removed={'y':1+arange(3)})
        cm.assert_event(removed=['y'])

        # Something more normal looking
        c.defer_events = True
        c['n'] = array([-1, 0, 1])
        for c['i'] in range(10):
            c['n'] *= 2
        del c['i']
        dm.assert_events()
        cm.assert_events()

        c.defer_events = False
        dm.assert_event(added={'n':array([-1024, 0, 1024])})
        cm.assert_event(added=['n'])
예제 #17
0
    def test_events_multiple_names(self):
        'Events: multiple names'

        c = self.factory()
        dm = DictModifiedEventMonitor(c)
        cm = ContextModifiedEventMonitor(c)

        c.update(a=1, x=arange(3), t=True)
        dm.assert_event(added={'a':1, 'x':arange(3), 't':True})
        cm.assert_event(changed='at', added='x')

        c.update(a=2, x=-arange(3), y=arange(3))
        dm.assert_event(changed={'a':1, 'x':arange(3)}, added={'y':arange(3)})
        cm.assert_events(dict(changed='a', modified='x', added='y'))

        del c['t']
        dm.assert_event(removed={'t':True})
        cm.assert_event(changed='t')

        del c['x']
        dm.assert_event(removed={'x':-arange(3)})
        cm.assert_event(removed='x')

        c.clear()
        dm.assert_event(removed={'a':2, 'y':arange(3)})
        cm.assert_event(changed='a', removed='y')
    def test_events_nested_contexts(self):
        'Events: nested contexts'

        ### setitem (inside-out)

        c1,c2,c3 = self.factory(), self.factory(), self.factory()
        dm = DictModifiedEventMonitor(c1)
        cm = ContextModifiedEventMonitor(c1)

        c3['a'] = 1
        c3['x'] = arange(3)
        dm.assert_events()
        cm.assert_events()

        c2['a'] = 2
        c2['x'] = arange(3)
        c2['c3'] = c3
        dm.assert_events()
        cm.assert_events()

        c1['a'] = 3
        c1['x'] = 1+arange(3)
        c1['c2'] = c2
        dm.assert_events(dict(added={'a':3}),
                         dict(added={'x':1+arange(3)}),
                         dict(added={'c2':c2}))
        cm.assert_events(dict(changed='a'),
                         dict(added='x'),
                         dict(changed=['c2'], added=['c2.x', 'c2.c3.x']))

        ### setitem (outside-in)

        c1,c2,c3 = self.factory(), self.factory(), self.factory()
        dm = DictModifiedEventMonitor(c1)
        cm = ContextModifiedEventMonitor(c1)

        c1['a'] = 3
        c1['x'] = 1+arange(3)
        c1['c2'] = c2
        dm.assert_events(dict(added={'a':3}),
                         dict(added={'x':1+arange(3)}),
                         dict(added={'c2':c2}))
        cm.assert_events(dict(changed='a'),
                         dict(added='x'),
                         dict(changed=['c2']))

        c2['a'] = 2
        c2['x'] = arange(3)
        c2['c3'] = c3
        dm.assert_events()
        cm.assert_event(added=['c2.x'])

        c3['a'] = 1
        c3['x'] = arange(3)
        dm.assert_events()
        cm.assert_event(added=['c2.c3.x'])

        ### update (inside-out)

        c1,c2,c3 = self.factory(), self.factory(), self.factory()
        dm = DictModifiedEventMonitor(c1)
        cm = ContextModifiedEventMonitor(c1)

        c3.update(a=1, x=arange(3))
        dm.assert_events()
        cm.assert_events()

        c2.update(a=2, x=-arange(3), c3=c3)
        dm.assert_events()
        cm.assert_events()

        c1.update(a=3, x=1+arange(3), c2=c2)
        dm.assert_event(added={'a':3, 'x':1+arange(3), 'c2':c2})
        cm.assert_event(added=['x', 'c2.x', 'c2.c3.x'], changed=['a', 'c2'])

        ### update (outside-in)

        c1,c2,c3 = self.factory(), self.factory(), self.factory()
        dm = DictModifiedEventMonitor(c1)
        cm = ContextModifiedEventMonitor(c1)

        c1.update(a=3, x=1+arange(3), c2=c2)
        dm.assert_event(added={'a':3, 'x':1+arange(3), 'c2':c2})
        cm.assert_event(added='x', changed=['a', 'c2'])

        c2.update(a=2, x=-arange(3), c3=c3)
        dm.assert_events()
        cm.assert_event(added=['c2.x'])

        c3.update(a=1, x=arange(3))
        dm.assert_events()
        cm.assert_event(added=['c2.c3.x'])

        ### Mutate

        c1,c2,c3 = self.factory(), self.factory(), self.factory()
        c3.update(a=1, l=[1], x=arange(3))
        c2.update(a=2, l=[2], x=-arange(3), c3=c3)
        c1.update(a=3, l=[3], x=1+arange(3), c2=c2)
        dm = DictModifiedEventMonitor(c1)
        cm = ContextModifiedEventMonitor(c1)

        c1['a'] += 3
        dm.assert_event(changed={'a':3})
        cm.assert_event(changed='a')

        c1['l'].append(0)
        dm.assert_events()
        cm.assert_events()

        c1['x'] += 3
        dm.assert_events()
        cm.assert_events()

        c1['x'] = c1['x'] + 3
        dm.assert_event(changed={'x':4+arange(3)})
        cm.assert_event(modified='x')

        c2['a'] += 3
        dm.assert_events()
        cm.assert_events()

        c2['l'].append(0)
        dm.assert_events()
        cm.assert_events()

        c2['x'] += 3
        dm.assert_events()
        cm.assert_event(modified=['c2.x'])

        c3['a'] += 3
        dm.assert_events()
        cm.assert_events()

        c3['l'].append(0)
        dm.assert_events()
        cm.assert_events()

        c3['x'] += 3
        dm.assert_events()
        cm.assert_event(modified=['c2.c3.x'])

        ### delitem (outside-in)

        c1,c2,c3 = self.factory(), self.factory(), self.factory()
        c3.update(a=1, x=arange(3))
        c2.update(a=2, x=-arange(3), c3=c3)
        c1.update(a=3, x=1+arange(3), c2=c2)
        dm = DictModifiedEventMonitor(c1)
        cm = ContextModifiedEventMonitor(c1)

        del c1['a']
        del c1['x']
        del c1['c2']
        dm.assert_events(dict(removed={'a':3}),
                         dict(removed={'x':1+arange(3)}),
                         dict(removed={'c2':c2}))
        cm.assert_events(dict(changed='a'),
                         dict(removed='x'),
                         dict(changed=['c2'], removed=['c2.x', 'c2.c3.x']))

        del c2['a']
        del c2['x']
        del c2['c3']
        dm.assert_events()
        cm.assert_events()

        del c3['a']
        del c3['x']
        dm.assert_events()
        cm.assert_events()

        ### delitem (inside-out)

        c1,c2,c3 = self.factory(), self.factory(), self.factory()
        c3.update(a=1, x=arange(3))
        c2.update(a=2, x=-arange(3), c3=c3)
        c1.update(a=3, x=1+arange(3), c2=c2)
        dm = DictModifiedEventMonitor(c1)
        cm = ContextModifiedEventMonitor(c1)

        del c3['a']
        del c3['x']
        dm.assert_events()
        cm.assert_event(removed=['c2.c3.x'])

        del c2['a']
        del c2['x']
        del c2['c3']
        dm.assert_events()
        cm.assert_event(removed=['c2.x'])

        del c1['a']
        del c1['x']
        del c1['c2']
        dm.assert_events(dict(removed={'a':3}),
                         dict(removed={'x':1+arange(3)}),
                         dict(removed={'c2':c2}))
        cm.assert_events(dict(changed='a'),
                         dict(removed='x'),
                         dict(changed=['c2']))

        ### clear (outside-in)

        c1,c2,c3 = self.factory(), self.factory(), self.factory()
        c3.update(a=1, x=arange(3))
        c2.update(a=2, x=-arange(3), c3=c3)
        c1.update(a=3, x=1+arange(3), c2=c2)
        dm = DictModifiedEventMonitor(c1)
        cm = ContextModifiedEventMonitor(c1)

        c1.clear()
        dm.assert_event(removed={'a':3, 'x':1+arange(3), 'c2':c2})
        cm.assert_event(changed=['a', 'c2'], removed=['x', 'c2.x', 'c2.c3.x'])

        c2.clear()
        dm.assert_events()
        cm.assert_events()

        c3.clear()
        dm.assert_events()
        cm.assert_events()

        ### clear (inside-out)

        c1,c2,c3 = self.factory(), self.factory(), self.factory()
        c3.update(a=1, x=arange(3))
        c2.update(a=2, x=-arange(3), c3=c3)
        c1.update(a=3, x=1+arange(3), c2=c2)
        dm = DictModifiedEventMonitor(c1)
        cm = ContextModifiedEventMonitor(c1)

        c3.clear()
        dm.assert_events()
        cm.assert_event(removed=['c2.c3.x'])

        c2.clear()
        dm.assert_events()
        cm.assert_event(removed=['c2.x'])

        c1.clear()
        dm.assert_event(removed={'a':3, 'x':1+arange(3), 'c2':c2})
        cm.assert_event(changed=['a', 'c2'], removed='x')
    def test_events_multiple_names(self):
        'Events: multiple names'

        c = self.factory()
        dm = DictModifiedEventMonitor(c)
        cm = ContextModifiedEventMonitor(c)

        c.update(a=1, x=arange(3), t=True)
        dm.assert_event(added={'a':1, 'x':arange(3), 't':True})
        cm.assert_event(changed='at', added='x')

        c.update(a=2, x=-arange(3), y=arange(3))
        dm.assert_event(changed={'a':1, 'x':arange(3)}, added={'y':arange(3)})
        cm.assert_events(dict(changed='a', modified='x', added='y'))

        del c['t']
        dm.assert_event(removed={'t':True})
        cm.assert_event(changed='t')

        del c['x']
        dm.assert_event(removed={'x':-arange(3)})
        cm.assert_event(removed='x')

        c.clear()
        dm.assert_event(removed={'a':2, 'y':arange(3)})
        cm.assert_event(changed='a', removed='y')
    def test_events_single_name(self):
        'Events: single name'

        c = self.factory()
        dm = DictModifiedEventMonitor(c)
        cm = ContextModifiedEventMonitor(c)

        c['x'] = 1
        dm.assert_event(added={'x':1})
        cm.assert_event(changed='x')

        c['x'] = 1
        dm.assert_events()
        cm.assert_events()

        c['x'] = 'foo'
        dm.assert_event(changed={'x':1})
        cm.assert_event(changed='x')

        c['x'] = 'foo'
        dm.assert_events()
        cm.assert_events()

        del c['x']
        dm.assert_event(removed={'x':'foo'})
        cm.assert_event(changed='x')

        c['x'] = 'foo'
        dm.flush();
        cm.flush()

        c['x'] = arange(3)
        dm.assert_event(changed={'x':'foo'})
        cm.assert_event(added='x', changed='x')

        c['x'] = arange(3) # This fires because 'arange(3) is not arange(3)'
        dm.assert_event(changed={'x':arange(3)})
        cm.assert_event(modified='x')

        c['x'] = -arange(4)
        dm.assert_event(changed={'x':arange(3)})
        cm.assert_event(modified='x')

        del c['x']
        dm.assert_event(removed={'x':-arange(4)})
        cm.assert_event(removed='x')

        c['x'] = arange(3)
        dm.assert_event(added={'x':arange(3)})
        cm.assert_event(added='x')

        c['x'] = 3
        dm.assert_event(changed={'x':arange(3)})
        cm.assert_event(removed='x', changed='x')
예제 #21
0
    def test_events_nested_contexts(self):
        'Events: nested contexts'

        ### setitem (inside-out)

        c1,c2,c3 = self.factory(), self.factory(), self.factory()
        dm = DictModifiedEventMonitor(c1)
        cm = ContextModifiedEventMonitor(c1)

        c3['a'] = 1
        c3['x'] = arange(3)
        dm.assert_events()
        cm.assert_events()

        c2['a'] = 2
        c2['x'] = arange(3)
        c2['c3'] = c3
        dm.assert_events()
        cm.assert_events()

        c1['a'] = 3
        c1['x'] = 1+arange(3)
        c1['c2'] = c2
        dm.assert_events(dict(added={'a':3}),
                         dict(added={'x':1+arange(3)}),
                         dict(added={'c2':c2}))
        cm.assert_events(dict(changed='a'),
                         dict(added='x'),
                         dict(changed=['c2'], added=['c2.x', 'c2.c3.x']))

        ### setitem (outside-in)

        c1,c2,c3 = self.factory(), self.factory(), self.factory()
        dm = DictModifiedEventMonitor(c1)
        cm = ContextModifiedEventMonitor(c1)

        c1['a'] = 3
        c1['x'] = 1+arange(3)
        c1['c2'] = c2
        dm.assert_events(dict(added={'a':3}),
                         dict(added={'x':1+arange(3)}),
                         dict(added={'c2':c2}))
        cm.assert_events(dict(changed='a'),
                         dict(added='x'),
                         dict(changed=['c2']))

        c2['a'] = 2
        c2['x'] = arange(3)
        c2['c3'] = c3
        dm.assert_events()
        cm.assert_event(added=['c2.x'])

        c3['a'] = 1
        c3['x'] = arange(3)
        dm.assert_events()
        cm.assert_event(added=['c2.c3.x'])

        ### update (inside-out)

        c1,c2,c3 = self.factory(), self.factory(), self.factory()
        dm = DictModifiedEventMonitor(c1)
        cm = ContextModifiedEventMonitor(c1)

        c3.update(a=1, x=arange(3))
        dm.assert_events()
        cm.assert_events()

        c2.update(a=2, x=-arange(3), c3=c3)
        dm.assert_events()
        cm.assert_events()

        c1.update(a=3, x=1+arange(3), c2=c2)
        dm.assert_event(added={'a':3, 'x':1+arange(3), 'c2':c2})
        cm.assert_event(added=['x', 'c2.x', 'c2.c3.x'], changed=['a', 'c2'])

        ### update (outside-in)

        c1,c2,c3 = self.factory(), self.factory(), self.factory()
        dm = DictModifiedEventMonitor(c1)
        cm = ContextModifiedEventMonitor(c1)

        c1.update(a=3, x=1+arange(3), c2=c2)
        dm.assert_event(added={'a':3, 'x':1+arange(3), 'c2':c2})
        cm.assert_event(added='x', changed=['a', 'c2'])

        c2.update(a=2, x=-arange(3), c3=c3)
        dm.assert_events()
        cm.assert_event(added=['c2.x'])

        c3.update(a=1, x=arange(3))
        dm.assert_events()
        cm.assert_event(added=['c2.c3.x'])

        ### Mutate

        c1,c2,c3 = self.factory(), self.factory(), self.factory()
        c3.update(a=1, l=[1], x=arange(3))
        c2.update(a=2, l=[2], x=-arange(3), c3=c3)
        c1.update(a=3, l=[3], x=1+arange(3), c2=c2)
        dm = DictModifiedEventMonitor(c1)
        cm = ContextModifiedEventMonitor(c1)

        c1['a'] += 3
        dm.assert_event(changed={'a':3})
        cm.assert_event(changed='a')

        c1['l'].append(0)
        dm.assert_events()
        cm.assert_events()

        c1['x'] += 3
        dm.assert_events()
        cm.assert_events()

        c1['x'] = c1['x'] + 3
        dm.assert_event(changed={'x':4+arange(3)})
        cm.assert_event(modified='x')

        c2['a'] += 3
        dm.assert_events()
        cm.assert_events()

        c2['l'].append(0)
        dm.assert_events()
        cm.assert_events()

        c2['x'] += 3
        dm.assert_events()
        cm.assert_event(modified=['c2.x'])

        c3['a'] += 3
        dm.assert_events()
        cm.assert_events()

        c3['l'].append(0)
        dm.assert_events()
        cm.assert_events()

        c3['x'] += 3
        dm.assert_events()
        cm.assert_event(modified=['c2.c3.x'])

        ### delitem (outside-in)

        c1,c2,c3 = self.factory(), self.factory(), self.factory()
        c3.update(a=1, x=arange(3))
        c2.update(a=2, x=-arange(3), c3=c3)
        c1.update(a=3, x=1+arange(3), c2=c2)
        dm = DictModifiedEventMonitor(c1)
        cm = ContextModifiedEventMonitor(c1)

        del c1['a']
        del c1['x']
        del c1['c2']
        dm.assert_events(dict(removed={'a':3}),
                         dict(removed={'x':1+arange(3)}),
                         dict(removed={'c2':c2}))
        cm.assert_events(dict(changed='a'),
                         dict(removed='x'),
                         dict(changed=['c2'], removed=['c2.x', 'c2.c3.x']))

        del c2['a']
        del c2['x']
        del c2['c3']
        dm.assert_events()
        cm.assert_events()

        del c3['a']
        del c3['x']
        dm.assert_events()
        cm.assert_events()

        ### delitem (inside-out)

        c1,c2,c3 = self.factory(), self.factory(), self.factory()
        c3.update(a=1, x=arange(3))
        c2.update(a=2, x=-arange(3), c3=c3)
        c1.update(a=3, x=1+arange(3), c2=c2)
        dm = DictModifiedEventMonitor(c1)
        cm = ContextModifiedEventMonitor(c1)

        del c3['a']
        del c3['x']
        dm.assert_events()
        cm.assert_event(removed=['c2.c3.x'])

        del c2['a']
        del c2['x']
        del c2['c3']
        dm.assert_events()
        cm.assert_event(removed=['c2.x'])

        del c1['a']
        del c1['x']
        del c1['c2']
        dm.assert_events(dict(removed={'a':3}),
                         dict(removed={'x':1+arange(3)}),
                         dict(removed={'c2':c2}))
        cm.assert_events(dict(changed='a'),
                         dict(removed='x'),
                         dict(changed=['c2']))

        ### clear (outside-in)

        c1,c2,c3 = self.factory(), self.factory(), self.factory()
        c3.update(a=1, x=arange(3))
        c2.update(a=2, x=-arange(3), c3=c3)
        c1.update(a=3, x=1+arange(3), c2=c2)
        dm = DictModifiedEventMonitor(c1)
        cm = ContextModifiedEventMonitor(c1)

        c1.clear()
        dm.assert_event(removed={'a':3, 'x':1+arange(3), 'c2':c2})
        cm.assert_event(changed=['a', 'c2'], removed=['x', 'c2.x', 'c2.c3.x'])

        c2.clear()
        dm.assert_events()
        cm.assert_events()

        c3.clear()
        dm.assert_events()
        cm.assert_events()

        ### clear (inside-out)

        c1,c2,c3 = self.factory(), self.factory(), self.factory()
        c3.update(a=1, x=arange(3))
        c2.update(a=2, x=-arange(3), c3=c3)
        c1.update(a=3, x=1+arange(3), c2=c2)
        dm = DictModifiedEventMonitor(c1)
        cm = ContextModifiedEventMonitor(c1)

        c3.clear()
        dm.assert_events()
        cm.assert_event(removed=['c2.c3.x'])

        c2.clear()
        dm.assert_events()
        cm.assert_event(removed=['c2.x'])

        c1.clear()
        dm.assert_event(removed={'a':3, 'x':1+arange(3), 'c2':c2})
        cm.assert_event(changed=['a', 'c2'], removed='x')
예제 #22
0
    def test_events_single_name(self):
        'Events: single name'

        c = self.factory()
        dm = DictModifiedEventMonitor(c)
        cm = ContextModifiedEventMonitor(c)

        c['x'] = 1
        dm.assert_event(added={'x':1})
        cm.assert_event(changed='x')

        c['x'] = 1
        dm.assert_events()
        cm.assert_events()

        c['x'] = 'foo'
        dm.assert_event(changed={'x':1})
        cm.assert_event(changed='x')

        c['x'] = 'foo'
        dm.assert_events()
        cm.assert_events()

        del c['x']
        dm.assert_event(removed={'x':'foo'})
        cm.assert_event(changed='x')

        c['x'] = 'foo'
        dm.flush();
        cm.flush()

        c['x'] = arange(3)
        dm.assert_event(changed={'x':'foo'})
        cm.assert_event(added='x', changed='x')

        c['x'] = arange(3) # This fires because 'arange(3) is not arange(3)'
        dm.assert_event(changed={'x':arange(3)})
        cm.assert_event(modified='x')

        c['x'] = -arange(4)
        dm.assert_event(changed={'x':arange(3)})
        cm.assert_event(modified='x')

        del c['x']
        dm.assert_event(removed={'x':-arange(4)})
        cm.assert_event(removed='x')

        c['x'] = arange(3)
        dm.assert_event(added={'x':arange(3)})
        cm.assert_event(added='x')

        c['x'] = 3
        dm.assert_event(changed={'x':arange(3)})
        cm.assert_event(removed='x', changed='x')
예제 #23
0
    def test_events_dynamic_binding(self):
        'Events: dynamic binding'

        # TODO Consolidate events

        p = self.factory()
        c = self.factory(context_name='c')
        c['x'] = arange(3)
        c['a'] = 8

        dm = DictModifiedEventMonitor(p)
        cm = ContextModifiedEventMonitor(p)

        p.bind_dynamic(c, 'context_name')
        dm.assert_event(added={'c':c})
        cm.assert_event(changed='c', added=['c.x'])

        c.context_name = 'bar'
        dm.assert_events(dict(removed={'c':c}),
                         dict(added={'bar':c}))
        cm.assert_events(dict(changed='c', removed=['c.x']),
                         dict(changed=['bar'], added=['bar.x']))

        p['foo'] = c
        dm.assert_events(dict(removed={'bar':c}),
                         dict(added={'foo':c}))
        cm.assert_events(dict(changed=['bar'], removed=['bar.x']),
                         dict(changed=['foo'], added=['foo.x']))

        p['c'] = 3
        dm.assert_event(added={'c':3})
        cm.assert_event(changed='c')

        p['c'] = c
        dm.assert_events(dict(removed={'foo':c}),
                         dict(changed={'c':3}))
        cm.assert_events(dict(changed=['foo'], removed=['foo.x']),
                         dict(changed='c', added=['c.x']))

        p['x'] = arange(3)
        dm.assert_event(added={'x':arange(3)})
        cm.assert_event(added='x')

        p['x'] = c
        dm.assert_events(dict(removed={'c':c}),
                         dict(changed={'x':arange(3)}))
        cm.assert_events(dict(changed='c', removed=['c.x']),
                         dict(removed='x', changed='x', added=['x.x']))

        ### 'del' a dynamic binding:

        del p['x']
        dm.assert_event(removed={'x':c})
        cm.assert_event(changed='x', removed=['x.x'])

        c.context_name = 'c'
        assert 'c' not in p
        dm.assert_events()
        cm.assert_events()

        p.update(c=3, x=arange(3), foo='foo')
        dm.assert_event(added={'c':3, 'x':arange(3), 'foo':'foo'})
        cm.assert_event(added='x', changed=['c', 'foo'])

        ### Clobber dynamic bindings

        c.context_name = 'b'
        p.bind_dynamic(c, 'context_name')
        dm.assert_event(added={'b':c})
        cm.assert_event(changed='b', added=['b.x'])

        # If a dynamic binding tries to indirectly clobber a dictionary
        # mapping, then we drop the dynamic binding entirely
        c.context_name = 'c'
        dm.assert_event(removed={'b':c})
        cm.assert_event(changed='b', removed=['b.x'])
        assert_equal(c.context_name, 'c')

        # But new dictionary mappings are allowed to clobber dynamic bindings
        c.context_name = 'b'
        p.bind_dynamic(c, 'context_name')
        dm.assert_event(added={'b':c})
        cm.assert_event(changed='b', added=['b.x'])
        p['b'] = 2
        dm.assert_event(changed={'b':c})
        cm.assert_event(changed='b', removed=['b.x'])