Files

132 lines
3.6 KiB
Lua

-- 用假 cc 环境跑一遍 gm.lua,检查运行期(非语法)错误
local objects = {}
local clicks = {}
local function newobj(kind, props)
local o = props or {}
o._kind = kind or "node"
o._children = {}
o._parent = nil
o._visible = true
o._string = nil
return setmetatable(o, {
__index = function(t, k)
return function(self, ...)
if k == "addChild" then
local c = ...
c._parent = self
table.insert(self._children, c)
return nil
elseif k == "getParent" then
return self._parent
elseif k == "addTouchEventListener" then
table.insert(clicks, { node = self, cb = ... })
return nil
elseif k == "getVisible" then
return self._visible
elseif k == "setVisible" then
self._visible = (...) and true or false
return nil
elseif k == "setString" then
self._string = ...
return nil
elseif k == "setWidth" then
self._width = ...
return nil
elseif k == "getContentSize" then
return { width = 100, height = 50 }
elseif k == "retain" or k == "release" then
return nil
end
return self
end
end,
})
end
local scene = nil
local sched_cb = nil
local director = newobj("director", {
getWinSize = function() return { width = 1280, height = 720 } end,
getRunningScene = function() return scene end,
getScheduler = function()
return newobj("sched", {
scheduleScriptFunc = function(self, cb, interval, paused)
sched_cb = cb
return 1
end,
})
end,
})
_G.cc = {
Director = {
getInstance = function() return director end,
},
Node = { create = function() return newobj("node") end },
Label = {
createWithSystemFont = function(txt, font, size)
local o = newobj("label")
o._string = txt
return o
end,
},
size = function(w, h) return { width = w, height = h } end,
p = function(x, y) return { x = x, y = y } end,
c3b = function(r, g, b) return { r = r, g = g, b = b } end,
c4b = function(r, g, b, a) return { r = r, g = g, b = b, a = a } end,
FileUtils = {
getInstance = function()
return {
writeStringToFile = function(self, txt, path)
print("[FileUtils.writeStringToFile] " .. tostring(path) .. " (" .. #txt .. " bytes)")
return true
end,
getStringFromFile = function(self, path) return "" end,
}
end,
},
}
_G.ccui = {
Layout = { create = function() return newobj("layout") end },
LayoutBackGroundColorType = { solid = 1 },
}
-- 假 io / os.date:写到临时文件
local real_io_open = io.open
local tmp_log = "/tmp/gm_sim.log"
os.remove(tmp_log)
io.open = function(path, mode)
if path and path:find("gm%.log") then path = tmp_log end
return real_io_open(path, mode)
end
-- 控制 _G 里没有游戏数据也没关系
package.loaded = { ["app.common.GameInfo"] = { gold = 123, diamond = 45 } }
local chunk = assert(loadfile("src/gm.lua"))
chunk()
print("== 第一次 tick(无场景)==")
if sched_cb then
local ok, err = pcall(sched_cb, 0.25)
print("tick1 ok=", ok, err)
end
print("== 给个场景再 tick ==")
scene = newobj("scene")
for i = 1, 3 do
local ok, err = pcall(sched_cb, 0.25)
print("tick ok=", ok, err)
end
print("== 模拟点击所有按钮 ==")
for i, c in ipairs(clicks) do
local ok, err = pcall(c.cb, c.node, 2)
print(string.format("click #%d %s ok=%s err=%s", i, tostring(c.node._kind), tostring(ok), tostring(err)))
end
print("== 目录里生成的日志 ==")
local f = io.open(tmp_log, "r")
if f then print(f:read("*a")); f:close() end