Skip to content

Commit

Permalink
Merge branch 'testthevfs' into 'master'
Browse files Browse the repository at this point in the history
Fix vfs bindings and add tests

Closes #8157

See merge request OpenMW/openmw!4371
  • Loading branch information
psi29a committed Sep 15, 2024
2 parents f2be407 + 2978b32 commit 96ec3a8
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 5 deletions.
14 changes: 9 additions & 5 deletions apps/openmw/mwlua/vfsbindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,11 @@ namespace MWLua
values.push_back(sol::make_object<std::string>(lua, msg));
}
else
values.push_back(sol::make_object<std::streampos>(lua, self.mFilePtr->tellg()));
{
// tellg returns std::streampos which is not required to be a numeric type. It is required to be
// convertible to std::streamoff which is a number
values.push_back(sol::make_object<std::streamoff>(lua, self.mFilePtr->tellg()));
}
}
catch (std::exception& e)
{
Expand Down Expand Up @@ -205,7 +209,7 @@ namespace MWLua
});
};

handle["close"] = [](lua_State* L, FileHandle& self) {
handle["close"] = [](sol::this_state lua, FileHandle& self) {
sol::variadic_results values;
try
{
Expand All @@ -214,16 +218,16 @@ namespace MWLua
{
auto msg = "Can not close file '" + self.mFileName + "': file handle is still opened.";
values.push_back(sol::nil);
values.push_back(sol::make_object<std::string>(L, msg));
values.push_back(sol::make_object<std::string>(lua, msg));
}
else
values.push_back(sol::make_object<bool>(L, true));
values.push_back(sol::make_object<bool>(lua, true));
}
catch (std::exception& e)
{
auto msg = "Can not close file '" + self.mFileName + "': " + std::string(e.what());
values.push_back(sol::nil);
values.push_back(sol::make_object<std::string>(L, msg));
values.push_back(sol::make_object<std::string>(lua, msg));
}

return values;
Expand Down
38 changes: 38 additions & 0 deletions scripts/data/integration_tests/test_lua_api/test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ local core = require('openmw.core')
local async = require('openmw.async')
local util = require('openmw.util')
local types = require('openmw.types')
local vfs = require('openmw.vfs')
local world = require('openmw.world')

local function testTimers()
Expand Down Expand Up @@ -224,6 +225,42 @@ local function initPlayer()
coroutine.yield()
end

local function testVFS()
local file = 'test_vfs_dir/lines.txt'
testing.expectEqual(vfs.fileExists(file), true, 'lines.txt should exist')
testing.expectEqual(vfs.fileExists('test_vfs_dir/nosuchfile'), false, 'nosuchfile should not exist')

local getLine = vfs.lines(file)
for _,v in pairs({ '1', '2', '', '4' }) do
testing.expectEqual(getLine(), v)
end
testing.expectEqual(getLine(), nil, 'All lines should have been read')
local ok = pcall(function()
vfs.lines('test_vfs_dir/nosuchfile')
end)
testing.expectEqual(ok, false, 'Should not be able to read lines from nonexistent file')

local getPath = vfs.pathsWithPrefix('test_vfs_dir/')
testing.expectEqual(getPath(), file)
testing.expectEqual(getPath(), nil, 'All paths should have been read')

local handle = vfs.open(file)
testing.expectEqual(vfs.type(handle), 'file', 'File should be open')
testing.expectEqual(handle.fileName, file)

local n1, n2, _, l3, l4 = handle:read("*n", "*number", "*l", "*line", "*l")
testing.expectEqual(n1, 1)
testing.expectEqual(n2, 2)
testing.expectEqual(l3, '')
testing.expectEqual(l4, '4')

testing.expectEqual(handle:seek('set', 0), 0, 'Reading should happen from the start of the file')
testing.expectEqual(handle:read("*a"), '1\n2\n\n4')

testing.expectEqual(handle:close(), true, 'File should be closeable')
testing.expectEqual(vfs.type(handle), 'closed file', 'File should be closed')
end

tests = {
{'timers', testTimers},
{'rotating player with controls.yawChange should change rotation', function()
Expand Down Expand Up @@ -283,6 +320,7 @@ tests = {
world.createObject('basic_dagger1h', 1):moveInto(player)
testing.runLocalTest(player, 'playerWeaponAttack')
end},
{'vfs', testVFS},
}

return {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
1
2

4

0 comments on commit 96ec3a8

Please sign in to comment.