def test_it_should_concatenate_iterables(self): stream = Stream.concat(Stream.range(10), Stream.range(10)) self.assertListEqual(list(stream), list(xrange(10)) + list(xrange(10))) stream = Stream.concat(xrange(10), xrange(10), xrange(10)) self.assertListEqual(list(stream.distinct()), list(xrange(10))) stream = Stream.concat(xrange(10), xrange(10), xrange(10)) self.assertEqual(stream.count(), 30)
def test_no_cache(self): # Make a normal stream. stream = Stream.range(10) # Iterate once self.assertEqual(list(stream), list(range(10))) # Iterate twice self.assertEqual(list(stream), [])
def test_for_each(self): class Apply: def __init__(self): self.total = 0 def apply(self, val): self.total += val stream = Stream.range(3) apply = Apply() stream.for_each(apply.apply) self.assertEqual(apply.total, 0 + 1 + 2)
def test_cache(self): # Make a cached stream. stream = Stream.range(10).cache() # Iterate once self.assertEqual(list(stream), list(range(10))) # Iterate twice, this time from the cache. self.assertEqual(list(stream), list(range(10))) # Now, we make a new, smaller, cached stream fromm our cached stream. stream = stream.cache(5) # Iterate once self.assertEqual(list(stream), list(range(10))) # Iterate twice, this time from the cache. We get the last 5 values. self.assertEqual(list(stream), list(range(5, 10)))
def test_it_should_count_the_number_of_items_in_the_stream(self): stream = Stream.range(100) self.assertEqual(stream.count(), 100)
def test_it_should_iterate_over_an_iterable_multiple_times(self): stream = Stream.range(10) self.assertListEqual(list(stream), list(xrange(10))) self.assertListEqual(list(stream), [])
def test_it_should_reduce_the_stream(self): stream = Stream.range(10) reduced = stream.reduce(add) self.assertEqual(reduced, sum(range(10)))
def test_it_should_reverse_sort_the_stream(self): stream = Stream.range(10) sorted = stream.sorted(reverse=True) self.assertListEqual(list(sorted), list(reversed(range(10))))
def test_range(self): self.assertListEqual(list(Stream.range(100)), list(xrange(100)))
def test_it_should_return_the_first_element_without_consuming_it(self): stream = Stream.range(10) self.assertEqual(stream.first, 0) self.assertEqual(stream.first, 0) self.assertEqual(stream.first, 0) self.assertListEqual(list(stream), list(xrange(10)))
def test_dict_is_created_correctly(self): stream = Stream.range(3).tuplify() self.assertDictEqual(stream.to_dict(), {0: 0, 1: 1, 2: 2})
def test_it_should_map_a_predicate_to_values_in_key_value_pairs(self): stream = Stream.range(10).tuplify() mapped = stream.value_map(lambda v: v**2) self.assertListEqual(list(mapped), [(x, x**2) for x in xrange(10)])
def test_it_should_expand_a_stream_to_tuples(self): tuples = Stream.range(10).tuplify() self.assertListEqual(list(tuples), [(i, i) for i in range(10)]) tuples = Stream.range(10).tuplify(clones=4) self.assertListEqual(list(tuples), [(i, i, i, i) for i in range(10)])
def test_reversed_should_reverse_the_stream(self): stream = Stream.range(10) reverse = reversed(stream) self.assertListEqual(list(reverse), list(reversed(range(10))))
def test_it_should_exclude_items(self): stream = Stream.range(10) odds = stream.exclude(lambda x: x % 2) self.assertListEqual(list(odds), [1, 3, 5, 7, 9])
def test_set_is_created_correctly(self): stream = Stream.range(3) self.assertSetEqual(stream.to_set(), {0, 1, 2})
def test_list_is_created_correctly(self): stream = Stream.range(3) self.assertListEqual(stream.to_list(), [0, 1, 2])
def test_it_should_apply_a_side_effect_to_the_stream(self): side_list = [] stream = Stream.range(10) self.assertEqual(side_list, list(stream.peek(side_list.append)))
def test_it_should_map_a_predicate_to_values_in_key_value_pairs(self): stream = Stream.range(10).tuplify() mapped = stream.value_map(lambda v: v ** 2) self.assertListEqual(list(mapped), [(x, x ** 2) for x in xrange(10)])
def test_it_should_produce_a_range(self): stream = Stream.range(10) self.assertIsInstance(stream, Stream) self.assertEqual(list(stream), list(range(10)))