From 2c65e8188c1e4abdfc3c8f9c74b731baf1704a9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=85ke=20Forslund?= Date: Sat, 28 Dec 2019 23:34:24 +0100 Subject: [PATCH] Add tests for MycroftSkill translate methods Verifies that resources are read from both locale and dialog folders. Also checks that fallbacks to en-us from other languages works as expected. --- test/unittests/skills/test_mycroft_skill.py | 50 +++++++++++++++++++ .../in-dialog/dialog/en-us/good_things.list | 3 ++ .../in-dialog/dialog/en-us/named_things.value | 4 ++ .../in-dialog/dialog/en-us/test.template | 1 + .../in-locale/locale/de-de/good_things.list | 3 ++ .../in-locale/locale/de-de/named_things.value | 3 ++ .../in-locale/locale/de-de/test.template | 1 + .../in-locale/locale/en-us/good_things.list | 3 ++ .../in-locale/locale/en-us/named_things.value | 4 ++ .../in-locale/locale/en-us/not_in_german.list | 3 ++ .../in-locale/locale/en-us/test.template | 1 + 11 files changed, 76 insertions(+) create mode 100644 test/unittests/skills/translate/in-dialog/dialog/en-us/good_things.list create mode 100644 test/unittests/skills/translate/in-dialog/dialog/en-us/named_things.value create mode 100644 test/unittests/skills/translate/in-dialog/dialog/en-us/test.template create mode 100644 test/unittests/skills/translate/in-locale/locale/de-de/good_things.list create mode 100644 test/unittests/skills/translate/in-locale/locale/de-de/named_things.value create mode 100644 test/unittests/skills/translate/in-locale/locale/de-de/test.template create mode 100644 test/unittests/skills/translate/in-locale/locale/en-us/good_things.list create mode 100644 test/unittests/skills/translate/in-locale/locale/en-us/named_things.value create mode 100644 test/unittests/skills/translate/in-locale/locale/en-us/not_in_german.list create mode 100644 test/unittests/skills/translate/in-locale/locale/en-us/test.template diff --git a/test/unittests/skills/test_mycroft_skill.py b/test/unittests/skills/test_mycroft_skill.py index db099431b9..5274bae0f1 100644 --- a/test/unittests/skills/test_mycroft_skill.py +++ b/test/unittests/skills/test_mycroft_skill.py @@ -552,6 +552,56 @@ class TestMycroftSkill(unittest.TestCase): self.assertFalse(s.voc_match("My hovercraft is full of eels", "turn_off_test")) + def test_translate_locations(self): + """Assert that the a translatable list can be loaded from dialog and + locale. + """ + # Check that translatables can be loaded from the dialog directory + s = SimpleSkill1() + s.root_dir = abspath(join(dirname(__file__), + 'translate', 'in-dialog/')) + lst = s.translate_list('good_things') + self.assertTrue(isinstance(lst, list)) + vals = s.translate_namedvalues('named_things') + self.assertTrue(isinstance(vals, dict)) + template = s.translate_template('test', + data={'thing': 'test framework'}) + self.assertEqual(template, + ['Oh look it\'s my favourite test framework']) + # Check that translatables can be loaded from locale folder + s = SimpleSkill1() + s.root_dir = abspath(join(dirname(__file__), + 'translate', 'in-locale')) + lst = s.translate_list('good_things') + self.assertTrue(isinstance(lst, list)) + vals = s.translate_namedvalues('named_things') + self.assertTrue(isinstance(vals, dict)) + template = s.translate_template('test', + data={'thing': 'test framework'}) + self.assertEqual(template, + ['Oh look it\'s my favourite test framework']) + + # Check loading in a non-en-us language + s = SimpleSkill1() + s.config_core['lang'] = 'de-de' + s.root_dir = abspath(join(dirname(__file__), + 'translate', 'in-locale')) + lst = s.translate_list('good_things') + self.assertEqual(lst, ['sonne', 'mycroft', 'zahne']) + vals = s.translate_namedvalues('named_things') + self.assertEqual(vals['blau'], '2') + template = s.translate_template('test', + data={'thing': 'test framework'}) + self.assertEqual(template, + ['Aber setzen sie sich herr test framework']) + + # Check fallback to english + lst = s.translate_list('not_in_german') + self.assertEqual(lst, ['not', 'in', 'German']) + + # Restore lang to en-us + s.config_core['lang'] = 'en-us' + class _TestSkill(MycroftSkill): def __init__(self): diff --git a/test/unittests/skills/translate/in-dialog/dialog/en-us/good_things.list b/test/unittests/skills/translate/in-dialog/dialog/en-us/good_things.list new file mode 100644 index 0000000000..78a4efa0eb --- /dev/null +++ b/test/unittests/skills/translate/in-dialog/dialog/en-us/good_things.list @@ -0,0 +1,3 @@ +sunshine +mycroft +licorice diff --git a/test/unittests/skills/translate/in-dialog/dialog/en-us/named_things.value b/test/unittests/skills/translate/in-dialog/dialog/en-us/named_things.value new file mode 100644 index 0000000000..5e44c5cc06 --- /dev/null +++ b/test/unittests/skills/translate/in-dialog/dialog/en-us/named_things.value @@ -0,0 +1,4 @@ +dot,0 +line,1 +circle,2 +ball,3 diff --git a/test/unittests/skills/translate/in-dialog/dialog/en-us/test.template b/test/unittests/skills/translate/in-dialog/dialog/en-us/test.template new file mode 100644 index 0000000000..8a6a6b8477 --- /dev/null +++ b/test/unittests/skills/translate/in-dialog/dialog/en-us/test.template @@ -0,0 +1 @@ +Oh look it's my favourite {thing} diff --git a/test/unittests/skills/translate/in-locale/locale/de-de/good_things.list b/test/unittests/skills/translate/in-locale/locale/de-de/good_things.list new file mode 100644 index 0000000000..1ca068048f --- /dev/null +++ b/test/unittests/skills/translate/in-locale/locale/de-de/good_things.list @@ -0,0 +1,3 @@ +sonne +mycroft +zahne diff --git a/test/unittests/skills/translate/in-locale/locale/de-de/named_things.value b/test/unittests/skills/translate/in-locale/locale/de-de/named_things.value new file mode 100644 index 0000000000..3d7ac32475 --- /dev/null +++ b/test/unittests/skills/translate/in-locale/locale/de-de/named_things.value @@ -0,0 +1,3 @@ +gelb,0 +rot,1 +blau,2 diff --git a/test/unittests/skills/translate/in-locale/locale/de-de/test.template b/test/unittests/skills/translate/in-locale/locale/de-de/test.template new file mode 100644 index 0000000000..c7d26b08ef --- /dev/null +++ b/test/unittests/skills/translate/in-locale/locale/de-de/test.template @@ -0,0 +1 @@ +Aber setzen sie sich herr {thing} diff --git a/test/unittests/skills/translate/in-locale/locale/en-us/good_things.list b/test/unittests/skills/translate/in-locale/locale/en-us/good_things.list new file mode 100644 index 0000000000..78a4efa0eb --- /dev/null +++ b/test/unittests/skills/translate/in-locale/locale/en-us/good_things.list @@ -0,0 +1,3 @@ +sunshine +mycroft +licorice diff --git a/test/unittests/skills/translate/in-locale/locale/en-us/named_things.value b/test/unittests/skills/translate/in-locale/locale/en-us/named_things.value new file mode 100644 index 0000000000..5e44c5cc06 --- /dev/null +++ b/test/unittests/skills/translate/in-locale/locale/en-us/named_things.value @@ -0,0 +1,4 @@ +dot,0 +line,1 +circle,2 +ball,3 diff --git a/test/unittests/skills/translate/in-locale/locale/en-us/not_in_german.list b/test/unittests/skills/translate/in-locale/locale/en-us/not_in_german.list new file mode 100644 index 0000000000..5755f36c66 --- /dev/null +++ b/test/unittests/skills/translate/in-locale/locale/en-us/not_in_german.list @@ -0,0 +1,3 @@ +not +in +German diff --git a/test/unittests/skills/translate/in-locale/locale/en-us/test.template b/test/unittests/skills/translate/in-locale/locale/en-us/test.template new file mode 100644 index 0000000000..8a6a6b8477 --- /dev/null +++ b/test/unittests/skills/translate/in-locale/locale/en-us/test.template @@ -0,0 +1 @@ +Oh look it's my favourite {thing}