def test_op(): assert v.parse('[op #a]') == C([O('op', M('#a'))]) assert v.parse('[op]') == C([O('op', N())]) assert v.parse('[o p #a]') == C([O('o', O('p', M('#a')))]) with pytest.raises(ParseError): v.parse('[#a op]') with pytest.raises(ParseError): v.parse('[op #a op]') assert v.parse('[!@$%^&*()\n<>?]') == C([O('!@$%^&*()', O('<>?', N()))])
def test_recursive_braces(): assert v.parse('[[]]') == C([]) assert v.parse('[a #d [b #e]]') == C( [O('a', C([M('#d'), O('b', M('#e'))]))]) assert v.parse('[a #d [b #e] [c #f]]') == C( [O('a', C([M('#d'), O('b', M('#e')), O('c', M('#f'))]))]) with pytest.raises(ParseError): v.parse('[op [] op]')
def test_macro_special_chars(): for char in '`~!@$%^&*()-_+,./;:<>?{}\\': for name in char, 'a' + char, char * 3: print name assert v.parse('[#%s]' % name) == C([M('#%s' % name)]), name assert v.parse('[#a\t#b]') == C([M('#a'), M('#b')]) assert v.parse('[#a\n#b]') == C([M('#a'), M('#b')]) assert v.parse('[#a\r#b]') == C([M('#a'), M('#b')])
def test_either(): assert v.parse('[#a | #b]') == C([E([M('#a'), M('#b')])]) assert v.parse('[#a | #b | #c]') == C([E([M('#a'), M('#b'), M('#c')])]) assert v.parse('[op [#a | #b]]') == C([O('op', E([M('#a'), M('#b')]))]) assert v.parse('[op [#a #b | #c]]') == C( [O('op', E([C([M('#a'), M('#b')]), M('#c')]))]) with pytest.raises(ParseError): v.parse('[op #a | #b]') with pytest.raises(ParseError): v.parse('[op #a #b | #c]') with pytest.raises(ParseError): v.parse('[#a | op #b]') assert v.parse('[a #d [b #e] [c #f]]') == C( [O('a', C([M('#d'), O('b', M('#e')), O('c', M('#f'))]))]) with pytest.raises(ParseError): v.parse('[#a|]') with pytest.raises(ParseError): v.parse('[op #a|]') with pytest.raises(ParseError): v.parse('[op | #a]')
def test_range_macros(): assert v.parse('[#a..b]') == C([R('a', 'b')]) assert v.parse('[#0..5]') == C([R('0', '5')]) assert v.parse('[#a..9]') == C([R('a', '9')]) assert v.parse('[#....]') == C([M('#....')])
def test_macro(): assert v.parse('[#a]') == C([M('#a')]) assert v.parse('[#aloHa19]') == C([M('#aloHa19')]) assert v.parse('[#a #b]') == C([M('#a'), M('#b')]) assert v.parse('[ #a ]') == C([M('#a')])
def test_real(): assert v.parse( "[#save_num] Reasons To Switch To re2, The [#save_num]th Made Me [case_insensitive ['Laugh' | 'Cry']][#save_num=[capture 1+ #digit]]" ) == C([ M('#save_num'), L(' Reasons To Switch To re2, The '), M('#save_num'), L('th Made Me '), O('case_insensitive', E([L('Laugh'), L('Cry')])), D('#save_num', O('capture', O('1+', M('#digit')))) ]) assert v.parse("""[ [capture 0-1 #proto] [capture #domain] '.' [capture #tld] [capture #path] #proto=['http' [0-1 's'] '://'] #domain=[1+ [#digit | #lowercase | '.' | '-']] #tld=[2-6 [#lowercase | '.']] #path=['/' [0+ ['/' | #alphanum | '.' | '-']]] ]""") == C([ O('capture', O('0-1', M('#proto'))), O('capture', M('#domain')), L('.'), O('capture', M('#tld')), O('capture', M('#path')), D('#proto', C([L('http'), O('0-1', L('s')), L('://')])), D('#domain', O('1+', E([M('#digit'), M('#lowercase'), L('.'), L('-')]))), D('#tld', O('2-6', E([M('#lowercase'), L('.')]))), D('#path', C([L('/'), O('0+', E([L('/'), M('#alphanum'), L('.'), L('-')]))])) ])
def test_def(): assert v.parse('[#a=[#x]]') == C([D('#a', M('#x'))]) assert v.parse('[#a #a=[#x #y]]') == C( [M('#a'), D('#a', C([M('#x'), M('#y')]))])