Skip to content

Commit

Permalink
add tests for SemanticsDict
Browse files Browse the repository at this point in the history
  • Loading branch information
nobu-g committed Aug 18, 2022
1 parent 699ba51 commit 5b0eaa8
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
4 changes: 1 addition & 3 deletions src/rhoknp/props/semantics.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,8 @@ def from_sstring(cls, sstring: str) -> "SemanticsDict":

def to_sstring(self) -> str:
"""意味情報文字列に変換."""
if self.is_nil:
return self.NIL
if len(self) == 0:
return ""
return "" if self.is_nil is False else self.NIL
return f'"{" ".join(self._item_to_sstring(k, v) for k, v in self.items())}"'

@staticmethod
Expand Down
33 changes: 28 additions & 5 deletions tests/props/test_semantics.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,43 @@
from typing import Any

import pytest

from rhoknp.props import SemanticsDict

CASES = [
{
"sstring": '"代表表記:天気/てんき カテゴリ:抽象物"',
"dict_": {"代表表記": "天気/てんき", "カテゴリ": "抽象物"},
},
{
"sstring": '"代表表記:新/しん 内容語 NE:ORGANIZATION:head"',
"dict_": {"代表表記": "新/しん", "内容語": True, "NE": "ORGANIZATION:head"},
},
{
"sstring": "NIL",
"dict_": {},
},
]


@pytest.mark.parametrize("case", CASES)
def test_from_fstring(case: dict[str, Any]) -> None:
semantics = SemanticsDict.from_sstring(case["sstring"])
assert dict(semantics) == case["dict_"]

@pytest.mark.parametrize("sstring", ['"代表表記:天気/てんき カテゴリ:抽象物"', "NIL"])
def test_from_fstring(sstring: str) -> None:
semantics = SemanticsDict.from_sstring(sstring)
assert semantics.to_sstring() == sstring

@pytest.mark.parametrize("case", CASES)
def test_to_fstring(case: dict[str, Any]) -> None:
semantics = SemanticsDict(case["dict_"], is_nil=True)
assert semantics.to_sstring() == case["sstring"]


def test_false():
assert SemanticsDict._item_to_sstring("sem", False) == ""


def test_empty_dict():
semantics = SemanticsDict()
semantics = SemanticsDict({})
assert semantics.to_sstring() == ""


Expand Down

0 comments on commit 5b0eaa8

Please sign in to comment.