8000 Implement is_sorted for collections by selakavon · Pull Request #99 · assertpy/assertpy · GitHub
[go: up one dir, main page]

Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion assertpy/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

import operator
import sys
import collections

Expand Down Expand Up @@ -90,3 +90,35 @@ def is_subset_of(self, *supersets):
self._err('Expected <%s> to be subset of %s, but %s %s missing.' % (self.val, self._fmt_items(superset), self._fmt_items(missing), 'was' if len(missing) == 1 else 'were'))

return self

def is_sorted(self, compare_op=operator.ge):
"""Asserts that value is sorted by at least one of the provided compare functions."""

# Make compare_op a list
try:
self._check_iterable(compare_op)
compare_ops = compare_op
except TypeError as e:
compare_ops = [compare_op]

# Loop through compare_ops and find two unsorted items.
op_it = iter(compare_ops)
sorted_by_any = False

while not sorted_by_any:
try:
op = next(op_it)
except StopIteration as si:
break

sorted_by_any = True

for i in range(len(self.val) - 1):
if not op(self.val[i + 1], self.val[i]):
sorted_by_any = False
break

if not sorted_by_any:
self._err('Expected value to be sorted by any of %s, but was not.' % str(compare_ops))

return self
33 changes: 31 additions & 2 deletions tests/test_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

import operator
import sys
import collections

Expand Down Expand Up @@ -162,5 +162,34 @@ def test_is_subset_of_bad_arg_failure():
assert_that(str(ex)).is_equal_to('one or more superset args must be given')

def test_chaining():
assert_that(['a','b','c']).is_iterable().is_type_of(list).is_length(3)
assert_that(['a','b','c']).is_iterable().is_type_of(list).is_sorted().is_length(3)

def test_is_sorted_default_compare_op():
assert_that([1,2,5,6]).is_sorted()

def test_is_sorted_default_compare_op_failure():
try:
assert_that([1,5,2,6]).is_sorted()
assert fail("should have raised error")
except AssertionError as ae:
pass

def test_is_sorted_one_compare_op():
assert_that([6, 5, 2, 1]).is_sorted(operator.le)

def test_is_sorted_one_compare_op_failure():
try:
assert_that([6, 2, 5, 1]).is_sorted(operator.le)
assert fail("should have raised error")
except AssertionError as ae:
pass

def test_is_sorted_multiple_compare_ops():
assert_that(["d", "c", "b", "a"]).is_sorted([operator.ge, operator.le])

def test_is_sorted_multiple_compare_ops_failure():
try:
assert_that(["d", "b", "c", "a"]).is_sorted([operator.ge, operator.le])
assert fail("should have raised error")
except AssertionError as ae:
pass
0