Compare with Previous |
Blame |
View Log
import "Turbine.UI";
--------------------------------------------------------------------------
-- An iterator function to iterate over contents of a ControlList
function ControlListIter(controlList)
-- Validate input argument
local actualType = type(controlList);
local expectedType = "table";
if( expectedType ~= actualType ) then
error(string.format("Parameter expected to be [%s], not [%s]",
expectedType, actualType), 2 );
end
local idx = 0;
local maxIdx = controlList:GetCount();
return function()
idx = idx + 1;
if( idx <= maxIdx ) then
local control = controlList:Get(idx);
return control;
end
end
end
--------------------------------------------------------------------------
-- An iterator function to iterate over contents of a ControlList
function ForEachControlList(controlList, fnc)
for item in ControlListIter(controlList) do
fnc(item)
end
end
--------------------------------------------------------------------------
--------------------------------------------------------------------------
-- Unit Tests
function IteratorUnitTest()
Turbine.Shell.WriteLine("\n\n>>> IteratorUnitTest: START ...");
Turbine.Shell.WriteLine("LUA version: " .. _VERSION);
local parentCtl = Turbine.UI.Control();
for idx = 1,5 do
local childCtl = Turbine.UI.Control();
childCtl.name = "child " .. tostring(idx);
childCtl:SetParent(parentCtl);
end
-- Use ControlListIter()
Turbine.Shell.WriteLine("test ControlListIter");
for childCtl in ControlListIter(parentCtl:GetControls()) do
Turbine.Shell.WriteLine(string.format(" saw childCtl [%s]",childCtl.name));
childCtl.seen = (childCtl.seen or 0) + 1; -- set seen for later verification
end
-- Verify ControlListIter() hit all items
local ctrlList = parentCtl:GetControls();
for idx = 1, ctrlList:GetCount() do
local ctrl = ctrlList:Get(idx);
Turbine.Shell.WriteLine(string.format(" verify childCtl [%s] seen[%d]",
ctrl.name,
ctrl.seen));
assert( 1 == ctrl.seen, "Unexpected value for seen");
end
-- Use ForEachControlList()
Turbine.Shell.WriteLine("test ForEachControlList");
function printName(ctrl)
Turbine.Shell.WriteLine(string.format(" ctrl [%s]",ctrl.name));
end
ForEachControlList(ctrlList,printName);
--assert(false,"ABORT");
-- if you get this far - it passed!
Turbine.Shell.WriteLine("\n>>> IteratorUnitTest: -- PASS!\n.\n\n");
assert(false, "ABORT");
end
--IteratorUnitTest();
Compare with Previous |
Blame
All times are GMT -5. The time now is 07:13 PM.