Skip to content

Commit

Permalink
❇️ Add hooks to MelusineRegex
Browse files Browse the repository at this point in the history
  • Loading branch information
HugoPerrier committed Oct 4, 2024
1 parent 44b8a63 commit bdacb74
Showing 1 changed file with 37 additions and 1 deletion.
38 changes: 37 additions & 1 deletion tests/base/test_melusine_regex.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Dict, List, Optional, Union
from typing import Dict, List, Optional, Union, Any

import pytest

Expand Down Expand Up @@ -152,6 +152,42 @@ def no_match_list(self):
assert regex.negative is None


class PreMatchHookVirusRegex(VirusRegex):

def pre_match_hook(self, text: str) -> str:
text = text.replace("virrrrus", "virus")
return text


def test_pre_match_hook():
reg = PreMatchHookVirusRegex()

bool_match_result = reg.get_match_result("I see a virrrrus !")

assert bool_match_result is True


class PostMatchHookVirusRegex(VirusRegex):

def post_match_hook(self, match_dict: dict[str, Any]) -> dict[str, Any]:
"""Test custom post processing of match data"""
if match_dict[self.MATCH_RESULT] is True:
if "NEUTRAL_MEDICAL_VIRUS" in match_dict[self.NEUTRAL_MATCH_FIELD] and "NEUTRAL_INSECT" in match_dict[self.NEUTRAL_MATCH_FIELD]:
match_dict[self.MATCH_RESULT] = False

return match_dict


def test_post_match_hook():
reg = PostMatchHookVirusRegex()

bool_match_result = reg.get_match_result("I see a virus, a corona virus and a ladybug")
assert bool_match_result is False

bool_match_result = reg.get_match_result("I see a virus and a ladybug")
assert bool_match_result is True


class PairedMatchRegex(MelusineRegex):
"""
Test paired matching.
Expand Down

0 comments on commit bdacb74

Please sign in to comment.