class TestImageCollection():
    pattern = [
        os.path.join(data_dir, pic) for pic in ['camera.png', 'color.png']
    ]
    pattern_matched = [
        os.path.join(data_dir, pic) for pic in ['camera.png', 'moon.png']
    ]

    def setUp(self):
        self.collection = ImageCollection(self.pattern)
        self.collection_matched = ImageCollection(self.pattern_matched)

    def test_len(self):
        assert len(self.collection) == 2

    def test_getitem(self):
        num = len(self.collection)
        for i in range(-num, num):
            assert type(self.collection[i]) is ioImage
        assert_array_almost_equal(self.collection[0], self.collection[-num])

        #assert_raises expects a callable, hence this do-very-little func
        def return_img(n):
            return self.collection[n]

        assert_raises(IndexError, return_img, num)
        assert_raises(IndexError, return_img, -num - 1)

    def test_slicing(self):
        assert type(self.collection[:]) is ImageCollection
        assert len(self.collection[:]) == 2
        assert len(self.collection[:1]) == 1
        assert len(self.collection[1:]) == 1
        assert_array_almost_equal(self.collection[0], self.collection[:1][0])
        assert_array_almost_equal(self.collection[1], self.collection[1:][0])
        assert_array_almost_equal(self.collection[1], self.collection[::-1][0])
        assert_array_almost_equal(self.collection[0], self.collection[::-1][1])

    def test_files_property(self):
        assert isinstance(self.collection.files, list)

        def set_files(f):
            self.collection.files = f

        assert_raises(AttributeError, set_files, 'newfiles')

    def test_custom_load(self):
        load_pattern = [(1, 'one'), (2, 'two')]

        def load_fn(x):
            return x

        ic = ImageCollection(load_pattern, load_func=load_fn)
        assert_equal(ic[1], (2, 'two'))

    def test_concatenate(self):
        ar = self.collection_matched.concatenate()
        assert_equal(ar.shape, (len(self.collection_matched), ) +
                     self.collection[0].shape)
        assert_raises(ValueError, self.collection.concatenate)