|
| 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) |
0 commit comments