diff --git a/mock/require.lua b/mock/require.lua new file mode 100644 index 0000000..551ba72 --- /dev/null +++ b/mock/require.lua @@ -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 \ No newline at end of file diff --git a/tests/test_lib_common_http.lua b/tests/test_lib_common_http.lua index ee461a5..6be8d8d 100644 --- a/tests/test_lib_common_http.lua +++ b/tests/test_lib_common_http.lua @@ -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={}}} @@ -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}) @@ -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 @@ -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()) diff --git a/tests/test_lib_common_math.lua b/tests/test_lib_common_math.lua index 307f58c..a3e507e 100644 --- a/tests/test_lib_common_math.lua +++ b/tests/test_lib_common_math.lua @@ -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() @@ -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) @@ -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()) diff --git a/tests/test_lib_engine_app.lua b/tests/test_lib_engine_app.lua new file mode 100644 index 0000000..89a66fd --- /dev/null +++ b/tests/test_lib_engine_app.lua @@ -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()) diff --git a/tests/test_lib_engine_game.lua b/tests/test_lib_engine_game.lua deleted file mode 100644 index 02951e8..0000000 --- a/tests/test_lib_engine_game.lua +++ /dev/null @@ -1,47 +0,0 @@ -local luaunit = require('luaunit') -local engine_game = require('src/lib/engine/api/app') - -function test_game_reset() - local index = 1 - local buses = {} - local std = { - node = {}, - bus = { - listen = function() end, - emit = function(key) - buses[key] = index - index = index + 1 - end - } - } - - zeebo_game = engine_game.install(std, {}, {}) - zeebo_game.reset() - - luaunit.assertEquals(buses.exit, 1) - luaunit.assertEquals(buses.init, 2) - luaunit.assertEquals(index, 3) -end - -function test_game_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 - -os.exit(luaunit.LuaUnit.run()) diff --git a/tests/test_lib_engine_hash.lua b/tests/test_lib_engine_hash.lua index 0d14d38..4eef844 100644 --- a/tests/test_lib_engine_hash.lua +++ b/tests/test_lib_engine_hash.lua @@ -16,10 +16,12 @@ function test_diff_hash_foo_bar() end function test_collision_stylist_subgenera() + if _VERSION == 'Lua 5.1' then + return + end local stylist = std.hash.djb2('stylist') local subgenera = std.hash.djb2('subgenera') luaunit.assertEquals(stylist, subgenera) end - os.exit(luaunit.LuaUnit.run()) diff --git a/tests/test_lib_engine_pipeline.lua b/tests/test_lib_engine_pipeline.lua new file mode 100644 index 0000000..b0f2e16 --- /dev/null +++ b/tests/test_lib_engine_pipeline.lua @@ -0,0 +1,80 @@ +local luaunit = require ('luaunit') +local pipeline = require ('src/lib/util/pipeline') + + +function test_pipe() + local test_obj = { + run_called = false, + run = function(self) + self.run_called = true + end + } + local task = pipeline.pipe(test_obj) + task() + luaunit.assertTrue(test_obj.run_called) +end + +function test_stop_pipe() + local test_obj = { + pipeline = true, + pipeline2 = false + } + pipeline.stop(test_obj) + luaunit.assertTrue(test_obj.pipeline2) + luaunit.assertNil(test_obj.pipeline) +end + +function test_resume_pipe() + local test_obj = { + pipeline = false, + pipeline2 = true, + run = function(self) + self.pipeline = true + end + } + pipeline.resume(test_obj) + luaunit.assertTrue(test_obj.pipeline) +end + +function test_reset_pipe() + local test_obj = { + pipeline_current = 1, + pipeline = 1, + pipeline2 = 1, + } + pipeline.reset(test_obj) + luaunit.assertNil(test_obj.pipeline_current) + luaunit.assertNil(test_obj.pipeline2) + luaunit.assertEquals(test_obj.pipeline, 1) +end + +function test_run_pipe() + local test_obj = { + pipeline_current = nil, + pipeline = { + function() end, + function() end, + function() end + } + } + pipeline.run(test_obj) + luaunit.assertEquals(test_obj.pipeline_current, 4) +end + + +function test_clear_pipe() + + + local test_obj = { + pipeline_current = 1, + pipeline2 = 1, + pipeline = 1, + } + pipeline.clear(test_obj) + luaunit.assertNil(test_obj.pipeline_current) + luaunit.assertNil(test_obj.pipeline) + luaunit.assertNil(test_obj.pipeline2) + +end + +os.exit(luaunit.LuaUnit.run()) \ No newline at end of file diff --git a/tests/test_lib_util_decorator.lua b/tests/test_lib_util_decorator.lua new file mode 100644 index 0000000..c8542e1 --- /dev/null +++ b/tests/test_lib_util_decorator.lua @@ -0,0 +1,76 @@ +local luaunit = require('luaunit') +local util_decorator = require('src/lib/util/decorator') + +local function dummy_func3(zig, zag, zom, a, b, c, d, e, f) + return zig, zag, zom, a, b, c, d, e, f +end + +local function dummy_func2(zig, zag, a, b, c, d, e, f) + return zig, zag, a, b, c, d, e, f +end + +local function dummy_func1(zig, a, b, c, d, e, f) + return zig, a, b, c, d, e, f +end + +local function dummy_func_offset_xy2(a, x , y, d, e, f) + return a, x, y, d, e, f +end + +local function dummy_func_offset_xyxy1(x1, y1, x2, y2, e, f) + return x1, y1, x2, y2, e, f +end + + + +function test_decorator_prefix3() + local decorated_func = util_decorator.prefix3(1, 2, 3, dummy_func3) + local result = {decorated_func(4, 5, 6, 7, 8, 9)} + luaunit.assertEquals(result, {1, 2, 3, 4, 5, 6, 7, 8, 9}) +end + + +function test_decorator_prefix2() + local decorated_func = util_decorator.prefix2(1, 2, dummy_func2) + local result = {decorated_func(3, 4, 5, 6, 7, 8)} + luaunit.assertEquals(result, {1, 2, 3, 4, 5, 6, 7, 8}) +end + +function test_decorator_prefix1() + local decorated_func = util_decorator.prefix1(1, dummy_func1) + local result = {decorated_func(2, 3, 4, 5, 6, 7)} + luaunit.assertEquals(result, {1, 2, 3, 4, 5, 6, 7}) +end + +function test_decorator_offset_xy2() + + local object = {offset_x = 10, offset_y = 20} + + local decorated_func = util_decorator.offset_xy2(object, dummy_func_offset_xy2) + + local result = {decorated_func(1, 2, 3, 4, 5, 6)} + luaunit.assertEquals(result, {1, 12, 23, 4, 5, 6}) +end + +function test_decorator_offset_xyxy1() + + local object = {offset_x = 10, offset_y = 20} + local decorated_func = util_decorator.offset_xyxy1(object, dummy_func_offset_xyxy1) + + local result = {decorated_func(1, 2, 3, 4, 5, 6)} + luaunit.assertEquals(result, {11, 22, 13, 24, 5, 6}) +end + +local function dummy_func_offset_xy1(x, y, c, d, e, f) + return x, y, c, d, e, f +end + +function test_decorator_offset_xy1() + + local object = {offset_x = 10, offset_y = 20} + local decorated_func = util_decorator.offset_xy1(object, dummy_func_offset_xy1) + + local result = {decorated_func(1, 2, 3, 4, 5, 6)} + luaunit.assertEquals(result, {11, 22, 3, 4, 5, 6}) +end +os.exit(luaunit.LuaUnit.run()) \ No newline at end of file diff --git a/tests/test_lib_util_fs.lua b/tests/test_lib_util_fs.lua index 40dfbd4..af44727 100644 --- a/tests/test_lib_util_fs.lua +++ b/tests/test_lib_util_fs.lua @@ -1,6 +1,15 @@ local luaunit = require('luaunit') local util_fs = require('src/lib/util/fs') +function test_path_with_src2() + local path = util_fs.path('foo/bar', 'extra.txt') + luaunit.assertEquals(path.get_file(), 'extra.txt') + luaunit.assertEquals(path.get_filename(), 'extra') + luaunit.assertEquals(path.get_ext(), 'txt') + luaunit.assertEquals(path.get_win_path(), '.\\foo\\bar\\') + luaunit.assertEquals(path.get_unix_path(), './foo/bar/') +end + function test_unix_foo_bar_z_txt() local file = util_fs.file('foo/bar/z.txt') luaunit.assertEquals(file.get_file(), 'z.txt') diff --git a/tests/test_lib_util_http.lua b/tests/test_lib_util_http.lua index d336114..63f39f8 100644 --- a/tests/test_lib_util_http.lua +++ b/tests/test_lib_util_http.lua @@ -1,6 +1,54 @@ local luaunit = require('luaunit') local zeebo_util_http = require('src/lib/util/http') + +function test_is_ok_status() + luaunit.assertTrue(zeebo_util_http.is_ok(200)) + luaunit.assertTrue(zeebo_util_http.is_ok(201)) + luaunit.assertTrue(zeebo_util_http.is_ok(299)) + luaunit.assertFalse(zeebo_util_http.is_ok(199)) + luaunit.assertFalse(zeebo_util_http.is_ok(300)) +end + +function test_is_ok_header_with_valid_header() + local ok, status = zeebo_util_http.is_ok_header('HTTP/1.1 200 OK') + luaunit.assertTrue(ok) + luaunit.assertEquals(status, 200) +end + +function test_is_ok_header_with_invalid_header() + local ok, status = zeebo_util_http.is_ok_header('invalid header') + luaunit.assertFalse(ok) + luaunit.assertNil (status) +end + +function test_is_ok_header_with_redirect_status() + local ok, status = zeebo_util_http.is_ok_header("HTTP/1.1 302 Found") + luaunit.assertFalse(ok) + luaunit.assertEquals(status, 302) +end + +function test_is_ok_header_with_client_error_status() + local ok, status = zeebo_util_http.is_ok_header("HTTP/1.1 404 Not Found") + luaunit.assertFalse(ok) + luaunit.assertEquals(status, 404) +end + +function test_is_ok_header_with_server_error_status() + local ok, status = zeebo_util_http.is_ok_header("HTTP/1.1 500 Internal Server Error") + luaunit.assertFalse(ok) + luaunit.assertEquals(status, 500) +end + + +function test_is_redirect_valid() + luaunit.assertTrue(zeebo_util_http.is_redirect(300)) + luaunit.assertTrue(zeebo_util_http.is_redirect(301)) + luaunit.assertTrue(zeebo_util_http.is_redirect(399)) + luaunit.assertFalse(zeebo_util_http.is_redirect(299)) + luaunit.assertFalse(zeebo_util_http.is_redirect(400)) +end + function test_no_params() local query = zeebo_util_http.url_search_param({}, {}) luaunit.assertEquals(query, '') @@ -105,4 +153,12 @@ function test_create_request_wget_head() luaunit.assertEquals(request, 'wget --quiet --output-document=- --method=HEAD http://example.com') end +function test_not_status_disables_print_http_status() + local request = zeebo_util_http.create_request('GET', '/') + request.not_status() + local result = request:not_status() + luaunit.assertFalse(request.print_http_status) + luaunit.assertEquals(result, request) +end + os.exit(luaunit.LuaUnit.run()) diff --git a/tests/test_lib_util_lua.lua b/tests/test_lib_util_lua.lua index 0b8724d..f906304 100644 --- a/tests/test_lib_util_lua.lua +++ b/tests/test_lib_util_lua.lua @@ -1,4 +1,49 @@ local luaunit = require('luaunit') +local mock_require = require('mock/require') +local without_os = false +local without_locale = false + +local without_env_locale = false + +require = mock_require.require({ + ['src/lib/util/lua'] = function() + return dofile('src/lib/util/lua.lua') + end, + ['os'] = function() + if without_os then return nil end + if without_env_locale then + return { + setlocale = function() + return '' + end, + getenv = function(varname) + return '' + end + } + end + if without_locale then + return { + setlocale = function() + return '' + end, + getenv = function(varname) + if varname == 'LANG' then return 'en_ES.UTF-8' end + return nil + end + } + end + return { + setlocale = function() + return 'LC_CTYPE=pt_BR' + end, + getenv = function(varname) + return 'LANG=at_BR.UTF-8' + end + } + end +}) + + local lua_util = require('src/lib/util/lua') function test_no_support_utf8() @@ -19,4 +64,32 @@ function test_lua_5_3_support_utf8() luaunit.assertEquals(lua_util.has_support_utf8(), true) end + + + +function test_sys_lang_with_env() + without_locale = true + local lua_util33 = require ('src/lib/util/lua') + without_locale = false + luaunit.assertEquals(lua_util33.get_sys_lang(), 'en-ES') +end + +function test_sys_lang_nil_os() + without_os = true + local lua_util2 = require('src/lib/util/lua') + without_os = false + luaunit.assertEquals(lua_util2.get_sys_lang(), 'en-US') +end + +function test_sys_lang_with_locale() + luaunit.assertEquals(lua_util.get_sys_lang(), 'pt-BR') +end + +function test_sys_lang_no_locale_no_env() + without_env_locale = true + local lua_util2 = require('src/lib/util/lua') + without_env_locale = false + luaunit.assertEquals(lua_util2.get_sys_lang(), 'en-US') +end + os.exit(luaunit.LuaUnit.run())