mirror of
https://github.com/nonebot/nonebot2.git
synced 2024-11-30 17:15:08 +08:00
✅ add unit test for message slice
This commit is contained in:
parent
3b4c4d3081
commit
b037be4485
54
tests/test_adapters/test_message.py
Normal file
54
tests/test_adapters/test_message.py
Normal file
@ -0,0 +1,54 @@
|
||||
from utils import make_fake_message
|
||||
|
||||
|
||||
def test_message_template():
|
||||
from nonebot.adapters import MessageTemplate
|
||||
|
||||
Message = make_fake_message()
|
||||
|
||||
template = MessageTemplate("{a:custom}{b:text}{c:image}", Message)
|
||||
|
||||
@template.add_format_spec
|
||||
def custom(input: str) -> str:
|
||||
return input + "-custom!"
|
||||
|
||||
formatted = template.format(a="test", b="test", c="https://example.com/test")
|
||||
assert formatted.extract_plain_text() == "test-custom!test"
|
||||
assert str(formatted) == "test-custom!test[fake:image]"
|
||||
|
||||
|
||||
def test_message_slice():
|
||||
|
||||
Message = make_fake_message()
|
||||
MessageSegment = Message.get_segment_class()
|
||||
|
||||
message = Message(
|
||||
[
|
||||
MessageSegment.text("test"),
|
||||
MessageSegment.image("test2"),
|
||||
MessageSegment.image("test3"),
|
||||
MessageSegment.text("test4"),
|
||||
]
|
||||
)
|
||||
|
||||
assert message[0] == MessageSegment.text("test")
|
||||
|
||||
assert message[0:2] == Message(
|
||||
[MessageSegment.text("test"), MessageSegment.image("test2")]
|
||||
)
|
||||
|
||||
assert message["image"] == Message(
|
||||
[MessageSegment.image("test2"), MessageSegment.image("test3")]
|
||||
)
|
||||
|
||||
assert message["image", 0] == MessageSegment.image("test2")
|
||||
assert message["image", 0:2] == message["image"]
|
||||
|
||||
assert message.index(message[0]) == 0
|
||||
assert message.index("image") == 1
|
||||
|
||||
assert message.get("image") == message["image"]
|
||||
assert message.get("image", 114514) == message["image"]
|
||||
assert message.get("image", 1) == Message([message["image", 0]])
|
||||
|
||||
assert message.count("image") == 2
|
@ -1,17 +0,0 @@
|
||||
from utils import make_fake_message
|
||||
|
||||
|
||||
def test_message_template():
|
||||
from nonebot.adapters import MessageTemplate
|
||||
|
||||
Message = make_fake_message()
|
||||
|
||||
template = MessageTemplate("{a:custom}{b:text}{c:image}", Message)
|
||||
|
||||
@template.add_format_spec
|
||||
def custom(input: str) -> str:
|
||||
return input + "-custom!"
|
||||
|
||||
formatted = template.format(a="test", b="test", c="https://example.com/test")
|
||||
assert formatted.extract_plain_text() == "test-custom!test"
|
||||
assert str(formatted) == "test-custom!test[fake:image]"
|
@ -1,4 +1,4 @@
|
||||
from typing import TYPE_CHECKING, Type, Optional
|
||||
from typing import TYPE_CHECKING, Type, Union, Mapping, Iterable, Optional
|
||||
|
||||
from pydantic import create_model
|
||||
|
||||
@ -34,8 +34,13 @@ def make_fake_message() -> Type["Message"]:
|
||||
return FakeMessageSegment
|
||||
|
||||
@staticmethod
|
||||
def _construct(msg: str):
|
||||
def _construct(msg: Union[str, Iterable[Mapping]]):
|
||||
if isinstance(msg, str):
|
||||
yield FakeMessageSegment.text(msg)
|
||||
else:
|
||||
for seg in msg:
|
||||
yield FakeMessageSegment(**seg)
|
||||
return
|
||||
|
||||
return FakeMessage
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user