aboutsummaryrefslogtreecommitdiff
path: root/tests/errors/frames.test.py
blob: 0ae0c1bf211a0d343f704844b48651219451d2b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
"""Tests for the structured open-frame stack surfaced by CannotCallSubexpressionError."""

import pytest

from edify import RegexBuilder
from edify.errors.quantifier import DanglingQuantifierError
from edify.errors.structure import CannotCallSubexpressionError


def test_single_open_capture_frame_is_named_in_the_error_message():
    unfinished = RegexBuilder().capture().digit()
    with pytest.raises(CannotCallSubexpressionError) as excinfo:
        unfinished.to_regex_string()
    text = str(excinfo.value)
    assert "1 open frame" in text
    assert "1. .capture()" in text


def test_nested_open_frames_are_listed_innermost_first():
    unfinished = RegexBuilder().capture().group().assert_ahead().digit()
    with pytest.raises(CannotCallSubexpressionError) as excinfo:
        unfinished.to_regex_string()
    text = str(excinfo.value)
    assert "3 open frame" in text
    innermost_index = text.index("1. .assert_ahead()")
    middle_index = text.index("2. .group()")
    outermost_index = text.index("3. .capture()")
    assert innermost_index < middle_index < outermost_index


def test_named_capture_frame_reports_the_declared_group_name():
    unfinished = RegexBuilder().named_capture("username").letter()
    with pytest.raises(CannotCallSubexpressionError) as excinfo:
        unfinished.to_regex_string()
    text = str(excinfo.value)
    assert '.named_capture("username")' in text


def test_open_frames_carry_a_file_line_column_pointer_when_the_context_is_captured():
    unfinished = RegexBuilder().assert_behind().digit()
    with pytest.raises(CannotCallSubexpressionError) as excinfo:
        unfinished.to_regex_string()
    text = str(excinfo.value)
    assert "opened at" in text
    assert __file__ in text or "frames.test.py" in text


def test_subexpression_lists_open_frames_from_the_argument_not_the_receiver():
    unfinished = RegexBuilder().named_capture("group").digit()
    parent = RegexBuilder()
    with pytest.raises(CannotCallSubexpressionError) as excinfo:
        parent.subexpression(unfinished)
    assert 'named_capture("group")' in str(excinfo.value)


def test_dangling_quantifier_reports_the_specific_pending_quantifier_name():
    with pytest.raises(DanglingQuantifierError) as excinfo:
        RegexBuilder().exactly(3).to_regex_string()
    text = str(excinfo.value)
    assert ".exactly(3)" in text


def test_dangling_quantifier_falls_back_to_generic_summary_when_name_unknown():
    error = DanglingQuantifierError()
    text = str(error)
    assert "dangling quantifier" in text