Beispiel #1
0
 def test_plain_field_plain_field_plain(self):
     self.assertEqual(library.split_message('Kappa{}Kappa{}Kappa'), [
         CustomFieldParts('Kappa', '', None, None, None, None, None, '{}'),
         CustomFieldParts('Kappa', '', None, None, None, None, None, '{}'),
         CustomFieldParts('Kappa', None, None, None, None, None, None,
                          None),
     ])
Beispiel #2
0
 def test_plain_escape(self):
     self.assertEqual(
         library.split_message('{{'),
         [CustomFieldParts('{', None, None, None, None, None, None, None)])
     self.assertEqual(
         library.split_message('}}'),
         [CustomFieldParts('}', None, None, None, None, None, None, None)])
Beispiel #3
0
 def test_plain(self):
     self.assertEqual(library.split_message('Kappa'), [
         CustomFieldParts('Kappa', None, None, None, None, None, None, None)
     ])
     self.assertEqual(library.split_message('Kappa KappaPride KappaRoss'), [
         CustomFieldParts('Kappa KappaPride KappaRoss', None, None, None,
                          None, None, None, None)
     ])
Beispiel #4
0
 def test_multiple_escape(self):
     self.assertEqual(library.split_message('{{{:::<<<>>>@@@!!!}}}'), [
         CustomFieldParts('{', ':', '<', '>', '@', '!', '}',
                          '{:::<<<>>>@@@!!!}}}')
     ])
     self.assertEqual(library.split_message('{{{{:<>@!}}}}'), [
         CustomFieldParts('{{:<>@!}}', None, None, None, None, None, None,
                          None)
     ])
Beispiel #5
0
 async def test_plain_field_plain(self):
     self.mock_split.return_value = [
         CustomFieldParts('Kappa ', 'field', 'format', 'prefix', 'suffix',
                          'param', 'default', 'original'),
         CustomFieldParts(' Kappa', None, None, None, None, None, None,
                          None)
     ]
     self.assertEqual(
         await library.create_messages(self.command, self.args),
         ['Kappa default Kappa'])
     self.data.hasFeature.assert_called_once_with('botgotsthis',
                                                  'textconvert')
     self.mock_split.assert_called_once_with('Kappa KappaRoss KappaPride')
     self.mock_convert.assert_called_once_with(TypeMatch(CustomFieldArgs))
     self.mock_format.assert_called_once_with('default', 'format', False)
Beispiel #6
0
 def test_field_format_prefix_suffix_param_default(self):
     self.assertEqual(
         library.split_message(
             '{field:format<prefix>suffix@param!default}'),
         [
             CustomFieldParts('', 'field', 'format', 'prefix', 'suffix',
                              'param', 'default',
                              '{field:format<prefix>suffix@param!default}')
         ])
Beispiel #7
0
 async def test_plain_plain(self):
     """
     In practice this should not be a scenario but just support it
     anyways
     """
     self.mock_split.return_value = [
         CustomFieldParts('KappaRoss', None, None, None, None, None, None,
                          None),
         CustomFieldParts(' KappaPride', None, None, None, None, None, None,
                          None),
     ]
     self.assertEqual(
         await library.create_messages(self.command, self.args),
         ['KappaRoss KappaPride'])
     self.data.hasFeature.assert_called_once_with('botgotsthis',
                                                  'textconvert')
     self.mock_split.assert_called_once_with('Kappa KappaRoss KappaPride')
     self.assertFalse(self.mock_convert.called)
     self.assertFalse(self.mock_format.called)
Beispiel #8
0
 async def test_field_field(self):
     self.mock_split.return_value = [
         CustomFieldParts('', 'field', 'format', 'prefix', 'suffix',
                          'param', 'default1', 'original'),
         CustomFieldParts('', 'field', 'format', 'prefix', 'suffix',
                          'param', 'default2', 'original'),
     ]
     self.assertEqual(
         await library.create_messages(self.command, self.args),
         ['default1default2'])
     self.data.hasFeature.assert_called_once_with('botgotsthis',
                                                  'textconvert')
     self.mock_split.assert_called_once_with('Kappa KappaRoss KappaPride')
     self.mock_convert.assert_has_calls([call(TypeMatch(CustomFieldArgs))] *
                                        2)
     self.assertEqual(self.mock_format.call_args_list, [
         call('default1', 'format', False),
         call('default2', 'format', False)
     ])
Beispiel #9
0
 async def test_plain(self):
     self.mock_split.return_value = [
         CustomFieldParts('Kappa', None, None, None, None, None, None, None)
     ]
     self.assertEqual(
         await library.create_messages(self.command, self.args), ['Kappa'])
     self.data.hasFeature.assert_called_once_with('botgotsthis',
                                                  'textconvert')
     self.mock_split.assert_called_once_with('Kappa KappaRoss KappaPride')
     self.assertFalse(self.mock_convert.called)
     self.assertFalse(self.mock_format.called)
