def test_filter(self): with self.subTest('Some :: true'): self.assertEqual(Some2(1).filter(bool).unwrap(), 1) with self.subTest('Some :: false'): self.assertTrue(Some2(0).filter(bool).is_none()) with self.subTest('None'): self.assertTrue(None2.filter(bool).is_none())
def test_xor(self): with self.subTest('Some ^ Some'): self.assertTrue(Some2(1).xor(Some2(2)).is_none()) with self.subTest('Some ^ None'): self.assertEqual(Some2(1).xor(None2).unwrap(), 1) with self.subTest('None ^ Some'): self.assertTrue(None2.xor(Some2(2)).unwrap(), 2) with self.subTest('None ^ None'): self.assertTrue(None2.xor(None2).is_none())
def spy(self, n=1, *, allow_partial=False): if not isinstance(self._iterator, SpyIterator): self._iterator = SpyIterator(self._iterator) ok, items = self._iterator.spy(n, allow_partial=allow_partial) if ok: return Some2(items) else: return None2
def take_last_into_option(self, number=1): ''' Returns chunk of *exactly* `number`-items from the end of iterator wrapped in `Some2`. In case of insufficient iterator returns `None2` and *returns* read items back to iterator. Note: If `None2` is returned then iterator is not invalidated! :param number: :return: Example: >>> it = Iterator2([1, 2, 3]); (it.take_into_option(4).is_none(), it.take_into_option(2).unwrap()) (True, (1, 2)) ''' taken = self.take_last_now(number) if len(taken) == number: return Some2(taken) else: self._iterator = iter( taken ) # returns back, `taken` contains all items *before* take-call return None2
def position(self, predicate, *, count_from=0): # Note: `algorithms.searching.position` returns number, `base_on` places it into `self._iterator` idx: int = self._iterator return None2 if idx < 0 else Some2(idx)
def next(self): try: return Some2(next(self._iterator)) except StopIteration: return None2
def test_or_else(self): with self.subTest('Some'): self.assertEqual( Some2(1).or_else(lambda: Some2(object())).unwrap(), 1) with self.subTest('None'): self.assertEqual(None2.or_else(lambda: 1), 1)
def test_or(self): with self.subTest('Some'): self.assertEqual(Some2(1).or_(Some2(object())).unwrap(), 1) with self.subTest('None'): self.assertEqual(None2 | 1, 1)
def test_expect(self): with self.subTest('Some'): self.assertEqual(Some2(1).expect(RuntimeError), 1) with self.subTest('None'): with self.assertRaises(RuntimeError): None2.expect(RuntimeError, 'lol')
def test_and(self): with self.subTest('Some'): self.assertEqual((Some2(object()) & Some2(1)).unwrap(), 1) with self.subTest('None'): self.assertTrue(None2.and_(object()).is_none())
def test_map_or_else(self): with self.subTest('Some'): self.assertEqual(Some2(-1).map_or_else(lambda: 2, abs), 1) with self.subTest('None'): self.assertEqual(None2.map_or_else(lambda: 2, abs), 2)
def test_bool(self): with self.subTest('Some'): self.assertTrue(Some2(1)) with self.subTest('None'): self.assertFalse(None2)
def test_map(self): with self.subTest('Some'): self.assertEqual(Some2(-1).map(abs).unwrap(), 1) with self.subTest('None'): self.assertTrue(None2.map(abs).is_none())
def test_unwrap_or_else(self): with self.subTest('Some'): self.assertEqual(Some2(1).unwrap_or_else(lambda: 2), 1) with self.subTest('None'): self.assertEqual(None2.unwrap_or_else(lambda: 2), 2)
def test_unwrap(self): with self.subTest('Some'): self.assertEqual(Some2(1).unwrap(), 1) with self.subTest('None'): with self.assertRaises(RuntimeError): None2.unwrap()
def test_is_none(self): with self.subTest('Some'): self.assertFalse(Some2(1).is_none()) with self.subTest('None'): self.assertTrue(None2.is_none())
def test_filter_none(self): self.assertEqual( iter2([Some2(1), None2, Some2(3)]).filter_none().to_tuple(), (1, 3))
def test_and_then(self): with self.subTest('Some'): self.assertEqual( Some2(object()).and_then(lambda: Some2(1)).unwrap(), 1) with self.subTest('None'): self.assertTrue(None2.and_then(lambda: Some2(object())).is_none())