File: gh-4761-json-per-call-options.test.lua

package info (click to toggle)
tarantool 2.6.0-1.4
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 85,412 kB
  • sloc: ansic: 513,775; cpp: 69,493; sh: 25,650; python: 19,190; perl: 14,973; makefile: 4,178; yacc: 1,329; sql: 1,074; pascal: 620; ruby: 190; awk: 18; lisp: 7
file content (37 lines) | stat: -rwxr-xr-x 1,303 bytes parent folder | download | duplicates (3)
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
#!/usr/bin/env tarantool

local json = require('json')
local tap = require('tap')

--
-- gh-4761: json.decode silently changes instance settings when
-- called with 2nd parameter.
--
-- Verify json.encode as well.
--
local res = tap.test('gh-4761-json-per-call-options', function(test)
    test:plan(2)

    -- Preparation code: call :decode() with a custom option.
    local ok, err = pcall(json.decode, '{"foo": {"bar": 1}}',
                          {decode_max_depth = 1})
    assert(not ok, 'expect "too many nested data structures" error')

    -- Verify that the instance option remains unchanged.
    local exp_res = {foo = {bar = 1}}
    local ok, res = pcall(json.decode, '{"foo": {"bar": 1}}')
    test:is_deeply({ok, res}, {true, exp_res},
                   'json instance settings remain unchanged after :decode()')

    -- Same check for json.encode.
    local nan = 1/0
    local ok, err = pcall(json.encode, {a = nan},
                          {encode_invalid_numbers = false})
    assert(not ok, 'expected "number must not be NaN or Inf" error')
    local exp_res = '{"a":inf}'
    local ok, res = pcall(json.encode, {a = nan})
    test:is_deeply({ok, res}, {true, exp_res},
                   'json instance settings remain unchanged after :encode()')
end)

os.exit(res and 0 or 1)