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:
Deukhoofd 2018-11-24 14:39:53 +01:00
parent 13ac6f2754
commit 194e7236c4
No known key found for this signature in database
GPG Key ID: B4C087AC81641654
2 changed files with 22 additions and 4 deletions

View File

@ -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,6 +71,7 @@ namespace Upsilon.BaseTypes
{
var key = baseEnumerator.Current;
var value = BaseIterator.GetValueFromIndex(key);
if (value.Type != Type.Nil)
yield return new SimpleScriptTable(new List<ScriptType>(){key, value});
}
}

View File

@ -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);
}
}
}