Example #1
0
 def test_inequality_masks_with_same_punched_pattern_are_equal_but_not_case_D(
         self):
     """case_D: if their Mask.generic_solid_repr was changed between instance creation"""
     Mask.generic_solid_repr = '*'
     mask_case_d__0_1_0 = Mask(size=3)
     mask_case_d__0_1_0._punch_mask([False, True, False])
     self.assertTrue(self.mask1__0_1_0 != mask_case_d__0_1_0)
Example #2
0
class TestMaskBase(unittest.TestCase):
    """Shared setUp() and tearDown() for Mask tests"""
    def setUp(self):
        self.blank_that_remains_blank = Mask(size=12)
        self.blank_ready_to_punch = Mask(size=12)

        self.mask1__0_1_0 = Mask(size=3)
        self.mask1__0_1_0._punch_mask([False, True, False])
        self.mask2__0_1_0 = Mask(size=3)
        self.mask2__0_1_0._punch_mask([False, True, False])

        # all instances of Mask created below, but within setUp() require
        # protected access to inject values and avoid using the methods
        # of Mask or the Mask factories to build
        self.all_punched_mask = Mask(size=12)
        self.all_punched_mask._mask = tuple(PUNCHED for _ in range(12))

        # the two following instances are 'negative' of each other and
        # can be compared via the invert() method
        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))

    def tearDown(self):

        # a hacky fix for the changes of class variable values that create problems in tests:
        Mask.generic_punched_repr = _STANDARD_PUNCHED_REPR
        Mask.generic_solid_repr = _STANDARD_SOLID_REPR
Example #3
0
 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))
Example #4
0
 def test_inequality_masks_with_same_punched_pattern_are_equal_in_case_F(
         self):
     """case_F: if Mask.generic_solid_repr was changed before the two instance compared are created"""
     Mask.generic_solid_repr = '*'
     mask_case_f1__0_1_0 = Mask(size=3)
     mask_case_f1__0_1_0._punch_mask([False, True, False])
     mask_case_f2__0_1_0 = Mask(size=3)
     mask_case_f2__0_1_0._punch_mask([False, True, False])
     self.assertFalse(mask_case_f1__0_1_0 != mask_case_f2__0_1_0)
Example #5
0
 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))
Example #6
0
 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=' '))
Example #7
0
 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))
Example #8
0
    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)
Example #9
0
 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)
Example #10
0
    def setUp(self):
        self.blank_that_remains_blank = Mask(size=12)
        self.blank_ready_to_punch = Mask(size=12)

        self.mask1__0_1_0 = Mask(size=3)
        self.mask1__0_1_0._punch_mask([False, True, False])
        self.mask2__0_1_0 = Mask(size=3)
        self.mask2__0_1_0._punch_mask([False, True, False])

        # all instances of Mask created below, but within setUp() require
        # protected access to inject values and avoid using the methods
        # of Mask or the Mask factories to build
        self.all_punched_mask = Mask(size=12)
        self.all_punched_mask._mask = tuple(PUNCHED for _ in range(12))

        # the two following instances are 'negative' of each other and
        # can be compared via the invert() method
        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))
Example #11
0
 def test_make_mask_from_pattern_pattern_is_size_0(self):
     with self.assertRaises(AssertionError) as context:
         a = Mask.from_pattern(pattern=[])
Example #12
0
 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=' '))
Example #13
0
 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=''))
Example #14
0
 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))
Example #15
0
 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)
Example #16
0
 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)
Example #17
0
 def test_equality_blanks_of_same_size_are_equal(self):
     self.assertTrue(Mask(size=12) == self.blank_that_remains_blank)
Example #18
0
 def test_make_even_mask_from_negative_indices(self):
     indices_to_punch = [-12, -10, -8, -6, -4, -2]
     evens = Mask.from_indices(size=12, indices_to_punch=indices_to_punch)
     self.assertEqual(self.even_are_punched, evens)
Example #19
0
 def test_make_even_mask_from_indices(self):
     indices_to_punch = [0, 2, 4, 6, 8, 10]
     evens = Mask.from_indices(size=12, indices_to_punch=indices_to_punch)
     self.assertEqual(self.even_are_punched, evens)
Example #20
0
 def test_make_mask_from_indices_too_small(self):
     with self.assertRaises(AssertionError) as context:
         indices_to_punch = [-13, -10, -2]
         evens = Mask.from_indices(size=12,
                                   indices_to_punch=indices_to_punch)
         self.assertEqual(self.even_are_punched, evens)
Example #21
0
 def test_make_mask_from_indices_too_large(self):
     with self.assertRaises(AssertionError) as context:
         indices_to_punch = [0, 2, 4, 14, 10]
         evens = Mask.from_indices(size=12,
                                   indices_to_punch=indices_to_punch)
Example #22
0
 def test_make_mask_from_indices_size_is_0(self):
     with self.assertRaises(AssertionError) as context:
         indices_to_punch = [0, 2, 4, 6, 8, 10]
         evens = Mask.from_indices(size=0,
                                   indices_to_punch=indices_to_punch)
Example #23
0
 def test_inequality_blanks_of_same_size_are_equal(self):
     self.assertFalse(Mask(size=12) != self.blank_that_remains_blank)
Example #24
0
 def test_make_odd_mask_from_negative_indices(self):
     indices_to_punch = [-11, -9, -7, -5, -3, -1]
     odds = Mask.from_indices(size=12, indices_to_punch=indices_to_punch)
     self.assertEqual(self.odd_are_punched, odds)
Example #25
0
 def test_mask_with_size_is_0(self):
     with self.assertRaises(AssertionError) as context:
         none_mask = Mask(size=0)
Example #26
0
 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)
Example #27
0
 def test_equality_blanks_of_different_size_are_not_equal(self):
     self.assertFalse(Mask(size=10) == self.blank_that_remains_blank)
Example #28
0
 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))
Example #29
0
 def test_make_odd_mask_from_indices(self):
     indices_to_punch = [1, 3, 5, 7, 9, 11]
     odds = Mask.from_indices(size=12, indices_to_punch=indices_to_punch)
     self.assertEqual(self.odd_are_punched, odds)
Example #30
0
 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)