diff options
| author | 夏音 / natsuoto.exe <[email protected]> | 2026-07-01 17:08:33 +0530 |
|---|---|---|
| committer | GitHub <[email protected]> | 2026-07-01 17:08:33 +0530 |
| commit | 2d9e7395dc62ca7ff9c0bf508ca4fb5e823b538e (patch) | |
| tree | f9fb46ce588f8269da5c58072bbf23729aebd0e4 /tests/builder/equality.test.py | |
| parent | 3d5094a875fbfb167d53278585414fc7884ad521 (diff) | |
| parent | 4a0a1fa3d8f7f0a54c36aae34b23b0585eb1fea7 (diff) | |
| download | edify-2d9e7395dc62ca7ff9c0bf508ca4fb5e823b538e.tar.xz edify-2d9e7395dc62ca7ff9c0bf508ca4fb5e823b538e.zip | |
feat: builder ergonomics — __repr__, __eq__/__hash__, .fork()/.copy() (#268)
Three builder-ergonomics wins so `RegexBuilder` / `Pattern` behave like
proper Python values in interactive shells, dicts, and comparisons.
## `__repr__` shows the pattern-so-far
Replaces the useless default `<edify.builder.builder.RegexBuilder object
at 0x...>` with `<RegexBuilder '^\d+'>`. Builders whose frames are still
open render as `<RegexBuilder '<unclosed>'>` so the repr never raises.
## `__eq__` / `__hash__` — value equality
Two builders are equal when their underlying immutable `BuilderState`
matches; the hash derives from the same state. Builders become safe as
dict keys, set members, or assertion targets. `RegexBuilder` and
`Pattern` share every mixin below the class name, so a builder and
pattern with identical state compare equal.
**Breaking:** identity-based `==` on builders becomes value-based. Only
code that intentionally relied on identity equality is affected.
## `.fork()` / `.copy()` — explicit branch construction
Chaining already yields implicit forking (immutable state means
`root.digit()` and `root.word()` share nothing after the split);
`.fork()` and `.copy()` are aliases that surface the property via
autocomplete and signal branching intent in code.
## Example
```python
from edify import RegexBuilder
builder = RegexBuilder().start_of_input().exactly(4).digit().end_of_input()
repr(builder) # <RegexBuilder '^\d{4}$'>
RegexBuilder().digit() == RegexBuilder().digit() # True
lookup = {RegexBuilder().digit(): "hit"} # hashable, dict-key safe
root = RegexBuilder().digit()
branch = root.fork().word() # explicit branch off root
```
Closes #136
Closes #137
Closes #139
Diffstat (limited to 'tests/builder/equality.test.py')
| -rw-r--r-- | tests/builder/equality.test.py | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/tests/builder/equality.test.py b/tests/builder/equality.test.py new file mode 100644 index 0000000..717d1ae --- /dev/null +++ b/tests/builder/equality.test.py @@ -0,0 +1,50 @@ +"""Tests for value-based ``__eq__`` and ``__hash__`` on :class:`BuilderCore`.""" + +from edify import Pattern, RegexBuilder + + +def test_two_builders_with_the_same_chain_are_equal(): + assert RegexBuilder().digit() == RegexBuilder().digit() + + +def test_two_patterns_with_the_same_chain_are_equal(): + assert Pattern().digit() == Pattern().digit() + + +def test_builders_with_different_chains_are_not_equal(): + assert RegexBuilder().digit() != RegexBuilder().word() + + +def test_equal_builders_produce_equal_hashes(): + assert hash(RegexBuilder().digit()) == hash(RegexBuilder().digit()) + + +def test_a_builder_is_usable_as_a_dict_key(): + lookup = {RegexBuilder().digit(): "digit-key"} + assert lookup[RegexBuilder().digit()] == "digit-key" + + +def test_a_builder_can_be_deduplicated_in_a_set(): + dedup = {RegexBuilder().digit(), RegexBuilder().digit(), RegexBuilder().word()} + assert len(dedup) == 2 + + +def test_a_pattern_and_a_regex_builder_with_the_same_state_compare_equal(): + assert Pattern().digit() == RegexBuilder().digit() + + +def test_a_builder_compared_to_a_non_builder_is_not_equal(): + assert (RegexBuilder().digit() == "foo") is False + assert (RegexBuilder().digit() == 42) is False + + +def test_a_builder_compared_to_a_non_builder_returns_not_implemented_from_the_dunder(): + result = RegexBuilder().digit().__eq__("foo") + assert result is NotImplemented + + +def test_hash_of_two_flags_that_differ_are_distinct(): + a = RegexBuilder().ignore_case().digit() + b = RegexBuilder().digit() + assert a != b + assert hash(a) != hash(b) |
