Unit tests for ipairs and pairs, as well as changing how they handle null. ipairs now breaks at first nil value, pairs skips it
This commit is contained in:
parent
13ac6f2754
commit
194e7236c4
|
@ -46,10 +46,10 @@ namespace Upsilon.BaseTypes
|
|||
while (baseEnumerator.MoveNext())
|
||||
{
|
||||
var key = baseEnumerator.Current;
|
||||
if (key == null)
|
||||
if (key.Type == Type.Nil)
|
||||
break;
|
||||
var value = BaseIterator.GetValueFromIndex(key);
|
||||
if (value == null)
|
||||
if (value.Type == Type.Nil)
|
||||
break;
|
||||
yield return new SimpleScriptTable(new List<ScriptType>(){key, value});
|
||||
}
|
||||
|
@ -71,7 +71,8 @@ namespace Upsilon.BaseTypes
|
|||
{
|
||||
var key = baseEnumerator.Current;
|
||||
var value = BaseIterator.GetValueFromIndex(key);
|
||||
yield return new SimpleScriptTable(new List<ScriptType>(){key, value});
|
||||
if (value.Type != Type.Nil)
|
||||
yield return new SimpleScriptTable(new List<ScriptType>(){key, value});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -29,7 +29,7 @@ namespace UpsilonTests.StandardLibraryTests
|
|||
public void IPairsTest()
|
||||
{
|
||||
const string input = @"
|
||||
arr = {100, 56, 28}
|
||||
arr = {100, 56, 28, nil, 100}
|
||||
value = 0
|
||||
for key, val in ipairs(arr) do
|
||||
value = value + val
|
||||
|
@ -56,5 +56,22 @@ return tonumber(""100"")
|
|||
Assert.Equal(100, result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PairsTest()
|
||||
{
|
||||
const string input = @"
|
||||
arr = {100, 56, 28, nil, 100}
|
||||
value = 0
|
||||
for key, val in pairs(arr) do
|
||||
value = value + val
|
||||
end
|
||||
return value
|
||||
";
|
||||
var script = new Script(input, BoundScope, StaticScope);
|
||||
Assert.Empty(script.Diagnostics.Messages);
|
||||
var result = script.Evaluate<long>();
|
||||
Assert.Empty(script.Diagnostics.Messages);
|
||||
Assert.Equal(284, result);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue