Repository: hedyorg/hedy

Issue:
Add level 2 to hedy_translation.py
Now that the keywords from level 1 can be translated, new levels can be added.


Discussion:

Resolving patch:
diff --git a/hedy_translation.py b/hedy_translation.py
--- a/hedy_translation.py
+++ b/hedy_translation.py
@@ -1,5 +1,5 @@
 from lark import Transformer, Tree
-from hedy import get_keywords_for_language, ExtractAST, get_parser
+import hedy
 
 
 TRANSPILER_LOOKUP = {}
@@ -8,9 +8,9 @@
 def keywords_to_dict(to_lang="nl"):
     """"Return a dictionary of keywords from language of choice. Key is english value is lang of choice"""
     keywords = {}
-    keywords_from = get_keywords_for_language("en").replace("\n\n", "\n").splitlines()
+    keywords_from = hedy.get_keywords_for_language("en").replace("\n\n", "\n").splitlines()
 
-    keywords_to = get_keywords_for_language(to_lang).replace("\n\n", "\n").splitlines()
+    keywords_to = hedy.get_keywords_for_language(to_lang).replace("\n\n", "\n").splitlines()
     keywords_from_withoutlvl = []
     for line in keywords_from:
         if line[0] != '/':
@@ -29,13 +29,13 @@ def keywords_to_dict(to_lang="nl"):
 
 def translate_keywords(input_string, from_lang="nl", to_lang="nl", level=1):
     """"Return code with keywords translated to language of choice in level of choice"""
-    parser = get_parser(level, from_lang)
+    parser = hedy.get_parser(level, from_lang)
 
     punctuation_symbols = ['!', '?', '.']
 
     keywordDict = keywords_to_dict(to_lang)
     program_root = parser.parse(input_string + '\n').children[0]
-    abstract_syntaxtree = ExtractAST().transform(program_root)
+    abstract_syntaxtree = hedy.ExtractAST().transform(program_root)
     translator = TRANSPILER_LOOKUP[level]
     abstract_syntaxtree = translator(keywordDict, punctuation_symbols).transform(program_root)
 
@@ -97,3 +97,50 @@ def invalid(self, args):
 
     def __default__(self, data, children, meta):
         return Tree(data, children, meta)
+
+@hedy_translator(level=2)
+class ConvertToLang2(ConvertToLang1):
+
+    def assign(self, args):
+        return args[0] + " " + self.keywords["is"] + " " + ''.join([str(c) for c in args[1:]])
+
+    def print(self, args):
+
+        argument_string = ""
+        i = 0
+
+        for argument in args:
+            # escape quotes if kids accidentally use them at level 2
+            argument = hedy.process_characters_needing_escape(argument)
+
+            # final argument and punctuation arguments do not have to be separated with a space, other do
+            if i == len(args) - 1 or args[i + 1] in self.punctuation_symbols:
+                space = ''
+            else:
+                space = " "
+
+            argument_string += argument + space
+
+            i = i + 1
+
+        return self.keywords["print"] + " " + argument_string
+
+    def punctuation(self, args):
+        return ''.join([str(c) for c in args])
+
+    def var(self, args):
+        var = args[0]
+        all_parameters = ["'" + hedy.process_characters_needing_escape(a) + "'" for a in args[1:]]
+        return var + ''.join(all_parameters)
+
+    def ask(self, args):
+        var = args[0]
+        all_parameters = [hedy.process_characters_needing_escape(a) for a in args]
+
+        return all_parameters[0] + " " + self.keywords["is"] + " " + self.keywords["ask"] + " " + ''.join(all_parameters[1:])
+
+    def ask_dep_2(self, args):
+        return self.keywords["ask"] + " " + ''.join([str(c) for c in args])
+
+    def echo_dep_2(self, args):
+        return self.keywords["echo"] + " " + ''.join([str(c) for c in args])


Test patch:
diff --git a/tests/test_translation_level_01.py b/tests/test_translation_level_01.py
--- a/tests/test_translation_level_01.py
+++ b/tests/test_translation_level_01.py
@@ -48,13 +48,13 @@ def test_echo_english_dutch(self):
         code = "ask Hallo welkom bij Hedy!\necho"
 
         result = hedy_translation.translate_keywords(code, from_lang="en", to_lang="nl", level=self.level)
-        expected = "vraag Hallo welkom bij Hedy!\nherhaal"
+        expected = "vraag Hallo welkom bij Hedy!\necho"
 
         self.assertEqual(result, expected)
 
     @check_local_lang_bool
     def test_ask_echo_english_dutch(self):
-        code = 'print Hallo welkom bij Hedy\'\'\nvraag hoe heet je\nherhaal'
+        code = 'print Hallo welkom bij Hedy\'\'\nvraag hoe heet je\necho'
 
         result = hedy_translation.translate_keywords(code, from_lang="nl", to_lang="en", level=self.level)
         expected = 'print Hallo welkom bij Hedy\'\'\nask hoe heet je\necho'
@@ -112,7 +112,7 @@ def test_ask_dutch_english(self):
 
     @check_local_lang_bool
     def test_echo_dutch_english(self):
-        code = "vraag stel je vraag\nherhaal tekst"
+        code = "vraag stel je vraag\necho tekst"
 
         result = hedy_translation.translate_keywords(code, from_lang="nl", to_lang="en", level=self.level)
         expected = "ask stel je vraag\necho tekst"
@@ -121,7 +121,7 @@ def test_echo_dutch_english(self):
 
     @check_local_lang_bool
     def test_ask_echo_dutch_english(self):
