Skip to content
This repository has been archived by the owner on Sep 8, 2024. It is now read-only.

Commit

Permalink
Add tests for commented dialogs and DialogLoader (#1853)
Browse files Browse the repository at this point in the history
* Add tests for DialogLoader

* Handle Path/PosixPath

LOG messages when files/directories were missing would fail when a PosixPath/Path object was sent to as argument. This uses format to get the correct string representation.

* Add test for dialog.get()
  • Loading branch information
forslund authored and Steve Penrod committed Jan 31, 2019
1 parent 8ff2ccc commit 31656d6
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 3 deletions.
4 changes: 2 additions & 2 deletions mycroft/dialog/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)):
Expand Down Expand Up @@ -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()
Expand Down
1 change: 1 addition & 0 deletions test/unittests/dialog/multiple_dialogs/one.dialog
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ONE
1 change: 1 addition & 0 deletions test/unittests/dialog/multiple_dialogs/two.dialog
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
TWO
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# This is a commented line
This is a line without comment
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is a line without comment
50 changes: 49 additions & 1 deletion test/unittests/dialog/test_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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()

0 comments on commit 31656d6

Please sign in to comment.