File: test_utils.py

package info (click to toggle)
python-parsel 1.6.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 332 kB
  • sloc: python: 1,467; makefile: 213; sh: 8
file content (38 lines) | stat: -rw-r--r-- 1,406 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
from parsel.utils import shorten, extract_regex

from pytest import mark, raises
import six


@mark.parametrize(
    'width,expected',
    (
        (-1, ValueError),
        (0, u''),
        (1, u'.'),
        (2, u'..'),
        (3, u'...'),
        (4, u'f...'),
        (5, u'fo...'),
        (6, u'foobar'),
        (7, u'foobar'),
    )
)
def test_shorten(width, expected):
    if isinstance(expected, six.string_types):
        assert shorten(u'foobar', width) == expected
    else:
        with raises(expected):
            shorten(u'foobar', width)


@mark.parametrize('regex, text, replace_entities, expected', (
    [r'(?P<month>\w+)\s*(?P<day>\d+)\s*\,?\s*(?P<year>\d+)', 'October  25, 2019', True, ['October', '25', '2019']],
    [r'(?P<month>\w+)\s*(?P<day>\d+)\s*\,?\s*(?P<year>\d+)', 'October  25 2019', True, ['October', '25', '2019']],
    [r'(?P<extract>\w+)\s*(?P<day>\d+)\s*\,?\s*(?P<year>\d+)', 'October  25 2019', True, ['October']],
    [r'\w+\s*\d+\s*\,?\s*\d+', 'October  25 2019', True, ['October  25 2019']],
    [r'^.*$', '&quot;sometext&quot; &amp; &quot;moretext&quot;', True, ['"sometext" &amp; "moretext"']],
    [r'^.*$', '&quot;sometext&quot; &amp; &quot;moretext&quot;', False, ['&quot;sometext&quot; &amp; &quot;moretext&quot;']],
))
def test_extract_regex(regex, text, replace_entities, expected):
    assert extract_regex(regex, text, replace_entities) == expected