def test_mask_invert_apply_to_str(self): """not quite unit test --> chaining of Mask methods""" input_str = 'all your bases are belong to us' pattern = '^^^------^^^^^-----^^^^^^----^^' expected = '--- your ----- are ------ to --' mask = Mask.from_pattern(pattern=pattern, values_to_punch='^') self.assertEqual(expected, mask.invert().apply_to(input_str))
def test_mask_invert_apply_to_str_with_substitute(self): """not quite unit test --> chaining of Mask methods""" input_str = 'all your bases are belong to us' pattern = '^^^------^^^^^-----^^^^^^----^^' expected = ' your are to ' mask = Mask.from_pattern(pattern=pattern, values_to_punch='^') self.assertEqual(expected, mask.invert().apply_to(input_str, substitute=' '))
def test_str_when_Mask_generic_solid_repr_is_changed_and_Mask_generic_punched_repr_is_changed( self): """Mask.generic_solid_repr is changed to '*' and Mask.generic_punched_repr is changed to 'w'""" Mask.generic_solid_repr = '*' Mask.generic_punched_repr = 'w' mask_1_0_1 = Mask.from_pattern('^-^') expected = 'w*w' self.assertEqual(expected, str(mask_1_0_1))
def test_apply_to_sequence_of_different_size_than_mask(self): with self.assertRaises(AssertionError) as context: input_str = 'all your bases are belong to us' unmatching_pattern = '-----------------------------' # pattern size != input_str size dummy_expected = '-------------------------------' mask = Mask.from_pattern(pattern=unmatching_pattern, values_to_punch='^') self.assertEqual(dummy_expected, mask.apply_to(input_str))
def test_make_evenly_punched_from_collection_of_values_to_punch(self): """this takes a sequence of values to punch, and assembles it in a pattern where locations at an even indice must be punched, and locations at odd indices must remain solid. It then creates a mask using this pattern, and the same values_to_punch provided in a diferent order to the factory""" size = 12 values_to_punch_in_order = ['3', ' ', 'x', '?', 'w', 't'] values_to_punch_in_disorder = set(elt for elt in ('t', '3', 'x', ' ', 'w', '?')) pattern = ''.join(values_to_punch_in_order[idx // 2] if idx % 2 == 0 else '-' for idx in range(size)) evenly_punched = Mask.from_pattern( pattern=pattern, values_to_punch=values_to_punch_in_disorder) self.assertTrue(evenly_punched == self.even_are_punched)
def setUp(self): # @TODO: use mocks for Mask? self.even_are_punched = Mask(size=12) self.even_are_punched._mask = tuple(SOLID if pos % 2 else PUNCHED for pos in range(12)) self.odd_are_punched = Mask(size=12) self.odd_are_punched._mask = tuple(PUNCHED if pos % 2 else SOLID for pos in range(12)) masks_eo = [ self.even_are_punched, self.odd_are_punched, self.even_are_punched, self.odd_are_punched, self.even_are_punched, self.odd_are_punched, self.even_are_punched, self.odd_are_punched, self.even_are_punched, self.odd_are_punched, self.even_are_punched, self.odd_are_punched, ] self.stencil_12_12_alternate_even_odd = Stencil.from_masks(masks_eo) masks_oe = [ self.odd_are_punched, self.even_are_punched, self.odd_are_punched, self.even_are_punched, self.odd_are_punched, self.even_are_punched, self.odd_are_punched, self.even_are_punched, self.odd_are_punched, self.even_are_punched, self.odd_are_punched, self.even_are_punched ] self.stencil_12_12_alternate_odd_even = Stencil.from_masks(masks_oe) self.seq_of_seq_1 = [ ' 0 1 2 3', ' 4 5 6 7', ' 8 9 10 11', '12 13 14 15' ] assert all( len(seq) == len(self.seq_of_seq_1[0]) for seq in self.seq_of_seq_1) patterns = ['^^^ ^ ^ ', ' ^^^^ ^ ', ' ^ ^^^^^^', '^^^ ^ ^ '] masks = [Mask.from_pattern(pattern) for pattern in patterns] self.stencil_1 = Stencil.from_masks(masks=masks)
def test_from_masks(self): mask1__0_1_0 = Mask(size=3) mask1__0_1_0.from_pattern('_^_') stencil = Stencil.from_masks([mask1__0_1_0, mask1__0_1_0]) self.assertIsInstance(stencil, Stencil)
def test_mask_apply_to_str_with_substitute(self): input_str = 'all your bases are belong to us' pattern = '^^^------^^^^^-----^^^^^^----^^' expected = 'all bases belong us' mask = Mask.from_pattern(pattern=pattern, values_to_punch='^') self.assertEqual(expected, mask.apply_to(input_str, substitute=' '))
def test_all_solid_apply_to_str_with_substitute_is_empty_str(self): input_str = 'all your bases are belong to us' pattern = '-------------------------------' expected = '-------------------------------' mask = Mask.from_pattern(pattern=pattern, values_to_punch='^') self.assertEqual(pattern, mask.apply_to(input_str, substitute=''))
def test_mask_apply_to_str_1(self): input_str = 'all your bases are belong to us' pattern = '^^^------^^^^^-----^^^^^^----^^' expected = 'all------bases-----belong----us' mask = Mask.from_pattern(pattern=pattern, values_to_punch='^') self.assertEqual(expected, mask.apply_to(input_str))
def test_all_solid_apply_to_str(self): input_str = 'all your bases are belong to us' pattern = '-------------------------------' expected = '-------------------------------' mask = Mask.from_pattern(pattern=pattern, values_to_punch='^') self.assertEqual(expected, mask.apply_to(input_str))
def test_make_evenly_punched(self): size = 12 pattern = ''.join('^' if idx % 2 == 0 else 'w' for idx in range(size)) evenly_punched = Mask.from_pattern(pattern=pattern) self.assertTrue(evenly_punched == self.even_are_punched)
def test_make_oddly_punched(self): size = 12 pattern = ''.join('^' if idx % 2 == 1 else 'w' for idx in range(size)) oddly_punched = Mask.from_pattern(pattern=pattern) self.assertTrue(oddly_punched == self.odd_are_punched)
def test_make_fully_punched(self): size = 12 pattern = '^' * size fully_punched = Mask.from_pattern(pattern=pattern) self.assertTrue(fully_punched == self.all_punched_mask)
def test_make_mask_from_pattern_pattern_is_size_0(self): with self.assertRaises(AssertionError) as context: a = Mask.from_pattern(pattern=[])