diff --git a/mycroft/dialog/__init__.py b/mycroft/dialog/__init__.py index c9adbeff604b..b170144699bb 100644 --- a/mycroft/dialog/__init__.py +++ b/mycroft/dialog/__init__.py @@ -113,7 +113,7 @@ def load(self, dialog_dir): """ directory = Path(dialog_dir) if not directory.exists() or not directory.is_dir(): - LOG.warning("No dialog files found: " + dialog_dir) + LOG.warning("No dialog files found: {}".format(dialog_dir)) return self.__renderer for path, _, files in os.walk(str(directory)): @@ -147,7 +147,7 @@ def get(phrase, lang=None, context=None): filename = "text/" + lang.lower() + "/" + phrase + ".dialog" template = resolve_resource_file(filename) if not template: - LOG.debug("Resource file not found: " + filename) + LOG.debug("Resource file not found: {}".format(filename)) return phrase stache = MustacheDialogRenderer() diff --git a/test/unittests/dialog/multiple_dialogs/one.dialog b/test/unittests/dialog/multiple_dialogs/one.dialog new file mode 100644 index 000000000000..a2628c1e0953 --- /dev/null +++ b/test/unittests/dialog/multiple_dialogs/one.dialog @@ -0,0 +1 @@ +ONE diff --git a/test/unittests/dialog/multiple_dialogs/two.dialog b/test/unittests/dialog/multiple_dialogs/two.dialog new file mode 100644 index 000000000000..6333d309717a --- /dev/null +++ b/test/unittests/dialog/multiple_dialogs/two.dialog @@ -0,0 +1 @@ +TWO diff --git a/test/unittests/dialog/mustache_templates_comments/example-comments.dialog b/test/unittests/dialog/mustache_templates_comments/example-comments.dialog new file mode 100644 index 000000000000..114efa33e938 --- /dev/null +++ b/test/unittests/dialog/mustache_templates_comments/example-comments.dialog @@ -0,0 +1,2 @@ +# This is a commented line +This is a line without comment diff --git a/test/unittests/dialog/mustache_templates_comments/example-comments.result b/test/unittests/dialog/mustache_templates_comments/example-comments.result new file mode 100644 index 000000000000..30246649ead5 --- /dev/null +++ b/test/unittests/dialog/mustache_templates_comments/example-comments.result @@ -0,0 +1 @@ +This is a line without comment diff --git a/test/unittests/dialog/test_dialog.py b/test/unittests/dialog/test_dialog.py index 3cd9ab5f7d58..da246f9b017a 100644 --- a/test/unittests/dialog/test_dialog.py +++ b/test/unittests/dialog/test_dialog.py @@ -18,7 +18,8 @@ import pathlib import json -from mycroft.dialog import MustacheDialogRenderer +from mycroft.dialog import MustacheDialogRenderer, DialogLoader, get +from mycroft.util import resolve_resource_file class DialogTest(unittest.TestCase): @@ -72,6 +73,53 @@ def test_multiple_dialog(self): self.assertIn( self.stache.render(file.name, context=context), results) + def test_comment_dialog(self): + """ + Test the loading and filling of valid mustache dialogs + where a dialog file contains multiple text versions + """ + template_path = self.topdir.joinpath('./mustache_templates_comments') + for f in template_path.iterdir(): + if f.suffix == '.dialog': + self.stache.load_template_file(f.name, str(f.absolute())) + results = [line.strip() + for line in f.with_suffix('.result').open('r')] + # Try all lines + for index, line in enumerate(results): + self.assertEqual(self.stache.render(f.name, index=index), + line.strip()) + + def test_dialog_loader(self): + template_path = self.topdir.joinpath('./multiple_dialogs') + loader = DialogLoader() + renderer = loader.load(template_path) + self.assertEqual(renderer.render('one'), 'ONE') + self.assertEqual(renderer.render('two'), 'TWO') + + def test_dialog_loader_missing(self): + template_path = self.topdir.joinpath('./missing_dialogs') + loader = DialogLoader() + renderer = loader.load(template_path) + self.assertEqual(renderer.render('test'), 'test') + + def test_get(self): + phrase = 'i didn\'t catch that' + res_file = pathlib.Path('text/en-us/').joinpath(phrase + '.dialog') + print(res_file) + resource = resolve_resource_file(str(res_file)) + with open(resource) as f: + results = [line.strip() for line in f] + string = get(phrase) + self.assertIn(string, results) + + # Check that the filename is returned if phrase is missing for lang + string = get(phrase, lang='ne-ne') + self.assertEqual(string, phrase) + + # Check that name is retured if phrase is missing + string = get('testing aardwark') + self.assertEqual(string, 'testing aardwark') + if __name__ == "__main__": unittest.main()