Path: blob/master/Assets/Lua/UnitTests/Joypad_RoundTrip.lua
2 views
console.clear()1console.writeline("Unit test for joypad.get and joypad.set")2console.writeline("Core Required: NES (or any multi-player core with U,D,L,R,select,start,A,B as buttons)")3console.writeline("")45local test_event6local test_function7local test_frame = 089function test_1()10-- clear previous input11if test_frame < 4 then12return13end1415local test_case = "test_1: set/get round trip"16local test_result = false1718-- set expected input19joypad.set({ ["P1 A"] = true, ["P1 B"] = true, ["P1 Select"] = true })2021-- get actual input22local t = joypad.get()2324-- assert_equal25if t["P1 A"] and t["P1 B"] and t["P1 Select"] then26test_result = true27end2829-- finish30console.writeline(string.format("%s (%s): %s", test_case, test_event, test_result and "OK" or "FAILED"))31-- if not test_result then32-- console.writeline("Returned Value:")33-- console.writeline(t)34-- end3536return next_test()37end3839local test_2_first = true40local test_2_start_pressed = false41function test_2()42if test_frame < 4 then43return44end4546local test_case = "test_2: get/set round trip"47if test_2_first then48test_2_first = false4950console.writeline(test_case)51console.writeline("Does joypad work normally?")52console.writeline("Press 'P1 Start' if it works good.")53end5455if test_frame == 4 then56console.writeline(string.format("%s: running...", test_event))57end5859-- round trip (no changes)60local t = joypad.get()61joypad.set(t)6263if t["P1 Start"] then64test_2_start_pressed = true65else66if test_2_start_pressed then67test_2_start_pressed = false68next_test()69end70end71end7273local test_3_first = true74function test_3()75local test_case = "test_3: test if joypad.get() == movie.getinput(now)?"7677if test_3_first then78test_3_first = false79console.writeline(test_case)80end8182console.writeline(string.format("%s: test code is not available yet: SKIPPED", test_event))83next_test()84end8586local tests = { test_1, test_2, test_3 }87local events = { "event.onframestart", "event.onframeend", "emu.frameadvance" }8889local test_event_index = 190local test_function_index = 191test_event = events[1]92test_function = tests[1]9394function test_reset()95test_frame = 096end97test_reset()9899function next_test()100test_event_index = test_event_index + 1101if test_event_index > #events then102console.writeline("")103test_event_index = 1104test_function_index = test_function_index + 1105if test_function_index > #tests then106console.writeline("Test Finished.")107console.writeline("")108error("Done.")109end110end111test_event = events[test_event_index]112test_function = tests[test_function_index]113test_reset()114end115116event.onframestart(function()117if test_event == "event.onframestart" then118test_function()119test_frame = test_frame + 1120end121end)122123event.onframeend(function()124if test_event == "event.onframeend" then125test_function()126test_frame = test_frame + 1127end128end)129130while true do131if test_event == "emu.frameadvance" then132test_function()133test_frame = test_frame + 1134end135emu.frameadvance()136end137138139