Skip to content

Commit 42fcded

Browse files
Merge pull request #431 from ossf/preprocessing_test
Labs: Add mechanism for testing preprocessor
2 parents 851e6f4 + 9e3ce01 commit 42fcded

File tree

3 files changed

+72
-4
lines changed

3 files changed

+72
-4
lines changed

docs/labs/checker.js

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,11 @@ function showDebugOutput(debugOutput, alwaysAlert = true) {
9090

9191
/**
9292
* 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.
9494
* @regexString - String to be converted into a compiled Regex
95-
* @description - Description of @regexString's purpose (for error reports)
9695
* @fullMatch - require full match (insert "^" at beginning, "$" at end).
9796
*/
98-
function processRegex(regexString, description, fullMatch = true) {
97+
function processRegexToString(regexString, fullMatch = true) {
9998
let processedRegexString = regexString;
10099
for (preprocessRegex of preprocessRegexes) {
101100
processedRegexString = processedRegexString.replace(
@@ -107,6 +106,18 @@ function processRegex(regexString, description, fullMatch = true) {
107106
// work correctly and the first capturing (...) will be the first.
108107
processedRegexString = '^(?:' + processedRegexString + ')$';
109108
}
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);
110121
try {
111122
let compiledRegex = new RegExp(processedRegexString);
112123
return compiledRegex;
@@ -118,6 +129,18 @@ function processRegex(regexString, description, fullMatch = true) {
118129
}
119130
}
120131

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+
121144
/**
122145
* Return true iff the indexed attempt matches the indexed correct.
123146
* @attempt - Array of strings that might be correct
@@ -304,7 +327,7 @@ function processInfo(configurationInfo) {
304327

305328
const allowedInfoFields = new Set([
306329
'hints', 'successes', 'failures', 'correct', 'expected',
307-
'debug']);
330+
'preprocessing', 'preprocessingTests', 'debug']);
308331
let usedFields = new Set(Object.keys(info));
309332
let forbiddenFields = usedFields.difference(allowedInfoFields);
310333
if (forbiddenFields.size != 0) {
@@ -364,6 +387,13 @@ function runSelftest() {
364387
}
365388
};
366389

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+
367397
// Run tests in successes and failures, if present
368398
if (info.successes) {
369399
for (let example of info.successes) {

docs/labs/create_checker.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,17 @@ Each array element should itself be an array of:
556556
these buffer boundary constructs are not in ECMAScript/JavaScript,
557557
though there [is a proposal to add them](https://github.com/tc39/proposal-regexp-buffer-boundaries).
558558

559+
You can also test preprocessing by setting the info field
560+
`preprocessingTests` - if you don't set `preprocessing` itself, you're
561+
testing the default preprocessor.
562+
The `preprocessingTests` field contains an array of examples that
563+
test the preprocessor.
564+
Each example array is two elements long;
565+
the first is a pattern that could be
566+
requested, and the second is post-processed pattern that should result.
567+
There's no need for a "failure" test suite here, because we
568+
demand exact results for every test case.
569+
559570
Here is an example:
560571

561572
~~~~yaml
@@ -572,6 +583,17 @@ preprocessing:
572583
- |-
573584
(\\s\*)?[ \t]+(\\s\*)?
574585
- "\\s*"
586+
preprocessingTests:
587+
-
588+
- |-
589+
\s* console \. log \( (["'`])Hello,\x20world!\1 \) ; \s*
590+
- |-
591+
\s*console\s*\.\s*log\s*\(\s*(["'`])Hello,\x20world!\1\s*\)\s*;\s*
592+
-
593+
- |-
594+
\s* foo \s+ bar \\string\\ \s*
595+
- |-
596+
\s*foo\s+bar\s*\\string\\\s*
575597
~~~~
576598

577599
Here is an explanation of each of these preprocessing elements
@@ -585,3 +607,6 @@ in this example:
585607
3. 1+ spaces/tabs are instead interpreted as `\s*` (0+ whitespace).
586608
The optional expressions before and after it are an optimization,
587609
to coalesce this for speed.
610+
611+
If you load `hello.html` you'll automatically run some self-tests on
612+
the default preprocessor.

docs/labs/hello.html

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,19 @@
9393
# - |-
9494
# (\\s\*)?\s+(\\s\*)?
9595
# - "\\s*"
96+
# Here are tests for default preprocessing. You do not need this in
97+
# every lab, but it demonstrates how to do it.
98+
preprocessingTests:
99+
-
100+
- |-
101+
\s* console \. log \( (["'`])Hello,\x20world!\1 \) ; \s*
102+
- |-
103+
\s*console\s*\.\s*log\s*\(\s*(["'`])Hello,\x20world!\1\s*\)\s*;\s*
104+
-
105+
- |-
106+
\s* foo \s+ bar \\string\\ \s*
107+
- |-
108+
\s*foo\s+bar\s*\\string\\\s*
96109
# debug: true
97110
</script>
98111
<!--

0 commit comments

Comments
 (0)