Beispiel #10
0
 async def test_format_except(self):
     self.mock_format.side_effect = Exception
     self.mock_split.return_value = [
         CustomFieldParts('Kappa ', 'field', 'format', 'prefix', 'suffix',
                          'param', 'default', 'original')
     ]
     self.assertEqual(
         await library.create_messages(self.command, self.args),
         ['Kappa original'])
     self.mock_split.assert_called_once_with('Kappa KappaRoss KappaPride')
     self.mock_convert.assert_called_once_with(TypeMatch(CustomFieldArgs))
     self.mock_format.assert_called_once_with('default', 'format', False)
Beispiel #11
0
 def test_default_escape(self):
     self.assertEqual(
         library.split_message('{!{{}'),
         [CustomFieldParts('', '', None, None, None, None, '{', '{!{{}')])
     self.assertEqual(
         library.split_message('{!}}}'),
         [CustomFieldParts('', '', None, None, None, None, '}', '{!}}}')])
     self.assertEqual(
         library.split_message('{!:}'),
         [CustomFieldParts('', '', None, None, None, None, ':', '{!:}')])
     self.assertEqual(
         library.split_message('{!<}'),
         [CustomFieldParts('', '', None, None, None, None, '<', '{!<}')])
     self.assertEqual(
         library.split_message('{!>}'),
         [CustomFieldParts('', '', None, None, None, None, '>', '{!>}')])
     self.assertEqual(
         library.split_message('{!@}'),
         [CustomFieldParts('', '', None, None, None, None, '@', '{!@}')])
     self.assertEqual(library.split_message('{! !!}'), [
         CustomFieldParts('', '', None, None, None, None, ' !!', '{! !!}')
     ])
Beispiel #12
0
 def test_suffix_escape(self):
     self.assertEqual(
         library.split_message('{>{{}'),
         [CustomFieldParts('', '', None, None, '{', None, None, '{>{{}')])
     self.assertEqual(
         library.split_message('{>}}}'),
         [CustomFieldParts('', '', None, None, '}', None, None, '{>}}}')])
     self.assertEqual(
         library.split_message('{>:}'),
         [CustomFieldParts('', '', None, None, ':', None, None, '{>:}')])
     self.assertEqual(
         library.split_message('{><}'),
         [CustomFieldParts('', '', None, None, '<', None, None, '{><}')])
     self.assertEqual(library.split_message('{> >>}'), [
         CustomFieldParts('', '', None, None, ' >>', None, None, '{> >>}')
     ])
     self.assertEqual(
         library.split_message('{>@@}'),
         [CustomFieldParts('', '', None, None, '@', None, None, '{>@@}')])
     self.assertEqual(
         library.split_message('{>!!}'),
         [CustomFieldParts('', '', None, None, '!', None, None, '{>!!}')])
Beispiel #13
0
 def test_param_escape(self):
     self.assertEqual(
         library.split_message('{@{{}'),
         [CustomFieldParts('', '', None, None, None, '{', None, '{@{{}')])
     self.assertEqual(
         library.split_message('{@}}}'),
         [CustomFieldParts('', '', None, None, None, '}', None, '{@}}}')])
     self.assertEqual(
         library.split_message('{@:}'),
         [CustomFieldParts('', '', None, None, None, ':', None, '{@:}')])
     self.assertEqual(
         library.split_message('{@<}'),
         [CustomFieldParts('', '', None, None, None, '<', None, '{@<}')])
     self.assertEqual(
         library.split_message('{@>}'),
         [CustomFieldParts('', '', None, None, None, '>', None, '{@>}')])
     self.assertEqual(library.split_message('{@ @@}'), [
         CustomFieldParts('', '', None, None, None, ' @@', None, '{@ @@}')
     ])
     self.assertEqual(
         library.split_message('{@!!}'),
         [CustomFieldParts('', '', None, None, None, '!', None, '{@!!}')])
Beispiel #14
0
 def test_format_escape(self):
     self.assertEqual(
         library.split_message('{:{{}'),
         [CustomFieldParts('', '', '{', None, None, None, None, '{:{{}')])
     self.assertEqual(
         library.split_message('{:}}}'),
         [CustomFieldParts('', '', '}', None, None, None, None, '{:}}}')])
     self.assertEqual(library.split_message('{: ::}'), [
         CustomFieldParts('', '', ' ::', None, None, None, None, '{: ::}')
     ])
     self.assertEqual(
         library.split_message('{:<<}'),
         [CustomFieldParts('', '', '<', None, None, None, None, '{:<<}')])
     self.assertEqual(
         library.split_message('{:>>}'),
         [CustomFieldParts('', '', '>', None, None, None, None, '{:>>}')])
     self.assertEqual(
         library.split_message('{:@@}'),
         [CustomFieldParts('', '', '@', None, None, None, None, '{:@@}')])
     self.assertEqual(
         library.split_message('{:!!}'),
         [CustomFieldParts('', '', '!', None, None, None, None, '{:!!}')])
