|
| 1 | +local checkargs = require("checkargs") |
| 2 | + |
| 3 | +local function test_check_arg() |
| 4 | + local status, err = pcall(function() |
| 5 | + checkargs.check_arg("test_func", "test_arg", { "string" }, "value", false, true) |
| 6 | + end) |
| 7 | + assert(status, err) |
| 8 | + |
| 9 | + status, err = pcall(function() |
| 10 | + checkargs.check_arg("test_func", "test_arg", { "string" }, 123, false, true) |
| 11 | + end) |
| 12 | + assert(not status, "Expected an error but got none") |
| 13 | +end |
| 14 | + |
| 15 | +local function test_check_list() |
| 16 | + local status, err = pcall(function() |
| 17 | + checkargs.check_list("test_func", "test_list", { "number" }, { 1, 2, 3 }, false, true) |
| 18 | + end) |
| 19 | + assert(status, err) |
| 20 | + |
| 21 | + status, err = pcall(function() |
| 22 | + checkargs.check_list("test_func", "test_list", { "number" }, { 1, "two", 3 }, false, true) |
| 23 | + end) |
| 24 | + assert(not status, "Expected an error but got none") |
| 25 | +end |
| 26 | + |
| 27 | +local function test_check_range() |
| 28 | + local status, err = pcall(function() |
| 29 | + checkargs.check_range("test_func", "test_value", 5, 1, 10, true) |
| 30 | + end) |
| 31 | + assert(status, err) |
| 32 | + |
| 33 | + status, err = pcall(function() |
| 34 | + checkargs.check_range("test_func", "test_value", 15, 1, 10, true) |
| 35 | + end) |
| 36 | + assert(not status, "Expected an error but got none") |
| 37 | +end |
| 38 | + |
| 39 | +local function test_check_fields() |
| 40 | + local status, err = pcall(function() |
| 41 | + checkargs.check_fields("test_func", "test_table", { a = 1, b = 2 }, { "a", "b" }, true) |
| 42 | + end) |
| 43 | + assert(status, err) |
| 44 | + |
| 45 | + status, err = pcall(function() |
| 46 | + checkargs.check_fields("test_func", "test_table", { a = 1 }, { "a", "b" }, true) |
| 47 | + end) |
| 48 | + assert(not status, "Expected an error but got none") |
| 49 | +end |
| 50 | + |
| 51 | +local function test_check_composite() |
| 52 | + local status, err = pcall(function() |
| 53 | + checkargs.check_composite("test_func", "test_composite", { a = 1, b = "test" }, { a = "number", b = "string" }, |
| 54 | + true) |
| 55 | + end) |
| 56 | + assert(status, err) |
| 57 | + |
| 58 | + status, err = pcall(function() |
| 59 | + checkargs.check_composite("test_func", "test_composite", { a = 1, b = 2 }, { a = "number", b = "string" }, true) |
| 60 | + end) |
| 61 | + assert(not status, "Expected an error but got none") |
| 62 | +end |
| 63 | + |
| 64 | +local function test_check_not_nil() |
| 65 | + local status, err = pcall(function() |
| 66 | + checkargs.check_not_nil("test_func", "test_value", "value", true) |
| 67 | + end) |
| 68 | + assert(status, err) |
| 69 | + |
| 70 | + status, err = pcall(function() |
| 71 | + checkargs.check_not_nil("test_func", "test_value", nil, true) |
| 72 | + end) |
| 73 | + assert(not status, "Expected an error but got none") |
| 74 | +end |
| 75 | + |
| 76 | +local function runtests() |
| 77 | + test_check_arg() |
| 78 | + test_check_list() |
| 79 | + test_check_range() |
| 80 | + test_check_fields() |
| 81 | + test_check_composite() |
| 82 | + test_check_not_nil() |
| 83 | + print("All tests passed successfully!") |
| 84 | +end |
| 85 | + |
| 86 | +runtests() |
0 commit comments