aboutsummaryrefslogtreecommitdiff
path: root/tests/pattern/call.test.py
blob: ff2647645d71639f1f813f86d2c88c1a26e50a70 (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
"""Tests for :meth:`Pattern.__call__` — validators-as-callables."""

from edify import Pattern


def test_call_returns_true_when_pattern_matches_anywhere_in_string():
    contains_digit = Pattern().digit()
    assert contains_digit("hello 42 world") is True


def test_call_returns_false_when_pattern_never_matches():
    contains_digit = Pattern().digit()
    assert contains_digit("hello world") is False


def test_call_delegates_to_test_with_search_semantics():
    only_letters = Pattern().start_of_input().one_or_more().letter().end_of_input()
    assert only_letters("abc") is True
    assert only_letters("abc123") is False


def test_call_result_is_a_bool_not_a_match_object():
    contains_digit = Pattern().digit()
    assert isinstance(contains_digit("42"), bool)


def test_call_reuses_the_cached_regex_across_repeat_calls():
    pattern = Pattern().string("hi")
    first_compiled = pattern.to_regex()
    pattern("hi world")
    pattern("bye world")
    assert pattern.to_regex() is first_compiled