Beispiel #1
0
 def test_lengthEmptySequence(self):
     """
     Verify that L{SlicedView.__len__} works with empty sequences.
     """
     sequence = []
     view = SlicedView([], slice(None))
     self.assertEqual(len(view), 0)
Beispiel #2
0
 def test_negativeStartLength(self):
     """
     Similar to L{test_length}, but for a L{SlicedView} constructed with a
     slice with a negative start value.
     """
     sequence = ['a', 'b', 'c']
     view = SlicedView(sequence, slice(-2, None))
     self.assertEqual(len(view), 2)
Beispiel #3
0
 def test_extendedSliceLength(self):
     """
     Verify that L{SlicedView.__len__} reports the correct length when a
     step is present.
     """
     sequence = ['a', 'b', 'c', 'd', 'e']
     view = SlicedView(sequence, slice(1, -1, 2))
     self.assertEqual(len(view), 2)
Beispiel #4
0
 def test_negativeStartNegativeStopLength(self):
     """
     Similar to L{test_length}, but for a L{SlicedView} constructed with a
     slice with negative start and stop values.
     """
     sequence = ['a', 'b', 'c']
     view = SlicedView(sequence, slice(-2, -1))
     self.assertEqual(len(view), 1)
Beispiel #5
0
 def test_positiveStopLength(self):
     """
     Similar to L{test_length}, but for a L{SlicedView} constructed with a
     slice with a positive stop value.
     """
     sequence = ['a', 'b', 'c']
     view = SlicedView(sequence, slice(None, 2))
     self.assertEqual(len(view), 2)
Beispiel #6
0
 def test_negativeStartNegativeStopNegativeIndices(self):
     """
     Similar to L{test_negativeStartPositiveStopPositiveIndices}, but cover
     negative requested indices instead of positive ones.
     """
     sequence = ['a', 'b', 'c']
     view = SlicedView(sequence, slice(-2, -1))
     self.assertEqual(view[-1], 'b')
     self.assertRaises(IndexError, getitem, view, -2)
Beispiel #7
0
 def test_negativeStartNegativeStopPositiveIndices(self):
     """
     Similar to L{test_negativeStartPositiveStopPositiveIndices}, but for a
     negative slice stop.
     """
     sequence = ['a', 'b', 'c']
     view = SlicedView(sequence, slice(-2, -1))
     self.assertEqual(view[0], 'b')
     self.assertRaises(IndexError, getitem, view, 1)
Beispiel #8
0
 def test_outOfBoundsNegativeStop(self):
     """
     Verify that the C{__getitem__} of a L{SlicedView} constructed with a
     slice with a negative stop value greater than the maximum allowed index
     clips that stop value to the beginning of the underlying sequence.
     """
     sequence = ['a', 'b', 'c']
     view = SlicedView(sequence, slice(None, -4))
     self.assertRaises(IndexError, getitem, view, 0)
Beispiel #9
0
 def test_slice(self):
     """
     Verify a L{SlicedView} itself can be sliced.
     """
     sequence = ['a', 'b', 'c']
     view = SlicedView(sequence, slice(1, None))
     viewView = view[1:]
     self.assertIdentical(viewView.sequence, view)
     self.assertEqual(viewView.bounds, slice(1, None, None))
Beispiel #10
0
 def test_length(self):
     """
     Verify that L{SlicedView.__len__} returns the length of the underlying
     sequence when the SlicedView is constructed with no start or stop
     values.
     """
     sequence = ['a', 'b', 'c']
     view = SlicedView(sequence, slice(None))
     self.assertEqual(len(view), 3)
Beispiel #11
0
 def test_outOfBoundsPositiveStart(self):
     """
     Verify that the C{__getitem__} of a L{SlicedView} constructed with a
     slice with a positive start value greater than the maximum allowed
     index clips that start value to the end of the underlying sequence.
     """
     sequence = ['a', 'b', 'c']
     view = SlicedView(sequence, slice(3, None))
     self.assertRaises(IndexError, getitem, view, 0)
Beispiel #12
0
 def test_negativeStopOnlyPositiveIndices(self):
     """
     Similar to L{test_positiveStopOnlyPositiveIndices}, but test a
     L{SlicedView} created with a slice with a negative stop value.
     """
     sequence = ['a', 'b', 'c']
     view = SlicedView(sequence, slice(None, -1))
     self.assertEqual(view[0], 'a')
     self.assertEqual(view[1], 'b')
     self.assertRaises(IndexError, getitem, view, 2)
Beispiel #13
0
 def test_positveStopOnlyNegativeIndices(self):
     """
     Similar to L{test_positiveStopOnlyPositiveIndices}, but cover negative
     requested indices instead of positive ones.
     """
     sequence = ['a', 'b', 'c']
     view = SlicedView(sequence, slice(None, 2))
     self.assertEqual(view[-1], 'b')
     self.assertEqual(view[-2], 'a')
     self.assertRaises(IndexError, getitem, view, -3)
