# Path: wikitextparser/_config.py
# def _pattern(trie: dict) -> str:
#     """Convert a trie to a regex pattern."""
#     if '' in trie:
#         if len(trie) == 1:
#             return ''
#         optional = True
#         del trie['']
#     else:
#         optional = False
# 
#     subpattern_to_chars = _defaultdict(list)
# 
#     for char, sub_trie in trie.items():
#         subpattern = _pattern(sub_trie)
#         subpattern_to_chars[subpattern].append(char)
# 
#     alts = []
#     for subpattern, chars in subpattern_to_chars.items():
#         if len(chars) == 1:
#             alts.append(chars[0] + subpattern)
#         else:
#             chars.sort(reverse=True)
#             alts.append('[' + ''.join(chars) + ']' + subpattern)
# 
#     if len(alts) == 1:
#         result = alts[0]
#         if optional:
#             if len(result) == 1:
#                 result += '?+'
#             else:  # more than one character in alts[0]
#                 result = '(?:' + result + ')?+'
#     else:
#         alts.sort(reverse=True)
#         result = '(?>' + '|'.join(alts) + ')'
#         if optional:
#             result += '?+'
#     return result
# 
# def _plant_trie(strings: _Iterable[str]) -> dict:
#     """Create a Trie out of a list of words and return an atomic regex pattern.
# 
#     The corresponding Regex should match much faster than a simple Regex union.
#     """
#     # plant the trie
#     trie = {}
#     for string in strings:
#         d = trie
#         for char in string:
#             d[char] = char in d and d[char] or {}
#             d = d[char]
#         d[''] = None  # EOS
#     return trie
# 
# def regex_pattern(words: _Iterable[str]) -> bytes:
#     """Convert words to a regex pattern that matches any of them."""
#     return _pattern(_plant_trie(words)).encode()


from wikitextparser._config import _pattern, _plant_trie, regex_pattern
# noinspection PyProtectedMember


# Test the _plant_trie function.
def test__plant_trie_empty():
    assert _plant_trie(['']) == {'': None}


def test_alternatives():
    assert _plant_trie(['a', 'b']) == {'a': {'': None}, 'b': {'': None}}


def test_string():

# Path: wikitextparser/_config.py
# def _pattern(trie: dict) -> str:
#     """Convert a trie to a regex pattern."""
#     if '' in trie:
#         if len(trie) == 1:
#             return ''
#         optional = True
#         del trie['']
#     else:
#         optional = False
# 
#     subpattern_to_chars = _defaultdict(list)
# 
#     for char, sub_trie in trie.items():
#         subpattern = _pattern(sub_trie)
#         subpattern_to_chars[subpattern].append(char)
# 
#     alts = []
#     for subpattern, chars in subpattern_to_chars.items():
#         if len(chars) == 1:
#             alts.append(chars[0] + subpattern)
#         else:
#             chars.sort(reverse=True)
#             alts.append('[' + ''.join(chars) + ']' + subpattern)
# 
#     if len(alts) == 1:
#         result = alts[0]
#         if optional:
#             if len(result) == 1:
#                 result += '?+'
#             else:  # more than one character in alts[0]
#                 result = '(?:' + result + ')?+'
#     else:
#         alts.sort(reverse=True)
#         result = '(?>' + '|'.join(alts) + ')'
#         if optional:
#             result += '?+'
#     return result
# 
# def _plant_trie(strings: _Iterable[str]) -> dict:
#     """Create a Trie out of a list of words and return an atomic regex pattern.
# 
#     The corresponding Regex should match much faster than a simple Regex union.
#     """
#     # plant the trie
#     trie = {}
#     for string in strings:
#         d = trie
#         for char in string:
#             d[char] = char in d and d[char] or {}
#             d = d[char]
#         d[''] = None  # EOS
#     return trie
# 
# def regex_pattern(words: _Iterable[str]) -> bytes:
#     """Convert words to a regex pattern that matches any of them."""
#     return _pattern(_plant_trie(words)).encode()


# Path: tests/test_config.py
from wikitextparser._config import _pattern, _plant_trie, regex_pattern

# noinspection PyProtectedMember


# Test the _plant_trie function.
def test__plant_trie_empty():
    assert _plant_trie(['']) == {'': None}


def test_alternatives():
    assert _plant_trie(['a', 'b']) == {'a': {'': None}, 'b': {'': None}}


def test_string():

    assert _plant_trie(['ab']) == {'a': {'b': {'': None}}}