Beispiel #15
0
 def test_prefix_escape(self):
     self.assertEqual(
         library.split_message('{<{{}'),
         [CustomFieldParts('', '', None, '{', None, None, None, '{<{{}')])
     self.assertEqual(
         library.split_message('{<}}}'),
         [CustomFieldParts('', '', None, '}', None, None, None, '{<}}}')])
     self.assertEqual(
         library.split_message('{<:}'),
         [CustomFieldParts('', '', None, ':', None, None, None, '{<:}')])
     self.assertEqual(library.split_message('{< <<}'), [
         CustomFieldParts('', '', None, ' <<', None, None, None, '{< <<}')
     ])
     self.assertEqual(
         library.split_message('{<>>}'),
         [CustomFieldParts('', '', None, '>', None, None, None, '{<>>}')])
     self.assertEqual(
         library.split_message('{<@@}'),
         [CustomFieldParts('', '', None, '@', None, None, None, '{<@@}')])
     self.assertEqual(
         library.split_message('{<!!}'),
         [CustomFieldParts('', '', None, '!', None, None, None, '{<!!}')])
Beispiel #16
0
 def test_field_escape(self):
     """Fields cannot start with { since {{{ will read the escape first"""
     self.assertEqual(library.split_message('{ {{}'), [
         CustomFieldParts('', ' {', None, None, None, None, None, '{ {{}')
     ])
     self.assertEqual(
         library.split_message('{}}}'),
         [CustomFieldParts('', '}', None, None, None, None, None, '{}}}')])
     self.assertEqual(
         library.split_message('{::}'),
         [CustomFieldParts('', ':', None, None, None, None, None, '{::}')])
     self.assertEqual(
         library.split_message('{<<}'),
         [CustomFieldParts('', '<', None, None, None, None, None, '{<<}')])
     self.assertEqual(
         library.split_message('{>>}'),
         [CustomFieldParts('', '>', None, None, None, None, None, '{>>}')])
     self.assertEqual(
         library.split_message('{@@}'),
         [CustomFieldParts('', '@', None, None, None, None, None, '{@@}')])
     self.assertEqual(
         library.split_message('{!!}'),
         [CustomFieldParts('', '!', None, None, None, None, None, '{!!}')])
