@@ -15,23 +15,49 @@ describe('Configuration Parser', function()
15
15
16
16
describe (' .decode' , function ()
17
17
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 ))
19
23
end )
20
- end )
21
24
22
- describe (' .encode' , function ()
23
- it (' encodes to json by default' , function ()
25
+ it (' when given a table returns the same table' , function ()
24
26
local t = { a = 1 , b = 2 }
25
- assert .same (' {"a":1,"b":2} ' , _M .encode (t ))
27
+ assert .same (t , _M .decode (t ))
26
28
end )
27
29
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 )
31
42
end )
32
43
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 )
35
61
end )
36
62
end )
37
63
end )
0 commit comments