Test a School Rule at Its Boundaries
Test a School Rule at Its Boundaries
A useful test checks expected behavior, especially where a decision changes. A pass rule should accept 50 and reject 49; testing only a typical high mark can miss an off-by-one error.
Before this topic, review Debug a Calculation if its terms are unfamiliar.
What you’ll learn
- Explain test a school rule at its boundaries through the school example.
- Trace the shown operations and distinguish their result from displayed output.
- Make the practice change and explain its consequence: The replacement is another valid expectation, so all three cases still pass.
Work through the school example
This is JavaScript using language features and the host console. Read the initial values, then follow each operation to its displayed result. A declaration names a value, a function call performs an operation, and console.log displays its arguments.
JavaScript · school.js · complete example
function passed(mark) {
return mark >= 50;
}
for (const [mark, expected] of [[49, false], [50, true], [80, true]]) {
if (passed(mark) !== expected) throw new Error("Failed: " + mark);
}
console.log("3 checks passed");
Expected output
3 checks passedRun in JavaScript compiler →Recognize the important boundary
Tests do not prove every possible behavior. Keep external-input validation separate from internal development checks, and test error paths as well as success.
Try a small variation
JavaScript · original source for practice
function passed(mark) {
return mark >= 50;
}
for (const [mark, expected] of [[49, false], [50, true], [80, true]]) {
if (passed(mark) !== expected) throw new Error("Failed: " + mark);
}
console.log("3 checks passed");
Your task: Replace [80,true] with [40,false]. Predict the result before running the edited program.
The source is repeated here. If the edit targets the HTML rather than the script, use the markup repeated below; the worked answer also repeats the changed markup.
Worked answer and reasoning
The replacement is another valid expectation, so all three cases still pass.
JavaScript · complete worked variation
function passed(mark) {
return mark >= 50;
}
for (const [mark, expected] of [[49, false], [50, true], [40, false]]) {
if (passed(mark) !== expected) throw new Error("Failed: " + mark);
}
console.log("3 checks passed");
Expected output
3 checks passedRun in JavaScript compiler →Predict the original program
JavaScript · original program for this question
function passed(mark) {
return mark >= 50;
}
for (const [mark, expected] of [[49, false], [50, true], [80, true]]) {
if (passed(mark) !== expected) throw new Error("Failed: " + mark);
}
console.log("3 checks passed");
Check the important distinction
Keep the useful idea
A useful test checks expected behavior, especially where a decision changes. A pass rule should accept 50 and reject 49; testing only a typical high mark can miss an off-by-one error.
Check your variation against this explanation: The replacement is another valid expectation, so all three cases still pass.
Use the JavaScript array reference library for individual array operations and boundary tests to check a rule independently.
Reaching the recap and completing the quizzes are separate milestones.