Ejemplo n.º 1
0
 def test_WHEN_sequence_contains_exactly_key_pattern_THEN_corresponding_value_SHOULD_be_returned(self):
     cases = [('1', 1, [('1', 1)]),
              ('2', 2, [('1', 1), ('2', 2)])]
     for key_pattern, expected_value, iterable in cases:
         with self.subTest(key_pattern=key_pattern, expected_value=expected_value, iterable=iterable):
             actual = sut.lookup(key_pattern, iterable)
             self._assertIsExactMatch(key_pattern, expected_value, actual)
Ejemplo n.º 2
0
 def test_iterable_should_not_need_to_be_a_list(self):
     # ARRANGE #
     iterable = iter([('1', 1)])
     key_pattern = '1'
     # ACT #
     actual = sut.lookup(key_pattern, iterable)
     # ASSERT #
     self._assertIsExactMatch('1', 1, actual)
Ejemplo n.º 3
0
 def test_iterable_should_not_need_to_be_a_list(self):
     # ARRANGE #
     iterable = iter([('1', 1)])
     key_pattern = '1'
     # ACT #
     actual = sut.lookup(key_pattern, iterable)
     # ASSERT #
     self._assertIsExactMatch('1', 1, actual)
Ejemplo n.º 4
0
 def test_exact_match_should_have_precedence_over_sub_string_match(self):
     # ARRANGE #
     iterable = [('snake skin', 1), ('rattle snake', 2), ('snake', 3)]
     key_pattern = 'snake'
     # ACT #
     actual = sut.lookup(key_pattern, iterable)
     # ASSERT #
     self._assertIsExactMatch('snake', 3, actual)
Ejemplo n.º 5
0
 def test_case_SHOULD_be_ignored(self):
     cases = [('a', 'A', 1, [('A', 1)]),
              ('B', 'b', 2, [('a', 1), ('b', 2)])]
     for key_pattern, expected_key, expected_value, key_value_list in cases:
         with self.subTest(key_pattern=key_pattern,
                           expected_value=expected_value,
                           key_value_list=key_value_list):
             actual = sut.lookup(key_pattern, key_value_list)
             self._assertIsExactMatch(expected_key, expected_value, actual)
Ejemplo n.º 6
0
 def test_case_SHOULD_be_ignored(self):
     cases = [('a', 'A', 1, [('A', 1)]), ('B', 'b', 2, [('a', 1),
                                                        ('b', 2)])]
     for key_pattern, expected_key, expected_value, key_value_list in cases:
         with self.subTest(key_pattern=key_pattern,
                           expected_value=expected_value,
                           key_value_list=key_value_list):
             actual = sut.lookup(key_pattern, key_value_list)
             self._assertIsExactMatch(expected_key, expected_value, actual)
Ejemplo n.º 7
0
 def test_WHEN_sequence_contains_key_pattern_as_sub_string_THEN_corresponding_value_SHOULD_be_returned(self):
     cases = [('b', 'abc', 1, [('abc', 1)]),
              ('ab', 'abc', 2, [('ac', 1), ('abc', 2)])]
     for key_pattern, expected_key, expected_value, iterable in cases:
         with self.subTest(key_pattern=key_pattern,
                           expected_key=expected_key, expected_value=expected_value,
                           iterable=iterable):
             actual = sut.lookup(key_pattern, iterable)
             self._assertIsInexactMatch(expected_key, expected_value, actual)
Ejemplo n.º 8
0
 def test_WHEN_sequence_contains_exactly_key_pattern_THEN_corresponding_value_SHOULD_be_returned(
         self):
     cases = [('1', 1, [('1', 1)]), ('2', 2, [('1', 1), ('2', 2)])]
     for key_pattern, expected_value, iterable in cases:
         with self.subTest(key_pattern=key_pattern,
                           expected_value=expected_value,
                           iterable=iterable):
             actual = sut.lookup(key_pattern, iterable)
             self._assertIsExactMatch(key_pattern, expected_value, actual)
