Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor #183

Merged
merged 13 commits into from
Jan 18, 2025
Merged
Prev Previous commit
Next Next commit
refactor: extracted MinMaxValue into min_max_value.py
15r10nk committed Jan 18, 2025
commit 1f27909c968688c2cab32558d5c1cfbc45f52781
97 changes: 4 additions & 93 deletions src/inline_snapshot/_inline_snapshot.py
Original file line number Diff line number Diff line change
@@ -185,10 +185,14 @@ def __eq__(self, other):
return self == other

def __le__(self, other):
from ._snapshot.min_max_value import MinValue

self._change(MinValue)
return self <= other

def __ge__(self, other):
from ._snapshot.min_max_value import MaxValue

self._change(MaxValue)
return self >= other

@@ -258,99 +262,6 @@ def _get_changes(self) -> Iterator[Change]:
return iter(self._changes)


class MinMaxValue(GenericValue):
"""Generic implementation for <=, >="""

@staticmethod
def cmp(a, b):
raise NotImplemented

def _generic_cmp(self, other):
if self._old_value is undefined:
state()._missing_values += 1

if self._new_value is undefined:
self._new_value = clone(other)
if self._old_value is undefined or ignore_old_value():
return True
return _return(self.cmp(self._old_value, other))
else:
if not self.cmp(self._new_value, other):
self._new_value = clone(other)

return _return(self.cmp(self._visible_value(), other))

def _new_code(self):
return self._file._value_to_code(self._new_value)

def _get_changes(self) -> Iterator[Change]:
new_token = value_to_token(self._new_value)
if not self.cmp(self._old_value, self._new_value):
flag = "fix"
elif not self.cmp(self._new_value, self._old_value):
flag = "trim"
elif (
self._ast_node is not None
and self._file._token_of_node(self._ast_node) != new_token
):
flag = "update"
else:
return

new_code = self._file._token_to_code(new_token)

yield Replace(
node=self._ast_node,
file=self._file,
new_code=new_code,
flag=flag,
old_value=self._old_value,
new_value=self._new_value,
)


class MinValue(MinMaxValue):
"""
handles:

>>> snapshot(5) <= 6
True

>>> 6 >= snapshot(5)
True

"""

_current_op = "x >= snapshot"

@staticmethod
def cmp(a, b):
return a <= b

__le__ = MinMaxValue._generic_cmp


class MaxValue(MinMaxValue):
"""
handles:

>>> snapshot(5) >= 4
True

>>> 4 <= snapshot(5)
True

"""

_current_op = "x <= snapshot"

@staticmethod
def cmp(a, b):
return a >= b

__ge__ = MinMaxValue._generic_cmp


T = TypeVar("T")