Skip to content

Commit

Permalink
test: Added 100% coverage for existing tests
Browse files Browse the repository at this point in the history
Co-authored-by: Nicolas Pires <[email protected]>
  • Loading branch information
Vicente Pereira and NicolasPires777 committed Nov 27, 2024
1 parent d184398 commit 740a42e
Show file tree
Hide file tree
Showing 11 changed files with 595 additions and 51 deletions.
16 changes: 16 additions & 0 deletions mock/require.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
local old_require = require

local function mock_require(decorators)
return function(src)
if decorators[src] then
return decorators[src]()
end
return old_require(src)
end
end

local P = {
require=mock_require
}

return P
93 changes: 91 additions & 2 deletions tests/test_lib_common_http.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
local luaunit = require('luaunit')
local zeebo_http = require('src/lib/engine/api/http')
local mock_http = require('mock/protocol_http')

local zeebo_pipeline = require('src/lib/util/pipeline')
local std = {node={emit=function()end}}
local engine = {current={callbacks={}}}

Expand All @@ -25,7 +25,11 @@ local http_handler = mock_http.requests({
body = function(http_object)
return string.lower(http_object.body_content)
end
}
},['example.com/status500'] = {
ok = false,
status = 500
}

})

zeebo_http.install(std, engine, {handler=http_handler})
Expand All @@ -44,6 +48,20 @@ function test_http_head_200()
luaunit.assertEquals(status, 200)
end

function test_http_get_500()
local status = 0
local ok = nil

std.http.head('example.com/status500')
:success(function (std) ok, status = true, std.http.status end)
:failed(function (std) ok, status = false, std.http.status end)
:error(function() ok, status = false, -1 end)
:run()

luaunit.assertEquals(status, 500)
luaunit.assertEquals(ok, false)
end

function test_http_head_404()
local status = 0
local ok = nil
Expand Down Expand Up @@ -112,4 +130,75 @@ function test_http_post_body()
luaunit.assertEquals(body, 'foobarz')
end

function test_http_fast()
local request = std.http.get('example.com/status200')
request:fast()
luaunit.assertEquals(request.speed, '_fast')
end

function test_http_header()
local request = std.http.get('example.com/status200')
request:header('Content-Type', 'application/json')
luaunit.assertEquals(request.header_dict['Content-Type'], 'application/json')
end

function test_http_promise()
local stop_called = false
zeebo_pipeline.stop = function(self) stop_called = true end
local request = std.http.get('example.com/status200')
request:promise()
luaunit.assertTrue(stop_called)
end

function test_http_resolve()
local resume_called = false
zeebo_pipeline.resume = function(self) resume_called = true end
local request = std.http.get('example.com/status200')
request:resolve()
luaunit.assertTrue(resume_called)
end


function test_http_set()
local mock_std = { http = {}}

local set_function = function (key, value)
mock_std.http[key] = value
end
set_function('timeout', 5000)
luaunit.assertEquals(mock_std.http.timeout, 5000)
end

function test_protocol_with_install()
local install_called = false
local protocol = {
install = function(std, engine)
install_called = true
luaunit.assertNotNil(std)
luaunit.assertNotNil(engine)
end
}
local std = {}
local engine = {}

local http_module = require('src/lib/engine/api/http')
http_module.install(std, engine, protocol)
luaunit.assertTrue(install_called)
end

function test_protocol_without_install()
local protocol = {}
local std = {}
local engine = {}

if protocol.install then
protocol.install(std, engine)
end
luaunit.assertTrue(true)

end




os.exit(luaunit.LuaUnit.run())
33 changes: 32 additions & 1 deletion tests/test_lib_common_math.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
local luaunit = require('luaunit')
local engine_math = require('src/lib/engine/api/math')

local zeebo_math = engine_math.install()

function test_clamp()
Expand Down Expand Up @@ -71,6 +70,26 @@ function test_max()
luaunit.assertEquals(zeebo_math.max({1, 2, 3, 4, 5}), 5)
end


function test_install()
local std ={}
local math_lib = engine_math.install(std)
luaunit.assertIsFunction(math_lib.abs)
luaunit.assertIsFunction(math_lib.clamp)
luaunit.assertIsFunction(math_lib.clamp2)
luaunit.assertIsFunction(math_lib.cycle)
luaunit.assertIsFunction(math_lib.dir)
luaunit.assertIsFunction(math_lib.dis)
luaunit.assertIsFunction(math_lib.dis2)
luaunit.assertIsFunction(math_lib.lerp)
luaunit.assertIsFunction(math_lib.map)
luaunit.assertIsFunction(math_lib.max)
luaunit.assertIsFunction(math_lib.min)
luaunit.assertIsFunction(math_lib.saw)
luaunit.assertEquals(math_lib, std.math)
end


