dev

This module contains testcases for its parent module, Json.

See also


-- <nowiki>
-- @attribution https://github.com/LuaDist/dkjson/blob/master/jsontest.lua
-- @attribution https://github.com/craigmj/json4lua/blob/master/examples/tests.lua
local json = require('Dev:Json')
local ScribuntoUnit = require("Module:ScribuntoUnit")
local suite = ScribuntoUnit:new()
local e, r, s, t, i
local unindent = require('Dev:Unindent')

function suite:testJsonEncode()
    -- Test basic string encoding (complicated strings).
    s = [[Hello, Lua!]]
    r = json.encode(s)
    self:assertTrue(r == '"Hello, Lua!"', "cannot encode multiline strings")
    s = [["\"
    ]]
    r = json.encode(s)
    self:assertTrue(r == "\"\\\"\\\\\\\"\\n    \"", "cannot encode multiline quotes")
    s = [["""\\\"]]
    r = json.encode(s)
    self:assertTrue(r == [["\"\"\"\\\\\\\""]], "cannot encode quoteception")

    -- Test numeric value encoding.
    s = 23
    r = json.encode(s)
    self:assertTrue(r == "23", "cannot encode integer number")
    s = 48.123
    r = json.encode(s)
    self:assertTrue(r == "48.123", "cannot encode decimal number")

    -- Test boolean value encoding.
    self:assertTrue(json.encode(true) == "true", "cannot encode true")
    self:assertTrue(json.encode(false) == 'false', "cannot encode false")
    self:assertTrue(json.encode(nil) == "null", "cannot encode null")

    -- Test array encoding.
    s = { 1, 2, 3 }
    r = json.encode(s)
    self:assertTrue(r == "[1,2,3]", "cannot encode array with elements 1,2,3")
    s = { 9, 9, 9 }
    r = json.encode(s)
    self:assertTrue(r == '[9,9,9]', "cannot encode array with elements 1,2,3")

    -- Test complex array encoding.
    s = { 2, "joe", false, nil, "hi" }
    r = json.encode(s)
    self:assertTrue(r == '[2,"joe",false,null,"hi"]', "cannot encode complex array")

    -- Test dictionary table encoding.
    s = { Name = "Craig", email = "craig@lateral.co.za", age = 35 }
    r = json.encode(s)
    self:assertTrue(r == [[{"age":35,"Name":"Craig","email":"craig@lateral.co.za"}]])

    -- Test sparse array encoding.
    t = { [1000] = "x" }
    r = json.encode(t)
    self:assertTrue(r == '{"1000":"x"}', "cannot encode sparse array (#=0)")
    t = { "a", [1000] = "x" }
    r = json.encode(t)
    self:assertTrue(r == '{"1":"a","1000":"x"}', "cannot encode sparse array (#=1)")

    -- Test mixed table encoding.
    t = { "a", [5] = "c", ["x"] = "x" }
    r = json.encode(t)
    self:assertTrue(r == '{"1":"a","5":"c","x":"x"}', "cannot encode mixed table")

    -- Test | Inf | and NaN encoding.
    t = { math.huge } -- positive Inf
    r = json.encode(t)
    self:assertTrue(r == '[null]', "cannot encode +Inf as valid JSON")
    t = { -math.huge } -- negative Inf
    r = json.encode(t)
    self:assertTrue(r == '[null]', "cannot encode -Inf as valid JSON")
    t = { math.huge * 0 } -- NaN
    r = json.encode(t)
    self:assertTrue(r == '[null]', "cannot encode NaN as valid JSON")

    -- Test json.null management.
    t = { 1, 2, json.null, 4 }
    self:assertTrue(json.encode(t) == "[1,2,null,4]")
    t = { x = json.null }
    r = json.encode(t)
    self:assertTrue(json.encode(t) == '{"x":null}')
end

function suite:testJsonDecode()
    -- Test table decoding.
    s = [[ {"one":1, "two":2, "three":"three", "four":true} ]]
    t = { one = 1, two = 2, three = "three", four = true }
    r = json.decode(s)
    self:assertDeepEquals(r, t)

    s = [[ { "one" : { "first":1,"second":2,"third":3}, "two":2, "three":false } ]]
    t = { one= { first = 1, second = 2, third = 3 }, two = 2, three = false }
    r = json.decode(s)
    self:assertDeepEquals(r, t)

    s = [[ { "primes" : [2,3,5,7,9], "user":{"name":"craig","age":35,"programs_lua":true},
    "lua_is_great":true } ]]
    t = { primes = {2, 3, 5, 7, 9}, user = { name = "craig", age=35, programs_lua = true }, lua_is_great = true }
    r = json.decode(s)
    self:assertDeepEquals(r, t)

    -- Test deep table decoding.
    s = unindent [[
    {"":
        {"":{"":{"":{"":{"":{"":{"":{"":{"":{"":{"":{"":{"":
        {"":{"":{"":{"":{"":{"":{"":{"":{"":{"":{"":{"":{"":
        {"":{"":{"":{"":{"":{"":{"":{"":{"":{"":{"":{"":{"":
        {"":{"":{"":{"":{"":{"":{"":{"":{"":{"":{"":{"":{"":
        {"":{"":{"":{"":{"":{"":{"":
            "deep down"
        }   }   }   }   }   }   }
        }   }   }   }   }   }   }   }   }   }   }   }   }
        }   }   }   }   }   }   }   }   }   }   }   }   }
        }   }   }   }   }   }   }   }   }   }   }   }   }
        }   }   }   }   }   }   }   }   }   }   }   }   }
    }
    ]]
    r = json.decode(s)
    i = 0
    while type(r) == 'table' do
      i = i + 1
      r = r['']
    end
    self:assertTrue(i == 60 and r == "deep down")

    -- Test big array decoding.
    s = "[" .. ("0,"):rep(999) .. "0]"
    r = json.decode(s)
    self:assertTrue(type(r) == "table" and #r == 1000)

    -- Test empty object decoding.
    s = "{}"
    r = json.decode(s)
    self:assertTrue(type(r) == "table" and next(r) == nil, "")

    -- Test empty array decoding.
    s = "[]"
    r = json.decode(s)
    self:assertTrue(type(r) == "table" and next(r) == nil)

    -- Test exponential notation decoding.
    s = "[1e+2]"
    r = json.decode(s)
    self:assertTrue(r[1] == 1e+2)

    -- Test string decoding.
    s = [["Test\u00A7\\\""]]
    r = json.decode(s)
    self:assertTrue(r == "Test§\\\"")

    -- Test comment decoding.
    s = [[ /* A comment
            that spans
            a few lines
         */
         "test"
      ]]
    r = json.decode(s)
    self:assertTrue(r == "test", "comment decoding failed")
end

return suite
-- </nowiki>