nonebot2/tests/utils.py

128 lines
3.2 KiB
Python
Raw Normal View History

from collections.abc import Iterable, Mapping
from typing import Optional, Union
from typing_extensions import override
from pydantic import create_model
from nonebot.adapters import Adapter, Bot, Event, Message, MessageSegment
2021-12-16 23:22:25 +08:00
def escape_text(s: str, *, escape_comma: bool = True) -> str:
s = s.replace("&", "&").replace("[", "[").replace("]", "]")
if escape_comma:
s = s.replace(",", ",")
return s
class FakeAdapter(Adapter):
@classmethod
@override
def get_name(cls) -> str:
return "fake"
@override
async def _call_api(self, bot: Bot, api: str, **data):
raise NotImplementedError
class FakeMessageSegment(MessageSegment["FakeMessage"]):
@classmethod
@override
def get_message_class(cls):
return FakeMessage
2021-12-20 00:28:02 +08:00
@override
def __str__(self) -> str:
return self.data["text"] if self.type == "text" else f"[fake:{self.type}]"
2021-12-20 00:28:02 +08:00
@classmethod
def text(cls,