def test_color_merge(): t1 = Tally(0) t1.set_color(TallyType.rh_tally | TallyType.lh_tally, TallyColor.RED) assert t1.rh_tally == TallyColor.RED assert t1.txt_tally == TallyColor.OFF assert t1.lh_tally == TallyColor.RED t1.set_color(TallyType.rh_tally | TallyType.lh_tally, TallyColor.GREEN) assert t1.rh_tally == TallyColor.GREEN assert t1.txt_tally == TallyColor.OFF assert t1.lh_tally == TallyColor.GREEN t1.merge_color(TallyType.all_tally, TallyColor.RED) assert t1.rh_tally == TallyColor.AMBER assert t1.txt_tally == TallyColor.RED assert t1.lh_tally == TallyColor.AMBER t1.merge_color(TallyType.all_tally, TallyColor.GREEN) assert t1.rh_tally == TallyColor.AMBER assert t1.txt_tally == TallyColor.AMBER assert t1.lh_tally == TallyColor.AMBER # Reset t1 to OFF, OFF, RED t1.rh_tally = TallyColor.OFF t1.txt_tally = TallyColor.OFF t1.lh_tally = TallyColor.RED # Another Tally with only `txt_tally` set t2 = Tally(1, txt_tally=TallyColor.GREEN) # Only `txt_tally` should change t1.merge(t2, TallyType.all_tally) assert t1.rh_tally == TallyColor.OFF assert t1.txt_tally == TallyColor.GREEN assert t1.lh_tally == TallyColor.RED # Reset t2 to GREEN, RED, GREEN # t1 is still OFF, GREEN, RED t2.rh_tally = TallyColor.GREEN t2.txt_tally = TallyColor.RED t2.lh_tally = TallyColor.GREEN t1.merge(t2, TallyType.rh_tally | TallyType.lh_tally) assert t1.rh_tally == TallyColor.GREEN assert t1.txt_tally == TallyColor.GREEN assert t1.lh_tally == TallyColor.AMBER t1.merge(t2, TallyType.all_tally) assert t1.rh_tally == TallyColor.GREEN assert t1.txt_tally == TallyColor.AMBER assert t1.lh_tally == TallyColor.AMBER t2.rh_tally = TallyColor.RED t1.merge(t2, TallyType.all_tally) assert t1.rh_tally == TallyColor.AMBER assert t1.txt_tally == TallyColor.AMBER assert t1.lh_tally == TallyColor.AMBER
def test_broadcast(faker): for _ in range(1000): i = faker.pyint(max_value=0xfffe) tally = Tally(i) assert not tally.is_broadcast assert not Display.from_tally(tally).is_broadcast tally1 = Tally(0xffff) tally2 = Tally.broadcast() assert tally1.is_broadcast assert tally2.is_broadcast assert Display.from_tally(tally1).is_broadcast assert Display.from_tally(tally2).is_broadcast
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