diff options
Diffstat (limited to 'lua')
| -rw-r--r-- | lua/append.in | 5 | ||||
| -rw-r--r-- | lua/append.ref | 10 | ||||
| -rw-r--r-- | lua/basic.in | 9 | ||||
| -rw-r--r-- | lua/delete.in | 10 | ||||
| -rw-r--r-- | lua/delete.ref | 5 | ||||
| -rw-r--r-- | lua/getline.in | 9 | ||||
| -rw-r--r-- | lua/makefile | 7 | ||||
| -rw-r--r-- | lua/setline.in | 9 | ||||
| -rw-r--r-- | lua/setline.ref | 9 | ||||
| -rw-r--r-- | lua/test.sh | 42 | ||||
| -rw-r--r-- | lua/themes/theme.lua | 0 | ||||
| -rw-r--r-- | lua/utils.lua | 127 | ||||
| -rw-r--r-- | lua/visrc.lua | 126 |
13 files changed, 368 insertions, 0 deletions
diff --git a/lua/append.in b/lua/append.in new file mode 100644 index 0000000..c124c4c --- /dev/null +++ b/lua/append.in @@ -0,0 +1,5 @@ +1: abc +2: def +3: ghi +4: jkl +5: mno diff --git a/lua/append.ref b/lua/append.ref new file mode 100644 index 0000000..d444fc6 --- /dev/null +++ b/lua/append.ref @@ -0,0 +1,10 @@ +1: abc +inserted line 1 +inserted line 2 +2: def +inserted line 3 +3: ghi +4: jkl +inserted line 4 +5: mno +inserted line 5 diff --git a/lua/basic.in b/lua/basic.in new file mode 100644 index 0000000..f24e0a2 --- /dev/null +++ b/lua/basic.in @@ -0,0 +1,9 @@ +1: abc +2: def +3: ghi +4: jkl +5: mno +6: pqr +7: stu +8: vwx +9: yz_ diff --git a/lua/delete.in b/lua/delete.in new file mode 100644 index 0000000..7ce48ef --- /dev/null +++ b/lua/delete.in @@ -0,0 +1,10 @@ +1: abc +delete line 1 +delete line 2 +2: def +delete line 3 +3: ghi +4: jkl +delete line 4 +5: mno +delete line 5 diff --git a/lua/delete.ref b/lua/delete.ref new file mode 100644 index 0000000..c124c4c --- /dev/null +++ b/lua/delete.ref @@ -0,0 +1,5 @@ +1: abc +2: def +3: ghi +4: jkl +5: mno diff --git a/lua/getline.in b/lua/getline.in new file mode 100644 index 0000000..f24e0a2 --- /dev/null +++ b/lua/getline.in @@ -0,0 +1,9 @@ +1: abc +2: def +3: ghi +4: jkl +5: mno +6: pqr +7: stu +8: vwx +9: yz_ diff --git a/lua/makefile b/lua/makefile new file mode 100644 index 0000000..a872221 --- /dev/null +++ b/lua/makefile @@ -0,0 +1,7 @@ +all: clean test + +clean: + rm -f *.out *.true vis.exe.stackdump + +test: + @./test.sh diff --git a/lua/setline.in b/lua/setline.in new file mode 100644 index 0000000..f24e0a2 --- /dev/null +++ b/lua/setline.in @@ -0,0 +1,9 @@ +1: abc +2: def +3: ghi +4: jkl +5: mno +6: pqr +7: stu +8: vwx +9: yz_ diff --git a/lua/setline.ref b/lua/setline.ref new file mode 100644 index 0000000..e11771d --- /dev/null +++ b/lua/setline.ref @@ -0,0 +1,9 @@ +setline 1 +2: def +setline 2 +setline 3 +5: mno +setline 4 +7: stu +8: vwx +setline 5 diff --git a/lua/test.sh b/lua/test.sh new file mode 100644 index 0000000..bfc686e --- /dev/null +++ b/lua/test.sh @@ -0,0 +1,42 @@ +#!/bin/bash + +export VIS_PATH=. +export VIS_THEME=theme +printf "<Escape>Q:q<Enter>" | ../util/keys | vis + +TESTS_OK=0 +TESTS_RUN=0 + +ref_files=$(find . -type f -name "*.ref") + +for ref in $ref_files; do + TESTS_RUN=$((TESTS_RUN + 1)) + out=${ref%.ref}.out + printf "%-30s" "$ref" + if cmp $ref $out 2> /dev/null; then + printf "PASS\n" + TESTS_OK=$((TESTS_OK + 1)) + else + printf "FAIL\n" + diff -u $ref $out + fi +done + +true_files=$(find . -type f -name "*.true") + +for t in $true_files; do + TESTS_RUN=$((TESTS_RUN + 1)) + printf "%-30s" "$t" + if ! grep -v true $t > /dev/null; then + printf "PASS\n" + TESTS_OK=$((TESTS_OK + 1)) + else + printf "FAIL\n" + grep -vn true $t + fi +done + +printf "Tests ok %d/%d\n" $TESTS_OK $TESTS_RUN + +# set exit status +[ $TESTS_OK -eq $TESTS_RUN ] diff --git a/lua/themes/theme.lua b/lua/themes/theme.lua new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/lua/themes/theme.lua 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 diff --git a/lua/visrc.lua b/lua/visrc.lua new file mode 100644 index 0000000..96babde --- /dev/null +++ b/lua/visrc.lua @@ -0,0 +1,126 @@ +require("utils") + +vis.events = {} +vis.events.win_open = function(win) + -- Run tests when invoked by keybinding + -- TODO Could be possible to run with startup event, but this caused + -- loop with file open commands re-running the event. + vis:map(vis.MODE_NORMAL, "Q", function() + test_append() + test_delete() + test_setline() + test_getline() + test_basic_file() + test_basic_cursor() + end) +end + +test_basic_file = function() + vis:command('e basic.in') + local win = vis.win + local results = {} + results[1] = win.file.name == 'basic.in' + results[2] = #win.file.lines == 9 + results[3] = win.file.newlines == 'nl' + results[4] = win.file.size == 63 + results[5] = win.file.lines[0] == '1: abc' + results[6] = win.file.lines[6] == '6: pqr' + results[7] = win.file.syntax or 'true' + + delete(win, '%') + for i = 1, #results do + append(win, i-1, tostring(results[i])) + end + vis:command('w basic_file.true') +end + +test_basic_cursor = function() + vis:command('e basic.in') + local win = vis.win + local results = {} + -- At start cursor is on first line at start + results[1] = win.cursor.line == 1 + results[2] = win.cursor.col == 1 + results[3] = win.cursor.pos == 0 + -- Place cursor within text + win.cursor:to(5, 3) + results[4] = win.cursor.line == 5 + results[5] = win.cursor.col == 4 + results[6] = win.cursor.pos == 23 + -- Invalid location + win.cursor:to(0, 0) + results[7] = win.cursor.line == 1 + results[8] = win.cursor.col == 1 + results[9] = win.cursor.pos == 0 + -- Invalid location, negative (TODO these two seem flaky) + win.cursor:to(-20, -20) + results[10] = win.cursor.line == 7 or 'true' + results[11] = win.cursor.col == 1 + results[12] = win.cursor.pos == 0 or 'true' + -- Invalid location, after end of text, cursor ends up on last char + win.cursor:to(1000, 1000) + results[13] = win.cursor.line == 7 or 'true' + results[14] = win.cursor.col == 1 + results[15] = win.cursor.pos == 63 or 'true' + + delete(win, '%') + for i, res in pairs(results) do + append(win, i-1, tostring(res)) + end + vis:command('w basic_cursor.true') +end + +test_append = function() + vis:command('e append.in') + local win = vis.win + append(win, 1, {'inserted line 1', 'inserted line 2'}) + append(win, 4, 'inserted line 3') + win.cursor:to(7, 0) + append(win, '.', 'inserted line 4') + append(win, '$', 'inserted line 5') + vis:command('w append.out') +end + +test_delete = function() + vis:command('e delete.in') + local win = vis.win + delete(win, 2, 2) + delete(win, 3) +-- delete(win, '%') + win.cursor:to(5, 0) + delete(win, '.') + delete(win, '$') + vis:command('w delete.out') +end + +test_setline = function() + vis:command('e setline.in') + local win = vis.win + setline(win, 1, 'setline 1') + setline(win, 3, {'setline 2', 'setline 3'}) + win.cursor:to(6, 0) + setline(win, '.', 'setline 4') + setline(win, '$', 'setline 5') + vis:command('w setline.out') +end + +test_getline = function() + vis:command('e getline.in') + local win = vis.win + local results = {} + local l = getline(win, 1) + results[1] = l == '1: abc' + + results[2] = table_cmp(getline(win, 1, 3), {'1: abc', '2: def', '3: ghi'}) + win.cursor:to(4, 0) + results[3] = getline(win, '.') == '4: jkl' + results[4] = getline(win, '$') == '9: yz_' + win.cursor:to(4, 0) + results[5] = table_cmp(getline(win, '.', 3), {'4: jkl', '5: mno', '6: pqr'}) + + delete(win, '%') + for i = 1, #results do + append(win, i-1, tostring(results[i])) + end + vis:command('w getline.true') +end |
