From 461a8db0892c9023595d1d745ac86b16bb5a6ca6 Mon Sep 17 00:00:00 2001 From: Sarah Rietkerk <49178322+srietkerk@users.noreply.github.com> Date: Wed, 14 Feb 2024 16:34:06 -0800 Subject: [PATCH] Teacher tool: field value validator added (#9867) * field value validator added * change template string for num equality validator Co-authored-by: Eric Anderson --------- Co-authored-by: Eric Anderson --- common-docs/teachertool/catalog-shared.json | 6 +++++ .../teachertool/validator-plans-shared.json | 27 +++++++++++++++++++ localtypings/validatorPlan.d.ts | 7 +++++ pxteditor/code-validation/runValidatorPlan.ts | 14 ++++++++++ .../validateBlockFieldValueExists.ts | 21 +++++++++++++++ 5 files changed, 75 insertions(+) create mode 100644 pxteditor/code-validation/validateBlockFieldValueExists.ts diff --git a/common-docs/teachertool/catalog-shared.json b/common-docs/teachertool/catalog-shared.json index 2c73aca4bd65..cab05f47fcf8 100644 --- a/common-docs/teachertool/catalog-shared.json +++ b/common-docs/teachertool/catalog-shared.json @@ -47,6 +47,12 @@ "use": "device_random_used", "template": "The math random number generator block is used", "docPath": "/teachertool" + }, + { + "id": "DC564060-F177-46CC-A3AC-890CD8545972", + "use": "num_compare_eq", + "template": "Compare two numbers for equality", + "docPath": "/teachertool" } ] } \ No newline at end of file diff --git a/common-docs/teachertool/validator-plans-shared.json b/common-docs/teachertool/validator-plans-shared.json index 2a493460bc61..0d0d06a40d84 100644 --- a/common-docs/teachertool/validator-plans-shared.json +++ b/common-docs/teachertool/validator-plans-shared.json @@ -123,6 +123,33 @@ } } ] + }, + { + ".desc": "make sure two numbers are equal", + "name": "num_compare_eq", + "threshold": 1, + "checks": [ + { + "validator": "blockFieldValueExists", + "fieldType": "OP", + "fieldValue": "EQ", + "blockType": "logic_compare", + "childValidatorPlans": ["two_nums_exist"] + } + ] + }, + { + ".desc": "two numbers exist", + "name": "two_nums_exist", + "threshold": 1, + "checks": [ + { + "validator": "blocksExist", + "blockCounts": { + "math_number": 2 + } + } + ] } ] } diff --git a/localtypings/validatorPlan.d.ts b/localtypings/validatorPlan.d.ts index dec5e911a7a6..53d683cfc9f0 100644 --- a/localtypings/validatorPlan.d.ts +++ b/localtypings/validatorPlan.d.ts @@ -39,4 +39,11 @@ declare namespace pxt.blocks { export interface EvaluationResult { result: boolean; } + + export interface BlockFieldValueExistsCheck extends ValidatorCheckBase { + validator: "blockFieldValueExists"; + fieldType: string; + fieldValue: string; + blockType: string; + } } \ No newline at end of file diff --git a/pxteditor/code-validation/runValidatorPlan.ts b/pxteditor/code-validation/runValidatorPlan.ts index 17b4d1eb9479..5deedbf35162 100644 --- a/pxteditor/code-validation/runValidatorPlan.ts +++ b/pxteditor/code-validation/runValidatorPlan.ts @@ -1,5 +1,6 @@ /// +import { validateBlockFieldValueExists } from "./validateBlockFieldValueExists"; import { validateBlocksExist } from "./validateBlocksExist"; import { validateBlocksInSetExist } from "./validateBlocksInSetExist"; import { validateBlockCommentsExist } from "./validateCommentsExist"; @@ -25,6 +26,9 @@ export function runValidatorPlan(usedBlocks: Blockly.Block[], plan: pxt.blocks.V case "blocksInSetExist": [successfulBlocks, checkPassed] = [...runBlocksInSetExistValidation(usedBlocks, check as pxt.blocks.BlocksInSetExistValidatorCheck)]; break; + case "blockFieldValueExists": + [successfulBlocks, checkPassed] = [...runBlockFieldValueExistsValidation(usedBlocks, check as pxt.blocks.BlockFieldValueExistsCheck)]; + break; default: pxt.debug(`Unrecognized validator: ${check.validator}`); checkPassed = false; @@ -81,4 +85,14 @@ function runValidateSpecificBlockCommentsExist(usedBlocks: Blockly.Block[], inpu function runBlocksInSetExistValidation(usedBlocks: Blockly.Block[], inputs: pxt.blocks.BlocksInSetExistValidatorCheck): [Blockly.Block[], boolean] { const blockResults = validateBlocksInSetExist({ usedBlocks, blockIdsToCheck: inputs.blocks, count: inputs.count }); return [blockResults.successfulBlocks, blockResults.passed]; +} + +function runBlockFieldValueExistsValidation(usedBlocks: Blockly.Block[], inputs: pxt.blocks.BlockFieldValueExistsCheck): [Blockly.Block[], boolean] { + const blockResults = validateBlockFieldValueExists({ + usedBlocks, + fieldType: inputs.fieldType, + fieldValue: inputs.fieldValue, + specifiedBlock: inputs.blockType + }); + return [blockResults.successfulBlocks, blockResults.passed]; } \ No newline at end of file diff --git a/pxteditor/code-validation/validateBlockFieldValueExists.ts b/pxteditor/code-validation/validateBlockFieldValueExists.ts new file mode 100644 index 000000000000..a6a50c294d15 --- /dev/null +++ b/pxteditor/code-validation/validateBlockFieldValueExists.ts @@ -0,0 +1,21 @@ +// validates that one or more blocks comments are in the project +// returns the blocks that have comments for teacher tool scenario +export function validateBlockFieldValueExists({ usedBlocks, fieldType, fieldValue, specifiedBlock }: { + usedBlocks: Blockly.Block[], + fieldType: string, + fieldValue: string, + specifiedBlock: string, +}): { + successfulBlocks: Blockly.Block[], + passed: boolean +} { + const enabledSpecifiedBlocks = usedBlocks.filter((block) => + block.isEnabled() && block.type === specifiedBlock + ); + + const successfulBlocks = enabledSpecifiedBlocks.filter((block) => + block.getFieldValue(fieldType) === fieldValue + ); + + return { successfulBlocks, passed: successfulBlocks.length > 0 }; +} \ No newline at end of file