def test_raises(): with pytest.raises(CmdParseError): parsed = cmdparse('client <cid:int>', 'client john') with pytest.raises(CmdParseError): parsed = cmdparse('client <cid:float>', 'client john') with pytest.raises(CmdParseError): parsed = cmdparse('client <cid:int>', 'client 24 description')
def test_custom_types_priority(): """ A user (custom) type qualifiers have priorities on build-in type qualifiers. """ cmd = 'newsubscr <cid:float> <alias:slug>[:<passwd>] [<description>]' with pytest.raises(CmdParseError): parsed = cmdparse( cmd, 'newsubscr -23.34 new-subscr:12345 a new subscription', { 'float': r'\d+', 'slug': r'[a-z\-]+' }) parsed = cmdparse(cmd, 'newsubscr 23 new-subscr:12345 a new subscription', { 'float': r'\d+', 'slug': r'[a-z\-]+' })
def parsed_int(): """ A fixture to create parsed with int option. """ return cmdparse( 'newsubscr <cid:int> <alias>[:<passwd>] [<description>]', 'newsubscr 24 myalias:pass12345 A description of a new subscription item.' )
def parsed_str(): """ A fixture to create parsed with int option which is interpreted as str option. """ return cmdparse( 'newsubscr <cid> <alias>[:<passwd>] [<description>]', 'newsubscr 24 myalias:pass12345 A description of a new subscription item.' )
def test_gaps(): """ Test extra gaps. They should be ignored. """ parsed = cmdparse( 'newsubscr <cid>' + ' ' * 3 + '<alias>[:<passwd>]' + ' ' * 20 + '[<description>]', 'newsubscr 24' + ' ' * 30 + 'myalias:pass12345 A description' + ' ' * 10 + 'of a new subscription item.' + ' ' * 9) assert parsed['cid'] == '24' assert parsed['alias'] == 'myalias' assert parsed['passwd'] == 'pass12345' assert parsed['description'] == 'A description of a new subscription item.'
def test_custom_types(): cmd = 'newsubscr <cid:float> <alias:slug>[:<passwd>] [<description>]' parsed = cmdparse(cmd, 'newsubscr -23.34 new-subscr:12345 a new subscription', {'slug': r'[a-z\-]+'}) assert parsed['cid'] == -23.34 assert parsed['alias'] == 'new-subscr' assert parsed['passwd'] == '12345' assert parsed['description'] == 'a new subscription' with pytest.raises(CmdParseError): parsed = cmdparse( cmd, 'newsubscr 24.0 new_subscr:12345 a new subscription', {'slug': r'[a-z\-]+'}) with pytest.raises(CmdFormatError): parsed = cmdparse( cmd, 'newsubscr 24.0 new_subscr:12345 a new subscription') try: parsed = cmdparse( cmd, 'newsubscr 24.0 new_subscr:12345 a new subscription', {'slug': r'[+'}) except CmdCustomTypeError as e: assert e.custom_types == ['slug'] else: raise Exception('Wrong format for "slug" is not detected.')
def test_format_error(): with pytest.raises(CmdFormatError): parsed = cmdparse('client <cid:str>', 'client john')