Grammalecte  Diff

Differences From Artifact [5a6c382af8]:

To Artifact [9717be7ff0]:


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36



37
38

39
40
41
42
43
44
45
        this.spfTests = spfTests;
        this._aRuleTested = new Set();
    }

    * testParse (bDebug=false) {
        const t0 = Date.now();
        let sURL;
        if(typeof(process) !== 'undefined') {
            sURL = (this.spfTests !== "") ? this.spfTests : "./"+this.gce.lang+"/tests_data.json";
        } else {
            sURL = (this.spfTests !== "") ? this.spfTests : "resource://grammalecte/"+this.gce.lang+"/tests_data.json";
        }
        const aData = JSON.parse(helpers.loadFile(sURL)).aData;
        let nInvalid = 0;
        let nTotal = 0;
        let sErrorText;
        let sSugg;
        let sExpectedErrors;



        let sTextToCheck;
        let sFoundErrors;

        let sListErr;
        let sLineNum;
        let i = 1;
        let sUntestedRules = "";
        let bShowUntested = false;
        let zOption = /^__([a-zA-Z0-9]+)__ /;
        let sOption;







|








|

>
>
>


>







19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
        this.spfTests = spfTests;
        this._aRuleTested = new Set();
    }

    * testParse (bDebug=false) {
        const t0 = Date.now();
        let sURL;
        if (typeof(process) !== 'undefined') {
            sURL = (this.spfTests !== "") ? this.spfTests : "./"+this.gce.lang+"/tests_data.json";
        } else {
            sURL = (this.spfTests !== "") ? this.spfTests : "resource://grammalecte/"+this.gce.lang+"/tests_data.json";
        }
        const aData = JSON.parse(helpers.loadFile(sURL)).aData;
        let nInvalid = 0;
        let nTotal = 0;
        let sErrorText;
        let sExpectedSuggs;
        let sExpectedErrors;
        let nTestWithExpectedError = 0;
        let nTestWithExpectedErrorAndSugg = 0;
        let nUnexpectedErrors = 0;
        let sTextToCheck;
        let sFoundErrors;
        let sFoundSuggs;
        let sListErr;
        let sLineNum;
        let i = 1;
        let sUntestedRules = "";
        let bShowUntested = false;
        let zOption = /^__([a-zA-Z0-9]+)__ /;
        let sOption;
53
54
55
56
57
58
59
60
61
62
63
64

65
66



67
68
69
70
71
72
73
74
75
76
77










78
79
80
81
82
83
84














85









































86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
                    sOption = false;
                    m = zOption.exec(sLine);
                    if (m) {
                        sLine = sLine.slice(sLine.indexOf(" ")+1);
                        sOption = m[1];
                    }
                    if (sLine.includes("->>")) {
                        [sErrorText, sSugg] = sLine.split("->>");
                        sErrorText = sErrorText.trim();
                        sSugg = sSugg.trim();
                    } else {
                        sErrorText = sLine.trim();

                    }
                    sExpectedErrors = this._getExpectedErrors(sErrorText);



                    sTextToCheck = sErrorText.replace(/\{\{/g, "").replace(/\}\}/g, "");
                    [sFoundErrors, sListErr] = this._getFoundErrors(sTextToCheck, bDebug, sOption);
                    if (sExpectedErrors !== sFoundErrors) {
                        yield "\n" + i.toString() +
                              "\n# Line num: " + sLineNum +
                              "\n> to check: " + sTextToCheck +
                              "\n  expected: " + sExpectedErrors +
                              "\n  found:    " + sFoundErrors +
                              "\n  errors:   \n" + sListErr;
                        nInvalid = nInvalid + 1;
                    }










                    nTotal = nTotal + 1;
                }
                i = i + 1;
                if (i % 1000 === 0) {
                    yield i.toString();
                }
            }














            bShowUntested = true;









































        }
        catch (e) {
            console.error(e);
        }

        if (bShowUntested) {
            i = 0;
            for (let [sOpt, sLineId, sRuleId] of this.gce.listRules()) {
                if (sOpt !== "@@@@" && !this._aRuleTested.has(sLineId) && !/^[0-9]+[sp]$|^[pd]_/.test(sRuleId)) {
                    sUntestedRules += sLineId + "/" + sRuleId + ", ";
                    i += 1;
                }
            }
            if (i > 0) {
                yield sUntestedRules + "\n[" + i.toString() + " untested rules]";
            }
        }

        const t1 = Date.now();
        yield "Tests parse finished in " + ((t1-t0)/1000).toString()
            + " s\nTotal errors: " + nInvalid.toString() + " / " + nTotal.toString();
    }

    _getExpectedErrors (sLine) {
        try {
            let sRes = " ".repeat(sLine.length);
            let z = /\{\{.+?\}\}/g;
            let m;







|
<
|


>


>
>
>

|









>
>
>
>
>
>
>
>
>
>







>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>




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







57
58
59
60
61
62
63
64

65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
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
                    sOption = false;
                    m = zOption.exec(sLine);
                    if (m) {
                        sLine = sLine.slice(sLine.indexOf(" ")+1);
                        sOption = m[1];
                    }
                    if (sLine.includes("->>")) {
                        [sErrorText, sExpectedSuggs] = this._splitTestLine(sLine);

                        nTestWithExpectedErrorAndSugg += 1
                    } else {
                        sErrorText = sLine.trim();
                        sExpectedSuggs = "";
                    }
                    sExpectedErrors = this._getExpectedErrors(sErrorText);
                    if (sExpectedErrors.trim() != "") {
                        nTestWithExpectedError += 1;
                    }
                    sTextToCheck = sErrorText.replace(/\{\{/g, "").replace(/\}\}/g, "");
                    [sFoundErrors, sListErr, sFoundSuggs] = this._getFoundErrors(sTextToCheck, bDebug, sOption);
                    if (sExpectedErrors !== sFoundErrors) {
                        yield "\n" + i.toString() +
                              "\n# Line num: " + sLineNum +
                              "\n> to check: " + sTextToCheck +
                              "\n  expected: " + sExpectedErrors +
                              "\n  found:    " + sFoundErrors +
                              "\n  errors:   \n" + sListErr;
                        nInvalid = nInvalid + 1;
                    }
                    else if (sExpectedSuggs) {
                        if (!this._checkSuggestions(sExpectedSuggs, sFoundSuggs)) {
                            yield  "\n# Line num: " + sLineNum +
                                   "\n> to check: " + sTextToCheck +
                                   "\n  expected: " + sExpectedSuggs +
                                   "\n  found:    " + sFoundSuggs +
                                   "\n  errors:   \n" + sListErr;
                            nUnexpectedErrors += 1;
                        }
                    }
                    nTotal = nTotal + 1;
                }
                i = i + 1;
                if (i % 1000 === 0) {
                    yield i.toString();
                }
            }
            yield "Tests with expected errors: " + nTestWithExpectedError + " and suggestions: " + nTestWithExpectedErrorAndSugg + " > " + nTestWithExpectedErrorAndSugg/nTestWithExpectedError*100 + " %";
            if (nUnexpectedErrors) {
                yield "Unexpected errors: " + nUnexpectedErrors;
            }
            yield* this._showUntestedRules()
        }
        catch (e) {
            console.error(e);
        }
        const t1 = Date.now();
        yield "Tests parse finished in " + ((t1-t0)/1000).toString()
            + " s\nTotal errors: " + nInvalid.toString() + " / " + nTotal.toString();
    }

    * _showUntestedRules () {
        let i = 0;
        for (let [sOpt, sLineId, sRuleId] of this.gce.listRules()) {
            if (sOpt !== "@@@@" && !this._aRuleTested.has(sLineId) && !/^[0-9]+[sp]$|^[pd]_/.test(sRuleId)) {
                sUntestedRules += sLineId + "/" + sRuleId + ", ";
                i += 1;
            }
        }
        if (i > 0) {
            yield sUntestedRules + "\n[" + i.toString() + " untested rules]";
        }
    }

    _splitTestLine (sLine) {
        let [sText, sSugg] = sLine.split("->>");
        sSugg = sSugg.trim().replace(/ /g, " ");
        if (sSugg.startsWith('"') && sSugg.endsWith('"')) {
            sSugg = sSugg.slice(1,-1);
        }
        return [sText.trim(), sSugg];
    }

    _getFoundErrors (sLine, bDebug, sOption) {
        try {
            let aErrs = [];
            if (sOption) {
                this.gce.setOption(sOption, true);
                aErrs = this.gce.parse(sLine, "FR", bDebug);
                this.gce.setOption(sOption, false);
            } else {
                aErrs = this.gce.parse(sLine, "FR", bDebug);
            }
            let sRes = " ".repeat(sLine.length);
            let sListErr = "";
            let lAllSugg = [];
            for (let oErr of aErrs.sort( (a,b) => a["nStart"] - b["nStart"] )) {
                sRes = sRes.slice(0, oErr["nStart"]) + "~".repeat(oErr["nEnd"] - oErr["nStart"]) + sRes.slice(oErr["nEnd"]);
                sListErr += "    * {" + oErr['sLineId'] + " / " + oErr['sRuleId'] + "}  at  " + oErr['nStart'] + ":" + oErr['nEnd'] + "\n";
                lAllSugg.push(oErr["aSuggestions"].join("|"));
                this._aRuleTested.add(oErr["sLineId"]);
            }
            return [sRes, sListErr, lAllSugg.join("|||")];
        }
        catch (e) {
            console.error(e);
        }
        return [" ".repeat(sLine.length), "", ""];
















    }

    _getExpectedErrors (sLine) {
        try {
            let sRes = " ".repeat(sLine.length);
            let z = /\{\{.+?\}\}/g;
            let m;
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
        }
        catch (e) {
            console.error(e);
        }
        return " ".repeat(sLine.length);
    }

    _getFoundErrors (sLine, bDebug, sOption) {
        try {
            let aErrs = [];
            if (sOption) {
                this.gce.setOption(sOption, true);
                aErrs = this.gce.parse(sLine, "FR", bDebug);
                this.gce.setOption(sOption, false);
            } else {
                aErrs = this.gce.parse(sLine, "FR", bDebug);
            }
            let sRes = " ".repeat(sLine.length);
            let sListErr = "";
            for (let dErr of aErrs) {
                sRes = sRes.slice(0, dErr["nStart"]) + "~".repeat(dErr["nEnd"] - dErr["nStart"]) + sRes.slice(dErr["nEnd"]);
                sListErr += "    * {" + dErr['sLineId'] + " / " + dErr['sRuleId'] + "}  at  " + dErr['nStart'] + ":" + dErr['nEnd'] + "\n";
                this._aRuleTested.add(dErr["sLineId"]);
            }

            return [sRes, sListErr];
        }
        catch (e) {
            console.error(e);
        }
        return [" ".repeat(sLine.length), ""];
    }

}


