Module:Json/testcases

local existsmod = require("Dev:Existsmod").module_exists local ScribuntoUnit = require("Module:ScribuntoUnit") local suite = ScribuntoUnit:new

function suite:test_works self:assertTrue(existsmod("Module:If", true, false)) self:assertFalse(existsmod("Module:!If", true, false)) end

function suite:test_CompareData local json = require('Dev:json') if nil then -- Test encodeString s = "\" r = json._encodeString(s) self:assertTrue(r=='\\"\\\\\\"\\n') s = """\\\" r = json._encodeString(s) self:assertTrue(r==\"\"\"\\\\\\\") end -- Test encode for basic strings (complicated strings) s = Hello, Lua! r = json.encode(s) self:assertTrue(r=='"Hello, Lua!"') s = "\" r = json.encode(s) self:assertTrue(r=='\"\\"\\\\\\"\\n\"') s = """\\\" r = json.encode(s) self:assertTrue(r=="\"\"\"\\\\\\\"") -- Test encode for numeric values s = 23 r = json.encode(s) self:assertTrue(r=='23') s=48.123 r = json.encode(s) self:assertTrue(r=='48.123') -- Test encode for boolean values self:assertTrue(json.encode(true)=='true') self:assertTrue(json.encode(false)=='false') self:assertTrue(json.encode(nil)=='null') -- Test encode for arrays s = {1,2,3} r = json.encode(s) self:assertTrue(r=="[1,2,3]") s = {9,9,9} r = json.encode(s) self:assertTrue(r=="[9,9,9]") -- Complex array test s = { 2, 'joe', false, nil, 'hi' } r = json.encode(s) self:assertTrue(r=='[2,"joe",false,null,"hi"]') -- Test encode for tables s = {Name='Craig',email='craig@lateral.co.za',age=35} r = json.encode(s) -- NB: This test can fail because of order: need to test further once -- decoding is supported. -- self:assertTrue(r=={"age":35,"Name":"Craig","email":"craig@lateral.co.za"}) -- Test decode_scanWhitespace if nil then s = "  \n   \r   \t   " e = json._decode_scanWhitespace(s,1) self:assertTrue(e==string.len(s)+1) s = " \n\r\t4" self:assertTrue(json._decode_scanWhitespace(s,1)==5) -- Test decode_scanString s = "Test" r,e = json._decode_scanString(s,1) self:assertTrue(r=='Test' and e==7) s = "This\nis a \"test"   r = json._decode_scanString(s,1)    self:assertTrue(r=="This\nis a \"test") s = "Test\u00A7\\" r,e = json._decode_scanString(s,1) self:assertTrue(r=="Test\xC2\xA7\\" and e==9) print(s,r) -- Test decode_scanNumber s = 354 r,e = json._decode_scanNumber(s,1) self:assertTrue(r==354 and e==4) s = 4565.23 AND OTHER THINGS r,e = json._decode_scanNumber(s,2) self:assertTrue(r==4565.23 and e==9) s = -23.22 and r,e = json._decode_scanNumber(s,2) self:assertTrue(r==-23.22 and e==8) -- Test decode_scanConstant s = "true" r,e = json._decode_scanConstant(s,1) self:assertTrue(r==true and e==5) s = " false  " r,e = json._decode_scanConstant(s,3) self:assertTrue(r==false and e==8) s = "1null6" r,e = json._decode_scanConstant(s,2) self:assertTrue(r==nil and e==6) -- Test decode_scanArray s = "[1,2,3]" r,e = json._decode_scanArray(s,1) self:assertTrue(compareData(r,{1,2,3})) s = [ 1,   3  ,5 , "Fred" , true, false, null, -23 ] r,e = json._decode_scanArray(s,1) self:assertTrue(compareData(r, {1,3,5,'Fred',true,false,nil,-23} ) ) s = "[3,5,null,7,9]" r,e = json._decode_scanArray(s,1) self:assertTrue(compareData(r, {3,5,nil,7,9})) s = "[3,5,null,7,9,null,null]" r,e = json._decode_scanArray(s,1) self:assertTrue(compareData(r, {3,5,nil,7,9,nil,nil})) end s = "Test\u00A7\\\""   r,e = json.decode(s)    self:assertTrue(r=="Test\xC2\xA7\\\"", r)    print(s,r) -- Test decode_scanObject s = {"one":1, "two":2, "three":"three", "four":true} r,e = json.decode(s) for x,y in pairs(r) do   print(x,y) end self:assertTrue(compareData(r,{one=1,two=2,three='three',four=true})) s = { "one" : { "first":1,"second":2,"third":3}, "two":2, "three":false } r,e = json.decode(s) self:assertTrue(compareData(r, {one={first=1,second=2,third=3},two=2,three=false})) s = { "primes" : [2,3,5,7,9], "user":{"name":"craig","age":35,"programs_lua":true},    "lua_is_great":true } r,e = json.decode(s) self:assertTrue(compareData(r, {primes={2,3,5,7,9},user={name='craig',age=35,programs_lua=true},lua_is_great=true})) -- 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}' ) -- Test comment decoding s = /* A comment            that spans            a few lines         */         "test" r,e = json.decode(s) self:assertTrue(r=='test',"Comment decoding failed") end

return suite