def test_comment(): assert compile(Operator('comment', Either([]))) == asm.Literal('') assert compile(Operator('comment', Literal('a'))) == asm.Literal('') assert compile(Operator('comment', Either([Literal('a'), Literal('b')]))) == asm.Literal('') assert compile( Concat([ Macro('#sl'), Operator('comment', Literal('yo')), Macro('#el') ])) == asm.Concat([asm.Boundary('^', None), asm.Boundary('$', None)])
def test_builtin_macros(): assert compile(Macro('#any')) == asm.ANY not_linefeed = asm.CharacterClass([r'\n'], inverted=True) assert compile(Macro('#not_linefeed')) == not_linefeed assert compile(Macro('#nlf')) == not_linefeed assert compile(Macro('#crlf')) == asm.Literal('\r\n') assert compile(Concat([Macro('#sl'), Literal('yo'), Macro('#el')])) == asm.Concat([ asm.Boundary('^', None), asm.Literal('yo'), asm.Boundary('$', None) ])
def test_builtin_macros(): assert compile(Macro('#any')) == asm.ANY not_linefeed = asm.CharacterClass([r'\n'], inverted=True) assert compile(Macro('#not_linefeed')) == not_linefeed assert compile(Macro('#not_linefeed')) == not_linefeed assert compile(Macro('#windows_newline')) == asm.Literal('\r\n') assert compile(Concat([Macro('#sl'), Literal('yo'), Macro('#el')])) == asm.Concat([ asm.Boundary('^', None), asm.Literal('yo'), asm.Boundary('$', None) ]) assert compile(Macro('#quote')) == asm.Literal("'") assert compile(Macro('#double_quote')) == asm.Literal('"') assert compile(Macro('#left_brace')) == asm.Literal("[") assert compile(Macro('#right_brace')) == asm.Literal(']')
def test_def_scoping(): assert compile(Concat([Concat([Macro('#x')]), Def('#x', Literal('x'))])) == asm.Literal('x') with pytest.raises(CompileError): compile(Concat([Concat([Def('#x', Literal('x'))]), Macro('#x')]))
def test_def(): assert compile(Concat([Def('#x', Literal('x')), Macro('#x')])) == asm.Literal('x') assert compile(Concat([Macro('#x'), Def('#x', Literal('x'))])) == asm.Literal('x')
def test_nothing(): assert compile(Nothing()) == asm.Literal('') assert compile(Concat([Nothing(), Nothing()])) == asm.Literal('')
def test_concat(): assert compile(Concat(map(Literal, 'abc'))) == asm.Concat( [asm.Literal('a'), asm.Literal('b'), asm.Literal('c')])