Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions js/blocks/NumberBlocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ function setupNumberBlocks(activity) {
*/
const handleMathError = (logo, error, blk, onError = NANERRORMSG) => {
logo.stopTurtle = true;
if (error && error.message === "DivByZeroError") {
Comment thread
rish106-hub marked this conversation as resolved.
activity.errorMsg(ZERODIVIDEERRORMSG, blk);
return;
}
activity.errorMsg(onError, blk);
};

Expand Down
13 changes: 13 additions & 0 deletions js/blocks/__tests__/NumberBlocks.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,19 @@ describe("setupNumberBlocks", () => {
expect(result).toEqual(0);
global.MathUtility.doMod = (a, b) => Number(a) % Number(b);
});

it("should call errorMsg with ZERODIVIDEERRORMSG when MathUtility.doMod throws DivByZeroError", () => {
activity.blocks.blockList[110] = { connections: [null, "c1", "c2"] };
logo.parseArg = jest.fn(() => 5);
global.MathUtility.doMod = () => {
throw new Error("DivByZeroError");
};
const modBlock = createdBlocks["mod"];
const result = modBlock.arg(logo, 0, 110, null);
expect(activity.errorMsg).toHaveBeenCalledWith(global.ZERODIVIDEERRORMSG, 110);
expect(result).toEqual(0);
global.MathUtility.doMod = (a, b) => Number(a) % Number(b);
});
});

describe("PowerBlock - extra branches", () => {
Expand Down
6 changes: 3 additions & 3 deletions js/utils/__tests__/mathutils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ describe("MathUtility", () => {
});

test("throws error for invalid inputs", () => {
expect(() => MathUtility.doMod("a", 3)).toThrow("Invalid number input");
expect(() => MathUtility.doMod("a", 3)).toThrow("NanError");
});

// Edge case tests
Expand Down Expand Up @@ -159,7 +159,7 @@ describe("MathUtility", () => {
});

test("throws error when second arg is string", () => {
expect(() => MathUtility.doMod(10, "a")).toThrow("Invalid number input");
expect(() => MathUtility.doMod(10, "a")).toThrow("NanError");
});
});

Expand Down Expand Up @@ -556,7 +556,7 @@ describe("MathUtility", () => {

describe("edge cases - Infinity, NaN, and boundary values", () => {
test("doMod throws an error when divisor is zero", () => {
expect(() => MathUtility.doMod(5, 0)).toThrow();
expect(() => MathUtility.doMod(5, 0)).toThrow("DivByZeroError");
});

test("doSqrt handles Infinity", () => {
Expand Down
4 changes: 2 additions & 2 deletions js/utils/mathutils.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,11 @@ class MathUtility {
static doMod(a, b) {
if (typeof a === "number" && typeof b === "number") {
if (Number(b) === 0) {
throw new Error("Division by zero");
throw new Error("DivByZeroError");
}
return Number(a) % Number(b);
} else {
throw new Error("Invalid number input");
throw new Error("NanError");
}
}

Expand Down
Loading