Ejemplo n.º 9
0
 def test_WHEN_sequence_is_empty_THEN_NoMatchError_SHOULD_be_raised(self):
     with self.assertRaises(sut.MultipleMatchesError) as cm:
         # ARRANGE #
         iterable = iter([('aB', 1), ('Bc', 2), ('no match', 3)])
         key_pattern = 'B'
         # ACT #
         actual = sut.lookup(key_pattern, iterable)
         # ASSERT #
         expected_matches = [('aB', 1), ('Bc', 2)]
         self.assertListEqual(expected_matches, cm.ex.matching_key_values)
Ejemplo n.º 10
0
 def test_exact_match_should_have_precedence_over_sub_string_match(self):
     # ARRANGE #
     iterable = [('snake skin', 1),
                 ('rattle snake', 2),
                 ('snake', 3)
                 ]
     key_pattern = 'snake'
     # ACT #
     actual = sut.lookup(key_pattern, iterable)
     # ASSERT #
     self._assertIsExactMatch('snake', 3, actual)
Ejemplo n.º 11
0
 def test_WHEN_sequence_contains_key_pattern_as_sub_string_THEN_corresponding_value_SHOULD_be_returned(
         self):
     cases = [('b', 'abc', 1, [('abc', 1)]),
              ('ab', 'abc', 2, [('ac', 1), ('abc', 2)])]
     for key_pattern, expected_key, expected_value, iterable in cases:
         with self.subTest(key_pattern=key_pattern,
                           expected_key=expected_key,
                           expected_value=expected_value,
                           iterable=iterable):
             actual = sut.lookup(key_pattern, iterable)
             self._assertIsInexactMatch(expected_key, expected_value,
                                        actual)
Ejemplo n.º 12
0
 def test_WHEN_sequence_is_empty_THEN_NoMatchError_SHOULD_be_raised(self):
     with self.assertRaises(sut.MultipleMatchesError) as cm:
         # ARRANGE #
         iterable = iter([('aB', 1),
                          ('Bc', 2),
                          ('no match', 3)])
         key_pattern = 'B'
         # ACT #
         actual = sut.lookup(key_pattern, iterable)
         # ASSERT #
         expected_matches = [('aB', 1),
                             ('Bc', 2)]
         self.assertListEqual(expected_matches, cm.ex.matching_key_values)
Ejemplo n.º 13
0
def lookup_argument(object_name: str, argument_pattern: str,
                    elements: Iterable[Tuple[str, T]]) -> Match[T]:
    try:
        return value_lookup.lookup(argument_pattern, elements)
    except value_lookup.NoMatchError:
        raise HelpError('No matching ' + object_name)
    except value_lookup.MultipleMatchesError as ex:
        matches_list = [
            '  ' + key_value[0] for key_value in ex.matching_key_values
        ]
        header = 'Multiple matches. Which %s do you mean?' % object_name
        lines = [header] + matches_list
        msg = os.linesep.join(lines)
        raise HelpError(msg)
Ejemplo n.º 14
0
 def test_WHEN_no_key_contains_key_pattern_THEN_NoMatchError_SHOULD_be_raised(self):
     with self.assertRaises(sut.NoMatchError):
         sut.lookup('key', [('1', 1), ('2', 2)])
Ejemplo n.º 15
0
 def test_WHEN_sequence_is_empty_THEN_NoMatchError_SHOULD_be_raised(self):
     with self.assertRaises(sut.NoMatchError):
         sut.lookup('key', [])
Ejemplo n.º 16
0
 def test_WHEN_sequence_is_empty_THEN_NoMatchError_SHOULD_be_raised(self):
     with self.assertRaises(sut.NoMatchError):
         sut.lookup('key', [])
Ejemplo n.º 17
0
 def test_WHEN_no_key_contains_key_pattern_THEN_NoMatchError_SHOULD_be_raised(
         self):
     with self.assertRaises(sut.NoMatchError):
         sut.lookup('key', [('1', 1), ('2', 2)])