vis

a vi-like editor based on Plan 9's structural regular expressions

git clone https://9o.is/git/vis.git

visrc.lua

(2567B)


      1 package.path = '../../lua/?.lua;'..package.path
      2 
      3 dofile("../../lua/vis.lua")
      4 
      5 local function str(e)
      6 	if type(e) == "string" then
      7 		if e == "" then
      8 			return "an empty string"
      9 		else
     10 			return '"'..e..'"'
     11 		end
     12 	else
     13 		return tostring(e)
     14 	end
     15 end
     16 
     17 
     18 local function same(a, b)
     19 	if type(a) ~= type(b) then
     20 		return string.format("expected %s - got %s", type(a), type(b))
     21 	end
     22 
     23 	if type(a) ~= 'table' then
     24 		if a == b then
     25 			return nil
     26 		else
     27 			return string.format("expected %s - got %s", str(a), str(b))
     28 		end
     29 	end
     30 
     31 	if #a ~= #b then
     32 		return string.format("expected table of size %d got %d", #a, #b)
     33 	end
     34 
     35 	for k, v in pairs(a) do
     36 		if b[k] == nil then
     37 			return string.format("expected %s got nil", str(k))
     38 		end
     39 		r = same(v, b[k])
     40 		if r ~= nil then
     41 			return r
     42 		end
     43 	end
     44 	return nil
     45 end
     46 
     47 
     48 local msg = ""
     49 function describe(s, fn)
     50 	group = {
     51 		before_each = function()
     52 		end,
     53 		tests = {},
     54 		after_each = function()
     55 		end,
     56 
     57 	}
     58 	function before_each(fn) group.before_each = fn end
     59 	function after_each(fn) group.after_each = fn end
     60 	function it(s, fn)
     61 		table.insert(group.tests, {
     62 			s = s,
     63 			fn = fn,
     64 			assertions = {},
     65 		})
     66 	end
     67 	fn()
     68 	for j, t in pairs(group.tests) do
     69 		group.before_each()
     70 		assert = {
     71 			has_error = function(fn)
     72 				local status, err = pcall(fn)
     73 				if err == nil then
     74 					msg = msg..string.format("%s %s: expected error\n", s, t.s)
     75 				end
     76 			end,
     77 			truthy = function(stat)
     78 				if not (stat) then
     79 					msg = msg..string.format("%s %s: expected to be truthy: %s\n", s, t.s, str(stat))
     80 				end
     81 			end,
     82 			falsy = function(stat)
     83 				if (stat) then
     84 					msg = msg..string.format("%s %s: expected to be falsy: %s\n", s, t.s, str(stat))
     85 				end
     86 			end,
     87 			are = {
     88 				equal = function(a, b)
     89 					if a ~= b then
     90 						msg = msg..string.format("%s %s: expected %s - got %s\n", s, t.s, str(a), str(b))
     91 					end
     92 				end,
     93 				same = function(a, b)
     94 					r = same(a, b) -- same returns a string which is a reason why a & b are not equal
     95 					if r ~= nil then
     96 						msg = msg..string.format("%s %s: %s\n", s, t.s, r)
     97 					end
     98 				end,
     99 			},
    100 		}
    101 		t.fn()
    102 		group.after_each()
    103 	end
    104 end
    105 
    106 
    107 vis.events.subscribe(vis.events.WIN_OPEN, function(win)
    108 	-- test.in file passed to vis
    109 	local in_file = win.file.name
    110 	if not in_file then
    111 		return
    112 	end
    113 
    114 	-- use the corresponding test.lua file
    115 	lua_file = string.gsub(in_file, '%.in$', '.lua')
    116 
    117 	local ok, err = pcall(dofile, lua_file)
    118 	if not ok then
    119 		print(tostring(err))
    120 		vis:exit(2) -- ERROR
    121 	elseif msg ~= "" then
    122 		io.write(msg) -- no newline
    123 		vis:exit(1) -- FAIL
    124 	else
    125 		vis:exit(0) -- SUCCESS
    126 	end
    127 end)