@@ -90,12 +90,11 @@ function showDebugOutput(debugOutput, alwaysAlert = true) {
90
90
91
91
/**
92
92
* Given take a regex string, preprocess it (using our array of
93
- * preprocessing regexes), and return a final compiled regex.
93
+ * preprocessing regexes), and return a processed regex as a String .
94
94
* @regexString - String to be converted into a compiled Regex
95
- * @description - Description of @regexString's purpose (for error reports)
96
95
* @fullMatch - require full match (insert "^" at beginning, "$" at end).
97
96
*/
98
- function processRegex ( regexString , description , fullMatch = true ) {
97
+ function processRegexToString ( regexString , fullMatch = true ) {
99
98
let processedRegexString = regexString ;
100
99
for ( preprocessRegex of preprocessRegexes ) {
101
100
processedRegexString = processedRegexString . replace (
@@ -107,6 +106,18 @@ function processRegex(regexString, description, fullMatch = true) {
107
106
// work correctly and the first capturing (...) will be the first.
108
107
processedRegexString = '^(?:' + processedRegexString + ')$' ;
109
108
}
109
+ return processedRegexString ;
110
+ }
111
+
112
+ /**
113
+ * Given take a regex string, preprocess it (using our array of
114
+ * preprocessing regexes), and return a final compiled Regexp.
115
+ * @regexString - String to be converted into a compiled Regexp
116
+ * @description - Description of @regexString's purpose (for error reports)
117
+ * @fullMatch - require full match (insert "^" at beginning, "$" at end).
118
+ */
119
+ function processRegex ( regexString , description , fullMatch = true ) {
120
+ let processedRegexString = processRegexToString ( regexString , fullMatch ) ;
110
121
try {
111
122
let compiledRegex = new RegExp ( processedRegexString ) ;
112
123
return compiledRegex ;
@@ -118,6 +129,18 @@ function processRegex(regexString, description, fullMatch = true) {
118
129
}
119
130
}
120
131
132
+ /*
133
+ * Determine if preprocessing produces the expected final regex answer.
134
+ * @example - 2-element array. LHS is to be processed, RHS is expected result
135
+ */
136
+ function validProcessing ( example ) {
137
+ let [ unProcessed , expectedProcessed ] = example ;
138
+ let actualProcessed = processRegexToString ( unProcessed , false ) ;
139
+ // alert(`actual\n${actualProcessed}\nexpected\n${expectedProcessed}`);
140
+ // alert(`actual length\n${actualProcessed.length}\nexpected length\n${expectedProcessed.length}`);
141
+ return ( actualProcessed == expectedProcessed ) ;
142
+ }
143
+
121
144
/**
122
145
* Return true iff the indexed attempt matches the indexed correct.
123
146
* @attempt - Array of strings that might be correct
@@ -304,7 +327,7 @@ function processInfo(configurationInfo) {
304
327
305
328
const allowedInfoFields = new Set ( [
306
329
'hints' , 'successes' , 'failures' , 'correct' , 'expected' ,
307
- 'debug' ] ) ;
330
+ 'preprocessing' , 'preprocessingTests' , 'debug' ] ) ;
308
331
let usedFields = new Set ( Object . keys ( info ) ) ;
309
332
let forbiddenFields = usedFields . difference ( allowedInfoFields ) ;
310
333
if ( forbiddenFields . size != 0 ) {
@@ -364,6 +387,13 @@ function runSelftest() {
364
387
}
365
388
} ;
366
389
390
+ // Run tests of the preprocessing process, if present
391
+ for ( let example of ( info . preprocessingTests || [ ] ) ) {
392
+ if ( ! validProcessing ( example ) ) {
393
+ alert ( `Lab Error: preprocessing\n${ example . join ( "\n\n" ) } \nshould pass but fails.` ) ;
394
+ } ;
395
+ } ;
396
+
367
397
// Run tests in successes and failures, if present
368
398
if ( info . successes ) {
369
399
for ( let example of info . successes ) {
0 commit comments