From 245cd57d24c2545de3eba3bc0673991f419fb23b Mon Sep 17 00:00:00 2001 From: Celtic Minstrel Date: Sun, 11 Apr 2021 01:33:55 -0400 Subject: [PATCH] Unit tests for each way of assigning an action to an event handler --- data/test/scenarios/events-test_actions.cfg | 99 +++++++++++++++++++++ wml_test_schedule | 5 ++ 2 files changed, 104 insertions(+) create mode 100644 data/test/scenarios/events-test_actions.cfg diff --git a/data/test/scenarios/events-test_actions.cfg b/data/test/scenarios/events-test_actions.cfg new file mode 100644 index 00000000000..36a66a8ccee --- /dev/null +++ b/data/test/scenarios/events-test_actions.cfg @@ -0,0 +1,99 @@ + +# This is kinda a duplicate of a basic unit test system sanity test, but... it seems to fit here as well... +{GENERIC_UNIT_TEST "event_test_action_wml" ( + [event] + name=start + {SUCCEED} + [/event] +)} + +# Test that dynamically adding an event from Lua works +# There's two different ways of calling game_events.add, so test both +{GENERIC_UNIT_TEST "event_test_lua" ( + [event] + name=preload + [lua] + code=<< + wesnoth.game_events.add('new turn', function() + unit_test.succeed() + end) + >> + [/lua] + [/event] +)} +{GENERIC_UNIT_TEST "event_test_lua_advanced" ( + [event] + name=preload + [lua] + code=<< + wesnoth.game_events.add{ + name = 'new turn', + action = function() + unit_test.succeed() + end + } + >> + [/lua] + [/event] +)} + +# Test that first_time_only works correctly in Lua events +{GENERIC_UNIT_TEST "event_test_lua_repeat" ( + [event] + name=preload + [lua] + code=<< + wml.variables.only_once = 0 + wml.variables.should_repeat = 0 + wesnoth.game_events.add{ + name = 'new turn', + first_time_only = true, + action = function() + wml.variables.only_once = wml.variables.only_once + 1 + end + } + wesnoth.game_events.add{ + name = 'new turn', + first_time_only = false, + action = function() + wml.variables.should_repeat = wml.variables.should_repeat + 1 + end + } + >> + [/lua] + [/event] + [event] + name=side 1 turn 1 + [end_turn][/end_turn] + [/event] + [event] + name=side 2 turn 1 + [end_turn][/end_turn] + [/event] + [event] + name=side 1 turn 2 + [end_turn][/end_turn] + [/event] + [event] + name=side 2 turn 2 + {ASSERT ({VARIABLE_CONDITIONAL only_once equals 1})} + {ASSERT ({VARIABLE_CONDITIONAL should_repeat equals 2})} + {SUCCEED} + [/event] +)} + +# Verify that a warning is produced if adding an event that would break saved games +{GENERIC_UNIT_TEST "event_test_lua_break_save" ( + [event] + name=start + [lua] + code=<< + wesnoth.game_events.add{ + name = 'new turn', + action = function() end + } + unit_test.succeed() + >> + [/lua] + [/event] +)} diff --git a/wml_test_schedule b/wml_test_schedule index 11b916c17b6..1b5b2c483c3 100644 --- a/wml_test_schedule +++ b/wml_test_schedule @@ -139,6 +139,11 @@ 0 test_ability_id_active 0 test_ability_id_not_active 0 event_test_filter_attack +0 event_test_action_wml +0 event_test_lua +0 event_test_lua_advanced +0 event_test_lua_repeat +9 event_test_lua_break_save 0 test_lua_name 0 filter_vision 0 test_shroud_init