Skip to content

Commit

Permalink
add some tests, fix _has method
Browse files Browse the repository at this point in the history
  • Loading branch information
zydmayday committed Aug 5, 2022
1 parent e18b3d6 commit 8b63020
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
10 changes: 8 additions & 2 deletions ramda/private/_has.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,14 @@ def _has(obj, key):
if key is None:
return isinstance(obj, dict) and key in obj
if isinstance(obj, dict):
return key in obj or hasattr(obj, key)
try:
return key in obj or hasattr(obj, key)
except(TypeError):
return False
if _isArrayLike(obj):
if isinstance(key, int):
return key < len(obj)
return hasattr(obj, key)
try:
return hasattr(obj, key)
except(TypeError):
return False
10 changes: 9 additions & 1 deletion test/test_assocPath.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,16 @@ def test_empty_path_replaces_the_whole_object(self):
def test_replaces_None_with_a_new_object(self):
self.assertEqual({'foo': {'bar': {'baz': 42}}}, R.assocPath(['foo', 'bar', 'baz'], 42, {'foo': None}))

def test_assoc_with_numeric_index(self):
self.assertEqual(['a'], R.assocPath([0], 'a', []))
self.assertEqual([['a']], R.assocPath([0, 0], 'a', []))
self.assertEqual([[None, 'a']], R.assocPath([0, 1], 'a', []))

self.assertEqual({0: 'a'}, R.assocPath([0], 'a', {}))
self.assertEqual({0: ['a']}, R.assocPath([0, 0], 'a', {}))

def test_throws_exception_if_3rd_argument_is_not_array_nor_object(self):
with self.assertRaises(Exception):
with self.assertRaises(Exception, msg='We only support dict or array for assoc'):
R.assocPath([1, 2, 3], 42, 'str')


Expand Down

0 comments on commit 8b63020

Please sign in to comment.