mirror of
https://gitlab.com/OpenMW/openmw.git
synced 2025-04-28 12:58:00 +03:00
Add test for saving and loading the game
This commit is contained in:
parent
cbcd4f6acd
commit
536325e0ba
2 changed files with 178 additions and 5 deletions
|
@ -1,12 +1,46 @@
|
|||
local testing = require('testing_util')
|
||||
local matchers = require('matchers')
|
||||
local menu = require('openmw.menu')
|
||||
|
||||
testing.registerMenuTest('save and load', function()
|
||||
menu.newGame()
|
||||
coroutine.yield()
|
||||
menu.saveGame('save and load')
|
||||
coroutine.yield()
|
||||
|
||||
local directorySaves = {}
|
||||
directorySaves['save_and_load.omwsave'] = {
|
||||
playerName = '',
|
||||
playerLevel = 1,
|
||||
timePlayed = 0,
|
||||
description = 'save and load',
|
||||
contentFiles = {
|
||||
'builtin.omwscripts',
|
||||
'template.omwgame',
|
||||
'landracer.omwaddon',
|
||||
'the_hub.omwaddon',
|
||||
'test_lua_api.omwscripts',
|
||||
},
|
||||
creationTime = matchers.isAny(),
|
||||
}
|
||||
local expectedAllSaves = {}
|
||||
expectedAllSaves[' - 1'] = directorySaves
|
||||
|
||||
testing.expectThat(menu.getAllSaves(), matchers.equalTo(expectedAllSaves))
|
||||
|
||||
menu.loadGame(' - 1', 'save_and_load.omwsave')
|
||||
coroutine.yield()
|
||||
|
||||
menu.deleteGame(' - 1', 'save_and_load.omwsave')
|
||||
testing.expectThat(menu.getAllSaves(), matchers.equalTo({}))
|
||||
end)
|
||||
|
||||
local function registerGlobalTest(name, description)
|
||||
testing.registerMenuTest(description or name, function()
|
||||
menu.newGame()
|
||||
coroutine.yield()
|
||||
testing.runGlobalTest(name)
|
||||
end)
|
||||
testing.registerMenuTest(description or name, function()
|
||||
menu.newGame()
|
||||
coroutine.yield()
|
||||
testing.runGlobalTest(name)
|
||||
end)
|
||||
end
|
||||
|
||||
registerGlobalTest('timers')
|
||||
|
|
|
@ -76,4 +76,143 @@ function module.isNotNan()
|
|||
end
|
||||
end
|
||||
|
||||
---
|
||||
-- Matcher accepting any value.
|
||||
-- @function isAny
|
||||
-- @usage
|
||||
-- expectThat(value, isAny())
|
||||
function module.isAny()
|
||||
return function(actual)
|
||||
return ''
|
||||
end
|
||||
end
|
||||
|
||||
local function serializeArray(a)
|
||||
local result = nil
|
||||
for _, v in ipairs(a) do
|
||||
if result == nil then
|
||||
result = string.format('{%s', serialize(v))
|
||||
else
|
||||
result = string.format('%s, %s', result, serialize(v))
|
||||
end
|
||||
end
|
||||
if result == nil then
|
||||
return '{}'
|
||||
end
|
||||
return string.format('%s}', result)
|
||||
end
|
||||
|
||||
local function serializeTable(t)
|
||||
local result = nil
|
||||
for k, v in pairs(t) do
|
||||
if result == nil then
|
||||
result = string.format('{%q = %s', k, serialize(v))
|
||||
else
|
||||
result = string.format('%s, %q = %s', result, k, serialize(v))
|
||||
end
|
||||
end
|
||||
if result == nil then
|
||||
return '{}'
|
||||
end
|
||||
return string.format('%s}', result)
|
||||
end
|
||||
|
||||
local function isArray(t)
|
||||
local i = 1
|
||||
for _ in pairs(t) do
|
||||
if t[i] == nil then
|
||||
return false
|
||||
end
|
||||
i = i + 1
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
function serialize(v)
|
||||
local t = type(v)
|
||||
if t == 'string' then
|
||||
return string.format('%q', v)
|
||||
elseif t == 'table' then
|
||||
if isArray(v) then
|
||||
return serializeArray(v)
|
||||
end
|
||||
return serializeTable(v)
|
||||
end
|
||||
return string.format('%s', v)
|
||||
end
|
||||
|
||||
local function compareScalars(v1, v2)
|
||||
if v1 == v2 then
|
||||
return ''
|
||||
end
|
||||
if type(v1) == 'string' then
|
||||
return string.format('%q ~= %q', v1, v2)
|
||||
end
|
||||
return string.format('%s ~= %s', v1, v2)
|
||||
end
|
||||
|
||||
local function collectKeys(t)
|
||||
local result = {}
|
||||
for key in pairs(t) do
|
||||
table.insert(result, key)
|
||||
end
|
||||
table.sort(result)
|
||||
return result
|
||||
end
|
||||
|
||||
local function compareTables(t1, t2)
|
||||
local keys1 = collectKeys(t1)
|
||||
local keys2 = collectKeys(t2)
|
||||
if #keys1 ~= #keys2 then
|
||||
return string.format('table size mismatch: %d ~= %d', #keys1, #keys2)
|
||||
end
|
||||
for i = 1, #keys1 do
|
||||
local key1 = keys1[i]
|
||||
local key2 = keys2[i]
|
||||
if key1 ~= key2 then
|
||||
return string.format('table keys mismatch: %q ~= %q', key1, key2)
|
||||
end
|
||||
local d = compare(t1[key1], t2[key2])
|
||||
if d ~= '' then
|
||||
return string.format('table values mismatch at key %s: %s', serialize(key1), d)
|
||||
end
|
||||
end
|
||||
return ''
|
||||
end
|
||||
|
||||
function compare(v1, v2)
|
||||
local type1 = type(v1)
|
||||
local type2 = type(v2)
|
||||
if type2 == 'function' then
|
||||
return v2(v1)
|
||||
end
|
||||
if type1 ~= type2 then
|
||||
return string.format('types mismatch: %s ~= %s', type1, type2)
|
||||
end
|
||||
if type1 == 'nil' then
|
||||
return ''
|
||||
elseif type1 == 'table' then
|
||||
return compareTables(v1, v2)
|
||||
elseif type1 == 'nil' or type1 == 'boolean' or type1 == 'number' or type1 == 'string' then
|
||||
return compareScalars(v1, v2)
|
||||
end
|
||||
error('unsupported type: %s', type1)
|
||||
end
|
||||
|
||||
---
|
||||
-- Matcher verifying that given value is equal to expected. Accepts nil, boolean, number, string and table or matcher
|
||||
-- function.
|
||||
-- @function equalTo
|
||||
-- @usage
|
||||
-- expectThat({a = {42, 'foo', {b = true}}}, equalTo({a = {42, 'foo', {b = true}}}))
|
||||
function module.equalTo(expected)
|
||||
return function(actual)
|
||||
local diff = compare(actual, expected)
|
||||
if diff == '' then
|
||||
return ''
|
||||
end
|
||||
return string.format('%s; actual: %s; expected: %s', diff, serialize(actual, ''), serialize(expected, ''))
|
||||
end
|
||||
end
|
||||
|
||||
return module
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue