summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBen Connors <benconnors@outlook.com>2019-09-29 17:58:03 -0400
committerBen Connors <benconnors@outlook.com>2019-09-29 17:58:19 -0400
commit08424555f82bad0831e4ffad6cac68950512be8e (patch)
treec9f59407e87a7e0b8f5b41df439f7120e6044205
parent2255278b0e81fc20faff48536f524e1466046c8a (diff)
Some fixes
- Remove print statements - Fix chaser scope and rendering
-rw-r--r--blc2/functions/chaser.py6
-rw-r--r--examples/workspace.xml29
-rw-r--r--tests/conftest.py5
-rw-r--r--tests/test_functions_audio.py24
-rw-r--r--tests/test_functions_chaserstep.py55
5 files changed, 103 insertions, 16 deletions
diff --git a/blc2/functions/chaser.py b/blc2/functions/chaser.py
index fe6fc17..c63f8c4 100644
--- a/blc2/functions/chaser.py
+++ b/blc2/functions/chaser.py
@@ -112,7 +112,6 @@ class Chaser(Function):
def next_steps(self, data):
"""Iterate over the next steps in the chaser."""
n = data.steps[-1].index
- print("Last index", n)
if self._advance_mode == RANDOM:
while True:
yield random.randint(0, len(self.steps)-1)
@@ -140,6 +139,7 @@ class Chaser(Function):
self._steps.insert(step.index, step)
self.w.register_function_delete_callback(step, self._step_deleted, self)
self._fix_indices()
+ self._recalculate()
def move_step(self, step, position):
"""Move a step around."""
@@ -239,7 +239,9 @@ class Chaser(Function):
for n, d in enumerate(data.steps):
s = d.step
slc, sac, data.steps[n] = s.render(t, data=d)
- lc.update(slc)
+ for c, v in slc:
+ if lc[c] < v:
+ lc[c] = v
ac.extend(sac)
return tuple(lc.items()), ac, data
diff --git a/examples/workspace.xml b/examples/workspace.xml
index c30e8ef..257c777 100644
--- a/examples/workspace.xml
+++ b/examples/workspace.xml
@@ -25,17 +25,30 @@
</fixtures>
<functions>
- <function type="Scene" id="0" name="Middle White">
- <value fixture="0" channel="0">127</value>
- <value fixture="0" channel="1">127</value>
- <value fixture="0" channel="2">127</value>
+ <function type="Scene" id="0" name="Red">
+ <value fixture="0" channel="0">255</value>
+ <value fixture="0" channel="1">0</value>
+ <value fixture="0" channel="2">0</value>
</function>
- <function type="Audio" id="1" name="Intro" fade-in="0" fade-out="0" filename="tests/silence.m4a"/>
+ <function type="Scene" id="1" name="Green">
+ <value fixture="0" channel="0">0</value>
+ <value fixture="0" channel="1">255</value>
+ <value fixture="0" channel="2">0</value>
+ </function>
+
+ <function type="Scene" id="2" name="Blue">
+ <value fixture="0" channel="0">0</value>
+ <value fixture="0" channel="1">0</value>
+ <value fixture="0" channel="2">255</value>
+ </function>
+
+ <function type="Audio" id="3" name="Intro" fade-in="0" fade-out="0" filename="tests/silence.m4a"/>
- <function type="Chaser" id="2" name="Chaser 1" advance-mode="Loop">
- <step id="3" name="Step 1" fade-in="10" fade-out="100" duration-mode="Manual" duration="infty" function="0"/>
- <step id="4" name="Step 2" fade-in="500" fade-out="500" duration-mode="Inherit" function="1"/>
+ <function type="Chaser" id="4" name="Chaser 1" advance-mode="Loop">
+ <step id="5" name="Step 1" fade-in="100" fade-out="100" duration-mode="Inherit" function="0"/>
+ <step id="6" name="Step 2" fade-in="100" fade-out="100" duration-mode="Inherit" function="1"/>
+ <step id="7" name="Step 3" fade-in="100" fade-out="100" duration-mode="Inherit" function="2"/>
</function>
</functions>
diff --git a/tests/conftest.py b/tests/conftest.py
index 896a2ba..ebc9855 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -21,19 +21,14 @@ def aws():
def elements_equal(e1, e2):
"""Determine if two XML elements are equal."""
if e1.tag != e2.tag:
- print("tag", e1.tag, e2.tag)
return False
if ("" if e1.text is None else e1.text).strip("\r\n\t ") != ("" if e2.text is None else e2.text).strip("\r\n\t "):
- print("text", repr(e1.text), repr(e2.text))
return False
if ("" if e1.tail is None else e1.tail).strip("\r\n\t ") != ("" if e2.tail is None else e2.tail).strip("\r\n\t "):
- print("tail", repr(e1.tail), repr(e2.tail))
return False
if e1.attrib != e2.attrib:
- print("attrib", repr(e1.attrib), repr(e2.attrib))
return False
if len(e1) != len(e2):
- print("len", len(e1), len(e2))
return False
return all(elements_equal(c1, c2) for c1, c2 in zip(e1, e2))
diff --git a/tests/test_functions_audio.py b/tests/test_functions_audio.py
index 5aaec5a..b6e6c6a 100644
--- a/tests/test_functions_audio.py
+++ b/tests/test_functions_audio.py
@@ -1,9 +1,12 @@
"""Tests for the audio primitive."""
+import xml.etree.ElementTree as et
+
import pytest
from blc2.functions.audio import Audio
from blc2.constants import BXW
+from blc2.exceptions import LoadError
def test_audio(aws):
a = Audio(aws)
@@ -76,3 +79,24 @@ def test_audio_serialize(aws, test_xml_eq):
for case, s in zip(test, success):
s = s.replace('\n', "").format(BXW.strip("{}"))
assert test_xml_eq(case.serialize(), s)
+
+def test_audio_deserialize(aws, test_xml_eq):
+ fail = [
+ """<function xmlns={} type="asdf"/>""",
+ """<asdf/>""",
+ """<function xmlns={} type="Audio" id="asdf"/>""",
+ """<function xmlns={} type="Audio" id="123" name="Test Audio" fade-out="asdf"/>""",
+ """<function xmlns={} type="Audio" id="123" name="Test Audio" fade-in="asdf"/>""",
+ ]
+
+ for case in fail:
+ case = case.format('"'+BXW.strip("{}")+'"')
+ with pytest.raises(LoadError):
+ Audio.deserialize(aws, et.fromstring(case))
+
+ case = """<function xmlns={} type="Audio" id="123" name="Test Audio" fade-in="123" fade-out="321" filename="test.wav"/>"""
+ case = case.format('"'+BXW.strip("{}")+'"')
+
+ s = Audio.deserialize(aws, et.fromstring(case))
+
+ assert test_xml_eq(s.serialize(), case)
diff --git a/tests/test_functions_chaserstep.py b/tests/test_functions_chaserstep.py
index 134b893..6185f46 100644
--- a/tests/test_functions_chaserstep.py
+++ b/tests/test_functions_chaserstep.py
@@ -11,10 +11,17 @@ from blc2.constants import INHERIT, MANUAL, EXTERNAL, INTERNAL, INFTY
class DummyChaser:
def register_step(self, *args, **kwargs):
- return
+ if self.throw:
+ raise Exception("blah")
+ self.count += 1
+
+ def delete_step(self, *args, **kwargs):
+ self.count -= 1
def __init__(self, w):
self.w = w
+ self.count = 0
+ self.throw = False
@pytest.fixture
def cw():
@@ -35,10 +42,24 @@ def test_chaserstep(cw):
a = cw.functions[2]
c0, = cw.fixtures[0].channels
+ ## Try adding a negative index
+ with pytest.raises(Exception):
+ ChaserStep(c, id_=42069, function=s0, duration_mode=INHERIT, index=-10)
+ assert c.count == 0
+ assert 42069 not in cw.functions
+
+ ## Test a failed add
+ with pytest.raises(Exception):
+ c.throw = True
+ ChaserStep(c, id_=42069, function=s0, duration_mode=INHERIT)
+ assert 42069 not in cw.functions
+ c.throw = False
+
## Test how it handles inherit
cs1 = ChaserStep(c, function=s0, duration_mode=INHERIT)
assert cs1.duration == INFTY
assert cs1.actual_duration == INFTY
+ assert cs1.duration_mode == INHERIT
assert cs1.fade_out_mode == s0.fade_out_mode
assert cs1.scope == s0.scope
assert cs1.audio_scope == s0.audio_scope
@@ -122,3 +143,35 @@ def test_chaserstep(cw):
lc, ac, data = cs1.render(3025, data)
assert not lc
assert not ac
+
+ cs1.function = None
+ assert cs1.duration == 0
+ assert cs1.actual_duration == 0
+
+ cs1.fade_in = 100
+ with pytest.raises(ValueError):
+ cs1.fade_in = -100
+ assert cs1.fade_in == 100
+
+ cs1.fade_out = 100
+ with pytest.raises(ValueError):
+ cs1.fade_out = -100
+ assert cs1.fade_out == 100
+
+ cs1.function = s0
+ s0.delete()
+ assert cs1.function is None
+ assert cs1.audio_scope == frozenset()
+ assert cs1.scope == frozenset()
+ cs1.duration_mode = INHERIT
+ with pytest.raises(AttributeError):
+ cs1.duration = 50
+ assert cs1.render(10000, data)[:2] == ((), ())
+
+ cs1.duration_mode = MANUAL
+ cs1.duration = 500
+ with pytest.raises(ValueError):
+ cs1.duration = -100
+ assert cs1.duration == 500
+
+