File: test_parser.py

package info (click to toggle)
pyvisa-sim 0.6.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 428 kB
  • sloc: python: 1,749; makefile: 129
file content (57 lines) | stat: -rw-r--r-- 1,941 bytes parent folder | download
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
# -*- 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"})