mirror of
https://github.com/nonebot/nonebot2.git
synced 2024-11-28 05:16:48 +08:00
✅ add tests for message template
This commit is contained in:
parent
43938a004e
commit
be1915381e
17
tests/adapters/test_template.py
Normal file
17
tests/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]"
|
@ -18,14 +18,18 @@ def make_fake_message() -> Type["Message"]:
|
|||||||
return FakeMessage
|
return FakeMessage
|
||||||
|
|
||||||
def __str__(self) -> str:
|
def __str__(self) -> str:
|
||||||
return self.data["text"]
|
return self.data["text"] if self.type == "text" else f"[fake:{self.type}]"
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def text(cls, text: str):
|
def text(cls, text: str):
|
||||||
return cls("text", {"text": text})
|
return cls("text", {"text": text})
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def image(cls, url: str):
|
||||||
|
return cls("image", {"url": url})
|
||||||
|
|
||||||
def is_text(self) -> bool:
|
def is_text(self) -> bool:
|
||||||
return True
|
return self.type == "text"
|
||||||
|
|
||||||
class FakeMessage(Message):
|
class FakeMessage(Message):
|
||||||
@classmethod
|
@classmethod
|
||||||
|
Loading…
Reference in New Issue
Block a user