Grammalecte  Check-in [a80c085797]

Overview
Comment:[build] include tests in Python package
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk | build
Files: files | file ages | folders
SHA3-256: a80c08579795457830ebb4c41bf1d5c938dd2c8e10c5e78bfd46ce92994d75a8
User & Date: olr on 2017-05-01 09:35:28
Other Links: manifest | tags
Context
2017-05-01
10:46
[build] useless conditions check-in: c4e9c97a0e user: olr tags: build, trunk
09:35
[build] include tests in Python package check-in: a80c085797 user: olr tags: build, trunk
07:21
[build] clarity + remove generated test file from repo check-in: d154fcd96b user: olr tags: build, trunk
Changes

Modified gc_lang/fr/modules/tests.py from [d8176c7c6e] to [5fc75f3fc6].

2
3
4
5
6
7
8
9

10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
...
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
...
229
230
231
232
233
234
235

236
237
238
239
240
241
242
# coding: UTF-8

import unittest
import os
import re
import time

try:

    import grammalecte.ibdawg as ibdawg
    import grammalecte.fr as gce
    import grammalecte.fr.conj as conj
    import grammalecte.fr.phonet as phonet
    import grammalecte.fr.mfsp as mfsp
    from grammalecte.echo import echo
except ImportError:
    import sys
    sys.path.append(os.path.abspath('.'))
    sys.path.append(os.path.abspath('..'))
    import grammalecte.ibdawg as ibdawg
    import grammalecte.fr as gce
    import grammalecte.fr.conj as conj
    import grammalecte.fr.phonet as phonet
    import grammalecte.fr.mfsp as mfsp
    from grammalecte.echo import echo




def _fuckBackslashUTF8 (s):
    "fuck that shit"
    return s.replace("\u2019", "'").replace("\u2013", "–").replace("\u2014", "—")


................................................................................
    def setUpClass (cls):
        gce.load()
        cls._zError = re.compile(r"\{\{.*?\}\}")
        cls._aRuleTested = set()

    def test_parse (self):
        zOption = re.compile("^__([a-zA-Z0-9]+)__ ")
        bShowUntested = False
        for sf in [ "gc_test.txt" ]:
            with self.subTest(msg=sf):
                with open("./tests/fr/"+sf, "r", encoding="utf-8") as hSrc:
                    for sLine in ( s for s in hSrc if not s.startswith("#") and s.strip() ):
                        sLineNum = sLine[:10].strip()
                        sLine = sLine[10:].strip()
                        sOption = None
                        m = zOption.search(sLine)
                        if m:
                            sLine = sLine[m.end():]
                            sOption = m.group(1)
                        if "->>" in sLine:
                            sErrorText, sExceptedSuggs = self._splitTestLine(sLine)
                            if sExceptedSuggs.startswith('"') and sExceptedSuggs.endswith('"'):
                                sExceptedSuggs = sExceptedSuggs[1:-1]
                        else:
                            sErrorText = sLine.strip()
                            sExceptedSuggs = ""
                        sExpectedErrors = self._getExpectedErrors(sErrorText)
                        sTextToCheck = sErrorText.replace("}}", "").replace("{{", "")
                        sFoundErrors, sListErr, sFoundSuggs = self._getFoundErrors(sTextToCheck, sOption)
                        self.assertEqual(sExpectedErrors, sFoundErrors, \
                                         "\n# Line num: " + sLineNum + \
                                         "\n> to check: " + _fuckBackslashUTF8(sTextToCheck) + \
                                         "\n  expected: " + sExpectedErrors + \
                                         "\n  found:    " + sFoundErrors + \
                                         "\n  errors:   \n" + sListErr)
                        if sExceptedSuggs:
                            self.assertEqual(sExceptedSuggs, sFoundSuggs, "\n# Line num: " + sLineNum + "\n> to check: " + _fuckBackslashUTF8(sTextToCheck) + "\n  errors:   \n" + sListErr)
                bShowUntested = True
        if bShowUntested:
            i = 0
            for sOpt, sLineId, sRuleId in gce.listRules():
                if sLineId not in self._aRuleTested and not re.match("[0-9]+[sp]$", sRuleId):
                    echo(sRuleId, end= ", ")
                    i += 1
            if i:
                echo("\n[{} untested rules]".format(i))

    def _splitTestLine (self, sLine):
        sText, sSugg = sLine.split("->>")
        return (sText.strip(), sSugg.strip())

    def _getFoundErrors (self, sLine, sOption):
        if sOption:
