aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2022-03-18 09:24:53 -0700
committerKartik K. Agaram <vc@akkartik.com>2022-03-18 09:24:53 -0700
commit4a90a28a157a5c7bbf8e526fb5b24aa1ab1e2bd7 (patch)
tree1a0c4610ba0d9d89318350341e7c4a31e75db45e /src
parent5c42b1de32d3a58f9f0141d84d9827e8b8772f40 (diff)
downloadteliva-4a90a28a157a5c7bbf8e526fb5b24aa1ab1e2bd7.tar.gz
fake to stand in for start_reading in tests
Diffstat (limited to 'src')
-rw-r--r--src/file.lua44
1 files changed, 44 insertions, 0 deletions
diff --git a/src/file.lua b/src/file.lua
index 2ac840f..372d49b 100644
--- a/src/file.lua
+++ b/src/file.lua
@@ -15,6 +15,50 @@ function start_reading(fs, filename)
}
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)
-- returns an object or nil on error
-- write to the object using .write()