def test_literal_range(src): parser = Literal(('\x41', '\x5A')) node, start = parser.parse(src, 0) assert node and node.value == src
def test_literal_case_insensitive(value, src): parser = Literal(value) node, start = parser.parse(src, 0) assert node.value == src
def test_empty_literal(): parser = Literal('') assert parser
def test_empty_literal_out_of_bounds(): parser = Literal('') src = 'a' with pytest.raises(ParseError): parser.parse(src, 1)
def test_literal_str(value, expected): parser = Literal(value) assert str(parser) == expected
def test_literal_range_fail(): parser = Literal(('a', 'b')) with pytest.raises(ParseError): parser.parse('c', 0)
def test_literal_range_out_of_bounds(): parser = Literal(('a', 'b')) with pytest.raises(ParseError): parser.parse('a', 1)
def test_Concatenation_str(): parser = Concatenation(Literal('foo'), Literal('bar')) assert str(parser) == "Concatenation(Literal('foo'), Literal('bar'))"
def test_option_str(): parser = Option(Alternation(Literal('foo'))) assert str(parser) == "Option(Alternation(Literal('foo')))"
def test_literal_bad_value(value): with pytest.raises(TypeError): Literal(value)
def test_Alternation_str(): parser = Alternation(Literal('foo'), Literal('bar')) assert str(parser) == "Alternation(Literal('foo'), Literal('bar'))"
def test_repetition_str(): parser = Repetition(Repeat(1, 2), Literal('a')) assert str(parser) == "Repetition(Repeat(1, 2), Literal('a'))"
def test_repetition(): parser = Repetition(Repeat(1, 2), Literal('a')) node, start = parser.parse('aa', 0) assert [x for x in flatten(node)] == [LiteralNode('a', x, 1) for x in range(0, 2)]
def test_alternation(src): parser = Alternation(Literal('foo'), Literal('bar')) node, start = parser.parse(src, 0) assert node.value == src
def test_literal(): parser = Literal('moof') source = 'moof' node, start = parser.parse(source, 0) assert node.value == 'moof'
from abnf.parser import Rule as _Rule, Literal from abnf.grammars.misc import load_grammar_rules class ImportRule(_Rule): pass ImportRule('test', Literal('test')) @load_grammar_rules([('test', ImportRule('test'))]) class Rule(_Rule): """Rules from RFC 5646.""" grammar = [] def test_misc_load_grammar_rules_import(): assert Rule('test').definition == ImportRule('test').definition