-
Notifications
You must be signed in to change notification settings - Fork 585
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/master' into dev/riknoll/the-gre…
…at-blockly-swap
- Loading branch information
Showing
14 changed files
with
235 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/// <reference path="../../localtypings/validatorPlan.d.ts" /> | ||
|
||
import * as Blockly from "blockly"; | ||
|
||
import { validateBlockFieldValueExists } from "./validateBlockFieldValueExists"; | ||
import { validateBlocksExist } from "./validateBlocksExist"; | ||
import { validateBlocksInSetExist } from "./validateBlocksInSetExist"; | ||
import { validateBlockCommentsExist } from "./validateCommentsExist"; | ||
import { validateSpecificBlockCommentsExist } from "./validateSpecificBlockCommentsExist"; | ||
|
||
export function runValidatorPlan(usedBlocks: Blockly.Block[], plan: pxt.blocks.ValidatorPlan, planLib: pxt.blocks.ValidatorPlan[]): boolean { | ||
const startTime = Date.now(); | ||
let checksSucceeded = 0; | ||
let successfulBlocks: Blockly.Block[] = []; | ||
|
||
for (const check of plan.checks) { | ||
let checkPassed = false; | ||
switch (check.validator) { | ||
case "blocksExist": | ||
[successfulBlocks, checkPassed] = [...runBlocksExistValidation(usedBlocks, check as pxt.blocks.BlocksExistValidatorCheck)]; | ||
break; | ||
case "blockCommentsExist": | ||
checkPassed = runValidateBlockCommentsExist(usedBlocks, check as pxt.blocks.BlockCommentsExistValidatorCheck); | ||
break; | ||
case "specificBlockCommentsExist": | ||
checkPassed = runValidateSpecificBlockCommentsExist(usedBlocks, check as pxt.blocks.SpecificBlockCommentsExistValidatorCheck); | ||
break; | ||
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; | ||
break; | ||
} | ||
|
||
if (checkPassed && check.childValidatorPlans) { | ||
for (const planName of check.childValidatorPlans) { | ||
let timesPassed = 0; | ||
for (const parentBlock of successfulBlocks) { | ||
const blocksToUse = parentBlock.getChildren(true); | ||
const childPlan = planLib.find((plan) => plan.name === planName); | ||
const childPassed = runValidatorPlan(blocksToUse, childPlan, planLib); | ||
timesPassed += childPassed ? 1 : 0; | ||
} | ||
checkPassed = checkPassed && timesPassed > 0; | ||
} | ||
} | ||
checksSucceeded += checkPassed ? 1 : 0; | ||
} | ||
|
||
const passed = checksSucceeded >= plan.threshold; | ||
|
||
pxt.tickEvent("validation.evaluation_complete", { | ||
plan: plan.name, | ||
durationMs: Date.now() - startTime, | ||
passed: `${passed}`, | ||
}); | ||
|
||
return passed; | ||
} | ||
|
||
function runBlocksExistValidation(usedBlocks: Blockly.Block[], inputs: pxt.blocks.BlocksExistValidatorCheck): [Blockly.Block[], boolean] { | ||
const blockResults = validateBlocksExist({ usedBlocks, requiredBlockCounts: inputs.blockCounts }); | ||
let successfulBlocks: Blockly.Block[] = []; | ||
if (blockResults.passed) { | ||
const blockIdsFromValidator = Object.keys(inputs.blockCounts) | ||
const blockId = blockIdsFromValidator?.[0]; | ||
successfulBlocks = blockResults.successfulBlocks.length ? blockResults.successfulBlocks[0][blockId] : []; | ||
} | ||
return [successfulBlocks, blockResults.passed]; | ||
} | ||
|
||
function runValidateBlockCommentsExist(usedBlocks: Blockly.Block[], inputs: pxt.blocks.BlockCommentsExistValidatorCheck): boolean { | ||
const blockResults = validateBlockCommentsExist({ usedBlocks, numRequired: inputs.count }); | ||
return blockResults.passed; | ||
} | ||
|
||
function runValidateSpecificBlockCommentsExist(usedBlocks: Blockly.Block[], inputs: pxt.blocks.SpecificBlockCommentsExistValidatorCheck): boolean { | ||
const blockResults = validateSpecificBlockCommentsExist({ usedBlocks, blockType: inputs.blockType }); | ||
return blockResults.passed; | ||
} | ||
|
||
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]; | ||
} |
This file was deleted.
Oops, something went wrong.
21 changes: 21 additions & 0 deletions
21
pxteditor/code-validation/validateBlockFieldValueExists.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.