Skip to content

Commit ce691a7

Browse files
authored
Merge pull request #461 from 3scale/add-missing-tests-config-parser
Add missing tests for the configuration parser module
2 parents 943199e + 97b9e05 commit ce691a7

File tree

2 files changed

+36
-19
lines changed

2 files changed

+36
-19
lines changed

apicast/src/configuration_parser.lua

-9
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,6 @@ function _M.decode(contents, encoder)
3030
return ret
3131
end
3232

33-
34-
function _M.encode(contents, encoder)
35-
if type(contents) == 'string' then return contents end
36-
37-
encoder = encoder or cjson
38-
39-
return encoder.encode(contents)
40-
end
41-
4233
function _M.parse(contents, encoder)
4334
local config, err = _M.decode(contents, encoder)
4435

spec/configuration_parser_spec.lua

+36-10
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,49 @@ describe('Configuration Parser', function()
1515

1616
describe('.decode', function()
1717
it('ignores empty string', function()
18-
assert.same(nil, _M.decode(''))
18+
assert.is_nil(_M.decode(''))
19+
end)
20+
21+
it('decodes nil as nil', function()
22+
assert.is_nil(_M.decode(nil))
1923
end)
20-
end)
2124

22-
describe('.encode', function()
23-
it('encodes to json by default', function()
25+
it('when given a table returns the same table', function()
2426
local t = { a = 1, b = 2 }
25-
assert.same('{"a":1,"b":2}', _M.encode(t))
27+
assert.same(t, _M.decode(t))
2628
end)
2729

28-
it('does not do double encoding', function()
29-
local str = '{"a":1,"b":2}'
30-
assert.same(str, _M.encode(str))
30+
describe('when a decoder is not specified', function()
31+
it('uses cjson', function()
32+
local contents = '{"a":1,"b":2}'
33+
assert.same(cjson.decode(contents), _M.decode(contents))
34+
end)
35+
36+
it('returns nil and an error when cjson fails to decode', function()
37+
local contents = '{"a:1}' --malformed
38+
local res, err = _M.decode(contents)
39+
assert.is_nil(res)
40+
assert.not_nil(err)
41+
end)
3142
end)
3243

33-
it('encodes nil to null', function()
34-
assert.same('null', _M.encode(nil))
44+
describe('when a decoder is specified', function()
45+
local custom_decoder = {
46+
decode = function(contents)
47+
return (contents == 'err' and error('oh no')) or contents
48+
end
49+
}
50+
51+
it('uses the given decoder instead of cjson', function()
52+
assert.equal(custom_decoder.decode('a'),
53+
_M.decode('a', custom_decoder))
54+
end)
55+
56+
it('returns nil and an error when the given decoder fails', function()
57+
local res, err = _M.decode('err', custom_decoder)
58+
assert.is_nil(res)
59+
assert.not_nil(err)
60+
end)
3561
end)
3662
end)
3763
end)

0 commit comments

Comments
 (0)