Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit c9b4758

Browse files
committedNov 15, 2024·
update
1 parent d12ffeb commit c9b4758

File tree

1 file changed

+65
-5
lines changed

1 file changed

+65
-5
lines changed
 

‎tests/test_netargparse.py

+65-5
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,16 @@ def main(args):
3333
nap.parser.add_argument("--var_true", action="store_true")
3434
nap(main, resp_delay=0.2, parse_args=["nap", "--port", "7001"])
3535

36+
def tcp_socket_autoformat_mult_args():
37+
def main(args):
38+
return vars(args)
39+
40+
nap = NetArgumentParser()
41+
nap.parser.add_argument("-x", type=int, nargs="+")
42+
nap.parser.add_argument("-y", type=int, action="append")
43+
nap.parser.add_argument("-z", type=int, nargs="+", action="append")
44+
nap(main, resp_delay=0.2, parse_args=["nap", "--port", "7002"])
45+
3646
def http_no_autoformat():
3747
def main(args):
3848
if args.var_str == "damn":
@@ -43,7 +53,7 @@ def main(args):
4353
nap.parser.add_argument("--var_str", type=str)
4454
nap.parser.add_argument("--var_int", type=int)
4555
nap.parser.add_argument("--var_true", action="store_true")
46-
nap(main, False, 0.2, ["nap", "--port", "7002", "--http"])
56+
nap(main, False, 0.2, ["nap", "--port", "7003", "--http"])
4757

4858
def http_autoformat():
4959
def main(args):
@@ -55,7 +65,17 @@ def main(args):
5565
nap.parser.add_argument("--var_str", type=str)
5666
nap.parser.add_argument("--var_int", type=int)
5767
nap.parser.add_argument("--var_true", action="store_true")
58-
nap(main, resp_delay=0.2, parse_args=["nap", "--port", "7003", "--http"])
68+
nap(main, resp_delay=0.2, parse_args=["nap", "--port", "7004", "--http"])
69+
70+
def http_autoformat_mult_args():
71+
def main(args):
72+
return vars(args)
73+
74+
nap = NetArgumentParser()
75+
nap.parser.add_argument("-x", type=int, nargs="+")
76+
nap.parser.add_argument("-y", type=int, action="append")
77+
nap.parser.add_argument("-z", type=int, nargs="+", action="append")
78+
nap(main, resp_delay=0.2, parse_args=["nap", "--port", "7005", "--http"])
5979

6080

6181
class TcpSocketRequest:
@@ -153,6 +173,16 @@ def test_plain_xml_a_func_exc(self):
153173
self.assertEqual(ans, b"<nap><response></response><exception>division by zero</exception><finished>1</finished></nap>")
154174
self.assertResponse(ans, "xml")
155175

176+
def test_plain_xml_a_ma(self):
177+
ans = s_tcp_a_ma.txrx(b"<nap><_x>1 2 3</_x><_x>11 22 33</_x><_y>1</_y><_y>11</_y><_z>1 2 3</_z><_z>11 22 33</_z></nap>")
178+
self.assertEqual(ans, b"<nap><response><x>[11, 22, 33]</x><y>[1, 11]</y><z>[[1, 2, 3], [11, 22, 33]]</z><_cmd>nap</_cmd></response><exception></exception><finished>1</finished></nap>")
179+
self.assertResponse(ans, "xml")
180+
181+
def test_plain_xml_invalid_a_ma(self):
182+
ans = s_tcp_a_ma.txrx(b"<nap><_x>1 2 3</_x><_x>11 22 33</_x><_y>1 2</_y><_y>11 22</_y><_z>1 2 3</_z><_z>11 22 33</_z></nap>")
183+
self.assertEqual(ans, b"<nap><response></response><exception>unrecognized arguments: 2 22</exception><finished>1</finished></nap>")
184+
self.assertResponse(ans, "xml")
185+
156186
# Plain json, autoformat
157187
def test_plain_json_a_valid_tx(self):
158188
ans = s_tcp_a.txrx(b'{"--var_str": "value", "--var_int": "2"}')
@@ -174,6 +204,21 @@ def test_plain_json_a_func_exc(self):
174204
self.assertEqual(ans, b'{"response": "", "exception": "division by zero", "finished": 1}')
175205
self.assertResponse(ans, "json")
176206

