From da9301d0c07a317e82339b24714368f7de99a88f Mon Sep 17 00:00:00 2001 From: "David A. Wheeler" Date: Wed, 29 Jan 2025 15:03:39 -0500 Subject: [PATCH 1/3] assert: Remove YAML Signed-off-by: David A. Wheeler --- docs/labs/assert.html | 78 +--------------------------------------- docs/labs/assert.js | 82 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+), 77 deletions(-) create mode 100644 docs/labs/assert.js diff --git a/docs/labs/assert.html b/docs/labs/assert.html index 8c434d45..49becb59 100644 --- a/docs/labs/assert.html +++ b/docs/labs/assert.html @@ -7,6 +7,7 @@ + @@ -25,83 +26,6 @@ \} \s* - diff --git a/docs/labs/assert.js b/docs/labs/assert.js new file mode 100644 index 00000000..02fc80ec --- /dev/null +++ b/docs/labs/assert.js @@ -0,0 +1,82 @@ +info = +{ + hints: [ + { + present: "assert\n", + text: "The whole point of this exercise is to NOT use `assert` as a way to validate input from untrusted users.", + "examples": [ + [ "assert !bindingResult.hasErrors();\n" ] + ] + }, + { + absent: String.raw`^\s*if +`, + text: "Begin with `if` so you can return a result if there are errors.", + "examples": [ + [ "return \"form\";\n" ] + ] + }, + { + present: "(bindingresult|BindingResult)", + text: "Java is case-sensitive. Use `bindingResult`, not `bindingresult` nor `BindingResult`." + }, + { + present: "(haserrors|HasErrors)", + text: "Java is case-sensitive. Use `hasErrors`, not `haserrors` nor `HasErrors`." + }, + { + present: String.raw`^\s*if\s*[^\(\s] +`, + text: "In Java, after the keyword `if` you must have an open left parenthesis. Conventionally there is one space between the `if` keyword and the open left parenthesis.", + "examples": [ + [ "if bindingResult.hasErrors\n" ] + ] + }, + { + present: String.raw`^\s*if\s*\(\s*\!binding +`, + text: "You have an extraneous `!` (not operator). Use the expression if (bindingResult.hasErrors()) ...", + "examples": [ + [ "if (!bindingResult.hasErrors())\n" ] + ] + }, + { + absent: String.raw`^ if \( bindingResult \. hasErrors \( \) \) +`, + text: "Begin the answer with the text `if (bindingResult.hasErrors())` so that a statement will be executed if that condition is true." + }, + { + present: String.raw`if \( bindingResult \. hasErrors \( \) \) [^\{\s] +`, + text: "Follow the conditional with an open brace, e.g., `if (bindingResult.hasErrors()) {...`." + }, + { + absent: String.raw`return "form" +`, + text: "You need to use `return \"form\";` somewhere." + }, + { + present: String.raw`return "form" +`, + absent: String.raw`return "form" ;`, + text: "You need to use `;` (semicolon) after `return \"form\"` because in Java statements must be followed by a semicolon." + }, + { + absent: String.raw`\} $ +`, + text: "The answer needs to end with `}` (closing brace)." + } + ], + successes: [ + [ + "if ( bindingResult.hasErrors() ) {\n return \"form\";\n}\n", + "if ( bindingResult . hasErrors ( ) ) { return \"form\" ; }\n" + ] + ], + failures: [ + "if ( bindingResult . hasErrors ( ) ) { return \"form\" }\n", + "if ( ! bindingResult . hasErrors ( ) ) { return \"form\" ; }\n", + "if bindingResult . hasErrors ( ) { return \"form\" ; }\n", + "if ( bindingResult . hasErrors ) { return \"form\" ; }\n" + ] +} From 34f3a3097566cb9c0a028fccef58bda363022106 Mon Sep 17 00:00:00 2001 From: "David A. Wheeler" Date: Wed, 29 Jan 2025 15:09:15 -0500 Subject: [PATCH 2/3] Improve assert.js Signed-off-by: David A. Wheeler --- docs/labs/assert.js | 45 ++++++++++++++++++--------------------------- 1 file changed, 18 insertions(+), 27 deletions(-) diff --git a/docs/labs/assert.js b/docs/labs/assert.js index 02fc80ec..4d190d0e 100644 --- a/docs/labs/assert.js +++ b/docs/labs/assert.js @@ -2,18 +2,17 @@ info = { hints: [ { - present: "assert\n", + present: "assert", text: "The whole point of this exercise is to NOT use `assert` as a way to validate input from untrusted users.", "examples": [ [ "assert !bindingResult.hasErrors();\n" ] ] }, { - absent: String.raw`^\s*if -`, + absent: String.raw`^\s* if `, text: "Begin with `if` so you can return a result if there are errors.", "examples": [ - [ "return \"form\";\n" ] + [ "return \"form\";" ] ] }, { @@ -25,29 +24,25 @@ info = text: "Java is case-sensitive. Use `hasErrors`, not `haserrors` nor `HasErrors`." }, { - present: String.raw`^\s*if\s*[^\(\s] -`, + present: String.raw`^\s*if\s*[^\(\s]`, text: "In Java, after the keyword `if` you must have an open left parenthesis. Conventionally there is one space between the `if` keyword and the open left parenthesis.", "examples": [ - [ "if bindingResult.hasErrors\n" ] + [ "if bindingResult.hasErrors" ] ] }, { - present: String.raw`^\s*if\s*\(\s*\!binding -`, + present: String.raw`^\s*if\s*\(\s*\!binding`, text: "You have an extraneous `!` (not operator). Use the expression if (bindingResult.hasErrors()) ...", "examples": [ - [ "if (!bindingResult.hasErrors())\n" ] + [ "if (!bindingResult.hasErrors())" ] ] }, { - absent: String.raw`^ if \( bindingResult \. hasErrors \( \) \) -`, + absent: String.raw`^ if \( bindingResult \. hasErrors \( \) \) `, text: "Begin the answer with the text `if (bindingResult.hasErrors())` so that a statement will be executed if that condition is true." }, { - present: String.raw`if \( bindingResult \. hasErrors \( \) \) [^\{\s] -`, + present: String.raw`if \( bindingResult \. hasErrors \( \) \) [^\{\s] `, text: "Follow the conditional with an open brace, e.g., `if (bindingResult.hasErrors()) {...`." }, { @@ -56,27 +51,23 @@ info = text: "You need to use `return \"form\";` somewhere." }, { - present: String.raw`return "form" -`, + present: String.raw`return "form"`, absent: String.raw`return "form" ;`, text: "You need to use `;` (semicolon) after `return \"form\"` because in Java statements must be followed by a semicolon." }, { - absent: String.raw`\} $ -`, + absent: String.raw`\} $`, text: "The answer needs to end with `}` (closing brace)." - } + }, ], successes: [ - [ - "if ( bindingResult.hasErrors() ) {\n return \"form\";\n}\n", - "if ( bindingResult . hasErrors ( ) ) { return \"form\" ; }\n" - ] + [ "if ( bindingResult.hasErrors() ) {\n return \"form\";\n}\n" ], + [ "if ( bindingResult . hasErrors ( ) ) { return \"form\" ; }\n" ], ], failures: [ - "if ( bindingResult . hasErrors ( ) ) { return \"form\" }\n", - "if ( ! bindingResult . hasErrors ( ) ) { return \"form\" ; }\n", - "if bindingResult . hasErrors ( ) { return \"form\" ; }\n", - "if ( bindingResult . hasErrors ) { return \"form\" ; }\n" + [ "if ( bindingResult . hasErrors ( ) ) { return \"form\" }\n" ], + [ "if ( ! bindingResult . hasErrors ( ) ) { return \"form\" ; }\n" ], + [ "if bindingResult . hasErrors ( ) { return \"form\" ; }\n" ], + [ "if ( bindingResult . hasErrors ) { return \"form\" ; }\n" ], ] } From 623665901c0e9f77f2069932524b5edc2ea0072c Mon Sep 17 00:00:00 2001 From: "David A. Wheeler" Date: Wed, 29 Jan 2025 15:14:10 -0500 Subject: [PATCH 3/3] Move answers into JavaScript Signed-off-by: David A. Wheeler --- docs/labs/assert.html | 14 -------------- docs/labs/assert.js | 10 ++++++++++ 2 files changed, 10 insertions(+), 14 deletions(-) diff --git a/docs/labs/assert.html b/docs/labs/assert.html index 49becb59..021d1e79 100644 --- a/docs/labs/assert.html +++ b/docs/labs/assert.html @@ -12,20 +12,6 @@ - - - - - - diff --git a/docs/labs/assert.js b/docs/labs/assert.js index 4d190d0e..04c28787 100644 --- a/docs/labs/assert.js +++ b/docs/labs/assert.js @@ -60,6 +60,16 @@ info = text: "The answer needs to end with `}` (closing brace)." }, ], + expected: [ +` if (bindingResult.hasErrors()) { + return "form"; + }` + ], + correct: [ + String.raw`\s* if \( bindingResult \. hasErrors \( \) \) \{ + return "form" ; + \} \s*`, + ], successes: [ [ "if ( bindingResult.hasErrors() ) {\n return \"form\";\n}\n" ], [ "if ( bindingResult . hasErrors ( ) ) { return \"form\" ; }\n" ],