From 2cd6867bd1a6f392f485e0a8f0741c38904f8109 Mon Sep 17 00:00:00 2001 From: yanyongyu <42488585+yanyongyu@users.noreply.github.com> Date: Sun, 30 Jan 2022 11:04:02 +0800 Subject: [PATCH] :white_check_mark: add more tests --- nonebot/adapters/_message.py | 10 ++++--- tests/test_adapters/test_message.py | 44 +++++++++++++++++++++++++++-- 2 files changed, 47 insertions(+), 7 deletions(-) diff --git a/nonebot/adapters/_message.py b/nonebot/adapters/_message.py index 21c4a46b..ffeb2bf5 100644 --- a/nonebot/adapters/_message.py +++ b/nonebot/adapters/_message.py @@ -115,7 +115,7 @@ class Message(List[TMS], abc.ABC): elif isinstance(message, Iterable): self.extend(message) else: - self.extend(self._construct(message)) + self.extend(self._construct(message)) # pragma: no cover @classmethod def template(cls: Type[TM], format_string: Union[str, TM]) -> MessageTemplate[TM]: @@ -166,6 +166,8 @@ class Message(List[TMS], abc.ABC): def _validate(cls, value): if isinstance(value, cls): return value + elif isinstance(value, Message): + raise ValueError(f"Type {type(value)} can not be converted to {cls}") elif isinstance(value, str): pass elif isinstance(value, dict): @@ -201,7 +203,7 @@ class Message(List[TMS], abc.ABC): elif isinstance(other, Iterable): self.extend(other) else: - raise ValueError(f"Unsupported type: {type(other)}") + raise ValueError(f"Unsupported type: {type(other)}") # pragma: no cover return self @overload @@ -276,7 +278,7 @@ class Message(List[TMS], abc.ABC): elif isinstance(arg1, str) and isinstance(arg2, slice): return self.__class__([seg for seg in self if seg.type == arg1][arg2]) else: - raise ValueError("Incorrect arguments to slice") + raise ValueError("Incorrect arguments to slice") # pragma: no cover def index(self, value: Union[TMS, str], *args) -> int: if isinstance(value, str): @@ -314,7 +316,7 @@ class Message(List[TMS], abc.ABC): elif isinstance(obj, str): self.extend(self._construct(obj)) else: - raise ValueError(f"Unexpected type: {type(obj)} {obj}") + raise ValueError(f"Unexpected type: {type(obj)} {obj}") # pragma: no cover return self def extend(self: TM, obj: Union[TM, Iterable[TMS]]) -> TM: diff --git a/tests/test_adapters/test_message.py b/tests/test_adapters/test_message.py index a5d88df0..fd6bd5a4 100644 --- a/tests/test_adapters/test_message.py +++ b/tests/test_adapters/test_message.py @@ -39,6 +39,26 @@ def test_segment_validate(): assert True +def test_segment(): + Message = make_fake_message() + MessageSegment = Message.get_segment_class() + + assert len(MessageSegment.text("text")) == 4 + assert MessageSegment.text("text") != MessageSegment.text("other") + assert MessageSegment.text("text").get("data") == {"text": "text"} + assert list(MessageSegment.text("text").keys()) == ["type", "data"] + assert list(MessageSegment.text("text").values()) == ["text", {"text": "text"}] + assert list(MessageSegment.text("text").items()) == [ + ("type", "text"), + ("data", {"text": "text"}), + ] + + origin = MessageSegment.text("text") + copy = origin.copy() + assert origin is not copy + assert origin == copy + + def test_message_add(): Message = make_fake_message() MessageSegment = Message.get_segment_class() @@ -55,6 +75,10 @@ def test_message_add(): [MessageSegment.text("text")] ) == Message([MessageSegment.text("text"), MessageSegment.text("text")]) + assert "text" + Message([MessageSegment.text("text")]) == Message( + [MessageSegment.text("text"), MessageSegment.text("text")] + ) + msg = Message([MessageSegment.text("text")]) msg += MessageSegment.text("text") assert msg == Message([MessageSegment.text("text"), MessageSegment.text("text")]) @@ -101,15 +125,29 @@ def test_message_validate(): Message = make_fake_message() MessageSegment = Message.get_segment_class() + Message_ = make_fake_message() + + assert parse_obj_as(Message, Message([])) == Message([]) + + try: + parse_obj_as(Message, Message_([])) + assert False + except ValidationError: + assert True + assert parse_obj_as(Message, "text") == Message([MessageSegment.text("text")]) assert parse_obj_as(Message, {"type": "text", "data": {"text": "text"}}) == Message( [MessageSegment.text("text")] ) - assert parse_obj_as( - Message, [{"type": "text", "data": {"text": "text"}}] - ) == Message([MessageSegment.text("text")]) + assert ( + parse_obj_as( + Message, + [MessageSegment.text("text"), {"type": "text", "data": {"text": "text"}}], + ) + == Message([MessageSegment.text("text"), MessageSegment.text("text")]) + ) try: parse_obj_as(Message, object())