Skip to content

Commit

Permalink
Merge pull request #185 from asmeurer/slice-constructor-tests
Browse files Browse the repository at this point in the history
Some more tests for the Slice constructor
  • Loading branch information
asmeurer authored Sep 10, 2024
2 parents 0c2f384 + e3caa0f commit 45558a0
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 3 deletions.
23 changes: 23 additions & 0 deletions ndindex/tests/test_integer.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,29 @@ def test_integer_args():
raises(TypeError, lambda: Integer(True))
raises(TypeError, lambda: Integer(bool_(True)))


class HasIndex:
def __init__(self, x):
self.x = x

def __index__(self):
return self.x

idx = Integer(HasIndex(0))
assert idx.args == (0,)
assert idx.raw == 0
assert type(idx.args[0]) is int
assert type(idx.raw) is int

class HasInt:
def __init__(self, x):
self.x = x

def __int__(self):
return self.x # pragma: no cover

raises(TypeError, lambda: Integer(HasInt(0)))

def test_integer_exhaustive():
a = arange(10)
for i in range(-12, 12):
Expand Down
35 changes: 32 additions & 3 deletions ndindex/tests/test_slice.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,54 @@ def test_slice_args():
raises(TypeError, lambda: slice())
raises(TypeError, lambda: Slice())
raises(TypeError, lambda: Slice(1.0))
raises(TypeError, lambda: Slice('1'))
# See docstring of operator_index()
raises(TypeError, lambda: Slice(True))
raises(TypeError, lambda: Slice(bool_(True)))

S = Slice(1)
assert S == Slice(S) == Slice(None, 1) == Slice(None, 1, None) == Slice(None, 1, None)
assert S == Slice(S) == Slice(slice(1)) == Slice(None, 1) == Slice(None, 1, None) == Slice(None, 1, None)
assert S.raw == slice(None, 1, None)
assert S.args == (S.start, S.stop, S.step)

S = Slice(0, 1)
assert S == Slice(S) == Slice(0, 1, None)
assert S == Slice(S) == Slice(slice(0, 1)) == Slice(0, 1, None)
assert S.raw == slice(0, 1, None)
assert S.args == (S.start, S.stop, S.step)

S = Slice(0, 1, 2)
assert S == Slice(S)
assert S == Slice(S) == Slice(slice(0, 1, 2))
assert S.raw == slice(0, 1, 2)
assert S.args == (S.start, S.stop, S.step)

class HasIndex:
def __init__(self, x):
self.x = x

def __index__(self):
return self.x

S = Slice(HasIndex(0), HasIndex(1), HasIndex(2))
assert S == Slice(0, 1, 2)
assert S.args == (0, 1, 2)
assert type(S.start) is int
assert type(S.stop) is int
assert type(S.step) is int
assert type(S.args[0]) is int
assert type(S.args[1]) is int
assert type(S.args[2]) is int

class HasInt:
def __init__(self, x):
self.x = x

def __int__(self):
return self.x # pragma: no cover

raises(TypeError, lambda: Slice(HasInt(0), None))
raises(TypeError, lambda: Slice(None, HasInt(0)))
raises(TypeError, lambda: Slice(None, None, HasInt(0)))

def test_slice_exhaustive():
for n in range(100):
a = arange(n)
Expand Down

0 comments on commit 45558a0

Please sign in to comment.