mirror of
https://github.com/nonebot/nonebot2.git
synced 2025-01-19 01:18:19 +08:00
✅ add message tests
This commit is contained in:
parent
5fa7806a2f
commit
5abf55d095
@ -1,23 +1,49 @@
|
|||||||
from utils import make_fake_message
|
from utils import make_fake_message
|
||||||
|
|
||||||
|
|
||||||
def test_message_template():
|
def test_segment_add():
|
||||||
from nonebot.adapters import MessageTemplate
|
|
||||||
|
|
||||||
Message = make_fake_message()
|
Message = make_fake_message()
|
||||||
|
MessageSegment = Message.get_segment_class()
|
||||||
|
|
||||||
template = MessageTemplate("{a:custom}{b:text}{c:image}", Message)
|
assert MessageSegment.text("text") + MessageSegment.text("text") == Message(
|
||||||
|
[MessageSegment.text("text"), MessageSegment.text("text")]
|
||||||
|
)
|
||||||
|
|
||||||
@template.add_format_spec
|
assert MessageSegment.text("text") + "text" == Message(
|
||||||
def custom(input: str) -> str:
|
[MessageSegment.text("text"), MessageSegment.text("text")]
|
||||||
return input + "-custom!"
|
)
|
||||||
|
|
||||||
formatted = template.format(a="test", b="test", c="https://example.com/test")
|
assert MessageSegment.text("text") + Message(
|
||||||
assert formatted.extract_plain_text() == "test-custom!test"
|
[MessageSegment.text("text")]
|
||||||
assert str(formatted) == "test-custom!test[fake:image]"
|
) == Message([MessageSegment.text("text"), MessageSegment.text("text")])
|
||||||
|
|
||||||
|
assert "text" + MessageSegment.text("text") == Message(
|
||||||
|
[MessageSegment.text("text"), MessageSegment.text("text")]
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def test_message_slice():
|
def test_message_add():
|
||||||
|
Message = make_fake_message()
|
||||||
|
MessageSegment = Message.get_segment_class()
|
||||||
|
|
||||||
|
assert Message([MessageSegment.text("text")]) + MessageSegment.text(
|
||||||
|
"text"
|
||||||
|
) == Message([MessageSegment.text("text"), MessageSegment.text("text")])
|
||||||
|
|
||||||
|
assert Message([MessageSegment.text("text")]) + "text" == Message(
|
||||||
|
[MessageSegment.text("text"), MessageSegment.text("text")]
|
||||||
|
)
|
||||||
|
|
||||||
|
assert Message([MessageSegment.text("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")])
|
||||||
|
|
||||||
|
|
||||||
|
def test_message_getitem():
|
||||||
|
|
||||||
Message = make_fake_message()
|
Message = make_fake_message()
|
||||||
MessageSegment = Message.get_segment_class()
|
MessageSegment = Message.get_segment_class()
|
||||||
|
17
tests/test_adapters/test_template.py
Normal file
17
tests/test_adapters/test_template.py
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
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]"
|
Loading…
Reference in New Issue
Block a user