Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ### Update 0:07 1/14/2020 Didn't test the tests. I was checking the opposite of what I wanted, which resulted in switching the functionality of __gt__ with __lt__ (kind of). It's since been fixed (although the SO question was also answered, so this code can probably, safely be ignored)
- from typing import Iterator
- from unittest import TestCase
- class ILengthComparator:
- def __init__(self, iterator: Iterator):
- self.iterator = iterator
- def has_more(self) -> bool:
- try:
- next(self.iterator)
- except StopIteration:
- return False
- return True
- def __eq__(self, length: int) -> bool:
- if length < 0:
- return False
- if not self.has_more():
- return length == 0
- return self == length - 1
- def __gt__(self, length: int) -> bool:
- if length < 0:
- return True
- if not self.has_more():
- return False
- return self > length - 1
- def __lt__(self, length: int) -> bool:
- if length < 1:
- return False
- if not self.has_more():
- return True
- return self < length - 1
- class TestCmpIterLength(TestCase):
- def test__eq(self):
- test_inputs = [(iter(range(10)), i) for i in range(-5, 15)]
- expected_outputs = [10 == i for i in range(-5, 15)]
- actual_outputs = [ILengthComparator(test_iter) == test_length for test_iter, test_length in test_inputs]
- self.assertEqual(expected_outputs, actual_outputs)
- def test__lt(self):
- test_inputs = [(iter(range(10)), i) for i in range(-5, 15)]
- expected_outputs = [10 < i for i in range(-5, 15)]
- actual_outputs = [ILengthComparator(test_iter) < test_length for test_iter, test_length in test_inputs]
- self.assertEqual(expected_outputs, actual_outputs)
- def test__gt(self):
- test_inputs = [(iter(range(10)), i) for i in range(-5, 15)]
- expected_outputs = [10 > i for i in range(-5, 15)]
- actual_outputs = [ILengthComparator(test_iter) > test_length for test_iter, test_length in test_inputs]
- self.assertEqual(expected_outputs, actual_outputs)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement