File: test_validators.py

package info (click to toggle)
python-rebulk 3.3.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 752 kB
  • sloc: python: 7,497; makefile: 3
file content (64 lines) | stat: -rw-r--r-- 2,157 bytes parent folder | download
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# pylint: disable=pointless-statement, missing-docstring, invalid-name,len-as-condition

from functools import partial

from rebulk.pattern import StringPattern

from ..validators import chars_before, chars_after, chars_surround, validators

chars = ' _.'
left = partial(chars_before, chars)
right = partial(chars_after, chars)
surrounding = partial(chars_surround, chars)


def test_left_chars():
    matches = list(StringPattern("word", validator=left).matches("xxxwordxxx"))
    assert len(matches) == 0

    matches = list(StringPattern("word", validator=left).matches("xxx_wordxxx"))
    assert len(matches) == 1

    matches = list(StringPattern("word", validator=left).matches("wordxxx"))
    assert len(matches) == 1


def test_right_chars():
    matches = list(StringPattern("word", validator=right).matches("xxxwordxxx"))
    assert len(matches) == 0

    matches = list(StringPattern("word", validator=right).matches("xxxword.xxx"))
    assert len(matches) == 1

    matches = list(StringPattern("word", validator=right).matches("xxxword"))
    assert len(matches) == 1


def test_surrounding_chars():
    matches = list(StringPattern("word", validator=surrounding).matches("xxxword xxx"))
    assert len(matches) == 0

    matches = list(StringPattern("word", validator=surrounding).matches("xxx.wordxxx"))
    assert len(matches) == 0

    matches = list(StringPattern("word", validator=surrounding).matches("xxx word_xxx"))
    assert len(matches) == 1

    matches = list(StringPattern("word", validator=surrounding).matches("word"))
    assert len(matches) == 1


def test_chain():
    matches = list(StringPattern("word", validator=validators(left, right)).matches("xxxword xxx"))
    assert len(matches) == 0

    matches = list(StringPattern("word", validator=validators(left, right)).matches("xxx.wordxxx"))
    assert len(matches) == 0

    matches = list(StringPattern("word", validator=validators(left, right)).matches("xxx word_xxx"))
    assert len(matches) == 1

    matches = list(StringPattern("word", validator=validators(left, right)).matches("word"))
    assert len(matches) == 1