-        code = 'vraag Hallo welkom bij Hedy!\nherhaal hoi'
+        code = 'vraag Hallo welkom bij Hedy!\necho hoi'
 
         result = hedy_translation.translate_keywords(code, from_lang="nl", to_lang="en", level=self.level)
         expected = 'ask Hallo welkom bij Hedy!\necho hoi'
diff --git a/tests/test_translation_level_02.py b/tests/test_translation_level_02.py
new file mode 100644
--- /dev/null
+++ b/tests/test_translation_level_02.py
@@ -0,0 +1,153 @@
+import hedy
+from test_level_01 import HedyTester
+import hedy_translation
+
+
+def check_local_lang_bool(func):
+    def inner(self):
+        if not hedy.local_keywords_enabled:
+            return
+
+        return func(self)
+
+    return inner
+
+
+    # tests should be ordered as follows:
+    # * Translation from English to Dutch
+    # * Translation from Dutch to English
+    # * Translation to several languages
+    # * Error handling
+
+
+class TestsTranslationLevel2(HedyTester):
+    level = 2
+
+    @check_local_lang_bool
+    def test_print(self):
+        code = "print Hallo welkom bij Hedy!"
+
+        result = hedy_translation.translate_keywords(code, "nl", "en", self.level)
+        expected = "print Hallo welkom bij Hedy!"
+
+        self.assertEqual(result, expected)
+
+    @check_local_lang_bool
+    def test_print_kewords(self):
+        code = "print print ask echo"
+
+        result = hedy_translation.translate_keywords(code, "en", "nl", self.level)
+        expected = "print print ask echo"
+
+        self.assertEqual(result, expected)
+
+    @check_local_lang_bool
+    def test_ask_assign_english_dutch(self):
+        code = "mens is ask Hallo welkom bij Hedy!"
+
+        result = hedy_translation.translate_keywords(code, "en", "nl", self.level)
+        expected = "mens is vraag Hallo welkom bij Hedy!"
+
+        self.assertEqual(result, expected)
+
+    @check_local_lang_bool
+    def test_forward_assigned_value_english_dutch(self):
+        code = "value is 50\nforward value"
+
+        result = hedy_translation.translate_keywords(code, "en", "nl", self.level)
+        expected = "value is 50\nvooruit value"
+
+        self.assertEqual(result, expected)
+
+    @check_local_lang_bool
+    def test_print_var_text(self):
+        code = "welkom is Hallo welkom bij Hedy\nprint welkom Veel plezier"
+
+        result = hedy_translation.translate_keywords(code, "en", "nl", self.level)
+        expected = "welkom is Hallo welkom bij Hedy\nprint welkom Veel plezier"
+
+        self.assertEqual(result, expected)
+
+    @check_local_lang_bool
+    def test_ask_kewords(self):
+        code = "hedy is vraag print ask echo"
+
+        result = hedy_translation.translate_keywords(code, "en", "nl", self.level)
+        expected = "hedy is vraag print ask echo"
+
+        self.assertEqual(result, expected)
+
+    @check_local_lang_bool
+    def test_ask_print(self):
+        code = "hedy is hello\nprint hedy"
+
+        result = hedy_translation.translate_keywords(code, "en", "nl", self.level)
+        expected = "hedy is hello\nprint hedy"
+
+        self.assertEqual(result, expected)
+
+    @check_local_lang_bool
+    def test_ask_assign_dutch_english(self):
+        code = "mens is vraag Hallo welkom bij Hedy!"
+
+        result = hedy_translation.translate_keywords(code, "nl", "en", self.level)
+        expected = "mens is ask Hallo welkom bij Hedy!"
+
+        self.assertEqual(result, expected)
+
+
+    @check_local_lang_bool
+    def test_translate_back(self):
+        code = "print welkom bij Hedy\nnaam is ask what is your name\nprint naam"
+
+        result = hedy_translation.translate_keywords(code, "en", "nl", self.level)
+        result = hedy_translation.translate_keywords(code, "nl", "en", self.level)
+
+        expected = "print welkom bij Hedy\nnaam is ask what is your name\nprint naam"
+
+        self.assertEqual(result, expected)
+
+    @check_local_lang_bool
+    def test_invalid(self):
+        code = "hallo"
+
+        result = hedy_translation.translate_keywords(code, "en", "nl", self.level)
+        expected = "hallo"
+
+        self.assertEqual(result, expected)
+
+    @check_local_lang_bool
+    def test_invalid_space(self):
+        code = " print Hedy"
+
+        result = hedy_translation.translate_keywords(code, "en", "nl", self.level)
+        expected = " print Hedy"
+
+        self.assertEqual(result, expected)
+
+    @check_local_lang_bool
+    def test_echo(self):
+        code = "echo Hedy"
+
+        result = hedy_translation.translate_keywords(code, "en", "nl", self.level)
+        expected = "echo Hedy"
+
+        self.assertEqual(result, expected)
+
+    @check_local_lang_bool
+    def no_argument_ask_english(self):
+        code = "ask"
+
+        result = hedy_translation.translate_keywords(code, "en", "nl", self.level)
+        expected = "vraag"
+
+        self.assertEqual(result, expected)
+
+    @check_local_lang_bool
+    def no_argument_ask_dutch(self):
+        code = "vraag"
+
+        result = hedy_translation.translate_keywords(code, "nl", "en", self.level)
+        expected = "ask"
+
+        self.assertEqual(result, expected)
\ No newline at end of file