Esempio n. 1
0
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
Esempio n. 2
0
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"
Esempio n. 3
0
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"
Esempio n. 4
0
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"
Esempio n. 5
0
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
Esempio n. 6
0
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"
Esempio n. 7
0
def test_strip_separators():
    strip_separators = StripSeparators()

    matches = Matches()

    m = Match(3, 11, input_string="pre.ABCDEF.post")

    assert m.raw == '.ABCDEF.'
    matches.append(m)

    returned_matches = strip_separators.when(matches, None)
    assert returned_matches == matches

    strip_separators.then(matches, returned_matches, None)

    assert m.raw == 'ABCDEF'
Esempio n. 8
0
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
Esempio n. 9
0
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"]
Esempio n. 10
0
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"]
Esempio n. 11
0
def test_strip_separators_keep_acronyms():
    strip_separators = StripSeparators()

    matches = Matches()

    m = Match(0, 13, input_string=".S.H.I.E.L.D.")
    m2 = Match(0, 22, input_string=".Agent.Of.S.H.I.E.L.D.")

    assert m.raw == '.S.H.I.E.L.D.'
    matches.append(m)
    matches.append(m2)

    returned_matches = strip_separators.when(matches, None)
    assert returned_matches == matches

    strip_separators.then(matches, returned_matches, None)

    assert m.raw == '.S.H.I.E.L.D.'
    assert m2.raw == 'Agent.Of.S.H.I.E.L.D.'
Esempio n. 12
0
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