diff --git a/test/unittests/test_bracket_expansion.py b/test/unittests/test_bracket_expansion.py index 8e1c31e..5d6bcbe 100644 --- a/test/unittests/test_bracket_expansion.py +++ b/test/unittests/test_bracket_expansion.py @@ -1,31 +1,118 @@ import unittest +from ovos_utils.bracket_expansion import expand_template, expand_slots -class TestBracketExpansion(unittest.TestCase): - def test_expand_parentheses(self): - from ovos_utils.bracket_expansion import expand_parentheses - # TODO - def test_expand_options(self): - from ovos_utils.bracket_expansion import expand_options - # TODO +class TestTemplateExpansion(unittest.TestCase): - def test_fragment(self): - from ovos_utils.bracket_expansion import Fragment - # TODO + def test_expand_slots(self): + # Test for expanding slots + template = "change [the ]brightness to {brightness_level} and color to {color_name}" + slots = { + "brightness_level": ["low", "medium", "high"], + "color_name": ["red", "green", "blue"] + } - def test_word(self): - from ovos_utils.bracket_expansion import Word - # TODO + expanded_sentences = expand_slots(template, slots) - def test_sentence(self): - from ovos_utils.bracket_expansion import Sentence - # TODO + expected_sentences = ['change brightness to low and color to red', + 'change brightness to low and color to green', + 'change brightness to low and color to blue', + 'change brightness to medium and color to red', + 'change brightness to medium and color to green', + 'change brightness to medium and color to blue', + 'change brightness to high and color to red', + 'change brightness to high and color to green', + 'change brightness to high and color to blue', + 'change the brightness to low and color to red', + 'change the brightness to low and color to green', + 'change the brightness to low and color to blue', + 'change the brightness to medium and color to red', + 'change the brightness to medium and color to green', + 'change the brightness to medium and color to blue', + 'change the brightness to high and color to red', + 'change the brightness to high and color to green', + 'change the brightness to high and color to blue'] + self.assertEqual(expanded_sentences, expected_sentences) - def test_options(self): - from ovos_utils.bracket_expansion import Options - # TODO + def test_expand_template(self): + # Test for template expansion + templates = [ + "[hello,] (call me|my name is) {name}", + "Expand (alternative|choices) into a list of choices.", + "sentences have [optional] words ", + "alternative words can be (used|written)", + "sentence[s] can have (pre|suf)fixes mid word too", + "do( the | )thing(s|) (old|with) style and( no | )spaces", + "[(this|that) is optional]", + "tell me a [{joke_type}] joke", + "play {query} [in ({device_name}|{skill_name}|{zone_name})]" + ] - def test_sentence_tree_parser(self): - from ovos_utils.bracket_expansion import SentenceTreeParser - # TODO + expected_outputs = { + "[hello,] (call me|my name is) {name}": [ + "call me {name}", + "hello, call me {name}", + "hello, my name is {name}", + "my name is {name}" + ], + "Expand (alternative|choices) into a list of choices.": [ + "Expand alternative into a list of choices.", + "Expand choices into a list of choices." + ], + "sentences have [optional] words ": [ + "sentences have words", + "sentences have optional words" + ], + "alternative words can be (used|written)": [ + "alternative words can be used", + "alternative words can be written" + ], + "sentence[s] can have (pre|suf)fixes mid word too": [ + "sentence can have prefixes mid word too", + "sentence can have suffixes mid word too", + "sentences can have prefixes mid word too", + "sentences can have suffixes mid word too" + ], + "do( the | )thing(s|) (old|with) style and( no | )spaces": [ + "do the thing old style and no spaces", + "do the thing old style and spaces", + "do the thing with style and no spaces", + "do the thing with style and spaces", + "do the things old style and no spaces", + "do the things old style and spaces", + "do the things with style and no spaces", + "do the things with style and spaces", + "do thing old style and no spaces", + "do thing old style and spaces", + "do thing with style and no spaces", + "do thing with style and spaces", + "do things old style and no spaces", + "do things old style and spaces", + "do things with style and no spaces", + "do things with style and spaces" + ], + "[(this|that) is optional]": [ + '', + 'that is optional', + 'this is optional'], + "tell me a [{joke_type}] joke": [ + "tell me a joke", + "tell me a {joke_type} joke" + ], + "play {query} [in ({device_name}|{skill_name}|{zone_name})]": [ + "play {query}", + "play {query} in {device_name}", + "play {query} in {skill_name}", + "play {query} in {zone_name}" + ] + } + + for template, expected_sentences in expected_outputs.items(): + with self.subTest(template=template): + expanded_sentences = expand_template(template) + self.assertEqual(expanded_sentences, expected_sentences) + + +if __name__ == '__main__': + unittest.main()