Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix support for dialogs without response #78

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyvisa_sim/channels.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ def _match_setters(self, query: bytes) -> Optional[OptionalBytes]:
self._selected = parsed["ch_id"]
self._properties[name].set_value(parsed["0"])
else:
self._properties[name].set_value(parsed)
self._properties[name].set_value(parsed) # type: ignore[arg-type]
return response
except ValueError:
if isinstance(error_response, bytes):
Expand Down
2 changes: 1 addition & 1 deletion pyvisa_sim/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def __getitem__(self, key: K) -> V:

def _get_pair(dd: Dict[str, str]) -> Tuple[str, str]:
"""Return a pair from a dialogue dictionary."""
return dd["q"].strip(" "), dd["r"].strip(" ")
return dd["q"].strip(" "), dd["r"].strip(" ") if "r" in dd else NoResponse # type: ignore[return-value]


def _get_triplet(
Expand Down
57 changes: 57 additions & 0 deletions pyvisa_sim/testsuite/test_parser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# -*- coding: utf-8 -*-
from typing import Dict, Tuple

import pytest

from pyvisa_sim import parser
from pyvisa_sim.component import NoResponse, OptionalStr


@pytest.mark.parametrize(
"dialogue_dict, want",
[
({"q": "foo", "r": "bar"}, ("foo", "bar")),
({"q": "foo ", "r": " bar"}, ("foo", "bar")),
({"q": " foo", "r": "bar "}, ("foo", "bar")),
({"q": " foo ", "r": " bar "}, ("foo", "bar")),
# Make sure to support queries that don't have responses
({"q": "foo"}, ("foo", NoResponse)),
# Ignore other keys
({"q": "foo", "bar": "bar"}, ("foo", NoResponse)),
],
)
def test_get_pair(dialogue_dict: Dict[str, str], want: Tuple[str, OptionalStr]) -> None:
got = parser._get_pair(dialogue_dict)
assert got == want


def test_get_pair_requires_query_key() -> None:
with pytest.raises(KeyError):
parser._get_pair({"r": "bar"})


@pytest.mark.parametrize(
"dialogue_dict, want",
[
({"q": "foo", "r": "bar", "e": "baz"}, ("foo", "bar", "baz")),
({"q": "foo ", "r": " bar", "e": " baz "}, ("foo", "bar", "baz")),
({"q": " foo", "r": "bar ", "e": "baz "}, ("foo", "bar", "baz")),
({"q": " foo ", "r": " bar ", "e": " baz"}, ("foo", "bar", "baz")),
# Make sure to support queries that don't have responses
({"q": "foo"}, ("foo", NoResponse, NoResponse)),
({"q": "foo", "r": "bar"}, ("foo", "bar", NoResponse)),
({"q": "foo", "e": "bar"}, ("foo", NoResponse, "bar")),
# Ignore other keys
({"q": "foo", "bar": "bar"}, ("foo", NoResponse, NoResponse)),
],
)
def test_get_triplet(
dialogue_dict: Dict[str, str], want: Tuple[str, OptionalStr, OptionalStr]
) -> None:
got = parser._get_triplet(dialogue_dict)
assert got == want


def test_get_triplet_requires_query_key() -> None:
with pytest.raises(KeyError):
parser._get_triplet({"r": "bar"})