diff options
| author | Marc André Tanner <mat@brain-dump.org> | 2017-03-04 18:26:54 +0100 |
|---|---|---|
| committer | Marc André Tanner <mat@brain-dump.org> | 2017-03-04 18:26:54 +0100 |
| commit | b97c6d29b6a1c66c2596033cd255d6803939aba0 (patch) | |
| tree | 4d0f68ad1f76e7e94214a18281d3419d5ffff591 /lua | |
| parent | 54dd1d2bf9a6cbfbd754e1e53fdda55f3acbbc15 (diff) | |
| download | vis-b97c6d29b6a1c66c2596033cd255d6803939aba0.tar.gz vis-b97c6d29b6a1c66c2596033cd255d6803939aba0.tar.xz | |
test/lua: convert tests to busted infrastructure
Diffstat (limited to 'lua')
32 files changed, 198 insertions, 457 deletions
diff --git a/lua/Makefile b/lua/Makefile index 84a1eed..a7afc80 100644 --- a/lua/Makefile +++ b/lua/Makefile @@ -5,7 +5,7 @@ all: clean ../../vis test @$(MAKE) -C ../.. clean: - @rm -f *.out *.err + @rm -f *.out *.err *.busted test: @./test.sh diff --git a/lua/append.in b/lua/append.in deleted file mode 100644 index c124c4c..0000000 --- a/lua/append.in +++ /dev/null @@ -1,5 +0,0 @@ -1: abc -2: def -3: ghi -4: jkl -5: mno diff --git a/lua/append.lua b/lua/append.lua deleted file mode 100644 index 3039919..0000000 --- a/lua/append.lua +++ /dev/null @@ -1,7 +0,0 @@ -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') diff --git a/lua/append.ref b/lua/append.ref deleted file mode 100644 index d444fc6..0000000 --- a/lua/append.ref +++ /dev/null @@ -1,10 +0,0 @@ -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_command_register.lua b/lua/basic_command_register.lua deleted file mode 100644 index ec21c66..0000000 --- a/lua/basic_command_register.lua +++ /dev/null @@ -1,28 +0,0 @@ -local win = vis.win -local cnt = 0 -vis:command_register("foo", function(argv, force, win, cursor, range) - cnt = cnt + 1 - append(win, '$', 'test: ' .. cnt) - append(win, '$', 'args: ' .. #argv) - for i,arg in ipairs(argv) do - append(win, '$', '\t' .. i .. ' ' .. arg) - end - append(win, '$', 'bang: ' .. (force and "yes" or "no")) - append(win, '$', 'name: ' .. win.file.name) - append(win, '$', 'pos: ' .. cursor.pos) - append(win, '$', 'range: ' .. (range ~= nil and - ('['..range.start..', '..range.finish..']') - or "invalid range")) - append(win, '$', '') - return true; -end) - -vis:command('foo') -vis:command('foo!') -vis:command('2,4foo!') -vis:command('%foo') -vis:command('foo one') -vis:command('foo one two') -vis:command('foo one two three four five six') - -vis:command('w! basic_command_register.out') diff --git a/lua/basic_command_register.ref b/lua/basic_command_register.ref deleted file mode 100644 index bb7e3b4..0000000 --- a/lua/basic_command_register.ref +++ /dev/null @@ -1,58 +0,0 @@ -test: 1 -args: 0 -bang: no -name: basic_command_register.in -pos: 57 -range: [0, 0] - -test: 2 -args: 0 -bang: yes -name: basic_command_register.in -pos: 138 -range: [0, 80] - -test: 3 -args: 0 -bang: yes -name: basic_command_register.in -pos: 221 -range: [8, 57] - -test: 4 -args: 0 -bang: no -name: basic_command_register.in -pos: 303 -range: [0, 246] - -test: 5 -args: 1 - 1 one -bang: no -name: basic_command_register.in -pos: 393 -range: [0, 329] - -test: 6 -args: 2 - 1 one - 2 two -bang: no -name: basic_command_register.in -pos: 490 -range: [0, 419] - -test: 7 -args: 6 - 1 one - 2 two - 3 three - 4 four - 5 five - 6 six -bang: no -name: basic_command_register.in -pos: 619 -range: [0, 516] - diff --git a/lua/basic_cursor.lua b/lua/basic_cursor.lua deleted file mode 100644 index 525d047..0000000 --- a/lua/basic_cursor.lua +++ /dev/null @@ -1,39 +0,0 @@ -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 == 3 -results[6] = win.cursor.pos == 30 -win.cursor:to(8, 1) -results[7] = win.cursor.line == 8 -results[8] = win.cursor.col == 1 -results[9] = win.cursor.pos == 49 --- Invalid location -win.cursor:to(0, 0) -results[10] = win.cursor.line == 1 -results[11] = win.cursor.col == 1 -results[12] = win.cursor.pos == 0 --- Invalid location, negative -local ok, msg = pcall(function() - win.cursor:to(-20, -20) -end) -results[13] = not ok -results[14] = win.cursor.line == 1 -results[15] = win.cursor.col == 1 -results[16] = win.cursor.pos == 0 --- Invalid location, after end of text, cursor ends up on last char -win.cursor:to(1000, 1000) -results[17] = win.cursor.line == 9 or true -results[18] = win.cursor.col == 1 -results[19] = 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.out') diff --git a/lua/basic_cursor.ref b/lua/basic_cursor.ref deleted file mode 100644 index 7710f13..0000000 --- a/lua/basic_cursor.ref +++ /dev/null @@ -1,19 +0,0 @@ -true -true -true -true -true -true -true -true -true -true -true -true -true -true -true -true -true -true -true diff --git a/lua/basic_empty_file.lua b/lua/basic_empty_file.lua deleted file mode 100644 index faa53b0..0000000 --- a/lua/basic_empty_file.lua +++ /dev/null @@ -1,15 +0,0 @@ --- Tests vis.win.file.* for a file that doesn't exist -local win = vis.win -local results = {} -results[1] = win.file.name == 'basic_empty_file.in' -results[2] = win.file.newlines == 'lf' -results[3] = win.file.size == 0 -results[4] = #win.file.lines == 0 -results[5] = win.file.lines[0] == '' -results[6] = win.file.syntax or 'true' - -delete(win, '%') -for i = 1, #results do - append(win, i-1, tostring(results[i])) -end -vis:command('w! basic_empty_file.out') diff --git a/lua/basic_empty_file.ref b/lua/basic_empty_file.ref deleted file mode 100644 index 5ec39bb..0000000 --- a/lua/basic_empty_file.ref +++ /dev/null @@ -1,6 +0,0 @@ -true -true -true -true -true -true diff --git a/lua/basic_file.in b/lua/basic_file.in deleted file mode 100644 index f24e0a2..0000000 --- a/lua/basic_file.in +++ /dev/null @@ -1,9 +0,0 @@ -1: abc -2: def -3: ghi -4: jkl -5: mno -6: pqr -7: stu -8: vwx -9: yz_ diff --git a/lua/basic_file.lua b/lua/basic_file.lua deleted file mode 100644 index a40446c..0000000 --- a/lua/basic_file.lua +++ /dev/null @@ -1,15 +0,0 @@ -local win = vis.win -local results = {} -results[1] = win.file.name == 'basic_file.in' -results[2] = #win.file.lines == 9 -results[3] = win.file.newlines == 'lf' -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.out') diff --git a/lua/basic_file.ref b/lua/basic_file.ref deleted file mode 100644 index 814ccfe..0000000 --- a/lua/basic_file.ref +++ /dev/null @@ -1,7 +0,0 @@ -true -true -true -true -true -true -true diff --git a/lua/basic_map.in b/lua/basic_map.in deleted file mode 100644 index e69de29..0000000 --- a/lua/basic_map.in +++ /dev/null diff --git a/lua/basic_map.lua b/lua/basic_map.lua deleted file mode 100644 index 2d955e2..0000000 --- a/lua/basic_map.lua +++ /dev/null @@ -1,24 +0,0 @@ -vis:map(vis.modes.NORMAL, "K", function() - vis:feedkeys("iNormal Mode<Enter><Escape>") -end) -vis:map(vis.modes.INSERT, "K", function() - vis:feedkeys("Insert Mode<Enter><Escape>") -end) -vis:map(vis.modes.VISUAL, "K", function() - vis:feedkeys("<Escape>iVisual Mode<Enter><Escape>") -end) -vis:map(vis.modes.VISUAL_LINE, "K", function() - vis:feedkeys("<Escape>iVisual Line Mode<Enter><Escape>") -end) -vis:map(vis.modes.REPLACE, "K", function() - vis:feedkeys("Replace Mode<Enter><Escape>") -end) - -vis:feedkeys("K") -vis:feedkeys("iK") -vis:feedkeys("vK") -vis:feedkeys("VK") -vis:feedkeys("RK") -vis:feedkeys("dd") - -vis:command("w! basic_map.out")
\ No newline at end of file diff --git a/lua/basic_map.ref b/lua/basic_map.ref deleted file mode 100644 index 9ac66ab..0000000 --- a/lua/basic_map.ref +++ /dev/null @@ -1,5 +0,0 @@ -Normal Mode -Insert Mode -Visual Mode -Visual Line Mode -Replace Mode diff --git a/lua/basic_cursor.in b/lua/cursor.in index f24e0a2..f24e0a2 100644 --- a/lua/basic_cursor.in +++ b/lua/cursor.in diff --git a/lua/cursor.lua b/lua/cursor.lua new file mode 100644 index 0000000..06d31a1 --- /dev/null +++ b/lua/cursor.lua @@ -0,0 +1,105 @@ +local busted = require 'busted.runner'() + +local win = vis.win + +-- check that cursor position remains unchanged after an invalid adjustment +local invalid_pos = function(place) + local pos = win.cursor.pos + local line = win.cursor.line + local col = win.cursor.col + win.cursor:to(line, col) + assert.has_error(place) + assert.are.equal(pos, win.cursor.pos) + assert.are.equal(line, win.cursor.line) + assert.are.equal(col, win.cursor.col) +end + +describe("win.cursor", function() + + it("initial position", function() + assert.are.equal(0, win.cursor.pos) + end) + + it("initial line", function() + assert.are.equal(1, win.cursor.line) + end) + + it("initial column", function() + assert.are.equal(1, win.cursor.col) + end) +end) + +describe("win.cursor.pos", function() + + it("= 0", function() + win.cursor.pos = 0 + assert.are.equal(0, win.cursor.pos) + assert.are.equal(1, win.cursor.line) + assert.are.equal(1, win.cursor.col) + end) + + it("= beyond end of file", function() + win.cursor.pos = win.file.size + local pos = win.cursor.pos + local line = win.cursor.line + local col = win.cursor.col + win.cursor.pos = 0 + -- cursor is placed on last valid position + win.cursor.pos = win.file.size+1 + assert.are.equal(pos, win.cursor.pos) + assert.are.equal(line, win.cursor.line) + assert.are.equal(col, win.cursor.col) + end) + +end) + +describe("win.cursor.to", function() + + it("(5, 3)", function() + win.cursor:to(5, 3) + assert.are.equal(30, win.cursor.pos) + assert.are.equal(5, win.cursor.line) + assert.are.equal(3, win.cursor.col) + end) + + it("(0, 0) invalid position", function() + -- is that what we want? + win.cursor:to(0, 0) + assert.are.equal(0, win.cursor.pos) + assert.are.equal(1, win.cursor.line) + assert.are.equal(1, win.cursor.col) + end) + + it("invalid position, negative line", function() + invalid_pos(function() win.cursor:to(-1, 0) end) + end) + + it("invalid position, negative column", function() + invalid_pos(function() win.cursor:to(0, -1) end) + end) + + it("invalid position, non-integer line", function() + invalid_pos(function() win.cursor:to(1.5, 1) end) + end) + + it("invalid position, non-integer column", function() + invalid_pos(function() win.cursor:to(1, 1.5) end) + end) + +--[[ + it("move beyond end of file", function() + win.cursor.pos = win.file.size + local pos = win.cursor.pos + local line = win.cursor.line + local col = win.cursor.col + win.cursor.pos = 0 + -- cursor is placed on last valid position + win.cursor:to(#win.file.lines+2, 1000) + assert.are.equal(pos, win.cursor.pos) + assert.are.equal(line, win.cursor.line) + assert.are.equal(col, win.cursor.col) + end) +--]] + +end) + diff --git a/lua/delete.in b/lua/delete.in deleted file mode 100644 index 7ce48ef..0000000 --- a/lua/delete.in +++ /dev/null @@ -1,10 +0,0 @@ -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.lua b/lua/delete.lua deleted file mode 100644 index 9cb5a96..0000000 --- a/lua/delete.lua +++ /dev/null @@ -1,8 +0,0 @@ -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') diff --git a/lua/delete.ref b/lua/delete.ref deleted file mode 100644 index c124c4c..0000000 --- a/lua/delete.ref +++ /dev/null @@ -1,5 +0,0 @@ -1: abc -2: def -3: ghi -4: jkl -5: mno diff --git a/lua/basic_command_register.in b/lua/file-empty.in index e69de29..e69de29 100644 --- a/lua/basic_command_register.in +++ b/lua/file-empty.in diff --git a/lua/file-empty.lua b/lua/file-empty.lua new file mode 100644 index 0000000..95b34b0 --- /dev/null +++ b/lua/file-empty.lua @@ -0,0 +1,25 @@ +local busted = require 'busted.runner'() + +local file = vis.win.file + +describe("empty file", function() + + it("has size zero", function() + assert.are.equal(0, file.size) + end) + + it("has \\n line endings", function() + assert.are.equal("lf", file.newlines) + end) + + it("has zero lines", function() + assert.are.equal(0, #file.lines) + end) + + it("has lines[1] == ''", function() + -- is that what we want? + assert.are.equal("", file.lines[1]) + end) + +end) + diff --git a/lua/getline.in b/lua/getline.in deleted file mode 100644 index f24e0a2..0000000 --- a/lua/getline.in +++ /dev/null @@ -1,9 +0,0 @@ -1: abc -2: def -3: ghi -4: jkl -5: mno -6: pqr -7: stu -8: vwx -9: yz_ diff --git a/lua/getline.lua b/lua/getline.lua deleted file mode 100644 index c8b0fe2..0000000 --- a/lua/getline.lua +++ /dev/null @@ -1,17 +0,0 @@ -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.out') diff --git a/lua/getline.ref b/lua/getline.ref deleted file mode 100644 index 36c7afa..0000000 --- a/lua/getline.ref +++ /dev/null @@ -1,5 +0,0 @@ -true -true -true -true -true diff --git a/lua/map-basic.lua b/lua/map-basic.lua new file mode 100644 index 0000000..56ad293 --- /dev/null +++ b/lua/map-basic.lua @@ -0,0 +1,65 @@ +local busted = require 'busted.runner'() + +vis:map(vis.modes.NORMAL, "K", function() + vis:feedkeys("iNormal Mode<Escape>") +end) + +vis:map(vis.modes.INSERT, "K", function() + vis:feedkeys("Insert Mode<Escape>") +end) + +vis:map(vis.modes.VISUAL, "K", function() + vis:feedkeys("<Escape>iVisual Mode<Escape>") +end) + +vis:map(vis.modes.VISUAL_LINE, "K", function() + vis:feedkeys("<Escape>iVisual Line Mode<Escape>") +end) + +vis:map(vis.modes.REPLACE, "K", function() + vis:feedkeys("Replace Mode<Escape>") +end) + +local win = vis.win +local file = win.file + +describe("map", function() + + before_each(function() + win.cursor.pos = 0 + end) + + after_each(function() + file:delete(0, file.size) + end) + + local same = function(expected) + local data = file:content(0, file.size) + assert.are.same(expected, data) + end + + it("normal mode", function() + vis:feedkeys("K") + same("Normal Mode") + end) + + it("insert mode", function() + vis:feedkeys("iK") + same("Insert Mode") + end) + + it("visual mode", function() + vis:feedkeys("vK") + same("Visual Mode") + end) + + it("visual line mode", function() + vis:feedkeys("VK") + same("Visual Line Mode") + end) + + it("replace mode", function() + vis:feedkeys("RK") + same("Replace Mode") + end) +end) diff --git a/lua/setline.in b/lua/setline.in deleted file mode 100644 index f24e0a2..0000000 --- a/lua/setline.in +++ /dev/null @@ -1,9 +0,0 @@ -1: abc -2: def -3: ghi -4: jkl -5: mno -6: pqr -7: stu -8: vwx -9: yz_ diff --git a/lua/setline.lua b/lua/setline.lua deleted file mode 100644 index f37a33f..0000000 --- a/lua/setline.lua +++ /dev/null @@ -1,7 +0,0 @@ -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') diff --git a/lua/setline.ref b/lua/setline.ref deleted file mode 100644 index e11771d..0000000 --- a/lua/setline.ref +++ /dev/null @@ -1,9 +0,0 @@ -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 index 717a02d..4d885de 100755 --- a/lua/test.sh +++ b/lua/test.sh @@ -20,13 +20,12 @@ TESTS_RUN=0 if [ $# -gt 0 ]; then test_files=$* else - #test_files="$(find . -type f -name "*.in") basic_empty_file.in" - test_files="lines.in" + test_files="$(find . -type f -name '*.lua' -a ! -name visrc.lua)" fi for t in $test_files; do TESTS_RUN=$((TESTS_RUN + 1)) - t=${t%.in} + t=${t%.lua} t=${t#./} printf "%-30s" "$t" $VIS "$t.in" < /dev/null 2> /dev/null > "$t.busted" diff --git a/lua/utils.lua b/lua/utils.lua deleted file mode 100644 index 134b2a6..0000000 --- a/lua/utils.lua +++ /dev/null @@ -1,127 +0,0 @@ --- 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 |
