Grammalecte  Diff

Differences From Artifact [d8176c7c6e]:

To Artifact [5fc75f3fc6]:


1
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
#! python3
# 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", "—")










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







1
2
3
4
5
6
7
8
9

10
11




12


13
14



15


16
17
18
19
20
21
22
#! python3
# 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", "—")


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
    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:







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







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
    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:
229
230
231
232
233
234
235

236
237
238
239
240
241
242
    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()







>







214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
    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()