mirror of
https://gitlab.com/OpenMW/openmw.git
synced 2025-02-22 03:40:49 +00:00
Hoist the FileHandle check and add more tests
This commit is contained in:
parent
6d8753e5a8
commit
c9ffd978ec
@ -190,9 +190,9 @@ namespace MWLua
|
|||||||
return seek(lua, self, std::ios_base::cur, off);
|
return seek(lua, self, std::ios_base::cur, off);
|
||||||
});
|
});
|
||||||
handle["lines"] = [](sol::this_state lua, sol::object self) {
|
handle["lines"] = [](sol::this_state lua, sol::object self) {
|
||||||
|
if (!self.is<FileHandle*>())
|
||||||
|
throw std::runtime_error("self should be a file handle");
|
||||||
return sol::as_function([lua, self]() -> sol::object {
|
return sol::as_function([lua, self]() -> sol::object {
|
||||||
if (!self.is<FileHandle*>())
|
|
||||||
return sol::nil;
|
|
||||||
FileHandle* handle = self.as<FileHandle*>();
|
FileHandle* handle = self.as<FileHandle*>();
|
||||||
validateFile(*handle);
|
validateFile(*handle);
|
||||||
return readLineFromFile(lua, *handle);
|
return readLineFromFile(lua, *handle);
|
||||||
|
@ -227,16 +227,18 @@ end
|
|||||||
|
|
||||||
local function testVFS()
|
local function testVFS()
|
||||||
local file = 'test_vfs_dir/lines.txt'
|
local file = 'test_vfs_dir/lines.txt'
|
||||||
|
local nosuchfile = 'test_vfs_dir/nosuchfile'
|
||||||
testing.expectEqual(vfs.fileExists(file), true, 'lines.txt should exist')
|
testing.expectEqual(vfs.fileExists(file), true, 'lines.txt should exist')
|
||||||
testing.expectEqual(vfs.fileExists('test_vfs_dir/nosuchfile'), false, 'nosuchfile should not exist')
|
testing.expectEqual(vfs.fileExists(nosuchfile), false, 'nosuchfile should not exist')
|
||||||
|
|
||||||
|
local expectedLines = { '1', '2', '', '4' }
|
||||||
local getLine = vfs.lines(file)
|
local getLine = vfs.lines(file)
|
||||||
for _,v in pairs({ '1', '2', '', '4' }) do
|
for _,v in pairs(expectedLines) do
|
||||||
testing.expectEqual(getLine(), v)
|
testing.expectEqual(getLine(), v)
|
||||||
end
|
end
|
||||||
testing.expectEqual(getLine(), nil, 'All lines should have been read')
|
testing.expectEqual(getLine(), nil, 'All lines should have been read')
|
||||||
local ok = pcall(function()
|
local ok = pcall(function()
|
||||||
vfs.lines('test_vfs_dir/nosuchfile')
|
vfs.lines(nosuchfile)
|
||||||
end)
|
end)
|
||||||
testing.expectEqual(ok, false, 'Should not be able to read lines from nonexistent file')
|
testing.expectEqual(ok, false, 'Should not be able to read lines from nonexistent file')
|
||||||
|
|
||||||
@ -259,6 +261,14 @@ local function testVFS()
|
|||||||
|
|
||||||
testing.expectEqual(handle:close(), true, 'File should be closeable')
|
testing.expectEqual(handle:close(), true, 'File should be closeable')
|
||||||
testing.expectEqual(vfs.type(handle), 'closed file', 'File should be closed')
|
testing.expectEqual(vfs.type(handle), 'closed file', 'File should be closed')
|
||||||
|
|
||||||
|
handle = vfs.open(nosuchfile)
|
||||||
|
testing.expectEqual(handle, nil, 'vfs.open should return nil on nonexistent files')
|
||||||
|
|
||||||
|
getLine = vfs.open(file):lines()
|
||||||
|
for _,v in pairs(expectedLines) do
|
||||||
|
testing.expectEqual(getLine(), v)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local function testCommitCrime()
|
local function testCommitCrime()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user