function test_install_math_clib()
local math_clib = engine_math.clib.install()
luaunit.assertIsFunction(math_clib.pow)
Expand All @@ -79,6 +98,18 @@ end
function test_install_math_clib_random()
local math_clib = engine_math.clib_random.install()
luaunit.assertIsFunction(math_clib.random)

local result = math_clib.random(1, 10)
luaunit.assertTrue(result >= 1 and result <= 10)

-- local result2 = math_clib.random(5)
-- luaunit.assertTrue(result >= 1 and result <= 5)

-- local result3 = math_clib.random()
-- luaunit.assertTrue(result >= 1)

-- local result4 = math_clib.random(nil, nil)
-- luaunit.assertTrue(result >= 1)
end

os.exit(luaunit.LuaUnit.run())
177 changes: 177 additions & 0 deletions tests/test_lib_engine_app.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
local luaunit = require('luaunit')
local engine_game = require('src/lib/engine/api/app')


function test_app_reset()
local index = 1
local buses = {}
local std = {
node = {},
bus = {
listen = function() end,
emit = function(key)
buses[key] = index
index = index + 1
end
}
}

local config = {
quit = function()
print("Config quit foi chamado!")
end
}

zeebo_game = engine_game.install(std, {}, {})
zeebo_game.reset()

luaunit.assertEquals(buses.exit, 1)
luaunit.assertEquals(buses.init, 2)
luaunit.assertEquals(index, 3)
std.bus:emit('post_quit')
end

function test_app_reset_no_node()
local exit_called = false
local init_called = false

local std = {
bus = {
listen = function() end,
emit = function() end
}
}


local engine = {
root = {
data = {},
callbacks = {}
}
}

engine.root.callbacks.exit = function(std_param, data_param)
luaunit.assertEquals(std_param, std)
luaunit.assertEquals(data_param, engine.root.data)
exit_called = true
end

engine.root.callbacks.init = function(std_param, data_param)
luaunit.assertEquals(std_param, std)
luaunit.assertEquals(data_param, engine.root.data)
init_called = true
end
zeebo_game = engine_game.install(std, engine, {})
zeebo_game.reset()

luaunit.assertTrue(exit_called)
luaunit.assertTrue(init_called)
end

function test_app_exit()
local index = 1
local buses = {}
local std = {
bus = {
listen = function() end,
emit = function(key)
buses[key] = index
index = index + 1
end
}
}

zeebo_game = engine_game.install(std, {}, {})
zeebo_game.exit()

luaunit.assertEquals(buses.exit, 1)
luaunit.assertEquals(buses.quit, 2)
luaunit.assertEquals(index, 3)
end

function test_app_title()

local function mock_set_title(window_name)
luaunit.assertEquals(window_name, "Teste")
end

local std = {
bus = {
listen = function() end,
emit = function() end
},
app ={
title = nil
}
}
local config = {
set_title = mock_set_title
}

local zeebo_game = engine_game.install(std, {}, config)

zeebo_game.title("Teste")

end

function test_app_install()
local exit_called = false
local init_called = false
local quit_called = false

local std = {
bus = {
listen = function(event, callback)
if event == 'post_quit' then
callback()
end
end,
emit = function() end
}
}

local engine = {
root = {
data = {},
callbacks = {}
}
}

engine.root.callbacks.exit = function(std_param, data_param)
luaunit.assertEquals(std_param, std)
luaunit.assertEquals(data_param, engine.root.data)
exit_called = true
end

engine.root.callbacks.init = function(std_param, data_param)
luaunit.assertEquals(std_param, std)
luaunit.assertEquals(data_param, engine.root.data)
init_called = true
end

local config = {
set_title = function(window_name)
luaunit.assertEquals(window_name, "Test Window")
end,
fps = 60,
quit = function()
quit_called = true
end
}

local zeebo_game = engine_game.install(std, engine, config)

luaunit.assertNotNil(zeebo_game.title)
luaunit.assertNotNil(zeebo_game.exit)
luaunit.assertNotNil(zeebo_game.reset)
luaunit.assertEquals(zeebo_game.get_fps, 60)

zeebo_game.title("Test Window")

zeebo_game.reset()

luaunit.assertTrue(exit_called)
luaunit.assertTrue(init_called)
luaunit.assertTrue(quit_called)
end
os.exit(luaunit.LuaUnit.run())
Loading

0 comments on commit 740a42e

Please sign in to comment.