-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
251 lines (203 loc) · 7.02 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
from typing import Optional
from arclet.alconna import Alconna, Args, Option
from src.arclet.alconna.tools import (
AlconnaString,
AlconnaFormat,
AlconnaFire,
AlconnaDecorate,
ObjectPattern,
delegate,
exclusion,
cool_down,
simple_type,
ShellTextFormatter,
MarkdownTextFormatter
)
def test_koishi_like():
con = AlconnaString("con <url:url>").build()
assert con.parse("con https://www.example.com").matched is True
con_1 = AlconnaString("con_1")\
.option("foo", "-f <foo:str=123> [bar:bool]")\
.option("--bar", default=True)\
.usage("test USAGE")\
.build()
assert con_1.parse("con_1 --bar").query("bar.value") is True
assert con_1.parse("con_1 --foo").query("foo.args") == {"foo": "123"}
print(con_1.get_help())
con_2 = AlconnaString("[!con_2|/con_2] <foo:str> <...bar>").build()
assert con_2.parse("!con_2 112 334").matched is True
assert con_2.parse("con_2 112 334").matched is False
con_3 = AlconnaString("test")\
.option("writer", "-w <id:int>")\
.option("writer", "--anonymous", default={"id": 0})\
.alias("book")\
.usage("测试")\
.action(lambda id: print(id))\
.build()
assert con_3.parse("test -w 123").query("writer.id") == 123
assert con_3.parse("book --anonymous").query("writer.id") == 0
def test_format_like():
con1 = AlconnaFormat("con1 {title:str} singer {name}")
print('')
print(repr(con1.get_help()))
assert con1.parse("con1 MadWorld singer Nameless").name == "Nameless"
con1_1 = AlconnaFormat("con1_1 user {target}", {"target": str})
assert con1_1.parse("con1_1 user Nameless").query("target") == "Nameless"
con1_2 = AlconnaFormat(
"con1_2 user {target} perm set {key} {default}",
{"target": str, "key": str, "default": Args["default", bool, True]},
)
print(repr(con1_2.get_help()))
res = con1_2.parse("con1_2 user Nameless perm set Admin.set True")
assert res.query("default") is True
assert res.query("key") == "Admin.set"
def test_fire_like_class():
class MyClass:
"""测试从类中构建对象"""
def __init__(self, sender: Optional[str] = None):
"""Constructor"""
self.sender = sender
def talk(self, name="world"):
"""Test Function"""
print(res := f"Hello {name} from {self.sender}")
return res
class MySub:
def __init__(self, name):
self.name = name
def output(self):
print("\tsub output: ", self.name)
class Config:
command = "con2"
description = "测试"
extra = "reject"
print('')
con2 = AlconnaFire(MyClass)
assert con2.parse("con2 Alc talk hhh").matched is True
assert con2.get_result(MyClass.talk) == "Hello hhh from Alc"
assert con2.parse("con2 talk Friend MySub abc output").query("talk.name") == "Friend"
print('')
print(con2.get_help())
print(con2.instance)
def test_fire_like_object():
class MyClass:
def __init__(self, action=sum):
self.action = action
def calculator(self, a, b, c, *nums: int, **kwargs: str):
"""calculator"""
print(a, b, c)
print(nums, kwargs)
print(self.action(nums))
def test(self, a: str, b: int, *, c: bool, d: str):
print(a, b, c, d)
class Config:
command = "con3"
con3 = AlconnaFire(MyClass(sum))
print('')
print(con3.get_help())
assert con3.parse("con3 calculator 1 2 3 4 5 d=6 f=7")
assert con3.parse("con3 test abc 123 -c -d foo")
def test_fire_like_func():
def my_function(name="world"):
"""测试从函数中构建对象"""
class Config:
command = "con4"
description = "测试"
print(f"Hello {name}!")
return name
con4 = AlconnaFire(my_function)
arp = con4.parse("con4 Friend")
assert arp.matched
assert con4.exec_result[my_function.__name__] == "Friend"
def test_delegate():
@delegate
class con5:
"""hello"""
prefix = "!"
print(repr(con5.get_help()))
def test_click_like():
con6 = AlconnaDecorate()
@con6.command("con6")
@con6.option("--count", Args["num", int], help_text="Test Option Count")
@con6.option("--foo", Args["bar", str], help_text="Test Option Foo")
def hello(bar: str, num: int = 1):
"""测试DOC"""
print(bar * num)
assert hello("con6 --foo John --count 2").matched is True
def test_object_pattern():
class A:
def __init__(self, username: str, userid: int):
self.name = username
self.id = userid
pat11 = ObjectPattern(A, flag='urlget')
assert pat11.validate("username=abcd&userid=123").success
def test_checker():
@simple_type()
def hello(num: int):
return num
assert hello(123) == 123
assert hello("123") == 123 # type: ignore
@simple_type()
def test(foo: 'bar'): # type: ignore
return foo
assert test("bar") == "bar"
assert test("foo") is None
def test_exclusion():
com2 = Alconna(
"comp2",
Option("foo"),
Option("bar"),
behaviors=[exclusion(target_path="options.foo", other_path="options.bar")]
)
assert com2.parse("comp2 foo").matched is True
assert com2.parse("comp2 bar").matched is True
assert com2.parse("comp2 foo bar").matched is False
def test_cooldown():
import time
com3 = Alconna("comp3", Args["bar", int], behaviors=[cool_down(0.3)])
print('')
for i in range(4):
time.sleep(0.2)
print(com3.parse(f"comp3 {i}"))
def test_formatter():
from arclet.alconna import Alconna, Args, Option, Subcommand, CommandMeta
alc = Alconna(
"test1", ["!"], Args["foo#abcd", int],
Option("--foo", Args["bar;?", str]),
Option("aaa baz|bar|baf"),
Option("aaa fox"),
Option("aaa bbb fire"),
Subcommand(
"qux",
Args["a"],
Option("aaa"),
Option("bbb", Args["ccc#ddd", bool]["eee#fff", str]),
Subcommand(
"qux",
Args["a"],
Option("aaa"),
Option("bbb", Args["ccc#ddd", bool]["eee#fff", str]),
),
),
formatter_type=MarkdownTextFormatter,
meta=CommandMeta("text1111", "text2222", "text3333")
)
alc.parse("!test1 bbb --help")
alc1 = Alconna(
"test2", ["!"], Args["foo#3322", int],
Option("--foo", Args["bar;?", str]),
Option("aaa baz|bar|baf"),
Option("aaa fox"),
Option("aaa bbb fire"),
Subcommand(
"qux",
Args["a"],
Option("aaa"),
Option("bbb", Args["ccc#ddd", bool]["eee#fff", str]),
),
formatter_type=ShellTextFormatter,
meta=CommandMeta("text1111", "text2222", "text3333")
)
alc1.parse("!test2 bbb --help")
if __name__ == '__main__':
import pytest
pytest.main([__file__, "-vs"])