207+
def test_plain_json_a_ma(self):
208+
ans = s_tcp_a_ma.txrx(b'{"-x": ["1 2 3", "11 22 33"], "-y": [1, 11], "-z": ["1 2 3", "11 22 33"]}')
209+
self.assertEqual(ans, b'{"response": {"x": [11, 22, 33], "y": [1, 11], "z": [[1, 2, 3], [11, 22, 33]], "_cmd": "nap"}, "exception": "", "finished": 1}')
210+
self.assertResponse(ans, "json")
211+
212+
def test_plain_json_a_ma_double_key(self):
213+
ans = s_tcp_a_ma.txrx(b'{"-x": "1 2 3", "-x": "11 22 33", "-y": 1, "-y": 11, "-z": "11 22 33", "-z": "11 22 33"}')
214+
self.assertEqual(ans, b'{"response": {"x": [11, 22, 33], "y": [11], "z": [[11, 22, 33]], "_cmd": "nap"}, "exception": "", "finished": 1}')
215+
self.assertResponse(ans, "json")
216+
217+
def test_plain_json_invalid_a_ma(self):
218+
ans = s_tcp_a_ma.txrx(b'{"-x": ["1 2 3", "11 22 33"], "-y": ["1 2", "11 22"], "-z": ["1 2 3", "11 22 33"]}')
219+
self.assertEqual(ans, b'{"response": "", "exception": "unrecognized arguments: 2 22", "finished": 1}')
220+
self.assertResponse(ans, "json")
221+
177222
# HTTP, json resp, no autoformat
178223
def test_http_json_na_valid_tx(self):
179224
ans = s_http_na.txrx("/?--var_str=value&--var_int=2")
@@ -237,6 +282,16 @@ def test_http_json_a_func_exc(self):
237282
self.assertEqual(ans, '{"response": "", "exception": "division by zero", "finished": 1}')
238283
self.assertResponse(ans, "json")
239284

285+
def test_http_json_a_ma(self):
286+
ans = s_http_a_ma.txrx("/?-x=1 2 3&-x=11 22 33&-y=1&-y=11&-z=1 2 3&-z=11 22 33")
287+
self.assertEqual(ans, '{"response": {"x": [11, 22, 33], "y": [1, 11], "z": [[1, 2, 3], [11, 22, 33]], "_cmd": "nap"}, "exception": "", "finished": 1}')
288+
self.assertResponse(ans, "json")
289+
290+
def test_http_json_invalid_a_ma(self):
291+
ans = s_http_a_ma.txrx("/?-x=1 2 3&-x=11 22 33&-y=1 2&-y=11 22&-z=1 2 3&-z=11 22 33")
292+
self.assertEqual(ans, '{"response": "", "exception": "unrecognized arguments: 2 22", "finished": 1}')
293+
self.assertResponse(ans, "json")
294+
240295
# HTTP, xml resp, autoformat
241296
def test_http_xml_a_valid_tx(self):
242297
ans = s_http_a.txrx("/xml?--var_str=value&--var_int=2")
@@ -260,7 +315,8 @@ def test_http_xml_a_func_exc(self):
260315

261316

262317
if __name__ == "__main__":
263-
for t in [tcp_socket_no_autoformat, tcp_socket_autoformat, http_no_autoformat, http_autoformat]:
318+
for t in [tcp_socket_no_autoformat, tcp_socket_autoformat, tcp_socket_autoformat_mult_args,
319+
http_no_autoformat, http_autoformat, http_autoformat_mult_args]:
264320
Thread(target=t, daemon=True).start()
265321

266322
for i in range(10):
@@ -271,10 +327,14 @@ def test_http_xml_a_func_exc(self):
271327
s_tcp_na = TcpSocketRequest(7000)
272328
if not "s_tcp_a" in globals():
273329
s_tcp_a = TcpSocketRequest(7001)
330+
if not "s_tcp_a_ma" in globals():
331+
s_tcp_a_ma = TcpSocketRequest(7002)
274332
if not "s_http_na" in globals():
275-
s_http_na = HttpRequest(7002)
333+
s_http_na = HttpRequest(7003)
276334
if not "s_http_a" in globals():
277-
s_http_a = HttpRequest(7003)
335+
s_http_a = HttpRequest(7004)
336+
if not "s_http_a_ma" in globals():
337+
s_http_a_ma = HttpRequest(7005)
278338
break
279339
except (ConnectionRefusedError, requests.exceptions.ConnectionError):
280340
time.sleep(1)

0 commit comments

Comments
 (0)
Please sign in to comment.