Skip to content

Commit

Permalink
hiding sure properties upon dir(obj)
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielfalcao committed Mar 15, 2013
1 parent 9fe4187 commit 58d9670
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
25 changes: 24 additions & 1 deletion sure/__init__.py
Expand Up @@ -41,6 +41,25 @@
from sure.six import string_types, text_type, PY3, get_function_code
from sure.six.moves import reduce


def safely_patch_dir(object_handler):
def custom_dir(self):
if not PY3:
try:
meta = list(self.__dict__)
except AttributeError:
meta = list(type(self).__dict__)

object_attrs = set(meta)
else:
object_attrs = set(super().__dir__())

sure_methods = set(POSITIVES + NEGATIVES)
return list(object_attrs.difference(sure_methods))

object_handler['__dir__'] = custom_dir


if PY3:
basestring = str

Expand All @@ -53,6 +72,9 @@
'Maybe you misspelled it ? Well, here are the options: %s'


original_obj_attrs = dir(object)


class VariablesBag(dict):
__varnames__ = None
__sure_actions_ran__ = None
Expand Down Expand Up @@ -886,10 +908,11 @@ def method(self):
return make_safe_property(method, name, prop)

object_handler = patchable_builtin(object)

safely_patch_dir(object_handler)
# None does not have a tp_dict associated to its PyObject, so this
# is the only way we could make it work like we expected.
none = patchable_builtin(None.__class__)

for name in POSITIVES:
object_handler[name] = positive_assertion(name)
none[name] = positive_assertion(name, False)
Expand Down
15 changes: 13 additions & 2 deletions tests/test_cpython_patches.py
Expand Up @@ -15,20 +15,31 @@
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from __future__ import unicode_literals

import sure


def test_it_works_with_objects():
(u"anything that inherits from object should be patched")
("anything that inherits from object should be patched")

(4).should.equal(2 + 2)
"foo".should.equal("f" + ("o" * 2))
{}.should.be.empty


def test_dir_conceals_sure_specific_attributes():
("dir(obj) should conceal names of methods that were grafted by sure")

x = 123

set(dir(x)).intersection(set(sure.POSITIVES)).should.be.empty
set(dir(x)).intersection(set(sure.NEGATIVES)).should.be.empty


# TODO
# def test_it_works_with_non_objects():
# (u"anything that inherits from non-object should also be patched")
# ("anything that inherits from non-object should also be patched")

# class Foo:
# pass
Expand Down
2 changes: 1 addition & 1 deletion tests/test_old_api.py
Expand Up @@ -396,9 +396,9 @@ def __init__(self, kind):

def test_that_raises():
"that(callable, with_args=[arg1], and_kwargs={'arg2': 'value'}).raises(SomeException)"
global called

called = False
global called

def function(arg1=None, arg2=None):
global called
Expand Down

0 comments on commit 58d9670

Please sign in to comment.