Skip to content

Commit

Permalink
Teacher tool: field value validator added (#9867)
Browse files Browse the repository at this point in the history
* field value validator added

* change template string for num equality validator

Co-authored-by: Eric Anderson <[email protected]>

---------

Co-authored-by: Eric Anderson <[email protected]>
  • Loading branch information
srietkerk and eanders-ms authored Feb 15, 2024
1 parent d2e0ba5 commit 461a8db
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 0 deletions.
6 changes: 6 additions & 0 deletions common-docs/teachertool/catalog-shared.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
]
}
27 changes: 27 additions & 0 deletions common-docs/teachertool/validator-plans-shared.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
]
}
]
}
7 changes: 7 additions & 0 deletions localtypings/validatorPlan.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
14 changes: 14 additions & 0 deletions pxteditor/code-validation/runValidatorPlan.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/// <reference path="../../localtypings/validatorPlan.d.ts" />

import { validateBlockFieldValueExists } from "./validateBlockFieldValueExists";
import { validateBlocksExist } from "./validateBlocksExist";
import { validateBlocksInSetExist } from "./validateBlocksInSetExist";
import { validateBlockCommentsExist } from "./validateCommentsExist";
Expand All @@ -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;
Expand Down Expand Up @@ -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];
}
21 changes: 21 additions & 0 deletions pxteditor/code-validation/validateBlockFieldValueExists.ts
Original file line number Diff line number Diff line change
@@ -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 };
}

0 comments on commit 461a8db

Please sign in to comment.