File: test_processors.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 (215 lines) | stat: -rw-r--r-- 5,974 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# pylint: disable=pointless-statement, missing-docstring, no-member, len-as-condition

from ..pattern import StringPattern, RePattern
from ..processors import ConflictSolver
from ..rules import execute_rule
from ..match import Matches


def test_conflict_1():
    input_string = "abcdefghijklmnopqrstuvwxyz"

    pattern = StringPattern("ijklmn", "kl", "abcdef", "ab", "ef", "yz")
    matches = Matches(pattern.matches(input_string))

    execute_rule(ConflictSolver(), matches, None)

    values = [x.value for x in matches]

    assert values == ["ijklmn", "abcdef", "yz"]


def test_conflict_2():
    input_string = "abcdefghijklmnopqrstuvwxyz"

    pattern = StringPattern("ijklmn", "jklmnopqrst")
    matches = Matches(pattern.matches(input_string))

    execute_rule(ConflictSolver(), matches, None)

    values = [x.value for x in matches]

    assert values == ["jklmnopqrst"]


def test_conflict_3():
    input_string = "abcdefghijklmnopqrstuvwxyz"

    pattern = StringPattern("ijklmnopqrst", "jklmnopqrst")
    matches = Matches(pattern.matches(input_string))

    execute_rule(ConflictSolver(), matches, None)

    values = [x.value for x in matches]

    assert values == ["ijklmnopqrst"]


def test_conflict_4():
    input_string = "123456789"

    pattern = StringPattern("123", "456789")
    matches = Matches(pattern.matches(input_string))

    execute_rule(ConflictSolver(), matches, None)

    values = [x.value for x in matches]
    assert values == ["123", "456789"]


def test_conflict_5():
    input_string = "123456789"

    pattern = StringPattern("123456", "789")
    matches = Matches(pattern.matches(input_string))

    execute_rule(ConflictSolver(), matches, None)

    values = [x.value for x in matches]
    assert values == ["123456", "789"]


def test_prefer_longer_parent():
    input_string = "xxx.1x02.xxx"

    re1 = RePattern("([0-9]+)x([0-9]+)", name='prefer', children=True, formatter=int)
    re2 = RePattern("x([0-9]+)", name='skip', children=True)

    matches = Matches(re1.matches(input_string))
    matches.extend(re2.matches(input_string))

    execute_rule(ConflictSolver(), matches, None)
    assert len(matches) == 2
    assert matches[0].value == 1
    assert matches[1].value == 2


def test_conflict_solver_1():
    input_string = "123456789"

    re1 = StringPattern("2345678", conflict_solver=lambda match, conflicting: '__default__')
    re2 = StringPattern("34567")

    matches = Matches(re1.matches(input_string))
    matches.extend(re2.matches(input_string))

    execute_rule(ConflictSolver(), matches, None)
    assert len(matches) == 1
    assert matches[0].value == "2345678"


def test_conflict_solver_2():
    input_string = "123456789"

    re1 = StringPattern("2345678", conflict_solver=lambda match, conflicting: '__default__')
    re2 = StringPattern("34567", conflict_solver=lambda match, conflicting: conflicting)

    matches = Matches(re1.matches(input_string))
    matches.extend(re2.matches(input_string))

    execute_rule(ConflictSolver(), matches, None)
    assert len(matches) == 1
    assert matches[0].value == "34567"


def test_conflict_solver_3():
    input_string = "123456789"

    re1 = StringPattern("2345678", conflict_solver=lambda match, conflicting: match)
    re2 = StringPattern("34567")

    matches = Matches(re1.matches(input_string))
    matches.extend(re2.matches(input_string))

    execute_rule(ConflictSolver(), matches, None)
    assert len(matches) == 1
    assert matches[0].value == "34567"


def test_conflict_solver_4():
    input_string = "123456789"

    re1 = StringPattern("2345678")
    re2 = StringPattern("34567", conflict_solver=lambda match, conflicting: conflicting)

    matches = Matches(re1.matches(input_string))
    matches.extend(re2.matches(input_string))

    execute_rule(ConflictSolver(), matches, None)
    assert len(matches) == 1
    assert matches[0].value == "34567"


def test_conflict_solver_5():
    input_string = "123456789"

    re1 = StringPattern("2345678", conflict_solver=lambda match, conflicting: conflicting)
    re2 = StringPattern("34567")

    matches = Matches(re1.matches(input_string))
    matches.extend(re2.matches(input_string))

    execute_rule(ConflictSolver(), matches, None)
    assert len(matches) == 1
    assert matches[0].value == "2345678"


def test_conflict_solver_6():
    input_string = "123456789"

    re1 = StringPattern("2345678")
    re2 = StringPattern("34567", conflict_solver=lambda match, conflicting: conflicting)

    matches = Matches(re1.matches(input_string))
    matches.extend(re2.matches(input_string))

    execute_rule(ConflictSolver(), matches, None)
    assert len(matches) == 1
    assert matches[0].value == "34567"


def test_conflict_solver_7():
    input_string = "102"

    re1 = StringPattern("102")
    re2 = StringPattern("02")

    matches = Matches(re2.matches(input_string))
    matches.extend(re1.matches(input_string))

    execute_rule(ConflictSolver(), matches, None)
    assert len(matches) == 1
    assert matches[0].value == "102"


def test_unresolved():
    input_string = "123456789"

    re1 = StringPattern("23456")
    re2 = StringPattern("34567")

    matches = Matches(re1.matches(input_string))
    matches.extend(re2.matches(input_string))

    execute_rule(ConflictSolver(), matches, None)
    assert len(matches) == 2

    re1 = StringPattern("34567")
    re2 = StringPattern("2345678", conflict_solver=lambda match, conflicting: None)

    matches = Matches(re1.matches(input_string))
    matches.extend(re2.matches(input_string))

    execute_rule(ConflictSolver(), matches, None)
    assert len(matches) == 2

    re1 = StringPattern("34567", conflict_solver=lambda match, conflicting: None)
    re2 = StringPattern("2345678")

    matches = Matches(re1.matches(input_string))
    matches.extend(re2.matches(input_string))

    execute_rule(ConflictSolver(), matches, None)
    assert len(matches) == 2