diff options
| author | Josh Wainwright <josh.wainwright@ldra.com> | 2016-04-20 12:26:50 +0100 |
|---|---|---|
| committer | Josh Wainwright <josh.wainwright@ldra.com> | 2016-04-20 12:26:50 +0100 |
| commit | b474d7aa119fc6d1d28a574296ed341194eede31 (patch) | |
| tree | 2d46f312dd4497ef08a5272e03208efa9b255cdc /lua/utils.lua | |
| parent | 6bff371e3cce703ffc2a9c0df7c89d361e4ebf00 (diff) | |
| download | vis-b474d7aa119fc6d1d28a574296ed341194eede31.tar.gz vis-b474d7aa119fc6d1d28a574296ed341194eede31.tar.xz | |
Add first set of basic lua api tests
There are two types of lua tests here:
1. Tests are formed from a <test>.in, <test>.ref and <test>.out triplet. The
<test>.in file is opened by vis, some operatations are performed and the
modified file is written to <test>.out. The new <test>.out is compared to
<test>.ref and the test passes if they are identical.
2. Tests are formed from a single <test>.true file. This file is created by the
lua code in the test. It contains a single line per test case, this single
line should be `true` if the test case passed. The <test>.true file is
checked to ensure it contains only `true` lines and if so, the test passes.
Diffstat (limited to 'lua/utils.lua')
| -rw-r--r-- | lua/utils.lua | 127 |
1 files changed, 127 insertions, 0 deletions
diff --git a/lua/utils.lua b/lua/utils.lua new file mode 100644 index 0000000..134b2a6 --- /dev/null +++ b/lua/utils.lua @@ -0,0 +1,127 @@ +-- table_cmp - return true if table1 is the same as table2 +table_cmp = function(table1, table2) + if type(table1) ~= 'table' or type(table2) ~= 'table' then + return false + end + if #table1 ~= #table2 then + return false + end + for i, item in pairs(table1) do + if item ~= table2[i] then + return false + end + end + return true +end + +-- strcasecmp - compare strings case-insensitively +strcasecmp = function(str1, str2) + if type(str1) ~= 'string' or type(str2) ~= 'string' then + return false + end + str1 = string.lower(str1) + str2 = string.lower(str2) + return str1 == str2 +end + +-- Setline - Set lines starting at [startline] to [lines] +-- - [startline] can be '.' to set the current line +-- - [startline] can be '$' to set the last line in the buffer +-- - [lines] can be left nil in which case, set lines to empty +setline = function(win, startline, lines) + if startline == '.' then + startline = win.cursor.line + elseif startline == '$' then + startline = #win.file.lines + end + + if type(lines) == 'string' then + lines = {lines} + end + + for i, line in pairs(lines or {}) do + i = i + ((startline - 1) or 0) + win.file.lines[i] = line + end +end + +-- Delete - Remove [number] lines starting at [startline] +-- - [number] can be left nil to remove single line +-- - [startline] can be '.' to delete the current line +-- - [startline] can be '$' to delete the last line in the buffer +-- - [startline] can be '%' to empty the buffer +delete = function(win, startline, number) + if startline == '.' then + startline = win.cursor.line + elseif startline == '$' then + startline = #win.file.lines + elseif startline == '%' then + startline = 1 + number = #win.file.lines + end + + -- Get position in file of startline + win.cursor:to(startline, 0) + startpos = win.cursor.pos + + -- Get position in file of endline, or delete single line + number = number or 1 + win.cursor:to(startline + number, 0) + endpos = win.cursor.pos + + -- Delete lines + len = endpos - startpos + win.file:delete(startpos, len) +end + +-- Getline - Return a table of [number] lines starting from [startline] +-- - [startline] can be '.' to return the current line +-- - [startline] can be '$' to return the last line in the buffer +-- - [startline] can be '%' to return the whole buffer +-- - [number] can be left nil, in which case return a string +-- containing line [startline] +getline = function(win, startline, number) + if startline == '.' then + startline = win.cursor.line + elseif startline == '$' or startline > #win.file.lines then + startline = #win.file.lines + elseif startline == '%' then + startline = 1 + number = #win.file.lines + elseif startline <= 0 then + startline = 1 + end + + if number == nil then + return win.file.lines[startline] + else + lines = {} + for i = startline, startline + number - 1, 1 do + table.insert(lines, win.file.lines[i]) + end + return lines + end +end + +-- Append - Insert lines after given line number +-- - [line] can be either string or table +-- - [startline] can be '.' to append after the current line +-- - [startline] can be '$' to append after the last line in the buffer +append = function(win, startline, lines) + if startline == '.' then + win.cursor:to(win.cursor.line + 1, 0) + elseif startline == '$' or startline > #win.file.lines then + win.cursor:to(#win.file.lines + 1, 0) + else + win.cursor:to(startline + 1, 0) + end + pos = win.cursor.pos + + if type(lines) == 'table' then + lines = table.concat(lines, "\n") .. "\n" + elseif type(lines) == 'string' then + lines = lines .. '\n' + end + + win.file:insert(pos, lines) +end |