Beispiel #17
0
def split_message(message: str) -> Iterable[CustomFieldParts]:
    # Format: {field:format<prefix>suffix@param!default}
    parsed: List[CustomFieldParts] = []
    i: int = 0
    length: int = len(message)

    while True:
        noFormat: List[str] = []
        while i < length:
            char: str = message[i]
            i += 1

            if char == '}':
                if i < length and message[i] == '}':
                    i += 1
                else:
                    raise ValueError()
            elif char == '{':
                if i < length and message[i] == '{':
                    i += 1
                else:
                    i -= 1
                    break

            noFormat.append(char)

        if i == length:
            if noFormat:
                parsed.append(
                    CustomFieldParts(''.join(noFormat), None, None, None, None,
                                     None, None, None))
            break

        s: int = i
        i += 1
        if i == length:
            raise ValueError()

        field: List[str] = []
        while True:
            if i == length:
                raise ValueError()

            char = message[i]
            i += 1

            if char == ':':
                if i < length and message[i] == ':':
                    i += 1
                else:
                    break
            if char == '<':
                if i < length and message[i] == '<':
                    i += 1
                else:
                    break
            if char == '>':
                if i < length and message[i] == '>':
                    i += 1
                else:
                    break
            if char == '@':
                if i < length and message[i] == '@':
                    i += 1
                else:
                    break
            if char == '!':
                if i < length and message[i] == '!':
                    i += 1
                else:
                    break
            if char == '{':
                if i < length and message[i] == '{':
                    i += 1
                else:
                    raise ValueError()
            if char == '}':
                if i < length and message[i] == '}':
                    i += 1
                else:
                    i -= 1
                    break
            field.append(char)

        format: Optional[List[str]] = None  # noqa: E701
        if char == ':':
            format = []
            while True:
                if i == length:
                    raise ValueError()

                char = message[i]
                i += 1

                if char == '@':
                    if i < length and message[i] == '@':
                        i += 1
                    else:
                        break
                if char == '<':
                    if i < length and message[i] == '<':
                        i += 1
                    else:
                        break
                if char == '>':
                    if i < length and message[i] == '>':
                        i += 1
                    else:
                        break
                if char == '!':
                    if i < length and message[i] == '!':
                        i += 1
                    else:
                        break
                if char == '{':
                    if i < length and message[i] == '{':
                        i += 1
                    else:
                        raise ValueError()
                if char == '}':
                    if i < length and message[i] == '}':
                        i += 1
                    else:
                        i -= 1
                        break
                format.append(char)

        prefix: Optional[List[str]] = None
        if char == '<':
            prefix = []
            while True:
                if i == length:
                    raise ValueError()

                char = message[i]
                i += 1

                if char == '>':
                    if i < length and message[i] == '>':
                        i += 1
                    else:
                        break
                if char == '@':
                    if i < length and message[i] == '@':
                        i += 1
                    else:
                        break
                if char == '!':
                    if i < length and message[i] == '!':
                        i += 1
                    else:
                        break
                if char == '{':
                    if i < length and message[i] == '{':
                        i += 1
                    else:
                        raise ValueError()
                if char == '}':
                    if i < length and message[i] == '}':
                        i += 1
                    else:
                        i -= 1
                        break
                prefix.append(char)

        suffix: Optional[List[str]] = None
        if char == '>':
            suffix = []
            while True:
                if i == length:
                    raise ValueError()

                char = message[i]
                i += 1

                if char == '@':
                    if i < length and message[i] == '@':
                        i += 1
                    else:
                        break
                if char == '!':
                    if i < length and message[i] == '!':
                        i += 1
                    else:
                        break
                if char == '{':
                    if i < length and message[i] == '{':
                        i += 1
                    else:
                        raise ValueError()
                if char == '}':
                    if i < length and message[i] == '}':
                        i += 1
                    else:
                        i -= 1
                        break
                suffix.append(char)

        param: Optional[List[str]] = None
        if char == '@':
            param = []
            while True:
                if i == length:
                    raise ValueError()

                char = message[i]
                i += 1

                if char == '!':
                    if i < length and message[i] == '!':
                        i += 1
                    else:
                        break
                if char == '{':
                    if i < length and message[i] == '{':
                        i += 1
                    else:
                        raise ValueError()
                if char == '}':
                    if i < length and message[i] == '}':
                        i += 1
                    else:
                        i -= 1
                        break
                param.append(char)

        default: Optional[List[str]] = None
        if char == '!':
            default = []
            while True:
                if i == length:
                    raise ValueError()

                char = message[i]
                i += 1

                if char == '{':
                    if i < length and message[i] == '{':
                        i += 1
                    else:
                        raise ValueError()
                if char == '}':
                    if i < length and message[i] == '}':
                        i += 1
                    else:
                        i -= 1
                        break
                default.append(char)

        if char != '}':
            raise ValueError()
        i += 1
        original: str = message[s:i]

        parsed.append(
            CustomFieldParts(''.join(noFormat), ''.join(field),
                             ''.join(format) if format is not None else None,
                             ''.join(prefix) if prefix is not None else None,
                             ''.join(suffix) if suffix is not None else None,
                             ''.join(param) if param is not None else None,
                             ''.join(default) if default is not None else None,
                             original))

    return parsed
Beispiel #18
0
 def test_field_param(self):
     self.assertEqual(library.split_message('{field@param}'), [
         CustomFieldParts('', 'field', None, None, None, 'param', None,
                          '{field@param}')
     ])
Beispiel #19
0
 def test_field_format(self):
     self.assertEqual(library.split_message('{field:ascii}'), [
         CustomFieldParts('', 'field', 'ascii', None, None, None, None,
                          '{field:ascii}')
     ])
Beispiel #20
0
 def test_field_field(self):
     self.assertEqual(library.split_message('{}{}'), [
         CustomFieldParts('', '', None, None, None, None, None, '{}'),
         CustomFieldParts('', '', None, None, None, None, None, '{}'),
     ])
Beispiel #21
0
 def test_field_format_prefix_suffix_param_default_all_blank(self):
     self.assertEqual(
         library.split_message('{:<>@!}'),
         [CustomFieldParts('', '', '', '', '', '', '', '{:<>@!}')])
Beispiel #22
0
 def test_field_prefix_suffix(self):
     self.assertEqual(library.split_message('{field<prefix>suffix}'), [
         CustomFieldParts('', 'field', None, 'prefix', 'suffix', None, None,
                          '{field<prefix>suffix}')
     ])
Beispiel #23
0
 def test_field_format_default(self):
     self.assertEqual(library.split_message('{field:format!default}'), [
         CustomFieldParts('', 'field', 'format', None, None, None,
                          'default', '{field:format!default}')
     ])