def test_optional():
    """
    Tests optional operator.
    """

    grammar = """
    S: "2" b? "3"?;

    terminals
    b: "1";
    """

    g = Grammar.from_string(grammar)
    assert g.get_nonterminal('b_opt')

    p = Parser(g)

    input_str = '2 1 3'
    result = p.parse(input_str)
    assert result == ["2", "1", "3"]

    input_str = '2 3'
    result = p.parse(input_str)
    assert result == ["2", None, "3"]

    input_str = '2 1'
    result = p.parse(input_str)
    assert result == ["2", "1", None]

    input_str = ' 1 3'
    with pytest.raises(ParseError) as e:
        p.parse(input_str)
    assert 'Expected: 2' in str(e.value)