Ejemplo n.º 1
0
def test_rules():
    g = lib.Bnf('s-indent(<n)')
    assert g.expr == ("rule", "s-indent", "<n")

    g = lib.Bnf('nb-json')
    assert g.expr == ("rule", "nb-json")

    g = lib.Bnf('s-separate(n,c)')
    assert g.expr == ("rule", "s-separate", "n", "c")
Ejemplo n.º 2
0
def test_star():
    g = lib.Bnf('"a"*')
    assert g.expr == ("repeat", 0, math.inf, "a")
Ejemplo n.º 3
0
def test_opt():
    g = lib.Bnf('"a"?')
    assert g.expr == ("repeat", 0, 1, "a")
Ejemplo n.º 4
0
def test_or():
    g = lib.Bnf('"0" | "9"')
    assert g.expr == {'0', '9'}
Ejemplo n.º 5
0
def test_range():
    g = lib.Bnf('[#x30-#x39]')
    assert g.expr == range(0x30, 0x3A)
Ejemplo n.º 6
0
def test_unicode():
    g = lib.Bnf('#x9')
    assert g.expr == '\x09'
    g = lib.Bnf('#x10FFFF')
    assert g.expr == '\U0010ffff'
Ejemplo n.º 7
0
def test_empty():
    g = lib.Bnf(' ')
    assert g.expr == ('concat', )
Ejemplo n.º 8
0
def test_times_n():
    g = lib.Bnf('"a" × n')
    assert g.expr == ("repeat", "n", "n", "a")
Ejemplo n.º 9
0
def test_str():
    g = lib.Bnf('"y" "a" "m" "l"')
    assert g.expr == ('concat', 'y', 'a', 'm', 'l')
Ejemplo n.º 10
0
def test_char():
    g = lib.Bnf('"c"')
    assert g.expr == 'c'
Ejemplo n.º 11
0
def test_bad_string():
    with pytest.raises(ValueError) as e_info:
        lib.Bnf('"1\' "2"')
    assert '"2"' in str(e_info.value)
    assert 'expected' in str(e_info.value)
Ejemplo n.º 12
0
def test_remaining():
    with pytest.raises(ValueError) as e_info:
        lib.Bnf('"1" ^^garbage')
    assert 'garbage' in str(e_info.value)
    assert 'remaining' in str(e_info.value)
Ejemplo n.º 13
0
def test_comments():
    g = lib.Bnf('[#x41-#x46] /* A-F */ | [#x61-#x66] /* a-f */ ')
    assert g.expr == {range(0x41, 0x47), range(0x61, 0x67)}
Ejemplo n.º 14
0
def test_comment():
    g = lib.Bnf(' /* Empty */ ')
    assert g.expr == ('concat', )
Ejemplo n.º 15
0
def test_plus():
    g = lib.Bnf('"a"+')
    assert g.expr == ("repeat", 1, math.inf, "a")
Ejemplo n.º 16
0
def test_times():
    g = lib.Bnf('"a" × 4')
Ejemplo n.º 17
0
def test_quote():
    g = lib.Bnf(r'"\""')
    assert g.expr == '"'
Ejemplo n.º 18
0
def test_backslash():
    g = lib.Bnf(r'"\\"')
    assert g.expr == '\\'
Ejemplo n.º 19
0
def test_switch():
    g = lib.Bnf('t=a⇒"-" t=b⇒"+"')
    assert g.expr == ("switch", "t", "a", "-", "b", "+")