fake to stand in for start_reading in tests
This commit is contained in:
parent
5c42b1de32
commit
4a90a28a15
44
src/file.lua
44
src/file.lua
|
@ -15,6 +15,50 @@ function start_reading(fs, filename)
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- fake file object with the same 'shape' as that returned by start_reading
|
||||||
|
function fake_file_stream(s)
|
||||||
|
local i = 1
|
||||||
|
local max = string.len(s)
|
||||||
|
return {
|
||||||
|
read = function(format)
|
||||||
|
if i > max then
|
||||||
|
return nil
|
||||||
|
end
|
||||||
|
if type(format) == 'number' then
|
||||||
|
local result = s:sub(i, i+format-1)
|
||||||
|
i = i+format
|
||||||
|
return result
|
||||||
|
elseif format == '*a' then
|
||||||
|
local result = s:sub(i)
|
||||||
|
i = max+1
|
||||||
|
return result
|
||||||
|
elseif format == '*l' then
|
||||||
|
local start = i
|
||||||
|
while i <= max do
|
||||||
|
if s:sub(i, i) == '\n' then
|
||||||
|
break
|
||||||
|
end
|
||||||
|
i = i+1
|
||||||
|
end
|
||||||
|
local result = s:sub(start, i)
|
||||||
|
i = i+1
|
||||||
|
return result
|
||||||
|
elseif format == '*n' then
|
||||||
|
error('fake file streams: *n not yet supported')
|
||||||
|
end
|
||||||
|
end,
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
function test_fake_file_system()
|
||||||
|
local s = fake_file_stream('abcdefgh\nijk\nlmn')
|
||||||
|
check_eq(s.read(1), 'a', 'fake_file_system: 1 char')
|
||||||
|
check_eq(s.read(1), 'b', 'fake_file_system: 1 more char')
|
||||||
|
check_eq(s.read(3), 'cde', 'fake_file_system: multiple chars')
|
||||||
|
check_eq(s.read('*l'), 'fgh\n', 'fake_file_system: line')
|
||||||
|
check_eq(s.read('*a'), 'ijk\nlmn', 'fake_file_system: all')
|
||||||
|
end
|
||||||
|
|
||||||
-- primitive for writing files to a file system (or, later, network)
|
-- primitive for writing files to a file system (or, later, network)
|
||||||
-- returns an object or nil on error
|
-- returns an object or nil on error
|
||||||
-- write to the object using .write()
|
-- write to the object using .write()
|
||||||
|
|
Loading…
Reference in New Issue