async def test_update_event(faker): loop = asyncio.get_event_loop() listener = Listener() tally = Tally(0) tally.bind_async(loop, on_update=listener.callback) tally.text = 'foo' _, props_changed = await listener.get() assert set(props_changed) == set(['text']) d = dict(rh_tally=TallyColor.RED, txt_tally=TallyColor.GREEN, lh_tally=TallyColor.AMBER) tally.update(**d) _, props_changed = await listener.get() assert set(props_changed) == set(d.keys()) disp = Display(index=0, text=tally.text) tally.update_from_display(disp) assert disp == tally _, props_changed = await listener.get() assert set(props_changed) == set(['rh_tally', 'txt_tally', 'lh_tally']) for tally_type, color in iter_tally_types_and_colors(): attr = tally_type.name should_change = getattr(tally, attr) != color # print(f'{tally_type=}, {color=}, {should_change=}') setattr(tally, attr, color) if should_change: _, props_changed = await listener.get() assert set(props_changed) == set([attr]) else: await asyncio.sleep(.01) assert listener.results.empty()
def test_tally_display_conversion(faker): # i = 0 # if True: for _ in range(100): i = faker.pyint(max_value=0xfffe) disp = Display(index=i) tally = Tally(i) for tally_type, color in iter_tally_types_and_colors(): brightness = faker.pyint(max_value=3) # print(f'{i=}, {tally_type=}, {color=}') disp.brightness = brightness tally.brightness = brightness assert 0 <= tally.normalized_brightness <= 1 assert tally.normalized_brightness == brightness / 3 setattr(tally, tally_type.name, color) setattr(disp, tally_type.name, color) for word in faker.words(3): # print(f'{word=}') tally.text = word disp.text = word assert disp == Tally.from_display(disp) == tally assert disp == Display.from_tally(tally) == tally assert Tally.from_display(disp).normalized_brightness == tally.normalized_brightness