From 2f8f3871b98d0a431ca90372f318df67e5b7d01f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Andr=C3=A9=20Tanner?= Date: Fri, 3 Mar 2017 11:09:37 +0100 Subject: test/lua: add infrastructure for busted based unit tests --- .gitignore | 1 + lua/lines.in | 5 ++++ lua/lines.lua | 93 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ lua/test.sh | 27 ++++++++--------- lua/visrc.lua | 27 +++++++++++++---- 5 files changed, 134 insertions(+), 19 deletions(-) create mode 100644 lua/lines.in create mode 100644 lua/lines.lua diff --git a/.gitignore b/.gitignore index 4b76c8e..b62c022 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ *.out *.err *.status +*.busted diff --git a/lua/lines.in b/lua/lines.in new file mode 100644 index 0000000..1c35362 --- /dev/null +++ b/lua/lines.in @@ -0,0 +1,5 @@ +1 + +3 + +5 diff --git a/lua/lines.lua b/lua/lines.lua new file mode 100644 index 0000000..7771530 --- /dev/null +++ b/lua/lines.lua @@ -0,0 +1,93 @@ +local busted = require 'busted.runner'() + +local file = vis.win.file + +describe("file.lines_iterator()", function() + + it("same as file.lines[]", function() + local i = 1 + local iterator_lines = {} + for line in file:lines_iterator() do + iterator_lines[i] = line + i = i + 1 + end + local array_lines = {} + for i = 1, #file.lines do + array_lines[i] = file.lines[i] + end + assert.are.same(array_lines, iterator_lines) + end) +end) + +describe("get file.lines[]", function() + + it("#lines", function() + assert.are.equals(5, #file.lines) + end) + + it("get lines[0]", function() + -- is that what we want? + assert.are.equals(file.lines[0], file.lines[1]) + end) + + it("get lines[1]", function() + assert.are.equals("1", file.lines[1]) + end) + + it("get empty \n line", function() + assert.are.equals("", file.lines[2]) + end) + + it("get empty \r\n line", function() + assert.are.equals("", file.lines[4]) + end) + + it("get lines[#lines]", function() + assert.are.equals("5", file.lines[#file.lines]) + end) + + it("get lines[#lines+1]", function() + -- is that what we want? + assert.are.equals("5", file.lines[#file.lines]) + end) + +end) + +describe("set file.lines[]", function() + + it("replace empty \n line", function() + local new = "line 2" + file.lines[2] = new + assert.are.equals(new, file.lines[2]) + end) + +--[[ + it("replace empty \r\n line", function() + local new = "line 4" + file.lines[4] = new + assert.are.equals(new, file.lines[4]) + end) +--]] + + it("set lines[0], add new line at start", function() + local lines = #file.lines + local new_first = "new first line" + local old_first = file.lines[1] + file.lines[0] = new_first + assert.are.equals(lines+1, #file.lines) + assert.are.equals(new_first, file.lines[1]) + assert.are.equals(old_first, file.lines[2]) + end) + + it("set lines[#lines+1], add new line at end", function() + local lines = #file.lines + local new_last = "new last line" + local old_last = file.lines[#file.lines] + file.lines[#file.lines+1] = new_last + assert.are.equals(lines+1, #file.lines) + assert.are.equals(new_last, file.lines[#file.lines]) + assert.are.equals(old_last, file.lines[lines]) + end) + +end) + diff --git a/lua/test.sh b/lua/test.sh index d4193e0..717a02d 100755 --- a/lua/test.sh +++ b/lua/test.sh @@ -9,33 +9,34 @@ if ! $VIS -v | grep '+lua' >/dev/null 2>&1; then exit 0 fi +type busted >/dev/null 2>&1 || { + echo "busted(1) not found, skipping tests" + exit 0 +} + TESTS_OK=0 TESTS_RUN=0 if [ $# -gt 0 ]; then test_files=$* else - printf ':help\n:/ Lua paths/,$ w help\n:qall\n' | $VIS 2> /dev/null && cat help && rm -f help - test_files="$(find . -type f -name "*.in") basic_empty_file.in" + #test_files="$(find . -type f -name "*.in") basic_empty_file.in" + test_files="lines.in" fi for t in $test_files; do TESTS_RUN=$((TESTS_RUN + 1)) t=${t%.in} t=${t#./} - $VIS "$t".in < /dev/null 2> /dev/null - printf "%-30s" "$t" - if [ -e "$t".out ]; then - if cmp -s "$t".ref "$t".out 2> /dev/null; then - printf "PASS\n" - TESTS_OK=$((TESTS_OK + 1)) - else - printf "FAIL\n" - diff -u "$t".ref "$t".out > "$t".err - fi + $VIS "$t.in" < /dev/null 2> /dev/null > "$t.busted" + + if [ $? -ne 0 ]; then + printf "FAIL\n" + cat "$t.busted" else - printf "ERROR\n" + TESTS_OK=$((TESTS_OK + 1)) + printf "OK\n" fi done diff --git a/lua/visrc.lua b/lua/visrc.lua index af40306..16aa8b9 100644 --- a/lua/visrc.lua +++ b/lua/visrc.lua @@ -1,13 +1,28 @@ -dofile("utils.lua") +package.path = '../../lua/?.lua;'..package.path +dofile("../../lua/vis.lua") -vis.events = {} -vis.events.win_open = function(win) +-- redirect output to stdout, stderr is used by the vis UI +io.stderr = io.stdout + +-- make sure we gracefully terminate, cleanup terminal state etc. +os.exit = function(status) + vis:exit(status) +end + +vis.events.subscribe(vis.events.WIN_OPEN, function(win) -- test.in file passed to vis local in_file = win.file.name if in_file then -- use the corresponding test.lua file lua_file = string.gsub(in_file, '%.in$', '.lua') - dofile(lua_file) - vis:command('qall!') + local ok, msg = pcall(dofile, lua_file) + if not ok then + if type(msg) == 'string' then + print(msg) + end + vis:exit(1) + return + end end -end + vis:exit(0) +end) -- cgit v1.2.3