................................................................................
    with open("./tests/fr/perf.txt", "r", encoding="utf-8") as hSrc, \
         open("./tests/fr/perf_memo.txt", "a", encoding="utf-8", newline="\n") as hDst:
        hDst.write("{:<12}{:<20}".format(sVersion, time.strftime("%Y.%m.%d %H:%M")))
        for sText in ( s.strip() for s in hSrc if not s.startswith("#") and s.strip() ):
            with timeblock(sText[:sText.find(".")], hDst):
                aErrs = gce.parse(sText)
        hDst.write("\n")


def main():
    unittest.main()


if __name__ == '__main__':
    main()







<
>
|
|
|
|
<
<
<
|
<
<
<
<
<
<
<
|
<
<







 







|
|
<
<
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<
|
|
|
|
|
|
|







 







>







2
3
4
5
6
7
8

9
10
11
12
13



14







15


16
17
18
19
20
21
22
...
123
124
125
126
127
128
129
130
131


132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158

159
160
161
162
163
164
165
166
167
168
169
170
171
172
...
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# coding: UTF-8

import unittest
import os
import re
import time



from .. import ibdawg
from . import gc_engine as gce
from . import conj
from . import phonet



from . import mfsp







from ..echo import echo




def _fuckBackslashUTF8 (s):
    "fuck that shit"
    return s.replace("\u2019", "'").replace("\u2013", "–").replace("\u2014", "—")


................................................................................
    def setUpClass (cls):
        gce.load()
        cls._zError = re.compile(r"\{\{.*?\}\}")
        cls._aRuleTested = set()

    def test_parse (self):
        zOption = re.compile("^__([a-zA-Z0-9]+)__ ")
        spHere, spfThisFile = os.path.split(__file__)
        with open(os.path.join(spHere, "gc_test.txt"), "r", encoding="utf-8") as hSrc:


            for sLine in ( s for s in hSrc if not s.startswith("#") and s.strip() ):
                sLineNum = sLine[:10].strip()
                sLine = sLine[10:].strip()
                sOption = None
                m = zOption.search(sLine)
                if m:
                    sLine = sLine[m.end():]
                    sOption = m.group(1)
                if "->>" in sLine:
                    sErrorText, sExceptedSuggs = self._splitTestLine(sLine)
                    if sExceptedSuggs.startswith('"') and sExceptedSuggs.endswith('"'):
                        sExceptedSuggs = sExceptedSuggs[1:-1]
                else:
                    sErrorText = sLine.strip()
                    sExceptedSuggs = ""
                sExpectedErrors = self._getExpectedErrors(sErrorText)
                sTextToCheck = sErrorText.replace("}}", "").replace("{{", "")
                sFoundErrors, sListErr, sFoundSuggs = self._getFoundErrors(sTextToCheck, sOption)
                self.assertEqual(sExpectedErrors, sFoundErrors, \
                                 "\n# Line num: " + sLineNum + \
                                 "\n> to check: " + _fuckBackslashUTF8(sTextToCheck) + \
                                 "\n  expected: " + sExpectedErrors + \
                                 "\n  found:    " + sFoundErrors + \
                                 "\n  errors:   \n" + sListErr)
                if sExceptedSuggs:
                    self.assertEqual(sExceptedSuggs, sFoundSuggs, "\n# Line num: " + sLineNum + "\n> to check: " + _fuckBackslashUTF8(sTextToCheck) + "\n  errors:   \n" + sListErr)
        # untested rules

        i = 0
        for sOpt, sLineId, sRuleId in gce.listRules():
            if sLineId not in self._aRuleTested and not re.match("[0-9]+[sp]$", sRuleId):
                echo(sRuleId, end= ", ")
                i += 1
        if i:
            echo("\n[{} untested rules]".format(i))

    def _splitTestLine (self, sLine):
        sText, sSugg = sLine.split("->>")
        return (sText.strip(), sSugg.strip())

    def _getFoundErrors (self, sLine, sOption):
        if sOption:
................................................................................
    with open("./tests/fr/perf.txt", "r", encoding="utf-8") as hSrc, \
         open("./tests/fr/perf_memo.txt", "a", encoding="utf-8", newline="\n") as hDst:
        hDst.write("{:<12}{:<20}".format(sVersion, time.strftime("%Y.%m.%d %H:%M")))
        for sText in ( s.strip() for s in hSrc if not s.startswith("#") and s.strip() ):
            with timeblock(sText[:sText.find(".")], hDst):
                aErrs = gce.parse(sText)
        hDst.write("\n")


def main():
    unittest.main()


if __name__ == '__main__':
    main()

