From 8c220eb491811d514f1ab9401a00f21713c88787 Mon Sep 17 00:00:00 2001 From: onyx-and-iris Date: Wed, 9 Aug 2023 16:37:10 +0100 Subject: [PATCH] refactor target add error test for ValueError test badges updated patch bump --- pyproject.toml | 2 +- tests/banana.svg | 2 +- tests/basic.svg | 2 +- tests/potato.svg | 2 +- tests/test_errors.py | 18 ++++++++++++++---- voicemeeterlib/remote.py | 25 ++++++++++++++++--------- 6 files changed, 34 insertions(+), 17 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index baf6fbc..cfdd9a8 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "voicemeeter-api" -version = "2.4.4" +version = "2.4.5" description = "A Python wrapper for the Voiceemeter API" authors = ["onyx-and-iris "] license = "MIT" diff --git a/tests/banana.svg b/tests/banana.svg index e3ed124..1030af5 100644 --- a/tests/banana.svg +++ b/tests/banana.svg @@ -1 +1 @@ -tests: 158tests158 \ No newline at end of file +tests: 159tests159 \ No newline at end of file diff --git a/tests/basic.svg b/tests/basic.svg index 03ad3e3..5e18ec9 100644 --- a/tests/basic.svg +++ b/tests/basic.svg @@ -1 +1 @@ -tests: 115tests115 \ No newline at end of file +tests: 116tests116 \ No newline at end of file diff --git a/tests/potato.svg b/tests/potato.svg index 729c38c..a1fed37 100644 --- a/tests/potato.svg +++ b/tests/potato.svg @@ -1 +1 @@ -tests: 183tests183 \ No newline at end of file +tests: 184tests184 \ No newline at end of file diff --git a/tests/test_errors.py b/tests/test_errors.py index 65fe3e5..364e10e 100644 --- a/tests/test_errors.py +++ b/tests/test_errors.py @@ -10,14 +10,14 @@ class TestErrors: def test_it_tests_an_unknown_kind(self): with pytest.raises( voicemeeterlib.error.VMError, - match=f"Unknown Voicemeeter kind 'unknown_kind'", + match="Unknown Voicemeeter kind 'unknown_kind'", ): voicemeeterlib.api("unknown_kind") def test_it_tests_an_unknown_parameter(self): with pytest.raises( voicemeeterlib.error.CAPIError, - match=f"VBVMR_SetParameterFloat returned -3", + match="VBVMR_SetParameterFloat returned -3", ) as exc_info: vm.set("unknown.parameter", 1) @@ -25,9 +25,9 @@ class TestErrors: assert e.code == -3 assert e.fn_name == "VBVMR_SetParameterFloat" - def test_it_tests_an_invalid_config(self): + def test_it_tests_an_unknown_config_name(self): EXPECTED_MSG = ( - f"No config with name 'unknown' is loaded into memory", + "No config with name 'unknown' is loaded into memory", f"Known configs: {list(vm.configs.keys())}", ) with pytest.raises(voicemeeterlib.error.VMError) as exc_info: @@ -35,3 +35,13 @@ class TestErrors: e = exc_info.value assert e.message == "\n".join(EXPECTED_MSG) + + def test_it_tests_an_invalid_config_key(self): + CONFIG = { + "strip-0": {"A1": True, "B1": True, "gain": -6.0}, + "bus-0": {"mute": True, "eq": {"on": True}}, + "unknown-0": {"state": True}, + "vban-out-1": {"name": "streamname"}, + } + with pytest.raises(ValueError, match="invalid config key 'unknown'"): + vm.apply(CONFIG) diff --git a/voicemeeterlib/remote.py b/voicemeeterlib/remote.py index 0de6125..a96466c 100644 --- a/voicemeeterlib/remote.py +++ b/voicemeeterlib/remote.py @@ -299,16 +299,23 @@ class Remote(CBindings): minor delay between each recursion """ - def param(key): - obj, m2, *rem = key.split("-") - index = int(m2) if m2.isnumeric() else int(*rem) - if obj in ("strip", "bus", "button"): - return getattr(self, obj)[index] - elif obj == "vban": - return getattr(getattr(self, obj), f"{m2}stream")[index] - raise ValueError(obj) + def target(key): + kls, m2, *rem = key.split("-") + match kls: + case "strip" | "bus" | "button": + index = m2 + target = getattr(self, kls) + case "vban": + dir = f"{m2.rstrip('stream')}stream" + index = rem[0] + target = getattr(self.vban, dir) + case _: + ERR_MSG = f"invalid config key '{kls}'" + self.logger.error(ERR_MSG) + raise ValueError(ERR_MSG) + return target[int(index)] - [param(key).apply(datum).then_wait() for key, datum in data.items()] + [target(key).apply(di).then_wait() for key, di in data.items()] def apply_config(self, name): """applies a config from memory"""