Mercurial > hgrepos > Python > apps > py-cutils
comparison tests/test_match.py @ 297:141a3aa0b403
First version of converting a glob-style pattern to a regex
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Tue, 04 Mar 2025 01:52:18 +0100 |
| parents | ca293f708cb4 |
| children | 16a5c337fcb9 |
comparison
equal
deleted
inserted
replaced
| 296:ca293f708cb4 | 297:141a3aa0b403 |
|---|---|
| 8 import _test_setup # noqa: F401 imported but unused | 8 import _test_setup # noqa: F401 imported but unused |
| 9 | 9 |
| 10 import sys | 10 import sys |
| 11 import unittest | 11 import unittest |
| 12 | 12 |
| 13 from cutils.util.glob import CharIter | 13 from cutils.util import PY2 |
| 14 from cutils.util.glob import CharIter, glob_to_regexp | |
| 14 | 15 |
| 15 | 16 |
| 16 class TestCharIter(unittest.TestCase): | 17 class TestCharIter(unittest.TestCase): |
| 17 | 18 |
| 18 def test_transitive_iter(self): | 19 def test_transitive_iter(self): |
| 61 chars = "".join(it) | 62 chars = "".join(it) |
| 62 self.assertEqual("234", chars) | 63 self.assertEqual("234", chars) |
| 63 self.assertIsNone(it.peek()) | 64 self.assertIsNone(it.peek()) |
| 64 | 65 |
| 65 | 66 |
| 67 class TestGlobToRegexp(unittest.TestCase): | |
| 68 | |
| 69 def test_empty(self): | |
| 70 self.assertEqual("", glob_to_regexp("")) | |
| 71 | |
| 72 def test_question_mark(self): | |
| 73 self.assertEqual(".", glob_to_regexp("?")) | |
| 74 | |
| 75 def test_single_star(self): | |
| 76 self.assertEqual("[^/]*", glob_to_regexp("*")) | |
| 77 | |
| 78 def test_double_star(self): | |
| 79 self.assertEqual(".*", glob_to_regexp("**")) | |
| 80 | |
| 81 def test_double_star_slash(self): | |
| 82 self.assertEqual("(?:.*/)?", glob_to_regexp("**/")) | |
| 83 | |
| 84 def test_double_star_in_between(self): | |
| 85 if PY2: | |
| 86 # Python 2 escapes all alnum characters in re.escape() | |
| 87 self.assertEqual("part1\\/(?:.*/)?part2", | |
| 88 glob_to_regexp("part1/**/part2")) | |
| 89 else: | |
| 90 | |
| 91 self.assertEqual("part1/(?:.*/)?part2", | |
| 92 glob_to_regexp("part1/**/part2")) | |
| 93 | |
| 94 def test_double_start_in_between2(self): | |
| 95 if PY2: | |
| 96 # Python 2 escapes all alnum characters in re.escape() | |
| 97 self.assertEqual("part1\\/.*\\.py", glob_to_regexp("part1/**.py")) | |
| 98 else: | |
| 99 self.assertEqual("part1/.*\\.py", glob_to_regexp("part1/**.py")) | |
| 100 | |
| 101 def test_bracket_simple(self): | |
| 102 self.assertEqual("[abc]", glob_to_regexp("[abc]")) | |
| 103 | |
| 104 def test_bracket_simple_range(self): | |
| 105 self.assertEqual("[a-c]", glob_to_regexp("[a-c]")) | |
| 106 | |
| 107 def test_bracket_with_special_chars(self): | |
| 108 self.assertEqual("[x*?!^]", glob_to_regexp("[x*?!^]")) | |
| 109 | |
| 110 def test_bracket_simple_range_with_escape(self): | |
| 111 self.assertEqual("[\\\\-c]", glob_to_regexp("[\\-c]")) | |
| 112 | |
| 113 def test_bracket_not_closed(self): | |
| 114 self.assertEqual("\\[a", glob_to_regexp("[a")) | |
| 115 | |
| 116 def test_bracket_not_closed_escapes(self): | |
| 117 self.assertEqual("\\[a\\*\\?", glob_to_regexp("[a*?")) | |
| 118 | |
| 119 def test_bracket_with_dash_as_first_character(self): | |
| 120 self.assertEqual("[\\-a]", glob_to_regexp("[-a]")) | |
| 121 | |
| 122 def test_bracket_with_dash_as_last_character(self): | |
| 123 self.assertEqual("[a\\-]", glob_to_regexp("[a-]")) | |
| 124 | |
| 125 def test_bracket_with_closing_bracket(self): | |
| 126 self.assertEqual("[\\]a]", glob_to_regexp("[]a]")) | |
| 127 | |
| 128 def test_bracket_with_caret_as_first_character(self): | |
| 129 self.assertEqual("[\\^a]", glob_to_regexp("[^a]")) | |
| 130 | |
| 131 def test_bracket_negating_with_dash_as_first_character(self): | |
| 132 self.assertEqual("[^\\-a]", glob_to_regexp("[!-a]")) | |
| 133 | |
| 134 def test_bracket_negating_with_dash_as_last_character(self): | |
| 135 self.assertEqual("[^a\\-]", glob_to_regexp("[!a-]")) | |
| 136 | |
| 137 def test_bracket_negating_with_closing_bracket(self): | |
| 138 self.assertEqual("[^\\]a]", glob_to_regexp("[!]a]")) | |
| 139 | |
| 140 def test_bracket_negating_with_caret_as_first_character(self): | |
| 141 self.assertEqual("[^^a]", glob_to_regexp("[!^a]")) | |
| 142 | |
| 143 def test_simple_escapes(self): | |
| 144 for c in "\\()[]{}*.?": | |
| 145 self.assertEqual("\\"+c, glob_to_regexp("\\"+c)) | |
| 146 | |
| 147 def test_simple_escapes_last_backslash(self): | |
| 148 self.assertEqual("\\\\", glob_to_regexp("\\")) | |
| 149 | |
| 150 def test_auto_escapes(self): | |
| 151 for c in "*.?": | |
| 152 self.assertEqual("\\"+c, glob_to_regexp("\\"+c)) | |
| 153 | |
| 154 def test_group_simple(self): | |
| 155 self.assertEqual("(?:abc|def)", glob_to_regexp("{abc,def}")) | |
| 156 | |
| 157 def test_group_complex_nested(self): | |
| 158 self.assertEqual("(?:abc|(?:[ABQ-Z]|[^A][^/]*))", | |
| 159 glob_to_regexp("{abc,{[ABQ-Z],[!A]*}}")) | |
| 160 | |
| 161 | |
| 66 if __name__ == "__main__": | 162 if __name__ == "__main__": |
| 67 sys.exit(unittest.main()) | 163 sys.exit(unittest.main()) |
