extend("test")
extend("json")
extend("array")
extend("string")
describe("json plugin json", () => {
it("should decode a simple JSON string into a keyed array", () => {
data = json:decode('{ "name": "Alice", "age": 30 }')
# The result => [ name:"Alice", age:30 ]
# Check it has the correct keys
has_key(data, "name")
has_key(data, "age")
prop_equals(data, "name", "Alice")
prop_equals(data, "age", 30)
})
it("should decode an array from a single string of JSON", () => {
out = json:decode('[1, 2, 3]')
# => int_array [1,2,3]
expect_equal(type(out), "int_array")
expect_equal(out, [1,2,3])
})
it("should decode partial usage => pass the string later", () => {
partialDec = json:decode()
res = partialDec('{ "ok": true }')
prop_equals(res, "ok", true)
})
it("should encode data to JSON => default (compact) or pretty", () => {
jsCompact = json:encode([ pretty:false ], [ name:"Bob", items:[1,2,3] ])
parsedCompact = json:decode(jsCompact)
expect_equal(type(parsedCompact), "keyed_array" )
prop_equals(parsedCompact, "name", "Bob")
prop_equals(parsedCompact, "items", [1,2,3])
})
it("should encode data with pretty printing", () => {
jsPretty = json:encode([ pretty:true], [ alpha:true, beta:false ])
parsed = json:decode(jsPretty)
has_key(parsed, "alpha")
has_key(parsed, "beta")
})
})