Skip to content

Commit

Permalink
✅ update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
RF-Tar-Railt committed Aug 25, 2023
1 parent 91a21ed commit 515280b
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/nonebot_plugin_alconna/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
"usage": "matcher = on_alconna(...)",
"homepage": "https://github.com/nonebot/plugin-alconna",
"type": "library",
"supported_uniseg": None,
"supported_adapters": None,
"config": Config,
"extra": {
"author": "RF-Tar-Railt",
Expand All @@ -97,7 +97,7 @@
_meta_source["extra"]["homepage"] = _meta_source.pop("homepage")
_meta_source["extra"]["type"] = _meta_source.pop("type")
_meta_source["extra"]["config"] = _meta_source.pop("config")
_meta_source["extra"]["supported_uniseg"] = _meta_source.pop("supported_uniseg")
_meta_source["extra"]["supported_adapters"] = _meta_source.pop("supported_adapters")


__plugin_meta__ = PluginMetadata(**_meta_source)
Expand Down
8 changes: 7 additions & 1 deletion src/nonebot_plugin_alconna/rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

from .config import Config
from .typings import TConvert
from .argv import FallbackMessage
from .model import CompConfig, CommandResult
from .consts import ALCONNA_RESULT, ALCONNA_EXEC_RESULT

Expand All @@ -42,7 +43,7 @@ class AlconnaRule:
use_cmd_start: 是否使用 nb 全局配置里的命令前缀
"""

default_converter: ClassVar[TConvert] = lambda _, x: Message(x) # type: ignore
default_converter: ClassVar[TConvert]

__slots__ = (
"command",
Expand Down Expand Up @@ -264,6 +265,7 @@ def alconna(
output_converter: Optional[TConvert] = None,
comp_config: Optional[CompConfig] = None,
use_origin: bool = False,
use_cmd_start: bool = False,
) -> Rule:
return Rule(
AlconnaRule(
Expand All @@ -273,9 +275,13 @@ def alconna(
output_converter,
comp_config,
use_origin,
use_cmd_start,
)
)


AlconnaRule.default_converter = lambda _, x: FallbackMessage(x)


def set_output_converter(fn: TConvert):
AlconnaRule.default_converter = fn
9 changes: 9 additions & 0 deletions src/nonebot_plugin_alconna/uniseg.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ class Segment:

origin: MessageSegment

def __str__(self):
return f"[{self.__class__.__name__.lower()}]"


@dataclass
class At(Segment):
Expand Down Expand Up @@ -99,6 +102,9 @@ class Reply(Segment):
class Other(Segment):
"""其他 Segment"""

def __str__(self):
return f"[{self.origin.type}]"


_Other = gen_unit(
Other,
Expand Down Expand Up @@ -464,6 +470,9 @@ class UniMessage(List[US]):
def __str__(self) -> str:
return "".join(str(seg) for seg in self)

def __repr__(self) -> str:
return "".join(repr(seg) for seg in self)

@overload
def __getitem__(self, args: Type[TS]) -> "UniMessage[TS]":
"""获取仅包含指定消息段类型的消息
Expand Down
2 changes: 1 addition & 1 deletion src/test/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def test_generic():
from nonebot.adapters.onebot.v11 import Message as Onebot11Message
from nonebot.adapters.onebot.v12 import Message as Onebot12Message

from nonebot_plugin_alconna.adapters import At as GenericAt
from nonebot_plugin_alconna.uniseg import At as GenericAt
from nonebot_plugin_alconna.adapters.onebot11 import At as Onebot11At
from nonebot_plugin_alconna.adapters.onebot11 import Image as Onebot11Image
from nonebot_plugin_alconna.adapters.onebot12 import Image as Onebot12Image
Expand Down
12 changes: 7 additions & 5 deletions src/test/plugins/demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,11 @@
Match,
Query,
Reply,
UniMsg,
Command,
AlconnaMatch,
AlconnaMatcher,
SegMatchResult,
assign,
seg_match,
funcommand,
on_alconna,
image_fetch,
Expand Down Expand Up @@ -68,7 +67,7 @@
)
i18n = on_alconna(Alconna("lang", Args["lang", ["zh_CN", "en_US"]]))
login = on_alconna(Alconna("login", Args["password?", str], Option("-r|--recall")))
bind = on_alconna(Alconna("bind"), seg_match(Reply, remove=True))
bind = on_alconna(Alconna("bind"))

class PipResult(Duplication):
list: SubcommandStub
Expand Down Expand Up @@ -183,8 +182,11 @@ async def login_handle(arp: Arparma):


@bind.handle()
async def bind_handle(reply: Reply = SegMatchResult(Reply)):
await bind.send(str(reply))
async def bind_handle(unimsg: UniMsg):
if unimsg.has(Reply):
reply = unimsg[Reply, 0]
await bind.send(repr(unimsg))
await bind.send(repr(reply))


mask_cmd = on_alconna(
Expand Down
2 changes: 1 addition & 1 deletion src/test/tg.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from nepattern import type_parser
from arclet.alconna import Args, Alconna

from nonebot_plugin_alconna.adapters import Image
from nonebot_plugin_alconna import Image
from nonebot_plugin_alconna.adapters.telegram import Bold, Photo, Underline

msg = "/com" + Bold("mand some_arg") + " " + Underline("some_arg ") + "some_arg"
Expand Down

0 comments on commit 515280b

Please sign in to comment.