Commit cb78e6b2 authored by Julien Muchembled's avatar Julien Muchembled

qa: add a basic assertion in Patch to detect when patched code changes

parent 43fdd059
...@@ -409,10 +409,14 @@ class Patch(object): ...@@ -409,10 +409,14 @@ class Patch(object):
Usage: Usage:
with Patch(someObject, attrToPatch=newValue) as patch: with Patch(someObject, [new,] attrToPatch=newValue) as patch:
[... code that runs with patches ...] [... code that runs with patches ...]
[... code that runs without patch ...] [... code that runs without patch ...]
The 'new' positional parameter defaults to False and it must be equal to
not hasattr(someObject, 'attrToPatch')
It is an assertion to detect when a Patch is obsolete.
' as patch' is optional: 'patch.revert()' can be used to revert patches ' as patch' is optional: 'patch.revert()' can be used to revert patches
in the middle of the 'with' clause. in the middle of the 'with' clause.
...@@ -424,7 +428,7 @@ class Patch(object): ...@@ -424,7 +428,7 @@ class Patch(object):
In this case, patches are automatically reverted when 'patch' is deleted. In this case, patches are automatically reverted when 'patch' is deleted.
For patched callables, the new one receives the original value as first For patched callables, the new one receives the original value as first
argument. argument if 'new' is True.
Alternative usage: Alternative usage:
...@@ -448,12 +452,18 @@ class Patch(object): ...@@ -448,12 +452,18 @@ class Patch(object):
return self return self
return patch return patch
def __init__(self, patched, **patch): def __init__(self, patched, *args, **patch):
new, = args or (0,)
(name, patch), = patch.iteritems() (name, patch), = patch.iteritems()
self._patched = patched self._patched = patched
self._name = name self._name = name
try:
wrapped = getattr(patched, name)
except AttributeError:
assert new, (patched, name)
else:
assert not new, (patched, name)
if callable(patch): if callable(patch):
wrapped = getattr(patched, name, None)
func = patch func = patch
patch = lambda *args, **kw: func(wrapped, *args, **kw) patch = lambda *args, **kw: func(wrapped, *args, **kw)
if callable(wrapped): if callable(wrapped):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment