mirror of
https://github.com/nonebot/nonebot2.git
synced 2025-06-07 04:45:53 +00:00
✅ Add test to prove the fix is valid
This commit is contained in:
parent
28cfa45d95
commit
f4395d77d7
@ -1,7 +1,15 @@
|
|||||||
from utils import make_fake_message
|
from utils import make_fake_message
|
||||||
|
|
||||||
|
|
||||||
def test_message_template():
|
def test_template_basis():
|
||||||
|
from nonebot.adapters import MessageTemplate
|
||||||
|
|
||||||
|
template = MessageTemplate("{key:.3%}")
|
||||||
|
formatted = template.format(key=0.123456789)
|
||||||
|
assert formatted == "12.346%"
|
||||||
|
|
||||||
|
|
||||||
|
def test_template_message():
|
||||||
from nonebot.adapters import MessageTemplate
|
from nonebot.adapters import MessageTemplate
|
||||||
|
|
||||||
Message = make_fake_message()
|
Message = make_fake_message()
|
||||||
@ -12,6 +20,16 @@ def test_message_template():
|
|||||||
def custom(input: str) -> str:
|
def custom(input: str) -> str:
|
||||||
return input + "-custom!"
|
return input + "-custom!"
|
||||||
|
|
||||||
formatted = template.format(a="test", b="test", c="https://example.com/test")
|
try:
|
||||||
assert formatted.extract_plain_text() == "test-custom!test"
|
template.add_format_spec(custom)
|
||||||
assert str(formatted) == "test-custom!test[fake:image]"
|
except ValueError:
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
raise AssertionError("Should raise ValueError")
|
||||||
|
|
||||||
|
format_args = {"a": "custom", "b": "text", "c": "https://example.com/test"}
|
||||||
|
formatted = template.format(**format_args)
|
||||||
|
|
||||||
|
assert template.format_map(format_args) == formatted
|
||||||
|
assert formatted.extract_plain_text() == "custom-custom!text"
|
||||||
|
assert str(formatted) == "custom-custom!text[fake:image]"
|
||||||
|
@ -21,9 +21,9 @@ def make_fake_message() -> Type["Message"]:
|
|||||||
def text(cls, text: str):
|
def text(cls, text: str):
|
||||||
return cls("text", {"text": text})
|
return cls("text", {"text": text})
|
||||||
|
|
||||||
@classmethod
|
@staticmethod
|
||||||
def image(cls, url: str):
|
def image(url: str):
|
||||||
return cls("image", {"url": url})
|
return FakeMessageSegment("image", {"url": url})
|
||||||
|
|
||||||
def is_text(self) -> bool:
|
def is_text(self) -> bool:
|
||||||
return self.type == "text"
|
return self.type == "text"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user