|
| 1 | +describe("Error module tests", function() |
| 2 | + local M = require("main") |
| 3 | + |
| 4 | + it("should divide safely without error", function() |
| 5 | + local result, err = M.divideSafely(10, 2) |
| 6 | + assert.are.equal(result, 5) |
| 7 | + assert.is_nil(err) |
| 8 | + end) |
| 9 | + |
| 10 | + it("should handle division by zero safely", function() |
| 11 | + local result, err = M.divideSafely(10, 0) |
| 12 | + assert.is_nil(result) |
| 13 | + assert.are.equal(err, "Division by zero") |
| 14 | + end) |
| 15 | + |
| 16 | + it("should divide using assert", function() |
| 17 | + local result = M.divideWithAssert(10, 2) |
| 18 | + assert.are.equal(result, 5) |
| 19 | + end) |
| 20 | + |
| 21 | + it("should raise error on division by zero with assert", function() |
| 22 | + assert.has_error(function() |
| 23 | + M.divideWithAssert(10, 0) |
| 24 | + end, "Division by zero") |
| 25 | + end) |
| 26 | + |
| 27 | + it("should divide with pcall successfully", function() |
| 28 | + local result = M.divideWithPCall(10, 2) |
| 29 | + assert.are.equal(result, 5) |
| 30 | + end) |
| 31 | + |
| 32 | + it("should catch division by zero with pcall", function() |
| 33 | + local result = M.divideWithPCall(10, 0) |
| 34 | + assert.are.equal(result:match("Division by zero"), "Division by zero") |
| 35 | + end) |
| 36 | + |
| 37 | + it("should divide with xpcall successfully", function() |
| 38 | + local result = M.divideWithXPCall(10, 2) |
| 39 | + assert.are.equal(result, 5) |
| 40 | + end) |
| 41 | + |
| 42 | + it("should handle division by zero with custom error handler in xpcall", function() |
| 43 | + local result = M.divideWithXPCall(10, 0) |
| 44 | + assert.are.equal(result.message:match("Division by zero"), "Division by zero") |
| 45 | + assert.are.equal(result.code, 400) |
| 46 | + end) |
| 47 | + |
| 48 | + it("should divide with coroutine successfully", function() |
| 49 | + local result = M.divideWithCoroutine(10, 2) |
| 50 | + assert.are.equal(result, 5) |
| 51 | + end) |
| 52 | + |
| 53 | + it("should handle division by zero with coroutine", function() |
| 54 | + local result = M.divideWithCoroutine(10, 0) |
| 55 | + assert.are.equal(result, "Division by zero") |
| 56 | + end) |
| 57 | +end) |
0 commit comments