-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
107 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import pytest | ||
import mock | ||
import sys | ||
|
||
_mock_rpi_ws281x = mock.MagicMock() | ||
channels = {} | ||
|
||
|
||
def ws2811_channel_get(new_ws2811_t, ch): | ||
if ch not in channels: | ||
channels[ch] = {'new_ws2811_t': new_ws2811_t} | ||
return channels[ch] | ||
|
||
|
||
def ws2811_channel_t_count_set(ch, count): | ||
ch['count'] = count | ||
ch['leds'] = [0 for _ in range(count)] | ||
|
||
|
||
def ws2811_channel_t_count_get(ch): | ||
return ch['count'] | ||
|
||
|
||
def ws2811_led_set(ch, n, rgbw): | ||
ch['leds'][n] = rgbw | ||
|
||
|
||
def ws2811_led_get(ch, n): | ||
return ch['leds'][n] | ||
|
||
|
||
_mock_rpi_ws281x.ws2811_channel_t_count_set = ws2811_channel_t_count_set | ||
_mock_rpi_ws281x.ws2811_channel_t_count_get = ws2811_channel_t_count_get | ||
_mock_rpi_ws281x.ws2811_channel_get = ws2811_channel_get | ||
_mock_rpi_ws281x.ws2811_led_set = ws2811_led_set | ||
_mock_rpi_ws281x.ws2811_led_get = ws2811_led_get | ||
|
||
|
||
@pytest.fixture(scope='function', autouse=False) | ||
def _rpi_ws281x(): | ||
_mock_rpi_ws281x.ws2811_init.return_value = 0 | ||
sys.modules['_rpi_ws281x'] = _mock_rpi_ws281x | ||
|
||
yield _mock_rpi_ws281x | ||
del sys.modules['_rpi_ws281x'] | ||
_mock_rpi_ws281x.reset_mock() | ||
channels.clear() | ||
|
||
|
||
@pytest.fixture(scope='function', autouse=True) | ||
def rpi_ws281x(_rpi_ws281x): | ||
if "." not in sys.path: | ||
sys.path.append(".") | ||
yield None | ||
del sys.modules['rpi_ws281x'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import pytest | ||
|
||
|
||
def test_setup(_rpi_ws281x): | ||
from rpi_ws281x import PixelStrip | ||
strip = PixelStrip(10, 20) | ||
strip.begin() | ||
|
||
|
||
def test_setup_init_fail(_rpi_ws281x): | ||
from rpi_ws281x import PixelStrip | ||
_rpi_ws281x.ws2811_init.return_value = 1 | ||
strip = PixelStrip(10, 20) | ||
with pytest.raises(RuntimeError): | ||
strip.begin() | ||
|
||
|
||
def test_num_pixels(_rpi_ws281x): | ||
from rpi_ws281x import PixelStrip | ||
strip = PixelStrip(10, 20) | ||
strip.begin() | ||
assert len(strip[:]) == 10 | ||
assert len(strip) == 10 | ||
assert strip.numPixels() == 10 | ||
|
||
|
||
def test_set_pixel(_rpi_ws281x): | ||
from rpi_ws281x import PixelStrip, RGBW | ||
strip = PixelStrip(10, 20) | ||
strip.begin() | ||
strip[0] = RGBW(255, 0, 0) | ||
assert strip[0] == strip.getPixelColor(0) | ||
assert strip[0] == RGBW(255, 0, 0) | ||
assert strip.getPixelColorRGB(0) == RGBW(255, 0, 0) | ||
assert strip.getPixelColorRGBW(0) == RGBW(255, 0, 0) | ||
assert strip.getPixelColorRGBW(0).r == 255 | ||
|
||
|
||
def test_set_multiple(_rpi_ws281x): | ||
from rpi_ws281x import PixelStrip, RGBW | ||
strip = PixelStrip(10, 20) | ||
strip.begin() | ||
strip[:] = RGBW(255, 0, 0) | ||
assert strip[:] == [RGBW(255, 0, 0)] * 10 | ||
|
||
|
||
def test_set_odd(_rpi_ws281x): | ||
from rpi_ws281x import PixelStrip, RGBW | ||
strip = PixelStrip(10, 20) | ||
strip.begin() | ||
strip[::2] = RGBW(255, 0, 0) | ||
assert strip[:] == [RGBW(255, 0, 0), RGBW(0, 0, 0)] * 5 |