Skip to content

Commit 038d056

Browse files
committed
test(error): add inner error tests
1 parent dc76b9d commit 038d056

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed

spec/error_spec.lua

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
local e = require('promise-async.error')
2+
local basics = require('spec.helpers.basics')
3+
4+
describe('Error for Promise and Async.', function()
5+
describe('Basic operations about error,', function()
6+
local msg = 'message test'
7+
it('create a error', function()
8+
local err = e:new(msg)
9+
assert.equal(msg, tostring(err))
10+
end)
11+
12+
it('append messages to a error', function()
13+
local err = e:new(msg)
14+
err:push('foo')
15+
err:push('bar')
16+
assert.equal('message test\nstack traceback:\nfoo\nbar', tostring(err))
17+
end)
18+
19+
it('do unshift for a error', function()
20+
local err = e:new(msg)
21+
err:push('foo')
22+
err:push('bar')
23+
err:unshift('UnhandledPromiseRejection baz:')
24+
assert.equal('UnhandledPromiseRejection baz:\nmessage test\nstack traceback:\nfoo\nbar', tostring(err))
25+
end)
26+
end)
27+
28+
describe('Build stacks.', function()
29+
describe('Build basic values for top of stack.', function()
30+
-- keep stack always from tail calls
31+
local function level3(v)
32+
local s = e:new(v):buildStack(2, '[C]')
33+
return tostring(s)
34+
end
35+
local function level2(v)
36+
local o = level3(v)
37+
return o
38+
end
39+
local function level1(v)
40+
local o = level2(v)
41+
return o
42+
end
43+
local src = debug.getinfo(1).short_src
44+
local level1Line = debug.getinfo(level1, 'S').linedefined
45+
local level2Line = debug.getinfo(level2, 'S').linedefined
46+
local level3Line = debug.getinfo(level3, 'S').linedefined
47+
local stackStr = ([[stack traceback:
48+
%s:%d: in function 'level3'
49+
%s:%d: in function 'level2'
50+
%s:%d: in function 'level1']]):format(src, level3Line + 1, src, level2Line + 1, src, level1Line + 1)
51+
local function testBasicTopStack(expectedValue, stringRepresentation)
52+
it('The value is ' .. stringRepresentation .. '.', function()
53+
local s = level1(expectedValue)
54+
assert.equal(tostring(expectedValue) .. '\n' .. stackStr, s)
55+
end)
56+
end
57+
58+
for valueStr, basicFn in pairs(basics) do
59+
testBasicTopStack(basicFn(), valueStr)
60+
end
61+
end)
62+
end)
63+
end)

spec/helpers/basics.lua

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@ Basic['`string`'] = function()
2020
end
2121

2222
Basic['a metatable'] = function()
23-
return setmetatable({}, {})
23+
return setmetatable({}, {
24+
__tostring = function()
25+
return '{}'
26+
end
27+
})
2428
end
2529

2630
Basic['a thread'] = function()

0 commit comments

Comments
 (0)