189 lines
4.9 KiB
Lua
189 lines
4.9 KiB
Lua
require("tests")
|
|
--local json = require("json")
|
|
|
|
-- Test data
|
|
local test_data = {
|
|
name = "John Doe",
|
|
age = 30,
|
|
active = true,
|
|
scores = {85, 92, 78, 90},
|
|
address = {
|
|
street = "123 Main St",
|
|
city = "Springfield",
|
|
zip = "12345"
|
|
},
|
|
tags = {"developer", "golang", "lua"}
|
|
}
|
|
|
|
-- Test 1: Basic encoding
|
|
test("Basic JSON Encoding", function()
|
|
local encoded = json.encode(test_data)
|
|
assert_equal(type(encoded), "string")
|
|
assert(string.find(encoded, "John Doe"), "should contain name")
|
|
assert(string.find(encoded, "30"), "should contain age")
|
|
end)
|
|
|
|
-- Test 2: Basic decoding
|
|
test("Basic JSON Decoding", function()
|
|
local encoded = json.encode(test_data)
|
|
local decoded = json.decode(encoded)
|
|
assert_equal(decoded.name, "John Doe")
|
|
assert_equal(decoded.age, 30)
|
|
assert_equal(decoded.active, true)
|
|
assert_equal(#decoded.scores, 4)
|
|
end)
|
|
|
|
-- Test 3: Round-trip encoding/decoding
|
|
test("Round-trip Encoding/Decoding", function()
|
|
local encoded = json.encode(test_data)
|
|
local decoded = json.decode(encoded)
|
|
local re_encoded = json.encode(decoded)
|
|
local re_decoded = json.decode(re_encoded)
|
|
|
|
assert_equal(re_decoded.name, test_data.name)
|
|
assert_equal(re_decoded.address.city, test_data.address.city)
|
|
end)
|
|
|
|
-- Test 4: Pretty printing
|
|
test("Pretty Printing", function()
|
|
local pretty = json.pretty(test_data)
|
|
assert_equal(type(pretty), "string")
|
|
assert(string.find(pretty, "\n"), "pretty should contain newlines")
|
|
assert(string.find(pretty, " "), "pretty should contain indentation")
|
|
|
|
-- Should still be valid JSON
|
|
local decoded = json.decode(pretty)
|
|
assert_equal(decoded.name, test_data.name)
|
|
end)
|
|
|
|
-- Test 5: Object merging
|
|
test("Object Merging", function()
|
|
local obj1 = {a = 1, b = 2}
|
|
local obj2 = {b = 3, c = 4}
|
|
local obj3 = {d = 5}
|
|
|
|
local merged = json.merge(obj1, obj2, obj3)
|
|
assert_equal(merged.a, 1)
|
|
assert_equal(merged.b, 3) -- later wins
|
|
assert_equal(merged.c, 4)
|
|
assert_equal(merged.d, 5)
|
|
end)
|
|
|
|
-- Test 6: Data extraction
|
|
test("Data Extraction", function()
|
|
local name = json.extract(test_data, "name")
|
|
assert_equal(name, "John Doe")
|
|
|
|
local city = json.extract(test_data, "address.city")
|
|
assert_equal(city, "Springfield")
|
|
|
|
local first_score = json.extract(test_data, "scores.[0]")
|
|
assert_equal(first_score, 85)
|
|
|
|
local missing = json.extract(test_data, "nonexistent.field")
|
|
assert_equal(missing, nil)
|
|
end)
|
|
|
|
-- Test 7: Schema validation
|
|
test("Schema Validation", function()
|
|
local schema = {
|
|
type = "table",
|
|
properties = {
|
|
name = {type = "string"},
|
|
age = {type = "number"},
|
|
active = {type = "boolean"}
|
|
},
|
|
required = {name = true, age = true}
|
|
}
|
|
|
|
local valid, err = json.validate(test_data, schema)
|
|
assert_equal(valid, true)
|
|
|
|
local invalid_data = {name = "John", age = "not_a_number"}
|
|
local invalid, err2 = json.validate(invalid_data, schema)
|
|
assert_equal(invalid, false)
|
|
assert_equal(type(err2), "string")
|
|
end)
|
|
|
|
-- Test 8: File operations
|
|
test("File Save/Load", function()
|
|
local filename = "test_output.json"
|
|
|
|
-- Save to file
|
|
json.save_file(filename, test_data, true) -- pretty format
|
|
|
|
-- Check file exists
|
|
assert(file_exists(filename), "file should exist after save")
|
|
|
|
-- Load from file
|
|
local loaded = json.load_file(filename)
|
|
assert_equal(loaded.name, test_data.name)
|
|
assert_equal(loaded.address.zip, test_data.address.zip)
|
|
|
|
-- Clean up
|
|
os.remove(filename)
|
|
end)
|
|
|
|
-- Test 9: Error handling
|
|
test("Error Handling", function()
|
|
-- Invalid JSON should throw error
|
|
local success, err = pcall(json.decode, '{"invalid": json}')
|
|
assert_equal(success, false)
|
|
assert_equal(type(err), "string")
|
|
|
|
-- Missing file should throw error
|
|
local success2, err2 = pcall(json.load_file, "nonexistent_file.json")
|
|
assert_equal(success2, false)
|
|
assert_equal(type(err2), "string")
|
|
end)
|
|
|
|
-- Test 10: Edge cases
|
|
test("Edge Cases", function()
|
|
-- Empty objects
|
|
local empty_obj = {}
|
|
local encoded_empty = json.encode(empty_obj)
|
|
local decoded_empty = json.decode(encoded_empty)
|
|
assert_equal(type(decoded_empty), "table")
|
|
|
|
-- Special numbers
|
|
local special = {
|
|
zero = 0,
|
|
negative = -42,
|
|
decimal = 3.14159
|
|
}
|
|
local encoded_special = json.encode(special)
|
|
local decoded_special = json.decode(encoded_special)
|
|
assert_equal(decoded_special.zero, 0)
|
|
assert_equal(decoded_special.negative, -42)
|
|
assert_close(decoded_special.decimal, 3.14159, 0.00001)
|
|
end)
|
|
|
|
-- Performance test
|
|
test("Performance Test", function()
|
|
local large_data = {}
|
|
for i = 1, 1000 do
|
|
large_data[i] = {
|
|
id = i,
|
|
name = "User " .. i,
|
|
data = {x = i * 2, y = i * 3, z = i * 4}
|
|
}
|
|
end
|
|
|
|
local start = os.clock()
|
|
local encoded = json.encode(large_data)
|
|
local encode_time = os.clock() - start
|
|
|
|
start = os.clock()
|
|
local decoded = json.decode(encoded)
|
|
local decode_time = os.clock() - start
|
|
|
|
print(string.format(" Encoded 1000 objects in %.3f seconds", encode_time))
|
|
print(string.format(" Decoded 1000 objects in %.3f seconds", decode_time))
|
|
|
|
assert_equal(#decoded, 1000)
|
|
assert_equal(decoded[500].name, "User 500")
|
|
end)
|
|
|
|
summary()
|
|
test_exit()
|