Skip to content

Commit

Permalink
ci: fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
bytemain committed Jan 18, 2025
1 parent 97159bb commit 7037799
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 26 deletions.
2 changes: 1 addition & 1 deletion internal/module/json/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func (j jsonValue) MarshalJSON() (data []byte, err error) {

switch key.Type() {
case lua.LTNil: // empty table
data = []byte(`null`)
data = []byte(`[]`)
case lua.LTNumber:
arr := make([]jsonValue, 0, converted.Len())
expectedKey := lua.LNumber(1)
Expand Down
83 changes: 58 additions & 25 deletions internal/module/json/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,48 +20,81 @@ import (
"encoding/json"
"testing"

"github.com/yuin/gopher-lua"
lua "github.com/yuin/gopher-lua"
)

func TestSimple(t *testing.T) {
const str = `
function printTable(t, indent)
indent = indent or 0
local strIndent = string.rep(" ", indent)
for key, value in pairs(t) do
local keyStr = tostring(key)
local valueStr = tostring(value)
if type(value) == "table" then
print(strIndent .. "[" .. keyStr .. "] =>")
printTable(value, indent + 1)
else
print(strIndent .. "[" .. keyStr .. "] => " .. valueStr)
end
end
end
local json = require("json")
assert(type(json) == "table")
assert(type(json.decode) == "function")
assert(type(json.encode) == "function")
assert(type(json) == "table", "json is not a table")
assert(type(json.decode) == "function", "json.decode is not a function")
assert(type(json.encode) == "function", "json.encode is not a function")
assert(json.encode(true) == "true")
assert(json.encode(1) == "1")
assert(json.encode(-10) == "-10")
assert(json.encode(nil) == "null")
assert(json.encode({}) == "[]")
assert(json.encode({1, 2, 3}) == "[1,2,3]")
assert(json.encode(true) == "true", "json.encode(true) is not 'true'")
assert(json.encode(1) == "1", "json.encode(1) is not '1'")
assert(json.encode(-10) == "-10", "json.encode(-10) is not '-10'")
assert(json.encode(nil) == "null", "json.encode(nil) is not 'null'")
assert(json.encode({}) == "[]", "json.encode({}) is not '[]'")
assert(json.encode({1, 2, 3}) == "[1,2,3]", "json.encode({1, 2, 3}) is not '[1,2,3]'")
local _, err = json.encode({1, 2, [10] = 3})
assert(string.find(err, "sparse array"))
assert(string.find(err, "sparse array"), "expected sparse array error, got: " .. (err or "nil"))
local _, err = json.encode({1, 2, 3, name = "Tim"})
assert(string.find(err, "mixed or invalid key types"))
assert(string.find(err, "mixed or invalid key types"), "expected mixed or invalid key types error, got: " .. (err or "nil"))
local _, err = json.encode({name = "Tim", [false] = 123})
assert(string.find(err, "mixed or invalid key types"))
assert(string.find(err, "mixed or invalid key types"), "expected mixed or invalid key types error, got: " .. (err or "nil"))
local obj = {"a",1,"b",2,"c",3}
local jsonStr = json.encode(obj)
local jsonObj = json.decode(jsonStr)
for i = 1, #obj do
assert(obj[i] == jsonObj[i])
assert(obj[i] == jsonObj[i], "obj[" .. i .. "] is not equal to jsonObj[" .. i .. "]")
end
local obj = {name="Tim",number=12345}
local jsonStr = json.encode(obj)
local jsonObj = json.decode(jsonStr)
assert(obj.name == jsonObj.name)
assert(obj.number == jsonObj.number)
assert(json.decode("null") == nil)
assert(json.decode(json.encode({person={name = "tim",}})).person.name == "tim")
assert(obj.name == jsonObj.name, "obj.name is not equal to jsonObj.name")
assert(obj.number == jsonObj.number, "obj.number is not equal to jsonObj.number")
local table1 = json.decode("{\"metadata\":[],\"spec\":{\"containers\":[{\"image\":\"centos:7\",\"name\":\"centos\",\"resources\":[]}]},\"status\":[]}")
printTable(table1)
--- assert table1.metadata is a empty table
assert(type(table1.metadata) == "table", "table1.metadata is not an empty table")
assert(next(table1.metadata) == nil, "table1.metadata is not an empty table")
--- assert table1.status is a empty table
assert(next(table1.status) == nil, "table1.status is not an empty table")
--- assert table1.spec.containers is a table
assert(type(table1.spec.containers) == "table", "table1.spec.containers is not a table")
--- assert table1.spec.containers[1] is a table
assert(type(table1.spec.containers[1]) == "table", "table1.spec.containers[1] is not a table")
--- assert table1.spec.containers[1].image is a string
assert(type(table1.spec.containers[1].image) == "string", "table1.spec.containers[1].image is not a string")
--- assert table1.spec.containers[1].resources is a table
assert(type(table1.spec.containers[1].resources) == "table", "table1.spec.containers[1].resources is not a table")
assert(json.decode("null") == nil, "json.decode('null') is not nil")
assert(json.decode(json.encode({person={name = "tim",}})).person.name == "tim", "json.decode(json.encode({person={name = 'tim',}})).person.name is not 'tim'")
local obj = {
abc = 123,
Expand All @@ -71,13 +104,13 @@ func TestSimple(t *testing.T) {
obj = obj,
}
obj.obj2 = obj2
assert(json.encode(obj) == nil)
assert(json.encode(obj) == nil, "json.encode(obj) is not nil")
local a = {}
for i=1, 5 do
a[i] = i
end
assert(json.encode(a) == "[1,2,3,4,5]")
assert(json.encode(a) == "[1,2,3,4,5]", "json.encode(a) is not '[1,2,3,4,5]'")
`
s := lua.NewState()
defer s.Close()
Expand All @@ -91,9 +124,9 @@ func TestSimple(t *testing.T) {
func TestCustomRequire(t *testing.T) {
const str = `
local j = require("JSON")
assert(type(j) == "table")
assert(type(j.decode) == "function")
assert(type(j.encode) == "function")
assert(type(j) == "table", "j is not a table")
assert(type(j.decode) == "function", "j.decode is not a function")
assert(type(j.encode) == "function", "j.encode is not a function")
`
s := lua.NewState()
defer s.Close()
Expand Down

0 comments on commit 7037799

Please sign in to comment.