Best JavaScript code snippet using playwright-internal
test_pattern.py
Source:test_pattern.py
...12 input_string = "An Abyssinian fly playing a Celtic violin was annoyed by trashy flags on " \13 "which were the Hebrew letter qoph."14 def test_single(self):15 pattern = StringPattern("Celtic")16 matches = list(pattern.matches(self.input_string))17 assert len(matches) == 118 assert isinstance(matches[0], Match)19 assert matches[0].pattern == pattern20 assert matches[0].span == (28, 34)21 assert matches[0].value == "Celtic"22 def test_repr(self):23 pattern = StringPattern("Celtic")24 assert repr(pattern) == '<StringPattern:(\'Celtic\',)>'25 def test_ignore_case(self):26 pattern = StringPattern("celtic", ignore_case=False)27 matches = list(pattern.matches(self.input_string))28 assert len(matches) == 029 pattern = StringPattern("celtic", ignore_case=True)30 matches = list(pattern.matches(self.input_string))31 assert len(matches) == 132 assert matches[0].value == "Celtic"33 def test_private_names(self):34 pattern = StringPattern("celtic", name="test", private_names=["test"], ignore_case=True)35 matches = list(pattern.matches(self.input_string))36 assert len(matches) == 137 assert matches[0].private38 def test_ignore_names(self):39 pattern = StringPattern("celtic", name="test", ignore_names=["test"], ignore_case=True)40 matches = list(pattern.matches(self.input_string))41 assert len(matches) == 042 def test_no_match(self):43 pattern = StringPattern("Python")44 matches = list(pattern.matches(self.input_string))45 assert not matches46 def test_multiple_patterns(self):47 pattern = StringPattern("playing", "annoyed", "Hebrew")48 matches = list(pattern.matches(self.input_string))49 assert len(matches) == 350 assert isinstance(matches[0], Match)51 assert matches[0].pattern == pattern52 assert matches[0].span == (18, 25)53 assert matches[0].value == "playing"54 assert isinstance(matches[1], Match)55 assert matches[1].pattern == pattern56 assert matches[1].span == (46, 53)57 assert matches[1].value == "annoyed"58 assert isinstance(matches[2], Match)59 assert matches[2].pattern == pattern60 assert matches[2].span == (88, 94)61 assert matches[2].value == "Hebrew"62 def test_start_end_kwargs(self):63 pattern = StringPattern("Abyssinian", start=20, end=40)64 matches = list(pattern.matches(self.input_string))65 assert len(matches) == 066 def test_matches_kwargs(self):67 pattern = StringPattern("Abyssinian", name="test", value="AB")68 matches = list(pattern.matches(self.input_string))69 assert len(matches) == 170 assert matches[0].name == "test"71 assert matches[0].value == "AB"72 def test_post_processor(self):73 def post_processor(matches, pattern):74 assert len(matches) == 175 assert isinstance(pattern, StringPattern)76 return []77 pattern = StringPattern("Abyssinian", name="test", value="AB", post_processor=post_processor)78 matches = list(pattern.matches(self.input_string))79 assert len(matches) == 080class TestRePattern(object):81 """82 Tests for RePattern matching83 """84 input_string = "An Abyssinian fly playing a Celtic violin was annoyed by trashy flags on " \85 "which were the Hebrew letter qoph."86 def test_single_compiled(self):87 pattern = RePattern(re.compile("Celt.?c"))88 matches = list(pattern.matches(self.input_string))89 assert len(matches) == 190 assert isinstance(matches[0], Match)91 assert matches[0].pattern == pattern92 assert matches[0].span == (28, 34)93 assert matches[0].value == "Celtic"94 def test_single_string(self):95 pattern = RePattern("Celt.?c")96 matches = list(pattern.matches(self.input_string))97 assert len(matches) == 198 assert isinstance(matches[0], Match)99 assert matches[0].pattern == pattern100 assert matches[0].span == (28, 34)101 assert matches[0].value == "Celtic"102 def test_single_kwargs(self):103 pattern = RePattern({"pattern": "celt.?c", "flags": re.IGNORECASE})104 matches = list(pattern.matches(self.input_string))105 assert len(matches) == 1106 assert isinstance(matches[0], Match)107 assert matches[0].pattern == pattern108 assert matches[0].span == (28, 34)109 assert matches[0].value == "Celtic"110 def test_single_vargs(self):111 pattern = RePattern(("celt.?c", re.IGNORECASE))112 matches = list(pattern.matches(self.input_string))113 assert len(matches) == 1114 assert isinstance(matches[0], Match)115 assert matches[0].pattern == pattern116 assert matches[0].span == (28, 34)117 assert matches[0].value == "Celtic"118 def test_no_match(self):119 pattern = RePattern("abc.?def")120 matches = list(pattern.matches(self.input_string))121 assert len(matches) == 0122 def test_shortcuts(self):123 pattern = RePattern("Celtic-violin", abbreviations=[("-", r"[\W_]+")])124 matches = list(pattern.matches(self.input_string))125 assert len(matches) == 1126 pattern = RePattern({"pattern": "celtic-violin", "flags": re.IGNORECASE}, abbreviations=[("-", r"[\W_]+")])127 matches = list(pattern.matches(self.input_string))128 assert len(matches) == 1129 def test_multiple_patterns(self):130 pattern = RePattern("pla.?ing", "ann.?yed", "Heb.?ew")131 matches = list(pattern.matches(self.input_string))132 assert len(matches) == 3133 assert isinstance(matches[0], Match)134 assert matches[0].pattern == pattern135 assert matches[0].span == (18, 25)136 assert matches[0].value == "playing"137 assert isinstance(matches[1], Match)138 assert matches[1].pattern == pattern139 assert matches[1].span == (46, 53)140 assert matches[1].value == "annoyed"141 assert isinstance(matches[2], Match)142 assert matches[2].pattern == pattern143 assert matches[2].span == (88, 94)144 assert matches[2].value == "Hebrew"145 def test_unnamed_groups(self):146 pattern = RePattern(r"(Celt.?c)\s+(\w+)")147 matches = list(pattern.matches(self.input_string))148 assert len(matches) == 1149 parent = matches[0]150 assert isinstance(parent, Match)151 assert parent.pattern == pattern152 assert parent.span == (28, 41)153 assert parent.name is None154 assert parent.value == "Celtic violin"155 assert len(parent.children) == 2156 group1, group2 = parent.children157 assert isinstance(group1, Match)158 assert group1.pattern == pattern159 assert group1.span == (28, 34)160 assert group1.name is None161 assert group1.value == "Celtic"162 assert group1.parent == parent163 assert isinstance(group2, Match)164 assert group2.pattern == pattern165 assert group2.span == (35, 41)166 assert group2.name is None167 assert group2.value == "violin"168 assert group2.parent == parent169 def test_named_groups(self):170 pattern = RePattern(r"(?P<param1>Celt.?c)\s+(?P<param2>\w+)")171 matches = list(pattern.matches(self.input_string))172 assert len(matches) == 1173 parent = matches[0]174 assert isinstance(parent, Match)175 assert parent.pattern == pattern176 assert parent.span == (28, 41)177 assert parent.name is None178 assert parent.value == "Celtic violin"179 assert len(parent.children) == 2180 group1, group2 = parent.children181 assert isinstance(group1, Match)182 assert group1.pattern == pattern183 assert group1.span == (28, 34)184 assert group1.name == "param1"185 assert group1.value == "Celtic"186 assert group1.parent == parent187 assert isinstance(group2, Match)188 assert group2.pattern == pattern189 assert group2.span == (35, 41)190 assert group2.name == "param2"191 assert group2.value == "violin"192 assert group2.parent == parent193 def test_children(self):194 pattern = RePattern(r"(?P<param1>Celt.?c)\s+(?P<param2>\w+)", children=True)195 matches = list(pattern.matches(self.input_string))196 assert len(matches) == 2197 group1, group2 = matches198 assert isinstance(group1, Match)199 assert group1.pattern == pattern200 assert group1.span == (28, 34)201 assert group1.name == "param1"202 assert group1.value == "Celtic"203 assert isinstance(group2, Match)204 assert group2.pattern == pattern205 assert group2.span == (35, 41)206 assert group2.name == "param2"207 assert group2.value == "violin"208 def test_children_parent_private(self):209 pattern = RePattern(r"(?P<param1>Celt.?c)\s+(?P<param2>\w+)", children=True, private_parent=True)210 matches = list(pattern.matches(self.input_string))211 assert len(matches) == 3212 parent, group1, group2 = matches213 assert isinstance(group1, Match)214 assert parent.private215 assert parent.pattern == pattern216 assert parent.span == (28, 41)217 assert parent.name is None218 assert parent.value == "Celtic violin"219 assert isinstance(group1, Match)220 assert not group1.private221 assert group1.pattern == pattern222 assert group1.span == (28, 34)223 assert group1.name == "param1"224 assert group1.value == "Celtic"225 assert isinstance(group2, Match)226 assert not group2.private227 assert group2.pattern == pattern228 assert group2.span == (35, 41)229 assert group2.name == "param2"230 assert group2.value == "violin"231 def test_parent_children_private(self):232 pattern = RePattern(r"(?P<param1>Celt.?c)\s+(?P<param2>\w+)", private_children=True)233 matches = list(pattern.matches(self.input_string))234 assert len(matches) == 3235 parent, group1, group2 = matches236 assert isinstance(group1, Match)237 assert not parent.private238 assert parent.pattern == pattern239 assert parent.span == (28, 41)240 assert parent.name is None241 assert parent.value == "Celtic violin"242 assert isinstance(group1, Match)243 assert group1.private244 assert group1.pattern == pattern245 assert group1.span == (28, 34)246 assert group1.name == "param1"247 assert group1.value == "Celtic"248 assert isinstance(group2, Match)249 assert group2.private250 assert group2.pattern == pattern251 assert group2.span == (35, 41)252 assert group2.name == "param2"253 assert group2.value == "violin"254 def test_every(self):255 pattern = RePattern(r"(?P<param1>Celt.?c)\s+(?P<param2>\w+)", every=True)256 matches = list(pattern.matches(self.input_string))257 assert len(matches) == 3258 parent, group1, group2 = matches259 assert isinstance(group1, Match)260 assert not parent.private261 assert parent.pattern == pattern262 assert parent.span == (28, 41)263 assert parent.name is None264 assert parent.value == "Celtic violin"265 assert isinstance(group1, Match)266 assert not group1.private267 assert group1.pattern == pattern268 assert group1.span == (28, 34)269 assert group1.name == "param1"270 assert group1.value == "Celtic"271 assert isinstance(group2, Match)272 assert not group2.private273 assert group2.pattern == pattern274 assert group2.span == (35, 41)275 assert group2.name == "param2"276 assert group2.value == "violin"277 def test_private_names(self):278 pattern = RePattern(r"(?P<param1>Celt.?c)\s+(?P<param2>\w+)", private_names=["param2"], children=True)279 matches = list(pattern.matches(self.input_string))280 assert len(matches) == 2281 assert matches[0].name == "param1"282 assert not matches[0].private283 assert matches[1].name == "param2"284 assert matches[1].private285 def test_ignore_names(self):286 pattern = RePattern(r"(?P<param1>Celt.?c)\s+(?P<param2>\w+)", ignore_names=["param2"], children=True)287 matches = list(pattern.matches(self.input_string))288 assert len(matches) == 1289 assert matches[0].name == "param1"290 def test_matches_kwargs(self):291 pattern = RePattern("He.rew", name="test", value="HE")292 matches = list(pattern.matches(self.input_string))293 assert len(matches) == 1294 assert matches[0].name == "test"295 assert matches[0].value == "HE"296 pattern = RePattern("H(e.)(rew)", name="test", value="HE")297 matches = list(pattern.matches(self.input_string))298 assert len(matches) == 1299 assert matches[0].name == "test"300 assert matches[0].value == "HE"301 children = matches[0].children302 assert len(children) == 2303 assert children[0].name == "test"304 assert children[0].value == "HE"305 assert children[1].name == "test"306 assert children[1].value == "HE"307 pattern = RePattern("H(?P<first>e.)(?P<second>rew)", name="test", value="HE")308 matches = list(pattern.matches(self.input_string))309 assert len(matches) == 1310 assert matches[0].name == "test"311 assert matches[0].value == "HE"312 children = matches[0].children313 assert len(children) == 2314 assert children[0].name == "first"315 assert children[0].value == "HE"316 assert children[1].name == "second"317 assert children[1].value == "HE"318class TestFunctionalPattern(object):319 """320 Tests for FunctionalPattern matching321 """322 input_string = "An Abyssinian fly playing a Celtic violin was annoyed by trashy flags on " \323 "which were the Hebrew letter qoph."324 def test_single_vargs(self):325 def func(input_string):326 i = input_string.find("fly")327 if i > -1:328 return i, i + len("fly"), "fly", "functional"329 pattern = FunctionalPattern(func)330 matches = list(pattern.matches(self.input_string))331 assert len(matches) == 1332 assert isinstance(matches[0], Match)333 assert matches[0].pattern == pattern334 assert matches[0].span == (14, 17)335 assert matches[0].name == "functional"336 assert matches[0].value == "fly"337 def test_single_kwargs(self):338 def func(input_string):339 i = input_string.find("fly")340 if i > -1:341 return {"start": i, "end": i + len("fly"), "name": "functional"}342 pattern = FunctionalPattern(func)343 matches = list(pattern.matches(self.input_string))344 assert len(matches) == 1345 assert isinstance(matches[0], Match)346 assert matches[0].pattern == pattern347 assert matches[0].span == (14, 17)348 assert matches[0].name == "functional"349 assert matches[0].value == "fly"350 def test_multiple_objects(self):351 def func(input_string):352 i = input_string.find("fly")353 matches = []354 if i > -1:355 matches.append((i, i + len("fly"), {'name': "functional"}))356 i = input_string.find("annoyed")357 if i > -1:358 matches.append((i, i + len("annoyed")))359 i = input_string.find("Hebrew")360 if i > -1:361 matches.append({"start": i, "end": i + len("Hebrew")})362 return matches363 pattern = FunctionalPattern(func)364 matches = list(pattern.matches(self.input_string))365 assert len(matches) == 3366 assert isinstance(matches[0], Match)367 assert matches[0].pattern == pattern368 assert matches[0].span == (14, 17)369 assert matches[0].name == "functional"370 assert matches[0].value == "fly"371 assert isinstance(matches[1], Match)372 assert matches[1].pattern == pattern373 assert matches[1].span == (46, 53)374 assert matches[1].value == "annoyed"375 assert isinstance(matches[2], Match)376 assert matches[2].pattern == pattern377 assert matches[2].span == (88, 94)378 assert matches[2].value == "Hebrew"379 def test_multiple_generator(self):380 def func(input_string):381 i = input_string.find("fly")382 if i > -1:383 yield (i, i + len("fly"), {'name': "functional"})384 i = input_string.find("annoyed")385 if i > -1:386 yield (i, i + len("annoyed"))387 i = input_string.find("Hebrew")388 if i > -1:389 yield (i, {"end": i + len("Hebrew")})390 pattern = FunctionalPattern(func)391 matches = list(pattern.matches(self.input_string))392 assert len(matches) == 3393 assert isinstance(matches[0], Match)394 assert matches[0].pattern == pattern395 assert matches[0].span == (14, 17)396 assert matches[0].name == "functional"397 assert matches[0].value == "fly"398 assert isinstance(matches[1], Match)399 assert matches[1].pattern == pattern400 assert matches[1].span == (46, 53)401 assert matches[1].value == "annoyed"402 assert isinstance(matches[2], Match)403 assert matches[2].pattern == pattern404 assert matches[2].span == (88, 94)405 assert matches[2].value == "Hebrew"406 def test_no_match(self):407 pattern = FunctionalPattern(lambda x: None)408 matches = list(pattern.matches(self.input_string))409 assert len(matches) == 0410 def test_multiple_patterns(self):411 def playing(input_string):412 i = input_string.find("playing")413 if i > -1:414 return i, i + len("playing")415 def annoyed(input_string):416 i = input_string.find("annoyed")417 if i > -1:418 return i, i + len("annoyed")419 def hebrew(input_string):420 i = input_string.find("Hebrew")421 if i > -1:422 return i, i + len("Hebrew")423 pattern = FunctionalPattern(playing, annoyed, hebrew)424 matches = list(pattern.matches(self.input_string))425 assert len(matches) == 3426 assert isinstance(matches[0], Match)427 assert matches[0].pattern == pattern428 assert matches[0].span == (18, 25)429 assert matches[0].value == "playing"430 assert isinstance(matches[1], Match)431 assert matches[1].pattern == pattern432 assert matches[1].span == (46, 53)433 assert matches[1].value == "annoyed"434 assert isinstance(matches[2], Match)435 assert matches[2].pattern == pattern436 assert matches[2].span == (88, 94)437 assert matches[2].value == "Hebrew"438 def test_matches_kwargs(self):439 def playing(input_string):440 i = input_string.find("playing")441 if i > -1:442 return i, i + len("playing")443 pattern = FunctionalPattern(playing, name="test", value="PLAY")444 matches = list(pattern.matches(self.input_string))445 assert len(matches) == 1446 assert matches[0].name == "test"447 assert matches[0].value == "PLAY"448class TestValue(object):449 """450 Tests for value option451 """452 input_string = "This string contains 1849 a number"453 def test_str_value(self):454 pattern = StringPattern("1849", name="dummy", value="test")455 matches = list(pattern.matches(self.input_string))456 assert len(matches) == 1457 assert isinstance(matches[0], Match)458 assert matches[0].pattern == pattern459 assert matches[0].span == (21, 25)460 assert matches[0].value == "test"461 def test_dict_child_value(self):462 pattern = RePattern(r"(?P<strParam>cont.?ins)\s+(?P<intParam>\d+)",463 formatter={'intParam': lambda x: int(x) * 2,464 'strParam': lambda x: "really " + x},465 format_all=True,466 value={'intParam': 'INT_PARAM_VALUE'})467 matches = list(pattern.matches(self.input_string))468 assert len(matches) == 1469 parent = matches[0]470 assert len(parent.children) == 2471 group1, group2 = parent.children472 assert isinstance(group1, Match)473 assert group1.pattern == pattern474 assert group1.span == (12, 20)475 assert group1.value == "really contains"476 assert isinstance(group2, Match)477 assert group2.pattern == pattern478 assert group2.span == (21, 25)479 assert group2.value == 'INT_PARAM_VALUE'480 def test_dict_default_value(self):481 pattern = RePattern(r"(?P<strParam>cont.?ins)\s+(?P<intParam>\d+)",482 formatter={'intParam': lambda x: int(x) * 2,483 'strParam': lambda x: "really " + x},484 format_all=True,485 value={'__children__': 'CHILD', 'strParam': 'STR_VALUE', '__parent__': 'PARENT'})486 matches = list(pattern.matches(self.input_string))487 assert len(matches) == 1488 parent = matches[0]489 assert parent.value == "PARENT"490 assert len(parent.children) == 2491 group1, group2 = parent.children492 assert isinstance(group1, Match)493 assert group1.pattern == pattern494 assert group1.span == (12, 20)495 assert group1.value == "STR_VALUE"496 assert isinstance(group2, Match)497 assert group2.pattern == pattern498 assert group2.span == (21, 25)499 assert group2.value == "CHILD"500class TestFormatter(object):501 """502 Tests for formatter option503 """504 input_string = "This string contains 1849 a number"505 def test_single_string(self):506 pattern = StringPattern("1849", name="dummy", formatter=lambda x: int(x) / 2)507 matches = list(pattern.matches(self.input_string))508 assert len(matches) == 1509 assert isinstance(matches[0], Match)510 assert matches[0].pattern == pattern511 assert matches[0].span == (21, 25)512 assert matches[0].value == 1849 / 2513 def test_single_re_no_group(self):514 pattern = RePattern(r"\d+", formatter=lambda x: int(x) * 2)515 matches = list(pattern.matches(self.input_string))516 assert len(matches) == 1517 assert isinstance(matches[0], Match)518 assert matches[0].pattern == pattern519 assert matches[0].span == (21, 25)520 assert matches[0].value == 1849 * 2521 def test_single_re_named_groups(self):522 pattern = RePattern(r"(?P<strParam>cont.?ins)\s+(?P<intParam>\d+)",523 formatter={'intParam': lambda x: int(x) * 2,524 'strParam': lambda x: "really " + x}, format_all=True)525 matches = list(pattern.matches(self.input_string))526 assert len(matches) == 1527 parent = matches[0]528 assert len(parent.children) == 2529 group1, group2 = parent.children530 assert isinstance(group1, Match)531 assert group1.pattern == pattern532 assert group1.span == (12, 20)533 assert group1.value == "really contains"534 assert isinstance(group2, Match)535 assert group2.pattern == pattern536 assert group2.span == (21, 25)537 assert group2.value == 1849 * 2538 def test_repeated_captures_option(self):539 pattern = RePattern(r"\[(\d+)\](?:-(\d+))*")540 matches = list(pattern.matches("[02]-03-04-05-06"))541 assert len(matches) == 1542 match = matches[0]543 if REGEX_AVAILABLE:544 assert len(match.children) == 5545 assert [child.value for child in match.children] == ["02", "03", "04", "05", "06"]546 else:547 assert len(match.children) == 2548 assert [child.value for child in match.children] == ["02", "06"]549 with pytest.raises(NotImplementedError):550 RePattern(r"\[(\d+)\](?:-(\d+))*", repeated_captures=True)551 pattern = RePattern(r"\[(\d+)\](?:-(\d+))*", repeated_captures=False)552 matches = list(pattern.matches("[02]-03-04-05-06"))553 assert len(matches) == 1554 match = matches[0]555 assert len(match.children) == 2556 assert [child.value for child in match.children] == ["02", "06"]557 def test_single_functional(self):558 def digit(input_string):559 i = input_string.find("1849")560 if i > -1:561 return i, i + len("1849")562 pattern = FunctionalPattern(digit, formatter=lambda x: int(x) * 3)563 matches = list(pattern.matches(self.input_string))564 assert len(matches) == 1565 assert isinstance(matches[0], Match)566 assert matches[0].pattern == pattern567 assert matches[0].span == (21, 25)568 assert matches[0].value == 1849 * 3569class TestValidator(object):570 """571 Tests for validator option572 """573 input_string = "This string contains 1849 a number"574 @staticmethod575 def true_validator(match):576 return int(match.value) < 1850577 @staticmethod578 def false_validator(match):579 return int(match.value) >= 1850580 def test_single_string(self):581 pattern = StringPattern("1849", name="dummy", validator=self.false_validator)582 matches = list(pattern.matches(self.input_string))583 assert len(matches) == 0584 pattern = StringPattern("1849", validator=self.true_validator)585 matches = list(pattern.matches(self.input_string))586 assert len(matches) == 1587 def test_single_re_no_group(self):588 pattern = RePattern(r"\d+", validator=self.false_validator)589 matches = list(pattern.matches(self.input_string))590 assert len(matches) == 0591 pattern = RePattern(r"\d+", validator=self.true_validator)592 matches = list(pattern.matches(self.input_string))593 assert len(matches) == 1594 def test_single_re_named_groups(self):595 pattern = RePattern(r"(?P<strParam>cont.?ins)\s+(?P<intParam>\d+)",596 validator={'intParam': self.false_validator}, validate_all=True)597 matches = list(pattern.matches(self.input_string))598 assert len(matches) == 0599 pattern = RePattern(r"(?P<strParam>cont.?ins)\s+(?P<intParam>\d+)",600 validator={'intParam': self.true_validator}, validate_all=True)601 matches = list(pattern.matches(self.input_string))602 assert len(matches) == 1603 def test_validate_all(self):604 pattern = RePattern(r"contains (?P<intParam>\d+)", formatter=int, validator=lambda match: match.value < 100,605 children=True)606 matches = list(pattern.matches(self.input_string))607 assert len(matches) == 0608 pattern = RePattern(r"contains (?P<intParam>\d+)", formatter=int, validator=lambda match: match.value > 100,609 children=True)610 matches = list(pattern.matches(self.input_string))611 assert len(matches) == 1612 def invalid_func(match):613 if match.name == 'intParam':614 return True615 return match.value.startswith('abc')616 pattern = RePattern(r"contains (?P<intParam>\d+)", formatter=int, validator=invalid_func, validate_all=True,617 children=True)618 matches = list(pattern.matches(self.input_string))619 assert len(matches) == 0620 def func(match):621 if match.name == 'intParam':622 return True623 return match.value.startswith('contains')624 pattern = RePattern(r"contains (?P<intParam>\d+)", formatter=int, validator=func, validate_all=True,625 children=True)626 matches = list(pattern.matches(self.input_string))627 assert len(matches) == 1628 def test_format_all(self):629 pattern = RePattern(r"contains (?P<intParam>\d+)", formatter=int,630 children=True)631 matches = list(pattern.matches(self.input_string))632 assert len(matches) == 1633 for match in matches:634 assert match.value is not None635 with pytest.raises(ValueError):636 pattern = RePattern(r"contains (?P<intParam>\d+)", formatter=int, format_all=True)637 matches = list(pattern.matches(self.input_string))638 for match in matches:639 assert match.value is not None640 def test_single_functional(self):641 def digit(input_string):642 i = input_string.find("1849")643 if i > -1:644 return i, i + len("1849")645 pattern = FunctionalPattern(digit, validator=self.false_validator)646 matches = list(pattern.matches(self.input_string))647 assert len(matches) == 0648 pattern = FunctionalPattern(digit, validator=self.true_validator)649 matches = list(pattern.matches(self.input_string))...
test_match.py
Source:test_match.py
...225class TestMaches(object):226 def test_names(self):227 input_string = "One Two Three"228 matches = Matches()229 matches.extend(StringPattern("One", name="1-str", tags=["One", "str"]).matches(input_string))230 matches.extend(RePattern("One", name="1-re", tags=["One", "re"]).matches(input_string))231 matches.extend(StringPattern("Two", name="2-str", tags=["Two", "str"]).matches(input_string))232 matches.extend(RePattern("Two", name="2-re", tags=["Two", "re"]).matches(input_string))233 matches.extend(StringPattern("Three", name="3-str", tags=["Three", "str"]).matches(input_string))234 matches.extend(RePattern("Three", name="3-re", tags=["Three", "re"]).matches(input_string))235 assert set(matches.names) == set(["1-str", "1-re", "2-str", "2-re", "3-str", "3-re"])236 def test_filters(self):237 input_string = "One Two Three"238 matches = Matches()239 matches.extend(StringPattern("One", name="1-str", tags=["One", "str"]).matches(input_string))240 matches.extend(RePattern("One", name="1-re", tags=["One", "re"]).matches(input_string))241 matches.extend(StringPattern("Two", name="2-str", tags=["Two", "str"]).matches(input_string))242 matches.extend(RePattern("Two", name="2-re", tags=["Two", "re"]).matches(input_string))243 matches.extend(StringPattern("Three", name="3-str", tags=["Three", "str"]).matches(input_string))244 matches.extend(RePattern("Three", name="3-re", tags=["Three", "re"]).matches(input_string))245 selection = matches.starting(0)246 assert len(selection) == 2247 selection = matches.starting(0, lambda m: "str" in m.tags)248 assert len(selection) == 1249 assert selection[0].pattern.name == "1-str"250 selection = matches.ending(7, predicate=lambda m: "str" in m.tags)251 assert len(selection) == 1252 assert selection[0].pattern.name == "2-str"253 selection = matches.previous(matches.named("2-str")[0])254 assert len(selection) == 2255 assert selection[0].pattern.name == "1-str"256 assert selection[1].pattern.name == "1-re"257 selection = matches.previous(matches.named("2-str", 0), lambda m: "str" in m.tags)258 assert len(selection) == 1259 assert selection[0].pattern.name == "1-str"260 selection = matches.next(matches.named("2-str", 0))261 assert len(selection) == 2262 assert selection[0].pattern.name == "3-str"263 assert selection[1].pattern.name == "3-re"264 selection = matches.next(matches.named("2-str", 0), index=0, predicate=lambda m: "re" in m.tags)265 assert selection is not None266 assert selection.pattern.name == "3-re"267 selection = matches.next(matches.named("2-str", index=0), lambda m: "re" in m.tags)268 assert len(selection) == 1269 assert selection[0].pattern.name == "3-re"270 selection = matches.named("2-str", lambda m: "re" in m.tags)271 assert len(selection) == 0272 selection = matches.named("2-re", lambda m: "re" in m.tags, 0)273 assert selection is not None274 assert selection.name == "2-re" # pylint:disable=no-member275 selection = matches.named("2-re", lambda m: "re" in m.tags)276 assert len(selection) == 1277 assert selection[0].name == "2-re"278 selection = matches.named("2-re", lambda m: "re" in m.tags, index=1000)279 assert selection is None280 def test_raw(self):281 input_string = "0123456789"282 match = Match(0, 10, input_string=input_string, formatter=lambda s: s*2)283 assert match.value == match.raw * 2284 assert match.raw == input_string285 match.raw_end = 9286 match.raw_start = 1287 assert match.value == match.raw * 2288 assert match.raw == input_string[1:9]289 match.raw_end = None290 match.raw_start = None291 assert match.value == match.raw * 2292 assert match.raw == input_string293 def test_formatter_chain(self):294 input_string = "100"295 match = Match(0, 3, input_string=input_string, formatter=formatters(int, lambda s: s*2, lambda s: s+10))296 assert match.raw == input_string297 assert match.value == 100 * 2 + 10298 def test_to_dict(self):299 input_string = "One Two Two Three"300 matches = Matches()301 matches.extend(StringPattern("One", name="1", tags=["One", "str"]).matches(input_string))302 matches.extend(RePattern("One", name="1", tags=["One", "re"]).matches(input_string))303 matches.extend(StringPattern("Two", name="2", tags=["Two", "str"]).matches(input_string))304 matches.extend(RePattern("Two", name="2", tags=["Two", "re"]).matches(input_string))305 matches.extend(RePattern("Two", name="2", tags=["Two", "reBis"]).matches(input_string))306 matches.extend(StringPattern("Three", name="3", tags=["Three", "str"]).matches(input_string))307 matches.extend(RePattern("Three", name="3bis", tags=["Three", "re"]).matches(input_string))308 matches.extend(RePattern(r"(\w+)", name="words").matches(input_string))309 kvalues = matches.to_dict(first_value=True)310 assert kvalues == {"1": "One",311 "2": "Two",312 "3": "Three",313 "3bis": "Three",314 "words": "One"}315 assert kvalues.values_list["words"] == ["One", "Two", "Three"]316 kvalues = matches.to_dict(enforce_list=True)317 assert kvalues["words"] == ["One", "Two", "Three"]318 kvalues = matches.to_dict(details=True)319 assert kvalues["1"].value == "One"320 assert len(kvalues["2"]) == 2321 assert kvalues["2"][0].value == "Two"322 assert kvalues["2"][1].value == "Two"323 assert kvalues["3"].value == "Three"324 assert kvalues["3bis"].value == "Three"325 assert len(kvalues["words"]) == 4326 assert kvalues["words"][0].value == "One"327 assert kvalues["words"][1].value == "Two"328 assert kvalues["words"][2].value == "Two"329 assert kvalues["words"][3].value == "Three"330 kvalues = matches.to_dict(details=True)331 assert kvalues["1"].value == "One"332 assert len(kvalues.values_list["2"]) == 2333 assert kvalues.values_list["2"][0].value == "Two"334 assert kvalues.values_list["2"][1].value == "Two"335 assert kvalues["3"].value == "Three"336 assert kvalues["3bis"].value == "Three"337 assert len(kvalues.values_list["words"]) == 4338 assert kvalues.values_list["words"][0].value == "One"339 assert kvalues.values_list["words"][1].value == "Two"340 assert kvalues.values_list["words"][2].value == "Two"341 assert kvalues.values_list["words"][3].value == "Three"342 def test_chains(self):343 input_string = "wordX 10 20 30 40 wordA, wordB, wordC 70 80 wordX"344 matches = Matches(input_string=input_string)345 matches.extend(RePattern(r"\d+", name="digit").matches(input_string))346 matches.extend(RePattern("[a-zA-Z]+", name="word").matches(input_string))347 assert len(matches) == 11348 a_start = input_string.find('wordA')349 b_start = input_string.find('wordB')350 b_end = b_start + len('wordB')351 c_start = input_string.find('wordC')352 c_end = c_start + len('wordC')353 chain_before = matches.chain_before(b_start, " ,", predicate=lambda match: match.name == "word")354 assert len(chain_before) == 1355 assert chain_before[0].value == 'wordA'356 chain_before = matches.chain_before(Match(b_start, b_start), " ,", predicate=lambda match: match.name == "word")357 assert len(chain_before) == 1358 assert chain_before[0].value == 'wordA'359 chain_before = matches.chain_before(b_start, " ,", predicate=lambda match: match.name == "digit")360 assert len(chain_before) == 0361 chain_before = matches.chain_before(a_start, " ,", predicate=lambda match: match.name == "digit")362 assert len(chain_before) == 4363 assert [match.value for match in chain_before] == ["40", "30", "20", "10"]364 chain_after = matches.chain_after(b_end, " ,", predicate=lambda match: match.name == "word")365 assert len(chain_after) == 1366 assert chain_after[0].value == 'wordC'367 chain_after = matches.chain_after(Match(b_end, b_end), " ,", predicate=lambda match: match.name == "word")368 assert len(chain_after) == 1369 assert chain_after[0].value == 'wordC'370 chain_after = matches.chain_after(b_end, " ,", predicate=lambda match: match.name == "digit")371 assert len(chain_after) == 0372 chain_after = matches.chain_after(c_end, " ,", predicate=lambda match: match.name == "digit")373 assert len(chain_after) == 2374 assert [match.value for match in chain_after] == ["70", "80"]375 chain_after = matches.chain_after(c_end, " ,", end=10000, predicate=lambda match: match.name == "digit")376 assert len(chain_after) == 2377 assert [match.value for match in chain_after] == ["70", "80"]378 def test_holes(self):379 input_string = '1'*10+'2'*10+'3'*10+'4'*10+'5'*10+'6'*10+'7'*10380 hole1 = Match(0, 10, input_string=input_string)381 hole2 = Match(20, 30, input_string=input_string)382 hole3 = Match(30, 40, input_string=input_string)383 hole4 = Match(60, 70, input_string=input_string)384 matches = Matches([hole1, hole2], input_string=input_string)385 matches.append(hole3)386 matches.append(hole4)387 holes = list(matches.holes())388 assert len(holes) == 2389 assert holes[0].span == (10, 20)390 assert holes[0].value == '2'*10391 assert holes[1].span == (40, 60)392 assert holes[1].value == '5' * 10 + '6' * 10393 holes = list(matches.holes(5, 15))394 assert len(holes) == 1395 assert holes[0].span == (10, 15)396 assert holes[0].value == '2'*5397 holes = list(matches.holes(5, 15, formatter=lambda value: "formatted"))398 assert len(holes) == 1399 assert holes[0].span == (10, 15)400 assert holes[0].value == "formatted"401 holes = list(matches.holes(5, 15, predicate=lambda hole: False))402 assert len(holes) == 0403 def test_holes_empty(self):404 input_string = "Test hole on empty matches"405 matches = Matches(input_string=input_string)406 holes = matches.holes()407 assert len(holes) == 1408 assert holes[0].value == input_string409 def test_holes_seps(self):410 input_string = "Test hole - with many separators + included"411 match = StringPattern("many").matches(input_string)412 matches = Matches(match, input_string)413 holes = matches.holes()414 assert len(holes) == 2415 holes = matches.holes(seps="-+")416 assert len(holes) == 4...
test_rebulk.py
Source:test_rebulk.py
...13 if i > -1:14 return i, i + len("over")15 rebulk.functional(func)16 input_string = "The quick brown fox jumps over the lazy dog"17 matches = rebulk.matches(input_string)18 assert len(matches) == 319 assert matches[0].value == "quick"20 assert matches[1].value == "fox"21 assert matches[2].value == "over"22def test_rebulk_composition():23 rebulk = Rebulk()24 rebulk.string("quick")25 rebulk.rebulk(Rebulk().regex("f.x"))26 rebulk.rebulk(Rebulk(disabled=lambda context: True).functional(lambda string: None))27 input_string = "The quick brown fox jumps over the lazy dog"28 matches = rebulk.matches(input_string)29 assert len(matches) == 230 assert matches[0].value == "quick"31 assert matches[1].value == "fox"32def test_rebulk_context():33 rebulk = Rebulk()34 context = {'nostring': True, 'word': 'lazy'}35 rebulk.string("quick", disabled=lambda context: context.get('nostring', False))36 rebulk.regex("f.x", disabled=lambda context: context.get('noregex', False))37 def func(input_string, context):38 word = context.get('word', 'over')39 i = input_string.find(word)40 if i > -1:41 return i, i + len(word)42 rebulk.functional(func)43 input_string = "The quick brown fox jumps over the lazy dog"44 matches = rebulk.matches(input_string, context)45 assert len(matches) == 246 assert matches[0].value == "fox"47 assert matches[1].value == "lazy"48def test_rebulk_prefer_longer():49 input_string = "The quick brown fox jumps over the lazy dog"50 matches = Rebulk().string("quick").string("own").regex("br.{2}n").matches(input_string)51 assert len(matches) == 252 assert matches[0].value == "quick"53 assert matches[1].value == "brown"54def test_rebulk_defaults():55 input_string = "The quick brown fox jumps over the lazy dog"56 def func(input_string):57 i = input_string.find("fox")58 if i > -1:59 return i, i + len("fox")60 matches = Rebulk()\61 .string_defaults(name="string", tags=["a", "b"])\62 .regex_defaults(name="regex") \63 .functional_defaults(name="functional") \64 .string("quick", tags=["c"])\65 .functional(func)\66 .regex("br.{2}n") \67 .matches(input_string)68 assert matches[0].name == "string"69 assert matches[0].tags == ["a", "b", "c"]70 assert matches[1].name == "functional"71 assert matches[2].name == "regex"72 matches = Rebulk() \73 .defaults(name="default", tags=["0"])\74 .string_defaults(name="string", tags=["a", "b"]) \75 .functional_defaults(name="functional", tags=["1"]) \76 .string("quick", tags=["c"]) \77 .functional(func) \78 .regex("br.{2}n") \79 .matches(input_string)80 assert matches[0].name == "string"81 assert matches[0].tags == ["0", "a", "b", "c"]82 assert matches[1].name == "functional"83 assert matches[1].tags == ["0", "1"]84 assert matches[2].name == "default"85 assert matches[2].tags == ["0"]86def test_rebulk_rebulk():87 input_string = "The quick brown fox jumps over the lazy dog"88 base = Rebulk().string("quick")89 child = Rebulk().string("own").regex("br.{2}n")90 matches = base.rebulk(child).matches(input_string)91 assert len(matches) == 292 assert matches[0].value == "quick"93 assert matches[1].value == "brown"94def test_rebulk_no_default():95 input_string = "The quick brown fox jumps over the lazy dog"96 matches = Rebulk(default_rules=False).string("quick").string("own").regex("br.{2}n").matches(input_string)97 assert len(matches) == 398 assert matches[0].value == "quick"99 assert matches[1].value == "own"100 assert matches[2].value == "brown"101def test_rebulk_empty_match():102 input_string = "The quick brown fox jumps over the lazy dog"103 matches = Rebulk(default_rules=False).string("quick").string("own").regex("br(.*?)own", children=True)\104 .matches(input_string)105 assert len(matches) == 2106 assert matches[0].value == "quick"107 assert matches[1].value == "own"108def test_rebulk_tags_names():109 rebulk = Rebulk()110 rebulk.string("quick", name="str", tags=["first", "other"])111 rebulk.regex("f.x", tags="other")112 def func(input_string):113 i = input_string.find("over")114 if i > -1:115 return i, i + len("over"), {'tags': ['custom']}116 rebulk.functional(func, name="fn")117 def func2(input_string):118 i = input_string.find("lazy")119 if i > -1:120 return {'start': i, 'end': i + len("lazy"), 'tags': ['custom']}121 rebulk.functional(func2, name="fn")122 input_string = "The quick brown fox jumps over the lazy dog"123 matches = rebulk.matches(input_string)124 assert len(matches) == 4125 assert len(matches.named("str")) == 1126 assert len(matches.named("fn")) == 2127 assert len(matches.named("false")) == 0128 assert len(matches.tagged("false")) == 0129 assert len(matches.tagged("first")) == 1130 assert len(matches.tagged("other")) == 2131 assert len(matches.tagged("custom")) == 2132def test_rebulk_rules_1():133 rebulk = Rebulk()134 rebulk.regex(r'\d{4}', name="year")135 rebulk.rules(rm.RemoveAllButLastYear)136 matches = rebulk.matches("1984 keep only last 1968 entry 1982 case")137 assert len(matches) == 1138 assert matches[0].value == "1982"139def test_rebulk_rules_2():140 rebulk = Rebulk()141 rebulk.regex(r'\d{4}', name="year")142 rebulk.string(r'year', name="yearPrefix", private=True)143 rebulk.string(r'keep', name="yearSuffix", private=True)144 rebulk.rules(rm.PrefixedSuffixedYear)145 matches = rebulk.matches("Keep suffix 1984 keep prefixed year 1968 and remove the rest 1982")146 assert len(matches) == 2147 assert matches[0].value == "1984"148 assert matches[1].value == "1968"149def test_rebulk_rules_3():150 rebulk = Rebulk()151 rebulk.regex(r'\d{4}', name="year")152 rebulk.string(r'year', name="yearPrefix", private=True)153 rebulk.string(r'keep', name="yearSuffix", private=True)154 rebulk.rules(rm.PrefixedSuffixedYearNoLambda)155 matches = rebulk.matches("Keep suffix 1984 keep prefixed year 1968 and remove the rest 1982")156 assert len(matches) == 2157 assert matches[0].value == "1984"158 assert matches[1].value == "1968"159def test_rebulk_rules_4():160 class FirstOnlyRule(Rule):161 def when(self, matches, context):162 grabbed = matches.named("grabbed", 0)163 if grabbed and matches.previous(grabbed):164 return grabbed165 def then(self, matches, when_response, context):166 matches.remove(when_response)167 rebulk = Rebulk()168 rebulk.regex("This match (.*?)grabbed", name="grabbed")169 rebulk.regex("if it's (.*?)first match", private=True)170 rebulk.rules(FirstOnlyRule)171 matches = rebulk.matches("This match is grabbed only if it's the first match")172 assert len(matches) == 1173 assert matches[0].value == "This match is grabbed"174 matches = rebulk.matches("if it's NOT the first match, This match is NOT grabbed")175 assert len(matches) == 0176class TestMarkers(object):177 def test_one_marker(self):178 class MarkerRule(Rule):179 def when(self, matches, context):180 word_match = matches.named("word", 0)181 marker = matches.markers.at_match(word_match, lambda marker: marker.name == "mark1", 0)182 if not marker:183 return word_match184 def then(self, matches, when_response, context):185 matches.remove(when_response)186 rebulk = Rebulk().regex(r'\(.*?\)', marker=True, name="mark1") \187 .regex(r'\[.*?\]', marker=True, name="mark2") \188 .string("word", name="word") \189 .rules(MarkerRule)190 matches = rebulk.matches("grab (word) only if it's in parenthesis")191 assert len(matches) == 1192 assert matches[0].value == "word"193 matches = rebulk.matches("don't grab [word] if it's in braket")194 assert len(matches) == 0195 matches = rebulk.matches("don't grab word at all")196 assert len(matches) == 0197 def test_multiple_marker(self):198 class MarkerRule(Rule):199 def when(self, matches, context):200 word_match = matches.named("word", 0)201 marker = matches.markers.at_match(word_match,202 lambda marker: marker.name == "mark1" or marker.name == "mark2")203 if len(marker) < 2:204 return word_match205 def then(self, matches, when_response, context):206 matches.remove(when_response)207 rebulk = Rebulk().regex(r'\(.*?\)', marker=True, name="mark1") \208 .regex(r'\[.*?\]', marker=True, name="mark2") \209 .regex("w.*?d", name="word") \210 .rules(MarkerRule)211 matches = rebulk.matches("[grab (word) only] if it's in parenthesis and brakets")212 assert len(matches) == 1213 assert matches[0].value == "word"214 matches = rebulk.matches("[don't grab](word)[if brakets are outside]")215 assert len(matches) == 0216 matches = rebulk.matches("(grab w[or)d even] if it's partially in parenthesis and brakets")217 assert len(matches) == 1218 assert matches[0].value == "w[or)d"219 def test_at_index_marker(self):220 class MarkerRule(Rule):221 def when(self, matches, context):222 word_match = matches.named("word", 0)223 marker = matches.markers.at_index(word_match.start,224 lambda marker: marker.name == "mark1", 0)225 if not marker:226 return word_match227 def then(self, matches, when_response, context):228 matches.remove(when_response)229 rebulk = Rebulk().regex(r'\(.*?\)', marker=True, name="mark1") \230 .regex("w.*?d", name="word") \231 .rules(MarkerRule)232 matches = rebulk.matches("gr(ab wo)rd only if starting of match is inside parenthesis")233 assert len(matches) == 1234 assert matches[0].value == "wo)rd"235 matches = rebulk.matches("don't grab wo(rd if starting of match is not inside parenthesis")236 assert len(matches) == 0237 def test_remove_marker(self):238 class MarkerRule(Rule):239 def when(self, matches, context):240 marker = matches.markers.named("mark1", 0)241 if marker:242 return marker243 def then(self, matches, when_response, context):244 matches.markers.remove(when_response)245 rebulk = Rebulk().regex(r'\(.*?\)', marker=True, name="mark1") \246 .regex("w.*?d", name="word") \247 .rules(MarkerRule)248 matches = rebulk.matches("grab word event (if it's not) inside parenthesis")249 assert len(matches) == 1250 assert matches[0].value == "word"251 assert not matches.markers252class TestUnicode(object):253 def test_rebulk_simple(self):254 input_string = u"ææ·çæ£è²çç¸è·³éæ¶ç"255 rebulk = Rebulk()256 rebulk.string(u"æ")257 rebulk.regex(u"æ·")258 def func(input_string):259 i = input_string.find(u"ç")260 if i > -1:261 return i, i + len(u"ç")262 rebulk.functional(func)263 matches = rebulk.matches(input_string)264 assert len(matches) == 3265 assert matches[0].value == u"æ"266 assert matches[1].value == u"æ·"267 assert matches[2].value == u"ç"268class TestImmutable(object):269 def test_starting(self):270 input_string = "The quick brown fox jumps over the lazy dog"271 matches = Rebulk().string("quick").string("over").string("fox").matches(input_string)272 for i in range(0, len(input_string)):273 starting = matches.starting(i)274 for match in list(starting):275 starting.remove(match)276 assert len(matches) == 3277 def test_ending(self):278 input_string = "The quick brown fox jumps over the lazy dog"279 matches = Rebulk().string("quick").string("over").string("fox").matches(input_string)280 for i in range(0, len(input_string)):281 starting = matches.ending(i)282 for match in list(starting):283 starting.remove(match)284 assert len(matches) == 3285 def test_named(self):286 input_string = "The quick brown fox jumps over the lazy dog"287 matches = Rebulk().defaults(name='test').string("quick").string("over").string("fox").matches(input_string)288 named = matches.named('test')289 for match in list(named):290 named.remove(match)291 assert len(named) == 0...
test_chain.py
Source:test_chain.py
...48 rebulk.chain()\49 .regex("(?P<test>test)") \50 .regex(" ").repeater("*") \51 .regex("(?P<testIgnore>testIgnore)")52 matches = rebulk.matches("test testIgnore")53 assert len(matches) == 154 assert matches[0].name == "test"55def test_matches():56 rebulk = Rebulk()57 def digit(input_string):58 i = input_string.find("1849")59 if i > -1:60 return i, i + len("1849")61 input_string = "1849testtestxxfixfux_foxabc1849testtestxoptionalfoxabc"62 chain = rebulk.chain() \63 .functional(digit) \64 .string("test").hidden().repeater(2) \65 .string("x").hidden().repeater('{1,3}') \66 .string("optional").hidden().repeater('?') \67 .regex("f.?x", name='result').repeater('+') \68 .close()69 matches = chain.matches(input_string)70 assert len(matches) == 271 children = matches[0].children72 assert children[0].value == '1849'73 assert children[1].value == 'fix'74 assert children[2].value == 'fux'75 children = matches[1].children76 assert children[0].value == '1849'77 assert children[1].value == 'fox'78 input_string = "_1850testtestxoptionalfoxabc"79 matches = chain.matches(input_string)80 assert len(matches) == 081 input_string = "_1849testtesttesttestxoptionalfoxabc"82 matches = chain.matches(input_string)83 assert len(matches) == 084 input_string = "_1849testtestxxxxoptionalfoxabc"85 matches = chain.matches(input_string)86 assert len(matches) == 087 input_string = "_1849testtestoptionalfoxabc"88 matches = chain.matches(input_string)89 assert len(matches) == 090 input_string = "_1849testtestxoptionalabc"91 matches = chain.matches(input_string)92 assert len(matches) == 093 input_string = "_1849testtestxoptionalfaxabc"94 matches = chain.matches(input_string)95 assert len(matches) == 196 children = matches[0].children97 assert children[0].value == '1849'98 assert children[1].value == 'fax'99def test_matches_2():100 rebulk = Rebulk() \101 .regex_defaults(flags=re.IGNORECASE) \102 .chain(children=True, formatter={'episode': int}) \103 .defaults(formatter={'version': int}) \104 .regex(r'e(?P<episode>\d{1,4})') \105 .regex(r'v(?P<version>\d+)').repeater('?') \106 .regex(r'[ex-](?P<episode>\d{1,4})').repeater('*') \107 .close()108 matches = rebulk.matches("This is E14v2-15E16x17")109 assert len(matches) == 5110 assert matches[0].name == 'episode'111 assert matches[0].value == 14112 assert matches[1].name == 'version'113 assert matches[1].value == 2114 assert matches[2].name == 'episode'115 assert matches[2].value == 15116 assert matches[3].name == 'episode'117 assert matches[3].value == 16118 assert matches[4].name == 'episode'119 assert matches[4].value == 17120def test_matches_3():121 alt_dash = (r'@', r'[\W_]') # abbreviation122 rebulk = Rebulk()123 rebulk.chain(formatter={'season': int, 'episode': int},124 tags=['SxxExx'],125 abbreviations=[alt_dash],126 private_names=['episodeSeparator', 'seasonSeparator'],127 children=True,128 private_parent=True,129 conflict_solver=lambda match, other: match130 if match.name in ['season', 'episode'] and other.name in131 ['screen_size', 'video_codec', 'audio_codec',132 'audio_channels', 'container', 'date']133 else '__default__') \134 .regex(r'(?P<season>\d+)@?x@?(?P<episode>\d+)') \135 .regex(r'(?P<episodeSeparator>x|-|\+|&)(?P<episode>\d+)').repeater('*') \136 .chain() \137 .regex(r'S(?P<season>\d+)@?(?:xE|Ex|E|x)@?(?P<episode>\d+)') \138 .regex(r'(?:(?P<episodeSeparator>xE|Ex|E|x|-|\+|&)(?P<episode>\d+))').repeater('*') \139 .chain() \140 .regex(r'S(?P<season>\d+)') \141 .regex(r'(?P<seasonSeparator>S|-|\+|&)(?P<season>\d+)').repeater('*')142 matches = rebulk.matches("test-01x02-03")143 assert len(matches) == 3144 assert matches[0].name == 'season'145 assert matches[0].value == 1146 assert matches[1].name == 'episode'147 assert matches[1].value == 2148 assert matches[2].name == 'episode'149 assert matches[2].value == 3150 matches = rebulk.matches("test-S01E02-03")151 assert len(matches) == 3152 assert matches[0].name == 'season'153 assert matches[0].value == 1154 assert matches[1].name == 'episode'155 assert matches[1].value == 2156 assert matches[2].name == 'episode'157 assert matches[2].value == 3158 matches = rebulk.matches("test-S01-02-03-04")159 assert len(matches) == 4160 assert matches[0].name == 'season'161 assert matches[0].value == 1162 assert matches[1].name == 'season'163 assert matches[1].value == 2164 assert matches[2].name == 'season'165 assert matches[2].value == 3166 assert matches[3].name == 'season'167 assert matches[3].value == 4168def test_matches_4():169 seps_surround = partial(chars_surround, " ")170 rebulk = Rebulk()171 rebulk.regex_defaults(flags=re.IGNORECASE)172 rebulk.defaults(private_names=['episodeSeparator', 'seasonSeparator'], validate_all=True,173 validator={'__parent__': seps_surround}, children=True, private_parent=True)174 rebulk.chain(formatter={'episode': int, 'version': int}) \175 .defaults(validator=None) \176 .regex(r'e(?P<episode>\d{1,4})') \177 .regex(r'v(?P<version>\d+)').repeater('?') \178 .regex(r'(?P<episodeSeparator>e|x|-)(?P<episode>\d{1,4})').repeater('*')179 matches = rebulk.matches("Some Series E01E02E03")180 assert len(matches) == 3181 assert matches[0].value == 1182 assert matches[1].value == 2183 assert matches[2].value == 3184def test_matches_5():185 seps_surround = partial(chars_surround, " ")186 rebulk = Rebulk()187 rebulk.regex_defaults(flags=re.IGNORECASE)188 rebulk.defaults(private_names=['episodeSeparator', 'seasonSeparator'], validate_all=True,189 validator={'__parent__': seps_surround}, children=True, private_parent=True)190 rebulk.chain(formatter={'episode': int, 'version': int}) \191 .defaults(validator=None) \192 .regex(r'e(?P<episode>\d{1,4})') \193 .regex(r'v(?P<version>\d+)').repeater('?') \194 .regex(r'(?P<episodeSeparator>e|x|-)(?P<episode>\d{1,4})').repeater('{2,3}')195 matches = rebulk.matches("Some Series E01E02E03")196 assert len(matches) == 3197 matches = rebulk.matches("Some Series E01E02")198 assert len(matches) == 0199 matches = rebulk.matches("Some Series E01E02E03E04E05E06") # Parent can't be validated, so no results at all200 assert len(matches) == 0201def test_matches_6():202 rebulk = Rebulk()203 rebulk.regex_defaults(flags=re.IGNORECASE)204 rebulk.defaults(private_names=['episodeSeparator', 'seasonSeparator'], validate_all=True,205 validator=None, children=True, private_parent=True)206 rebulk.chain(formatter={'episode': int, 'version': int}) \207 .defaults(validator=None) \208 .regex(r'e(?P<episode>\d{1,4})') \209 .regex(r'v(?P<version>\d+)').repeater('?') \210 .regex(r'(?P<episodeSeparator>e|x|-)(?P<episode>\d{1,4})').repeater('{2,3}')211 matches = rebulk.matches("Some Series E01E02E03")212 assert len(matches) == 3213 matches = rebulk.matches("Some Series E01E02")214 assert len(matches) == 0215 matches = rebulk.matches("Some Series E01E02E03E04E05E06") # No validator on parent, so it should give 4 episodes.216 assert len(matches) == 4217def test_matches_7():218 seps_surround = partial(chars_surround, ' .-/')219 rebulk = Rebulk()220 rebulk.regex_defaults(flags=re.IGNORECASE)221 rebulk.defaults(children=True, private_parent=True)222 rebulk.chain(). \223 regex(r'S(?P<season>\d+)', validate_all=True, validator={'__parent__': seps_surround}). \224 regex(r'[ -](?P<season>\d+)', validator=seps_surround).repeater('*')225 matches = rebulk.matches("Some S01")226 assert len(matches) == 1227 matches[0].value = 1228 matches = rebulk.matches("Some S01-02")229 assert len(matches) == 2230 matches[0].value = 1231 matches[1].value = 2232 matches = rebulk.matches("programs4/Some S01-02")233 assert len(matches) == 2234 matches[0].value = 1235 matches[1].value = 2236 matches = rebulk.matches("programs4/SomeS01middle.S02-03.andS04here")237 assert len(matches) == 2238 matches[0].value = 2239 matches[1].value = 3240 matches = rebulk.matches("Some 02.and.S04-05.here")241 assert len(matches) == 2242 matches[0].value = 4243 matches[1].value = 5244def test_chain_breaker():245 def chain_breaker(matches):246 seasons = matches.named('season')247 if len(seasons) > 1:248 if seasons[-1].value - seasons[-2].value > 10:249 return True250 return False251 seps_surround = partial(chars_surround, ' .-/')252 rebulk = Rebulk()253 rebulk.regex_defaults(flags=re.IGNORECASE)254 rebulk.defaults(children=True, private_parent=True, formatter={'season': int})255 rebulk.chain(chain_breaker=chain_breaker). \256 regex(r'S(?P<season>\d+)', validate_all=True, validator={'__parent__': seps_surround}). \257 regex(r'[ -](?P<season>\d+)', validator=seps_surround).repeater('*')258 matches = rebulk.matches("Some S01-02-03-50-51")259 assert len(matches) == 3260 matches[0].value = 1261 matches[1].value = 2262 matches[2].value = 3263def test_chain_breaker_defaults():264 def chain_breaker(matches):265 seasons = matches.named('season')266 if len(seasons) > 1:267 if seasons[-1].value - seasons[-2].value > 10:268 return True269 return False270 seps_surround = partial(chars_surround, ' .-/')271 rebulk = Rebulk()272 rebulk.regex_defaults(flags=re.IGNORECASE)273 rebulk.defaults(chain_breaker=chain_breaker, children=True, private_parent=True, formatter={'season': int})274 rebulk.chain(). \275 regex(r'S(?P<season>\d+)', validate_all=True, validator={'__parent__': seps_surround}). \276 regex(r'[ -](?P<season>\d+)', validator=seps_surround).repeater('*')277 matches = rebulk.matches("Some S01-02-03-50-51")278 assert len(matches) == 3279 matches[0].value = 1280 matches[1].value = 2281 matches[2].value = 3282def test_chain_breaker_defaults2():283 def chain_breaker(matches):284 seasons = matches.named('season')285 if len(seasons) > 1:286 if seasons[-1].value - seasons[-2].value > 10:287 return True288 return False289 seps_surround = partial(chars_surround, ' .-/')290 rebulk = Rebulk()291 rebulk.regex_defaults(flags=re.IGNORECASE)292 rebulk.chain_defaults(chain_breaker=chain_breaker)293 rebulk.defaults(children=True, private_parent=True, formatter={'season': int})294 rebulk.chain(). \295 regex(r'S(?P<season>\d+)', validate_all=True, validator={'__parent__': seps_surround}). \296 regex(r'[ -](?P<season>\d+)', validator=seps_surround).repeater('*')297 matches = rebulk.matches("Some S01-02-03-50-51")298 assert len(matches) == 3299 matches[0].value = 1300 matches[1].value = 2...
pre_processing.py
Source:pre_processing.py
...4import numpy as np5from sklearn.preprocessing import StandardScaler6from definitions import GEN_PATH7from utilities import helper as h8def process_matches(stats_filepath, proc_match_filepath, t_weights, base_weight, proc_years, t_levels, surfaces):9 # Generates a match matrix with certain statistics for each match10 print('----- GENERATING PRE-PROCESSED MATCHES -----')11 start_time = time.time()12 mutual_matches_clay = pd.read_hdf(stats_filepath, key='mm_clay')13 mutual_matches_grass = pd.read_hdf(stats_filepath, key='mm_grass')14 mutual_matches_hard = pd.read_hdf(stats_filepath, key='mm_hard')15 mutual_matches = mutual_matches_clay + mutual_matches_grass + mutual_matches_hard16 mutual_score = pd.read_hdf(stats_filepath, key='ms')17 cond_stats = pd.read_hdf(stats_filepath, key='cs')18 print('Generated statistics loaded')19 # Load rankings20 rankings = h.load_rankings()21 # Load raw_matches and sport by date22 print('Loading raw matches...')23 raw_matches = h.load_matches(proc_years)24 raw_matches.sort_values(by=['tourney_date'], inplace=True, ascending=True)25 # Load last years matches to calculate recent performance for matches in january26 last_year = proc_years['from'] - 127 recent_years = {28 'from': last_year,29 'to': last_year30 }31 current_tourney_date = raw_matches.iloc[0].tourney_date32 month_offset = 333 date_limit = current_tourney_date - pd.DateOffset(months=month_offset)34 print('Loading recent matches...')35 recent_matches = h.load_matches(recent_years)36 recent_matches = recent_matches.loc[recent_matches.tourney_date >= date_limit]37 # Load tournament details38 tourneys = pd.read_csv(os.path.join(GEN_PATH, 'tourneys_fixed.csv'), index_col=0)39 data_columns = ['tourney_date', 'rel_total_wins', 'rel_surface_wins', 'mutual_wins', 'mutual_surface_wins',40 'mutual_games', 'rank_diff', 'points_grad_diff', 'home_advantage', 'rel_climate_wins',41 'rel_recent_wins', 'rel_tourney_games', 'tourney_level', 'player_1', 'player_2', 'surface',42 'age_diff', 'outcome']43 matches = np.zeros((len(raw_matches), len(data_columns)), dtype=np.int64)44 matches = pd.DataFrame(matches, columns=data_columns)45 i = 046 no_matches = len(raw_matches)47 print('Pre-processing matches...')48 # Generate training matrix and update statistics matrices49 # Loop unavoidable...
test_processors.py
Source:test_processors.py
...7from ..match import Matches8def test_conflict_1():9 input_string = "abcdefghijklmnopqrstuvwxyz"10 pattern = StringPattern("ijklmn", "kl", "abcdef", "ab", "ef", "yz")11 matches = Matches(pattern.matches(input_string))12 execute_rule(ConflictSolver(), matches, None)13 values = [x.value for x in matches]14 assert values == ["ijklmn", "abcdef", "yz"]15def test_conflict_2():16 input_string = "abcdefghijklmnopqrstuvwxyz"17 pattern = StringPattern("ijklmn", "jklmnopqrst")18 matches = Matches(pattern.matches(input_string))19 execute_rule(ConflictSolver(), matches, None)20 values = [x.value for x in matches]21 assert values == ["jklmnopqrst"]22def test_conflict_3():23 input_string = "abcdefghijklmnopqrstuvwxyz"24 pattern = StringPattern("ijklmnopqrst", "jklmnopqrst")25 matches = Matches(pattern.matches(input_string))26 execute_rule(ConflictSolver(), matches, None)27 values = [x.value for x in matches]28 assert values == ["ijklmnopqrst"]29def test_conflict_4():30 input_string = "123456789"31 pattern = StringPattern("123", "456789")32 matches = Matches(pattern.matches(input_string))33 execute_rule(ConflictSolver(), matches, None)34 values = [x.value for x in matches]35 assert values == ["123", "456789"]36def test_conflict_5():37 input_string = "123456789"38 pattern = StringPattern("123456", "789")39 matches = Matches(pattern.matches(input_string))40 execute_rule(ConflictSolver(), matches, None)41 values = [x.value for x in matches]42 assert values == ["123456", "789"]43def test_prefer_longer_parent():44 input_string = "xxx.1x02.xxx"45 re1 = RePattern("([0-9]+)x([0-9]+)", name='prefer', children=True, formatter=int)46 re2 = RePattern("x([0-9]+)", name='skip', children=True)47 matches = Matches(re1.matches(input_string))48 matches.extend(re2.matches(input_string))49 execute_rule(ConflictSolver(), matches, None)50 assert len(matches) == 251 assert matches[0].value == 152 assert matches[1].value == 253def test_conflict_solver_1():54 input_string = "123456789"55 re1 = StringPattern("2345678", conflict_solver=lambda match, conflicting: '__default__')56 re2 = StringPattern("34567")57 matches = Matches(re1.matches(input_string))58 matches.extend(re2.matches(input_string))59 execute_rule(ConflictSolver(), matches, None)60 assert len(matches) == 161 assert matches[0].value == "2345678"62def test_conflict_solver_2():63 input_string = "123456789"64 re1 = StringPattern("2345678", conflict_solver=lambda match, conflicting: '__default__')65 re2 = StringPattern("34567", conflict_solver=lambda match, conflicting: conflicting)66 matches = Matches(re1.matches(input_string))67 matches.extend(re2.matches(input_string))68 execute_rule(ConflictSolver(), matches, None)69 assert len(matches) == 170 assert matches[0].value == "34567"71def test_conflict_solver_3():72 input_string = "123456789"73 re1 = StringPattern("2345678", conflict_solver=lambda match, conflicting: match)74 re2 = StringPattern("34567")75 matches = Matches(re1.matches(input_string))76 matches.extend(re2.matches(input_string))77 execute_rule(ConflictSolver(), matches, None)78 assert len(matches) == 179 assert matches[0].value == "34567"80def test_conflict_solver_4():81 input_string = "123456789"82 re1 = StringPattern("2345678")83 re2 = StringPattern("34567", conflict_solver=lambda match, conflicting: conflicting)84 matches = Matches(re1.matches(input_string))85 matches.extend(re2.matches(input_string))86 execute_rule(ConflictSolver(), matches, None)87 assert len(matches) == 188 assert matches[0].value == "34567"89def test_conflict_solver_5():90 input_string = "123456789"91 re1 = StringPattern("2345678", conflict_solver=lambda match, conflicting: conflicting)92 re2 = StringPattern("34567")93 matches = Matches(re1.matches(input_string))94 matches.extend(re2.matches(input_string))95 execute_rule(ConflictSolver(), matches, None)96 assert len(matches) == 197 assert matches[0].value == "2345678"98def test_conflict_solver_6():99 input_string = "123456789"100 re1 = StringPattern("2345678")101 re2 = StringPattern("34567", conflict_solver=lambda match, conflicting: conflicting)102 matches = Matches(re1.matches(input_string))103 matches.extend(re2.matches(input_string))104 execute_rule(ConflictSolver(), matches, None)105 assert len(matches) == 1106 assert matches[0].value == "34567"107def test_conflict_solver_7():108 input_string = "102"109 re1 = StringPattern("102")110 re2 = StringPattern("02")111 matches = Matches(re2.matches(input_string))112 matches.extend(re1.matches(input_string))113 execute_rule(ConflictSolver(), matches, None)114 assert len(matches) == 1115 assert matches[0].value == "102"116def test_unresolved():117 input_string = "123456789"118 re1 = StringPattern("23456")119 re2 = StringPattern("34567")120 matches = Matches(re1.matches(input_string))121 matches.extend(re2.matches(input_string))122 execute_rule(ConflictSolver(), matches, None)123 assert len(matches) == 2124 re1 = StringPattern("34567")125 re2 = StringPattern("2345678", conflict_solver=lambda match, conflicting: None)126 matches = Matches(re1.matches(input_string))127 matches.extend(re2.matches(input_string))128 execute_rule(ConflictSolver(), matches, None)129 assert len(matches) == 2130 re1 = StringPattern("34567", conflict_solver=lambda match, conflicting: None)131 re2 = StringPattern("2345678")132 matches = Matches(re1.matches(input_string))133 matches.extend(re2.matches(input_string))134 execute_rule(ConflictSolver(), matches, None)...
test_rules.py
Source:test_rules.py
1#!/usr/bin/env python2# -*- coding: utf-8 -*-3# pylint: disable=no-self-use, pointless-statement, missing-docstring, invalid-name, no-member, len-as-condition4import pytest5from rebulk.test.default_rules_module import RuleRemove0, RuleAppend0, RuleRename0, RuleAppend1, RuleRemove1, \6 RuleRename1, RuleAppend2, RuleRename2, RuleAppend3, RuleRename3, RuleAppendTags0, RuleRemoveTags0, \7 RuleAppendTags1, RuleRemoveTags18from ..rules import Rules9from ..match import Matches, Match10from .rules_module import Rule1, Rule2, Rule3, Rule0, Rule1Disabled11from . import rules_module as rm12def test_rule_priority():13 matches = Matches([Match(1, 2)])14 rules = Rules(Rule1, Rule2())15 rules.execute_all_rules(matches, {})16 assert len(matches) == 017 matches = Matches([Match(1, 2)])18 rules = Rules(Rule1(), Rule0)19 rules.execute_all_rules(matches, {})20 assert len(matches) == 121 assert matches[0] == Match(3, 4)22def test_rules_duplicates():23 matches = Matches([Match(1, 2)])24 rules = Rules(Rule1, Rule1)25 with pytest.raises(ValueError):26 rules.execute_all_rules(matches, {})27def test_rule_disabled():28 matches = Matches([Match(1, 2)])29 rules = Rules(Rule1Disabled(), Rule2())30 rules.execute_all_rules(matches, {})31 assert len(matches) == 232 assert matches[0] == Match(1, 2)33 assert matches[1] == Match(3, 4)34def test_rule_when():35 matches = Matches([Match(1, 2)])36 rules = Rules(Rule3())37 rules.execute_all_rules(matches, {'when': False})38 assert len(matches) == 139 assert matches[0] == Match(1, 2)40 matches = Matches([Match(1, 2)])41 rules.execute_all_rules(matches, {'when': True})42 assert len(matches) == 243 assert matches[0] == Match(1, 2)44 assert matches[1] == Match(3, 4)45class TestDefaultRules(object):46 def test_remove(self):47 rules = Rules(RuleRemove0)48 matches = Matches([Match(1, 2)])49 rules.execute_all_rules(matches, {})50 assert len(matches) == 051 rules = Rules(RuleRemove1)52 matches = Matches([Match(1, 2)])53 rules.execute_all_rules(matches, {})54 assert len(matches) == 055 def test_append(self):56 rules = Rules(RuleAppend0)57 matches = Matches([Match(1, 2)])58 rules.execute_all_rules(matches, {})59 assert len(matches) == 260 rules = Rules(RuleAppend1)61 matches = Matches([Match(1, 2)])62 rules.execute_all_rules(matches, {})63 assert len(matches) == 264 rules = Rules(RuleAppend2)65 matches = Matches([Match(1, 2)])66 rules.execute_all_rules(matches, {})67 assert len(matches) == 268 assert len(matches.named('renamed')) == 169 rules = Rules(RuleAppend3)70 matches = Matches([Match(1, 2)])71 rules.execute_all_rules(matches, {})72 assert len(matches) == 273 assert len(matches.named('renamed')) == 174 def test_rename(self):75 rules = Rules(RuleRename0)76 matches = Matches([Match(1, 2, name='original')])77 rules.execute_all_rules(matches, {})78 assert len(matches.named('original')) == 179 assert len(matches.named('renamed')) == 080 rules = Rules(RuleRename1)81 matches = Matches([Match(5, 10, name='original')])82 rules.execute_all_rules(matches, {})83 assert len(matches.named('original')) == 084 assert len(matches.named('renamed')) == 185 rules = Rules(RuleRename2)86 matches = Matches([Match(5, 10, name='original')])87 rules.execute_all_rules(matches, {})88 assert len(matches.named('original')) == 089 assert len(matches.named('renamed')) == 190 rules = Rules(RuleRename3)91 matches = Matches([Match(5, 10, name='original')])92 rules.execute_all_rules(matches, {})93 assert len(matches.named('original')) == 094 assert len(matches.named('renamed')) == 195 def test_append_tags(self):96 rules = Rules(RuleAppendTags0)97 matches = Matches([Match(1, 2, name='tags', tags=['other'])])98 rules.execute_all_rules(matches, {})99 assert len(matches.named('tags')) == 1100 assert matches.named('tags', index=0).tags == ['other', 'new-tag']101 rules = Rules(RuleAppendTags1)102 matches = Matches([Match(1, 2, name='tags', tags=['other'])])103 rules.execute_all_rules(matches, {})104 assert len(matches.named('tags')) == 1105 assert matches.named('tags', index=0).tags == ['other', 'new-tag']106 def test_remove_tags(self):107 rules = Rules(RuleRemoveTags0)108 matches = Matches([Match(1, 2, name='tags', tags=['other', 'new-tag'])])109 rules.execute_all_rules(matches, {})110 assert len(matches.named('tags')) == 1111 assert matches.named('tags', index=0).tags == ['other']112 rules = Rules(RuleRemoveTags1)113 matches = Matches([Match(1, 2, name='tags', tags=['other', 'new-tag'])])114 rules.execute_all_rules(matches, {})115 assert len(matches.named('tags')) == 1116 assert matches.named('tags', index=0).tags == ['other']117def test_rule_module():118 rules = Rules(rm)119 matches = Matches([Match(1, 2)])120 rules.execute_all_rules(matches, {})121 assert len(matches) == 1122def test_rule_repr():123 assert str(Rule0()) == "<Rule0>"124 assert str(Rule1()) == "<Rule1>"125 assert str(Rule2()) == "<Rule2>"...
test_validators.py
Source:test_validators.py
...8left = partial(chars_before, chars)9right = partial(chars_after, chars)10surrounding = partial(chars_surround, chars)11def test_left_chars():12 matches = list(StringPattern("word", validator=left).matches("xxxwordxxx"))13 assert len(matches) == 014 matches = list(StringPattern("word", validator=left).matches("xxx_wordxxx"))15 assert len(matches) == 116 matches = list(StringPattern("word", validator=left).matches("wordxxx"))17 assert len(matches) == 118def test_right_chars():19 matches = list(StringPattern("word", validator=right).matches("xxxwordxxx"))20 assert len(matches) == 021 matches = list(StringPattern("word", validator=right).matches("xxxword.xxx"))22 assert len(matches) == 123 matches = list(StringPattern("word", validator=right).matches("xxxword"))24 assert len(matches) == 125def test_surrounding_chars():26 matches = list(StringPattern("word", validator=surrounding).matches("xxxword xxx"))27 assert len(matches) == 028 matches = list(StringPattern("word", validator=surrounding).matches("xxx.wordxxx"))29 assert len(matches) == 030 matches = list(StringPattern("word", validator=surrounding).matches("xxx word_xxx"))31 assert len(matches) == 132 matches = list(StringPattern("word", validator=surrounding).matches("word"))33 assert len(matches) == 134def test_chain():35 matches = list(StringPattern("word", validator=validators(left, right)).matches("xxxword xxx"))36 assert len(matches) == 037 matches = list(StringPattern("word", validator=validators(left, right)).matches("xxx.wordxxx"))38 assert len(matches) == 039 matches = list(StringPattern("word", validator=validators(left, right)).matches("xxx word_xxx"))40 assert len(matches) == 141 matches = list(StringPattern("word", validator=validators(left, right)).matches("word"))...
Using AI Code Generation
1const { chromium } = require('playwright');2(async () => {3 const browser = await chromium.launch();4 const page = await browser.newPage();5 const element = await page.$('div');6 const handle = await element._page._delegate._session.send('DOM.describeNode', {7 });8 console.log(handle.node.matches('div'));9 await browser.close();10})();11element.matches(cssSelector)
Using AI Code Generation
1const { Page } = require('playwright/lib/page');2const { ElementHandle } = require('playwright/lib/elementHandle');3const { JSHandle } = require('playwright/lib/jsHandle');4const originalMatches = Page.prototype.matches;5Page.prototype.matches = function (selector, node) {6 return this.evaluate((selector, node) => {7 return node.matches(selector);8 }, selector, node);9};10ElementHandle.prototype.matches = function (selector) {11 return this.evaluate((selector) => {12 return this.matches(selector);13 }, selector);14};15JSHandle.prototype.matches = function (selector) {16 return this.evaluate((selector) => {17 return this.matches(selector);18 }, selector);19};20const { test, expect } = require('@playwright/test');21test('test', async ({ page }) => {22 await page.setContent('<div id="a">a</div>');23 const a = await page.$('#a');24 const result = await a.matches('div');25 expect(result).toBe(true);26});27const { test, expect } = require('@playwright/test');28test('test', async ({ page }) => {29 await page.setContent('<div id="a">a</div>');30 const a = await page.$('#a');31 const result = await a.evaluate((a) => {32 return a.matches('div');33 }, a);34 expect(result).toBe(true);35});
Using AI Code Generation
1const { matches } = require('@playwright/test/lib/matchers/matcher');2const { expect } = require('@playwright/test');3const { test } = require('@playwright/test');4test('My test', async ({ page }) => {5 const element = await page.$('text=Get started');6 const result = await matches(element, 'is visible');7 await expect(element).toBeVisible();8 await expect(result).toBe(true);9});10 ✓ My test (2s)
Using AI Code Generation
1const { matches } = require('playwright/lib/utils/utils');2const pattern = /google\.com/;3if (matches(url, pattern)) {4 console.log('Matched');5} else {6 console.log('Not matched');7}8const { matches } = require('playwright/lib/utils/utils');9const pattern = /google\.com/;10if (matche
LambdaTest’s Playwright tutorial will give you a broader idea about the Playwright automation framework, its unique features, and use cases with examples to exceed your understanding of Playwright testing. This tutorial will give A to Z guidance, from installing the Playwright framework to some best practices and advanced concepts.
Get 100 minutes of automation test minutes FREE!!