Modified make.py from [9f8a0c4296] to [9a1c31726d].

347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
...
376
377
378
379
380
381
382





383
384
385
386
387
388
389
...
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
    try:
        lRules = open(spLang + "/rules.grx", 'r', encoding="utf-8").readlines()
    except:
        print("Rules file in project [" + sLang + "] not found")
        return
    dVars.update(compile_rules.make(lRules, dVars['lang'], bJavaScript))

    # generating test files
    with open("tests/"+sLang+"/gc_test.txt", "w", encoding="utf-8", newline="\n") as hDstPy:
        hDstPy.write("# TESTS FOR LANG [" + sLang + "]\n\n")
        hDstPy.write(dVars['gctests'])

    ## READ GRAMMAR CHECKER PLUGINS
    print("PYTHON:")
    print("+ Plugins: ", end="")
    sCodePlugins = ""
    for sf in os.listdir(spLang+"/modules"):
        if re.match(r"gce_\w+[.]py$", sf):
            sCodePlugins += "\n\n" + open(spLang+'/modules/'+sf, "r", encoding="utf-8").read()
................................................................................
    print("+ Modules: ", end="")
    for sf in os.listdir(spLang+"/modules"):
        if not sf.endswith(".tpl.py") and not sf.startswith("gce_"):
            file_util.copy_file(spLang+"/modules/"+sf, spLangPack)
            print(sf, end=", ")
    print()






    createOXT(spLang, dVars, xConfig._sections['oxt'], spLangPack, bInstallOXT)

    createServerOptions(sLang, dVars)
    createServerZip(sLang, dVars, spLangPack)

    #### JAVASCRIPT
    if bJavaScript:
................................................................................
            # make
            sVersion = create(sLang, xConfig, xArgs.install, xArgs.javascript, )

            # tests
            if xArgs.tests or xArgs.perf:
                print("> Running tests")
                try:
                    tests = importlib.import_module("tests."+sLang+"_test")
                    print(tests.__file__)
                except ImportError:
                    print("# Error. Couldn't import file {}_test.py in folder tests".format(sLang))
                else:
                    if xArgs.tests:
                        xTestSuite = unittest.TestLoader().loadTestsFromModule(tests)
                        unittest.TextTestRunner().run(xTestSuite)







<
<
<
<
<







 







>
>
>
>
>







 







|







347
348
349
350
351
352
353





354
355
356
357
358
359
360
...
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
...
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
    try:
        lRules = open(spLang + "/rules.grx", 'r', encoding="utf-8").readlines()
    except:
        print("Rules file in project [" + sLang + "] not found")
        return
    dVars.update(compile_rules.make(lRules, dVars['lang'], bJavaScript))






    ## READ GRAMMAR CHECKER PLUGINS
    print("PYTHON:")
    print("+ Plugins: ", end="")
    sCodePlugins = ""
    for sf in os.listdir(spLang+"/modules"):
        if re.match(r"gce_\w+[.]py$", sf):
            sCodePlugins += "\n\n" + open(spLang+'/modules/'+sf, "r", encoding="utf-8").read()
................................................................................
    print("+ Modules: ", end="")
    for sf in os.listdir(spLang+"/modules"):
        if not sf.endswith(".tpl.py") and not sf.startswith("gce_"):
            file_util.copy_file(spLang+"/modules/"+sf, spLangPack)
            print(sf, end=", ")
    print()

    # TEST FILES
    with open("grammalecte/"+sLang+"/gc_test.txt", "w", encoding="utf-8", newline="\n") as hDstPy:
        hDstPy.write("# TESTS FOR LANG [" + sLang + "]\n\n")
        hDstPy.write(dVars['gctests'])

    createOXT(spLang, dVars, xConfig._sections['oxt'], spLangPack, bInstallOXT)

    createServerOptions(sLang, dVars)
    createServerZip(sLang, dVars, spLangPack)

    #### JAVASCRIPT
    if bJavaScript:
................................................................................
            # make
            sVersion = create(sLang, xConfig, xArgs.install, xArgs.javascript, )

            # tests
            if xArgs.tests or xArgs.perf:
                print("> Running tests")
                try:
                    tests = importlib.import_module("grammalecte."+sLang+".tests")
                    print(tests.__file__)
                except ImportError:
                    print("# Error. Couldn't import file {}_test.py in folder tests".format(sLang))
                else:
                    if xArgs.tests:
                        xTestSuite = unittest.TestLoader().loadTestsFromModule(tests)
                        unittest.TextTestRunner().run(xTestSuite)