def test_update_with_obj(): begin_update_count = 0 def begin_update(domain): nonlocal begin_update_count begin_update_count += 1 end_update_count = 0 def end_update(domain): nonlocal end_update_count end_update_count += 1 f = Curve() # Add and remove observer using object obj = T() f.add_observer(obj, begin=begin_update, end=end_update) f.begin_update(Interval(1, 3)) assert begin_update_count == 1 f.end_update(Interval(1, 3)) assert end_update_count == 1 f.remove_observer(obj) f.begin_update(Interval(1, 3)) assert begin_update_count == 1 f.end_update(Interval(1, 3)) assert end_update_count == 1
def test_func_update(): begin_update_count = 0 begin_update_interval = None def begin_update(domain): nonlocal begin_update_count nonlocal begin_update_interval begin_update_count += 1 begin_update_interval = domain end_update_count = 0 end_update_interval = None def end_update(domain): nonlocal end_update_count nonlocal end_update_interval end_update_count += 1 end_update_interval = domain f = Curve() t = f.add_observer(domain=(0, 2), begin=begin_update, end=end_update) f.begin_update(Interval(1, 3)) assert begin_update_count == 1 assert end_update_count == 0 assert begin_update_interval.start == 1 assert begin_update_interval.end == 3 f.end_update(Interval(1, 3)) assert begin_update_count == 1 assert end_update_count == 1 assert end_update_interval.start == 1 assert end_update_interval.end == 3 f.begin_update(Interval(3, 4)) assert begin_update_count == 1 assert end_update_count == 1 f.end_update(Interval(3, 4)) assert begin_update_count == 1 assert end_update_count == 1 f.remove_observer(t) f.begin_update(Interval(1, 3)) assert begin_update_count == 1 assert end_update_count == 1 f.end_update(Interval(1, 3)) assert begin_update_count == 1 assert end_update_count == 1