TestPatternMatch   A
last analyzed

Complexity

Total Complexity 1

Size/Duplication

Total Lines 15
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 15
rs 10
wmc 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A test_match_subgroup() 0 14 1
1
import re
0 ignored issues
show
Coding Style introduced by
This module should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
2
import unittest
3
4
from foil.patterns import (
5
    match_subgroup, match_group, add_regex_start_end, tokenize_group_pattern
6
)
7
8
9
class TestPatternMatch(unittest.TestCase):
10
    def test_match_subgroup(self):
11
        element1 = r'(?P<element1>[A-Z])'
12
        element2 = r'(?P<element2>[0-9])'
13
        pattern = re.compile(element1 + r'_' + element2)
14
        sequence = ['A_1', '1_Z', 'B_2', 'C_#']
15
16
        expected = [
17
            {'element1': 'A', 'element2': '1'},
18
            {'element1': 'B', 'element2': '2'}
19
        ]
20
21
        result = list(match_subgroup(sequence, pattern))
22
23
        self.assertEqual(expected, result)
24
25
26
class TestReHelpers(unittest.TestCase):
27
28
    def test_match_group(self):
29
        result = match_group(['apple', 'orange', 'peach'])
30
        expected = 'apple|orange|peach'
31
32
        self.assertEqual(expected, result)
33
34
    def test_add_regex_start_end(self):
35
        # NOTE: purposely using one positional and one keyword arg in test.
36
        @add_regex_start_end
37
        def foo(currency, country='[A-Z]{2}'):
0 ignored issues
show
introduced by
Black listed name "foo"
Loading history...
38
            return '{currency}_{country}'.format(currency=currency,
39
                                                 country=country)
40
41
        result = foo('USD')
42
        expected = '^USD_[A-Z]{2}$'
43
44
        self.assertEqual(expected, result)
45
46
47
class TestTokenizePattern(unittest.TestCase):
48
49
    def test_tokenize_group_pattern(self):
50
        token_name = 'greeting'
51
        choices = ['hello', 'bonjour']
52
        pattern = tokenize_group_pattern(token_name, choices)
53
54
        expected = {'greeting': 'bonjour'}
55
        result = re.match(pattern, 'bonjour').groupdict()
56
57
        self.assertEqual(expected, result)
58