if (typeof(exports) !== 'undefined') {
    exports.TestGrammarChecking = TestGrammarChecking;
}







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

|

<






184
185
186
187
188
189
190
191
192
193
194



195

196
197
198
199




200
201
202


203
204
205

206
207
208
209
210
211
        }
        catch (e) {
            console.error(e);
        }
        return " ".repeat(sLine.length);
    }

    _checkSuggestions (sAllExceptedSuggs, sAllFoundSuggs) {
        let lAllExpectedSuggs = sAllExceptedSuggs.split("|||");
        let lAllFoundSuggs = sAllFoundSuggs.split("|||");
        if (lAllExpectedSuggs.length != lAllFoundSuggs.length) {



            return false;

        }
        for (let i = 0;  i < lAllExpectedSuggs.length;  i++) {
            let aExpectedSuggs = new Set(lAllExpectedSuggs[i].split("|"));
            let aFoundSuggs = new Set(lAllFoundSuggs[i].split("|"));




            if (aExpectedSuggs.size !== aFoundSuggs.size || ![...aExpectedSuggs].every(value => aFoundSuggs.has(value))) {
                return false;
            }


        }
        return true;
    }

}


if (typeof(exports) !== 'undefined') {
    exports.TestGrammarChecking = TestGrammarChecking;
}