<commit_before>#!/usr/bin/env python3

import sys, os, unittest, logging, pkg_resources

# Extend PYTHONPATH with local 'lib' folder
jasyroot = os.path.normpath(os.path.join(os.path.abspath(sys.argv[0]), os.pardir, os.pardir, os.pardir))
sys.path.insert(0, jasyroot)

import jasy.js.parse.Parser as Parser
import jasy.js.parse.ScopeScanner as ScopeScanner
import jasy.js.output.Compressor as Compressor
import jasy.i18n.Translation as Translation


class Tests(unittest.TestCase):

    def process(self, code):
        node = Parser.parse(code)

        translation = Translation.Translation("de_DE", table={
            
            "Hello World": "Hallo Welt",
            "Short": "Kurz",
            "Thank you for the flowers": "Danke für die Blumen",
            
            "Hello %1!": "Hallo: %1!",
            "Hello %1! %1!": "Hallo: %1! %1!",
            
            "Chat (noum)": "Unterhaltung",
            "Chat (noum) %1": "Unterhaltung %1",
            
            "You have got a new mail": "Du hast eine neue E-Mail",
            "You have got new mails": "Du hast neue E-Mails",
            "You have got %1 new mails": "Du hast %1 neue E-Mail erhalten"
            
        })
        
        translation.patch(node)
        
        return Compressor.Compressor().compress(node)        


    def test_basic(self):
        self.assertEqual(self.process(
            '''
            function wrapper()
            {
                alert(this.tr("Hello World"));
                alert(tr("Short"));
                alert(core.Locale.tr("Thank you for the flowers"));
            }
            '''),
            'function wrapper(){alert("Hallo Welt");alert("Kurz");alert("Danke für die Blumen")}'
        )


    def test_vars1(self):
        self.assertEqual(self.process(
            '''
            function wrapper() {
                alert(tr("Hello %1!", "Peter"))
            }
            '''),
            'function wrapper(){alert("Hallo: "+("Peter")+"!")}'
        )        

    def test_vars2(self):
        self.assertEqual(self.process(
            '''
            function wrapper() {
                alert(tr("Hello %1! %1!", "Peter"))
            }
            '''),
            'function wrapper(){alert("Hallo: "+("Peter")+"! "+("Peter")+"!")}'
        )        

    def test_vars3(self):
        self.assertEqual(self.process(
            '''
            function wrapper() {
                alert(tr("Hello %1!", this.getGreetingName()))
            }
            '''),
            'function wrapper(){alert("Hallo: "+this.getGreetingName()+"!")}'
        )
            
    def test_vars4(self):
        self.assertEqual(self.process(
            '''
            function wrapper() {
                alert(tr("Hello %1! %1!", this.getGreetingName()))
            }
            '''),
            'function wrapper(){alert("Hallo: "+this.getGreetingName()+"! "+this.getGreetingName()+"!")}'
        )        
 
    def test_trc1(self):
        self.assertEqual(self.process(
            '''
            function wrapper() {
                alert(trc("Chat (noum)", "Chat"));
            }
            '''),
            'function wrapper(){alert("Unterhaltung")}'
        )
        
    def test_trc2(self):
        self.assertEqual(self.process(
            '''
            function wrapper() {
                alert(trc("Chat (noum) %1", "Chat %1", "Online"));
            }
            '''),
            'function wrapper(){alert("Unterhaltung "+("Online"))}'
        )
        
    def test_trc3(self):
        self.assertEqual(self.process(
            '''
            function wrapper() {
                alert(trc("Chat (noum) %1", "Chat %1", this.getChatStatus()));
            }
            '''),
            'function wrapper(){alert("Unterhaltung "+this.getChatStatus())}'
        )
        
        
    def test_trn1(self):
        self.assertEqual(self.process(
            '''
            function wrapper() {
                alert(trn("You have got a new mail", "You have got new mails", newMails));
            }
            '''),
            'function wrapper(){alert((newMails<=1?"Du hast eine neue E-Mail":"Du hast neue E-Mails"))}'
        )

    def test_trn2(self):
        self.assertEqual(self.process(
            '''
            function wrapper()
            {
                alert(trn("You have got a new mail", "You have got %1 new mails", newMails, newMails));
            }
            '''),
            'function wrapper(){alert((newMails<=1?"Du hast eine neue E-Mail":"Du hast "+newMails+" neue E-Mail erhalten"))}'
        )


    def test_marktr(self):
        self.assertEqual(self.process(
            '''
            function wrapper()
            {
                // Register strings in translation file (will be compiled out)
                // According to doc, marktr() does mark for tranlsation, but always returns the original text.
                marktr("Dog");
                marktr("Cat");
                marktr("Bird");

                // After marking the text these can be used for translation
                var objs = ["Dog","Cat","Bird"];
                for (var i=0, l=objs.length; i<l; i++) {
                    alert(tr(objs[i]));
                }
            }
            '''),<commit_msg>Updated for newest jasy translation features<commit_after>#!/usr/bin/env python3

import sys, os, unittest, logging, pkg_resources

# Extend PYTHONPATH with local 'lib' folder
jasyroot = os.path.normpath(os.path.join(os.path.abspath(sys.argv[0]), os.pardir, os.pardir, os.pardir))
sys.path.insert(0, jasyroot)

import jasy.js.parse.Parser as Parser
import jasy.js.parse.ScopeScanner as ScopeScanner
import jasy.js.output.Compressor as Compressor
import jasy.i18n.Translation as Translation


class Tests(unittest.TestCase):

    def process(self, code):
        node = Parser.parse(code)

        translation = Translation.Translation("de_DE", table={
            
            "Hello World": "Hallo Welt",
            "Short": "Kurz",
            "Thank you for the flowers": "Danke für die Blumen",
            
            "Hello %1!": "Hallo: %1!",
            "Hello %1! %1!": "Hallo: %1! %1!",
            
            "Chat[C:Chat (noum)]": "Unterhaltung",
            "Chat %1[C:Chat (noum) %1]": "Unterhaltung %1",
            
            "You have got a new mail[N:You have got new mails]": {0:"Du hast eine neue E-Mail", 1:"Du hast neue E-Mails"},
            "You have got a new mail[N:You have got %1 new mails]": {0:"Du hast eine neue E-Mail", 1:"Du hast %1 neue E-Mail erhalten"}
            
        })
        
        translation.patch(node)
        
        return Compressor.Compressor().compress(node)        


    def test_basic(self):
        self.assertEqual(self.process(
            '''
            function wrapper()
            {
                alert(this.tr("Hello World"));
                alert(tr("Short"));
                alert(core.Locale.tr("Thank you for the flowers"));
            }
            '''),
            'function wrapper(){alert("<NAME>");alert("Kurz");alert("<NAME> die Blumen")}'
        )


    def test_vars1(self):
        self.assertEqual(self.process(
            '''
            function wrapper() {
                alert(tr("Hello %1!", "Peter"))
            }
            '''),
            'function wrapper(){alert("Hallo: "+("Peter")+"!")}'
        )        

    def test_vars2(self):
        self.assertEqual(self.process(
            '''
            function wrapper() {
                alert(tr("Hello %1! %1!", "Peter"))
            }
            '''),
            'function wrapper(){alert("Hallo: "+("Peter")+"! "+("Peter")+"!")}'
        )        

    def test_vars3(self):
        self.assertEqual(self.process(
            '''
            function wrapper() {
                alert(tr("Hello %1!", this.getGreetingName()))
            }
            '''),
            'function wrapper(){alert("Hallo: "+this.getGreetingName()+"!")}'
        )
            
    def test_vars4(self):
        self.assertEqual(self.process(
            '''
            function wrapper() {
                alert(tr("Hello %1! %1!", this.getGreetingName()))
            }
            '''),
            'function wrapper(){alert("Hallo: "+this.getGreetingName()+"! "+this.getGreetingName()+"!")}'
        )        
 
    def test_trc1(self):
        self.assertEqual(self.process(
            '''
            function wrapper() {
                alert(trc("Chat (noum)", "Chat"));
            }
            '''),
            'function wrapper(){alert("Unterhaltung")}'
        )
        
    def test_trc2(self):
        self.assertEqual(self.process(
            '''
            function wrapper() {
                alert(trc("Chat (noum) %1", "Chat %1", "Online"));
            }
            '''),
            'function wrapper(){alert("Unterhaltung "+("Online"))}'
        )
        
    def test_trc3(self):
        self.assertEqual(self.process(
            '''
            function wrapper() {
                alert(trc("Chat (noum) %1", "Chat %1", this.getChatStatus()));
            }
            '''),
            'function wrapper(){alert("Unterhaltung "+this.getChatStatus())}'
        )
        
        
    def test_trn1(self):
        self.assertEqual(self.process(
            '''
            function wrapper() {
                alert(trn("You have got a new mail", "You have got new mails", newMails));
            }
            '''),
            'function wrapper(){alert(trnc({0:"Du hast eine neue E-Mail",1:"Du hast neue E-Mails"},newMails))}'
        )

    def test_trn2(self):
        self.assertEqual(self.process(
            '''
            function wrapper()
            {
                alert(trn("You have got a new mail", "You have got %1 new mails", newMails, newMails));
            }
            '''),
            'function wrapper(){alert(trnc({0:"Du hast eine neue E-Mail",1:"Du hast "+newMails+" neue E-Mail erhalten"},newMails))}'
        )


    def test_marktr(self):
        self.assertEqual(self.process(
            '''
            function wrapper()
            {
                // Register strings in translation file (will be compiled out)
                // According to doc, marktr() does mark for tranlsation, but always returns the original text.
                marktr("Dog");
                marktr("Cat");
                marktr("Bird");

                // After marking the text these can be used for translation
                var objs = ["Dog","Cat","Bird"];
                for (var i=0, l=objs.length; i<l; i++) {
                    alert(tr(objs[i]));
                }
            }
            '''),
{'fraction_non_alphanumeric': 0.1501585525088603, 'fraction_numerical': 0.007741093079649319, 'mean_word_length': 1.589471142236175, 'pattern_counts': {'":': 21, '<': 9, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4705215', 'n_tokens_mistral': 3117, 'n_tokens_neox': 3025, 'n_words': 667}