aboutsummaryrefslogtreecommitdiff
path: root/tests/pattern
diff options
context:
space:
mode:
author夏音 / natsuoto.exe <[email protected]>2026-07-01 16:55:53 +0530
committerGitHub <[email protected]>2026-07-01 16:55:53 +0530
commit3d5094a875fbfb167d53278585414fc7884ad521 (patch)
tree11609e05d57cff55dbbe0b81f21c7e2363b7011e /tests/pattern
parente859ce24e5ffc95653023669f9edf4d3a0344a8e (diff)
parenta3a0b80effd34e1ee58e6644ea04e2df2f08dc4b (diff)
downloadedify-3d5094a875fbfb167d53278585414fc7884ad521.tar.xz
edify-3d5094a875fbfb167d53278585414fc7884ad521.zip
feat: convenience char classes .letter() .uppercase() .lowercase() .alphanumeric() (#267)
Adds four convenience ASCII character-class shortcuts so callers don't have to spell out `.range("a","z").range("A","Z")` for the common cases. ## What is new - **Chain methods** on `ClassesMixin` — `.letter()` → `[a-zA-Z]`, `.uppercase()` → `[A-Z]`, `.lowercase()` → `[a-z]`, `.alphanumeric()` → `[a-zA-Z0-9]`. - **Leaf elements** — `LetterElement`, `UppercaseElement`, `LowercaseElement`, `AlphanumericElement` in `edify.elements.types.leaves`, joined into `LeafElement` and `Element` unions and rendered by `edify.compile.leaves.render_leaf`. - **Module constants** — `LETTER`, `UPPERCASE`, `LOWERCASE`, `ALPHANUMERIC` at both `edify.pattern` and the top-level `edify` namespace. ## Example ```python from edify import LETTER, ALPHANUMERIC, START, END, one_or_more username = START + one_or_more(ALPHANUMERIC) + END LETTER.test("Q") # True LETTER.test("4") # False ``` Follows the same dedicated-leaf pattern the existing `.digit()` / `.word()` / `.whitespace_char()` methods use. Closes #132
Diffstat (limited to 'tests/pattern')
-rw-r--r--tests/pattern/classes.test.py30
1 files changed, 30 insertions, 0 deletions
diff --git a/tests/pattern/classes.test.py b/tests/pattern/classes.test.py
index de6bfc0..8a82353 100644
--- a/tests/pattern/classes.test.py
+++ b/tests/pattern/classes.test.py
@@ -3,15 +3,19 @@
import pytest
from edify import (
+ ALPHANUMERIC,
ANY_CHAR,
CARRIAGE_RETURN,
DIGIT,
+ LETTER,
+ LOWERCASE,
NEW_LINE,
NON_DIGIT,
NON_WHITESPACE,
NON_WORD,
NULL_BYTE,
TAB,
+ UPPERCASE,
WHITESPACE,
WORD,
Pattern,
@@ -32,6 +36,10 @@ from edify import (
(CARRIAGE_RETURN, "\\r"),
(TAB, "\\t"),
(NULL_BYTE, "\\0"),
+ (LETTER, "[a-zA-Z]"),
+ (UPPERCASE, "[A-Z]"),
+ (LOWERCASE, "[a-z]"),
+ (ALPHANUMERIC, "[a-zA-Z0-9]"),
],
)
def test_character_class_constant_compiles_to_expected_regex(constant, expected):
@@ -52,7 +60,29 @@ def test_character_class_constant_compiles_to_expected_regex(constant, expected)
CARRIAGE_RETURN,
TAB,
NULL_BYTE,
+ LETTER,
+ UPPERCASE,
+ LOWERCASE,
+ ALPHANUMERIC,
],
)
def test_character_class_constant_is_a_pattern(constant):
assert isinstance(constant, Pattern)
+
+
+ ("constant", "hit_input", "miss_input"),
+ [
+ (LETTER, "A", "4"),
+ (LETTER, "z", " "),
+ (UPPERCASE, "Q", "q"),
+ (LOWERCASE, "q", "Q"),
+ (ALPHANUMERIC, "4", " "),
+ (ALPHANUMERIC, "A", "!"),
+ ],
+)
+def test_convenience_char_class_constant_matches_expected_characters(
+ constant, hit_input, miss_input
+):
+ assert constant.test(hit_input) is True
+ assert constant.test(miss_input) is False