summaryrefslogtreecommitdiff
path: root/test/test-lace.error.lua
blob: a2e09a73635759ee9c1b0872c5322c0f3b2fbba7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
-- test/test-lace.lua
--
-- Lua Access Control Engine -- Tests for the Lace error module
--
-- Copyright 2012 Daniel Silverstone <dsilvers@digital-scurf.org>
--
-- For Licence terms, see COPYING
--

-- Step one, start coverage

pcall(require, 'luacov')

local error = require 'lace.error'

local testnames = {}

local real_assert = assert
local total_asserts = 0
local function assert(...)
   real_assert(...)
   total_asserts = total_asserts + 1
end

local function add_test(suite, name, value)
   rawset(suite, name, value)
   testnames[#testnames+1] = name
end

local suite = setmetatable({}, {__newindex = add_test})

function suite.error_formation()
   local words = {}
   local ret1, ret2 = error.error("msg", words)
   assert(ret1 == false, "First return of error() should be false")
   assert(type(ret2) == "table", "Second return of error() should be a table")
   assert(ret2.msg == "msg", "Message should be passed through")
   assert(ret2.words == words, "Words should be passed through")
end

function suite.error_offset()
   local words = {3,5}
   local f, err = error.error("msg", words)
   local nerr = error.offset(err, 2)
   assert(nerr == err, "Offset should return the same error it was given")
   assert(words[1] == 5, "Offset should alter the first word")
   assert(words[2] == 7, "Offset should alter all the words")
end

function suite.error_augmentation()
   local f, err = error.error("msg")
   local src = {}
   local aug = error.augment(err, src, 10)
   assert(aug == err, "Augmentation should return the error")
   assert(err.words.source == src, "Augmented errors should contain their source data")
   assert(err.words.linenr == 10, "Augmented errors should contain their error line")
end

function suite.error_render()
   local f, err = error.error("msg", {1, 3})
   local src = { source = "SOURCE", lines = {
	 {
	    original = "  ORIG LINE FISH",
	    content = {
	       { spos = 3, epos = 6, str = "ORIG" },
	       { spos = 8, epos = 11, str = "LINE" },
	       { spos = 13, epos = 16, str = "FISH" },
	    }
	 }
      }
   }
   error.augment(err, src, 1)
   local estr = error.render(err)
   local line1, line2, line3, line4 = estr:match("^([^\n]*)\n([^\n]*)\n([^\n]*)\n([^\n]*)$")
   assert(line1, "There is a line 1")
   assert(line2, "There is a line 2")
   assert(line3, "There is a line 3")
   assert(line4, "There is a line 4")
   assert(line1 == "msg", "The first line should be the error message")
   assert(line2 == "SOURCE :: 1", "The second line is where the error happened")
   assert(line3 == src.lines[1].original, "The third line is the original line")
   assert(line4 == "  ^^^^      ^^^^", "The fourth line highlights relevant words")
end

function suite.error_render_bad_line()
   local f, err = error.error("msg", {1, 3})
   local src = { source = "SOURCE", lines = { } }
   error.augment(err, src, 1)
   local estr = error.render(err)
   local line1, line2, line3, line4 = estr:match("^([^\n]*)\n([^\n]*)\n([^\n]*)\n([^\n]*)$")
   assert(line1, "There is a line 1")
   assert(line2, "There is a line 2")
   assert(line3, "There is a line 3")
   assert(line4, "There is a line 4")
   assert(line1 == "msg", "The first line should be the error message")
   assert(line2 == "SOURCE :: 1", "The second line is where the error happened")
   assert(line3 == "???", "The third line is the original line")
   assert(line4 == "^^^", "The fourth line highlights relevant words")
end

local count_ok = 0
for _, testname in ipairs(testnames) do
--   print("Run: " .. testname)
   local ok, err = xpcall(suite[testname], debug.traceback)
   if not ok then
      print(err)
      print()
   else
      count_ok = count_ok + 1
   end
end

print(tostring(count_ok) .. "/" .. tostring(#testnames) .. " [" .. tostring(total_asserts) .. "] OK")

os.exit(count_ok == #testnames and 0 or 1)