Add a unit test for scoped arrays

The existing test for scoped vars is both copied and renamed.

(cherry picked from commit fba0970509
with files moved back to the old directory)
This commit is contained in:
Steve Cotton 2023-01-23 17:52:49 +01:00 committed by Steve Cotton
parent 24b03ad5de
commit 18040c1ad7
4 changed files with 101 additions and 28 deletions

View file

@ -0,0 +1,54 @@
# wmllint: no translatables
#####
# API(s) being tested: wml_utils.scoped_var
##
# Actions:
# Create a WML container array called test_var.
# Using Lua, run a block of WML with a scoped var shadowing test_var.
# Check that the shadow is initialized to an empty array.
# Check that the reading non-existent elements of the shadow does not access the original.
# Write to the shadow.
# Check that the shadow contains the new values.
# Exit the scope of the temporary shadow.
##
# Expected end state:
# Changes to the shadow did not change the original.
#####
{GENERIC_UNIT_TEST "test_scoped_array" (
[event]
name = prestart
{VARIABLE test_var[0].value 2}
{VARIABLE test_var[1].value 3}
{ASSERT {VARIABLE_CONDITIONAL test_var[0].value equals 2}}
{ASSERT {VARIABLE_CONDITIONAL test_var[1].value equals 3}}
[lua]
code = <<
local wml_utils = wesnoth.require "wml-utils"
local shadow_var <close> = wml_utils.scoped_var("test_var")
-- This runs the contents of [args] as WML actions
wml_utils.handle_event_commands(...)
>>
[args]
{ASSERT {VARIABLE_CONDITIONAL test_var.length equals 0}}
{ASSERT {VARIABLE_CONDITIONAL test_var[0].value equals ""}}
{ASSERT {VARIABLE_CONDITIONAL test_var[1].value equals ""}}
{VARIABLE test_var[0].value 6}
{VARIABLE test_var[1].value 7}
{ASSERT {VARIABLE_CONDITIONAL test_var[0].value equals 6}}
{ASSERT {VARIABLE_CONDITIONAL test_var[1].value equals 7}}
[/args]
[/lua]
{ASSERT {VARIABLE_CONDITIONAL test_var[0].value equals 2}}
{ASSERT {VARIABLE_CONDITIONAL test_var[1].value equals 3}}
{SUCCEED}
[/event]
)}

View file

@ -0,0 +1,45 @@
# wmllint: no translatables
#####
# API(s) being tested: wml_utils.scoped_var
##
# Actions:
# Create a WML scalar called test_var.
# Using Lua, run a block of WML with a scoped var shadowing test_var.
# Check that the shadowing variable is either uninitialised or initialised to "".
# Write to the shadow.
# Check that the shadow contains the new value.
# Exit the scope of the temporary shadow.
##
# Expected end state:
# Changes to the shadow did not change the original.
#####
{GENERIC_UNIT_TEST "test_scoped_scalar" (
[event]
name = prestart
{VARIABLE test_var 1}
{ASSERT {VARIABLE_CONDITIONAL test_var equals 1}}
[lua]
code = <<
local wml_utils = wesnoth.require "wml-utils"
local shadow_var <close> = wml_utils.scoped_var("test_var")
-- This runs the contents of [args] as WML actions
wml_utils.handle_event_commands(...)
>>
[args]
{ASSERT {VARIABLE_CONDITIONAL test_var equals ""}}
{VARIABLE test_var 5}
{ASSERT {VARIABLE_CONDITIONAL test_var equals 5}}
[/args]
[/lua]
{ASSERT {VARIABLE_CONDITIONAL test_var equals 1}}
{SUCCEED}
[/event]
)}

View file

@ -1,27 +0,0 @@
{GENERIC_UNIT_TEST "test_scoped_vars" (
[event]
name = prestart
{VARIABLE test_var 1}
#[inspect][/inspect]
{ASSERT {VARIABLE_CONDITIONAL test_var equals 1}}
[lua]
code = <<
local wml_utils = wesnoth.require "wml-utils"
local var <close> = wml_utils.scoped_var("test_var")
-- This runs the contents of [args] as WML actions
wml_utils.handle_event_commands(...)
>>
[args]
#[inspect][/inspect]
{ASSERT {VARIABLE_CONDITIONAL test_var equals ""}}
{VARIABLE test_var 5}
#[inspect][/inspect]
{ASSERT {VARIABLE_CONDITIONAL test_var equals 5}}
[/args]
[/lua]
#[inspect][/inspect]
{ASSERT {VARIABLE_CONDITIONAL test_var equals 1}}
{SUCCEED}
[/event]
)}

View file

@ -194,7 +194,8 @@
0 test_wml_actions
0 test_wml_conditionals
0 lua_wml_tagnames
0 test_scoped_vars
0 test_scoped_array
0 test_scoped_scalar
0 as_text
0 test_lua_version_api
#