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
|
from __future__ import annotations
from typing import Iterable
from unittest.mock import Mock
from pyartnet.base.channel import Channel
def to_buf(c: Channel, v: Iterable[int], buf: bytearray | None = None) -> bytearray:
c.set_values(v)
assert c.get_values() == list(v)
if buf is None:
buf = bytearray(b'\x00' * 5)
c.to_buffer(buf)
return buf
def test_channel_1b_values_single() -> None:
universe = Mock()
universe.output_correction = None
a = Channel(universe, 1, 1)
assert a.get_values() == [0]
assert to_buf(a, [255]) == b'\xff\x00\x00\x00\x00'
b = Channel(universe, 3, 1)
assert to_buf(b, [255]) == b'\x00\x00\xff\x00\x00'
c = Channel(universe, 5, 1)
buf = bytearray(b'\x00' * 5)
to_buf(a, [0xF0], buf=buf)
to_buf(b, [0xFF], buf=buf)
to_buf(c, [0x0F], buf=buf)
assert buf == b'\xf0\x00\xff\x00\x0f'
def test_channel_1b_values_multiple() -> None:
universe = Mock()
universe.output_correction = None
c = Channel(universe, 1, 3)
assert c.get_values() == [0, 0, 0]
assert to_buf(c, [128, 0, 255]) == b'\x80\x00\xff\x00\x00'
c = Channel(universe, 3, 3)
assert to_buf(c, [128, 0, 255]) == b'\x00\x00\x80\x00\xff'
def test_channel_2b_values_single() -> None:
universe = Mock()
universe.output_correction = None
c = Channel(universe, 1, 1, byte_size=2)
assert c.get_values() == [0]
assert to_buf(c, [65535]) == b'\xff\xff\x00\x00\x00'
assert to_buf(c, [0xF00F]) == b'\x0f\xf0\x00\x00\x00'
c = Channel(universe, 3, 1, byte_size=2)
assert to_buf(c, [0xF00F]) == b'\x00\x00\x0f\xf0\x00'
c = Channel(universe, 4, 1, byte_size=2)
assert to_buf(c, [0xF00F]) == b'\x00\x00\x00\x0f\xf0'
|