|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +from __future__ import print_function |
| 4 | +import unittest |
| 5 | + |
| 6 | +from rure.exceptions import CompiledTooBigError, RegexSyntaxError |
| 7 | +from rure.lib import CASEI, Rure |
| 8 | + |
| 9 | + |
| 10 | +class TestRure(unittest.TestCase): |
| 11 | + def test_is_match(self): |
| 12 | + haystack = b"snowman: \xE2\x98\x83" |
| 13 | + re = Rure(b"\\p{So}$") |
| 14 | + self.assertIsNotNone(re.is_match(haystack)) |
| 15 | + |
| 16 | + def test_shortest_match(self): |
| 17 | + haystack = b"aaaaa" |
| 18 | + re = Rure(b"a+") |
| 19 | + end = re.shortest_match(haystack) |
| 20 | + self.assertIsNotNone(end) |
| 21 | + self.assertEqual(end, 1, |
| 22 | + "Expected match end location 1 but got {}".format(end)) |
| 23 | + |
| 24 | + def test_find(self): |
| 25 | + haystack = b"snowman: \xE2\x98\x83" |
| 26 | + re = Rure(b"\\p{So}$") |
| 27 | + match = re.find(haystack) |
| 28 | + self.assertIsNotNone(match) |
| 29 | + |
| 30 | + expect_start = 9 |
| 31 | + expect_end = 12 |
| 32 | + self.assertEqual( |
| 33 | + (match.start, match.end), (expect_start, expect_end), |
| 34 | + "Expected match at ({}, {}) but got match at ({}, {})".format( |
| 35 | + expect_start, expect_end, match.start, match.end) |
| 36 | + ) |
| 37 | + |
| 38 | + def test_captures(self): |
| 39 | + haystack = b"snowman: \xE2\x98\x83" |
| 40 | + re = Rure(b".(.*(?P<snowman>\\p{So}))$") |
| 41 | + captures = re.captures(haystack) |
| 42 | + self.assertIsNotNone(captures) |
| 43 | + |
| 44 | + expect_capture_index = 2 |
| 45 | + capture_index = re.capture_name_index(b"snowman") |
| 46 | + self.assertEqual( |
| 47 | + capture_index, expect_capture_index, |
| 48 | + "Expected capture index {} for name 'snowman', but got {}".format( |
| 49 | + expect_capture_index, capture_index) |
| 50 | + ) |
| 51 | + |
| 52 | + expect_start = 9 |
| 53 | + expect_end = 12 |
| 54 | + match = captures[2] |
| 55 | + self.assertEqual( |
| 56 | + (match.start, match.end), (expect_start, expect_end), |
| 57 | + "Expected match at ({}, {}) but got match at ({}, {})".format( |
| 58 | + expect_start, expect_end, match.start, match.end) |
| 59 | + ) |
| 60 | + |
| 61 | + def test_iter(self): |
| 62 | + haystack = b"abc xyz" |
| 63 | + re = Rure(b"\\w+(\\w)") |
| 64 | + match = next(re.find_iter(haystack)) |
| 65 | + self.assertIsNotNone(match) |
| 66 | + |
| 67 | + expect_start = 0 |
| 68 | + expect_end = 3 |
| 69 | + self.assertEqual( |
| 70 | + (match.start, match.end), (expect_start, expect_end), |
| 71 | + "Expected match at ({}, {}) but got match at ({}, {})".format( |
| 72 | + expect_start, expect_end, match.start, match.end) |
| 73 | + ) |
| 74 | + |
| 75 | + # find_iter and captures_iter use distinct iterators; |
| 76 | + # emulate by advancing captures an additional time |
| 77 | + c_iter = re.captures_iter(haystack) |
| 78 | + next(c_iter) |
| 79 | + captures = next(c_iter) |
| 80 | + self.assertIsNotNone(captures) |
| 81 | + |
| 82 | + match = captures[1] |
| 83 | + expect_start = 6 |
| 84 | + expect_end = 7 |
| 85 | + self.assertEqual( |
| 86 | + (match.start, match.end), (expect_start, expect_end), |
| 87 | + "Expected match at ({}, {}) but got match at ({}, {})".format( |
| 88 | + expect_start, expect_end, match.start, match.end) |
| 89 | + ) |
| 90 | + |
| 91 | + def test_iter_capture_names(self): |
| 92 | + re = Rure(b"(?P<year>\\d{4})-(?P<month>\\d{2})-(?P<day>\\d{2})") |
| 93 | + cn_iter = re.capture_names() |
| 94 | + self.assertIsNone(next(cn_iter)) |
| 95 | + |
| 96 | + for word in [b"year", b"month", b"day"]: |
| 97 | + next_match = next(cn_iter) |
| 98 | + self.assertEqual( |
| 99 | + word, next_match, |
| 100 | + "Expected first capture name '{}', got '{}'.".format( |
| 101 | + word, next_match |
| 102 | + ) |
| 103 | + ) |
| 104 | + |
| 105 | + def test_flags(self): |
| 106 | + """Test whether we can set the flags correctly. |
| 107 | +
|
| 108 | + In this case, we disable all flags, which includes disabling |
| 109 | + Unicode mode. When we disable Unicode mode, we can match |
| 110 | + arbitrary possibly invalid UTF-8 bytes, such as \xFF. |
| 111 | + (When Unicode mode is enabled, \xFF won't match .) |
| 112 | + """ |
| 113 | + pattern = b"." |
| 114 | + haystack = b"\xFF" |
| 115 | + re = Rure(pattern, flags=CASEI) |
| 116 | + self.assertTrue(re.is_match(haystack)) |
| 117 | + |
| 118 | + def test_compile_error(self): |
| 119 | + try: |
| 120 | + Rure(b"(") |
| 121 | + except RegexSyntaxError as err: |
| 122 | + self.assertIn("Unclosed parenthesis", err.message) |
| 123 | + |
| 124 | + def test_compile_error_size_limit(self): |
| 125 | + try: |
| 126 | + Rure(b"\\w{100}", size_limit=0) |
| 127 | + except CompiledTooBigError as err: |
| 128 | + self.assertIn("exceeds size", err.message) |
0 commit comments