1
0
mirror of https://github.com/Refactorio/RedMew.git synced 2024-12-14 10:13:13 +02:00
RedMew/utils/test/assert.lua

76 lines
1.8 KiB
Lua
Raw Normal View History

local table = require 'utils.table'
2020-09-19 13:41:24 +02:00
local error = error
local concat = table.concat
local Public = {}
2020-09-29 21:50:07 +02:00
local function append_optional_message(main_message, optional_message)
if optional_message then
return concat {main_message, ' - ', optional_message}
end
return main_message
end
2020-09-19 13:41:24 +02:00
function Public.equal(a, b, optional_message)
if a == b then
return
end
2020-09-19 15:48:08 +02:00
local message = {tostring(a), ' ~= ', tostring(b)}
2020-09-19 13:41:24 +02:00
if optional_message then
2020-09-19 15:48:08 +02:00
message[#message + 1] = ' - '
message[#message + 1] = optional_message
2020-09-19 13:41:24 +02:00
end
2020-09-19 15:48:08 +02:00
message = concat(message)
2020-09-19 13:41:24 +02:00
error(message, 2)
end
function Public.is_nil(value, optional_message)
if value == nil then
return
end
local message = {tostring(value), ' was not nil'}
if optional_message then
message[#message + 1] = ' - '
message[#message + 1] = optional_message
end
message = concat(message)
error(message, 2)
end
function Public.table_equal(a, b)
-- Todo write own table equal
if not table.equals(a, b) then
error('tables not equal', 2)
end
end
2020-09-19 15:48:08 +02:00
function Public.is_true(condition, optional_message)
if not condition then
error(optional_message or 'condition was not true', 2)
end
end
2020-09-29 21:50:07 +02:00
function Public.valid(lua_object, optional_message)
if not lua_object then
2020-10-04 12:44:12 +02:00
error(append_optional_message('lua_object was nil', optional_message), 2)
2020-09-29 21:50:07 +02:00
end
if not lua_object.valid then
2020-10-04 12:44:12 +02:00
error(append_optional_message('lua_object was not valid', optional_message), 2)
2020-09-29 21:50:07 +02:00
end
end
function Public.is_lua_object_with_name(lua_object, name, optional_message)
Public.valid(lua_object, optional_message)
if lua_object.name ~= name then
2020-10-04 12:44:12 +02:00
error(append_optional_message("lua_object did not have name '" .. tostring(name) .. "'", optional_message), 2)
2020-09-29 21:50:07 +02:00
end
end
2020-09-19 13:41:24 +02:00
return Public