Merge pull request #2554 from hugovk/test-imagemorph2

More unit tests for ImageMorph
This commit is contained in:
wiredfool 2017-06-13 13:06:52 +01:00 committed by GitHub
commit 38be701a9a
2 changed files with 105 additions and 3 deletions

View File

@ -123,7 +123,7 @@ class LutBuilder(object):
.replace('0', 'Z')
.replace('1', '0')
.replace('Z', '1'))
res = '%d' % (1-int(res))
res = 1-int(res)
patterns.append((pattern, res))
return patterns
@ -152,8 +152,8 @@ class LutBuilder(object):
patterns += self._pattern_permute(pattern, options, result)
# # Debugging
# for p,r in patterns:
# print(p,r)
# for p, r in patterns:
# print(p, r)
# print('--')
# compile the patterns into regular expressions for speed

View File

@ -176,6 +176,40 @@ class MorphTests(PillowTestCase):
self.assertEqual(len(coords), 4)
self.assertEqual(tuple(coords), ((2, 2), (4, 2), (2, 4), (4, 4)))
def test_mirroring(self):
# Test 'M' for mirroring
mop = ImageMorph.MorphOp(patterns=['1:(... ... ...)->0',
'M:(00. 01. ...)->1'])
count, Aout = mop.apply(self.A)
self.assertEqual(count, 7)
self.assert_img_equal_img_string(Aout,
"""
.......
.......
..1.1..
.......
.......
.......
.......
""")
def test_negate(self):
# Test 'N' for negate
mop = ImageMorph.MorphOp(patterns=['1:(... ... ...)->0',
'N:(00. 01. ...)->1'])
count, Aout = mop.apply(self.A)
self.assertEqual(count, 8)
self.assert_img_equal_img_string(Aout,
"""
.......
.......
..1....
.......
.......
.......
.......
""")
def test_non_binary_images(self):
im = hopper('RGB')
mop = ImageMorph.MorphOp(op_name="erosion8")
@ -184,6 +218,74 @@ class MorphTests(PillowTestCase):
self.assertRaises(Exception, lambda: mop.match(im))
self.assertRaises(Exception, lambda: mop.get_on_pixels(im))
def test_add_patterns(self):
# Arrange
lb = ImageMorph.LutBuilder(op_name='corner')
self.assertEqual(lb.patterns, ['1:(... ... ...)->0',
'4:(00. 01. ...)->1'])
new_patterns = ['M:(00. 01. ...)->1',
'N:(00. 01. ...)->1']
# Act
lb.add_patterns(new_patterns)
# Assert
self.assertEqual(
lb.patterns,
['1:(... ... ...)->0',
'4:(00. 01. ...)->1',
'M:(00. 01. ...)->1',
'N:(00. 01. ...)->1'])
def test_unknown_pattern(self):
self.assertRaises(
Exception,
lambda: ImageMorph.LutBuilder(op_name='unknown'))
def test_pattern_syntax_error(self):
# Arrange
lb = ImageMorph.LutBuilder(op_name='corner')
new_patterns = ['a pattern with a syntax error']
lb.add_patterns(new_patterns)
# Act / Assert
self.assertRaises(
Exception,
lambda: lb.build_lut())
def test_load_invalid_mrl(self):
# Arrange
invalid_mrl = 'Tests/images/hopper.png'
mop = ImageMorph.MorphOp()
# Act / Assert
self.assertRaises(Exception, lambda: mop.load_lut(invalid_mrl))
def test_roundtrip_mrl(self):
# Arrange
tempfile = self.tempfile('temp.mrl')
mop = ImageMorph.MorphOp(op_name='corner')
initial_lut = mop.lut
# Act
mop.save_lut(tempfile)
mop.load_lut(tempfile)
# Act / Assert
self.assertEqual(mop.lut, initial_lut)
def test_set_lut(self):
# Arrange
lb = ImageMorph.LutBuilder(op_name='corner')
lut = lb.build_lut()
mop = ImageMorph.MorphOp()
# Act
mop.set_lut(lut)
# Assert
self.assertEqual(mop.lut, lut)
if __name__ == '__main__':
unittest.main()