Beispiel #14
0
 def test_negativeStartOnlyPositiveIndices(self):
     """
     Similar to L{test_positiveStartOnlyPositiveIndices}, but for the case
     of a negative slice start value.
     """
     sequence = ['a', 'b', 'c']
     view = SlicedView(sequence, slice(-2, None))
     self.assertEqual(view[0], 'b')
     self.assertEqual(view[1], 'c')
     self.assertRaises(IndexError, getitem, view, 2)
Beispiel #15
0
 def test_positiveStepPositiveIndices(self):
     """
     Verify that a positive step produces the correct results, skipping over
     the appropriate elements.
     """
     sequence = ['a', 'b', 'c', 'd', 'e']
     view = SlicedView(sequence, slice(1, -1, 2))
     self.assertEqual(view[0], 'b')
     self.assertEqual(view[1], 'd')
     self.assertRaises(IndexError, getitem, view, 2)
Beispiel #16
0
 def test_negativeStepNegativeIndices(self):
     """
     Verify that a negative step produces the correct results, skipping over
     the appropriate elements.
     """
     sequence = ['a', 'b', 'c', 'd', 'e']
     view = SlicedView(sequence, slice(-1, 1, -2))
     self.assertEqual(view[-1], 'c')
     self.assertEqual(view[-2], 'e')
     self.assertRaises(IndexError, getitem, view, -3)
Beispiel #17
0
 def test_negativeIndices(self):
     """
     Similar to L{test_positiveIndices}, but for negative indices.
     """
     sequence = ['a', 'b', 'c']
     view = SlicedView(sequence, slice(None))
     self.assertEqual(view[-1], 'c')
     self.assertEqual(view[-2], 'b')
     self.assertEqual(view[-3], 'a')
     self.assertRaises(IndexError, getitem, view, -4)
Beispiel #18
0
 def __getitem__(self, index):
     """
     Retrieve the storeID field of the requested hit, converting it to an
     integer before returning it.  This handles integer indexes as well as
     slices.
     """
     if isinstance(index, slice):
         return SlicedView(self, index)
     if index >= len(self.hits):
         raise IndexError(index)
     return _PyLuceneHitWrapper(self.hits[index])
Beispiel #19
0
 def test_positiveStartOnlyPositiveIndices(self):
     """
     Verify that the C{__getitem__} of a L{SlicedView} constructed with a
     slice with only a positive start value returns elements at the
     requested index plus the slice's start value for positive requested
     indices.
     """
     sequence = ['a', 'b', 'c']
     view = SlicedView(sequence, slice(1, None))
     self.assertEqual(view[0], 'b')
     self.assertEqual(view[1], 'c')
     self.assertRaises(IndexError, getitem, view, 2)
Beispiel #20
0
 def test_positiveIndices(self):
     """
     Verify that the C{__getitem__} of a L{SlicedView} constructed with a
     slice with no start or stop value behaves in the same way as the
     underlying sequence with respect to indexing with positive values.
     """
     sequence = ['a', 'b', 'c']
     view = SlicedView(sequence, slice(None))
     self.assertEqual(view[0], 'a')
     self.assertEqual(view[1], 'b')
     self.assertEqual(view[2], 'c')
     self.assertRaises(IndexError, getitem, view, 3)
Beispiel #21
0
 def test_positiveStopOnlyPositiveIndices(self):
     """
     Verify that L{__getitem__} of L{SlicedView} constructed with a slice
     with a positive stop value returns elements at the requested index for
     indices less than the stop value and raises IndexError for positive
     requested indices greater than or equal to the stop value.
     """
     sequence = ['a', 'b', 'c']
     view = SlicedView(sequence, slice(None, 2))
     self.assertEqual(view[0], 'a')
     self.assertEqual(view[1], 'b')
     self.assertRaises(IndexError, getitem, view, 2)
Beispiel #22
0
 def test_positiveStartNegativeStopPositiveIndices(self):
     """
     Verify that L{__getitem__} of a L{SlicedView} constructed with a slice
     with a positive start and a negative stop value returns elements at the
     requested index plus the slice's start value for positive requested
     indices within the bounds defined by the stop value and raises an
     IndexError for positive requested indices outside those bounds.
     """
     sequence = ['a', 'b', 'c']
     view = SlicedView(sequence, slice(1, -1))
     self.assertEqual(view[0], 'b')
     self.assertRaises(IndexError, getitem, view, 1)
Beispiel #23
0
 def test_positiveStartPositiveStopPositiveIndices(self):
     """
     Verify that L{__getitem__} of L{SlicedView} constructed with a slice
     with positive start and stop values returns elements at the requested
     index plus the slice's start value for positive requested indices less
     than the difference between the stop and start values and raises
     IndexError for positive requested indices greater than or equal to that
     difference.
     """
     sequence = ['a', 'b', 'c', 'd']
     view = SlicedView(sequence, slice(1, 3))
     self.assertEqual(view[0], 'b')
     self.assertEqual(view[1], 'c')
     self.assertRaises(IndexError, getitem, view, 2)