From 194e7236c44bf973e7aa626966d532b70b8bb894 Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Sat, 24 Nov 2018 14:39:53 +0100 Subject: [PATCH] Unit tests for ipairs and pairs, as well as changing how they handle null. ipairs now breaks at first nil value, pairs skips it --- Upsilon/BaseTypes/ScriptIterator.cs | 7 ++++--- .../BasicFunctionsTests.cs | 19 ++++++++++++++++++- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/Upsilon/BaseTypes/ScriptIterator.cs b/Upsilon/BaseTypes/ScriptIterator.cs index abf9d76..fdec204 100644 --- a/Upsilon/BaseTypes/ScriptIterator.cs +++ b/Upsilon/BaseTypes/ScriptIterator.cs @@ -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(){key, value}); } @@ -71,7 +71,8 @@ namespace Upsilon.BaseTypes { var key = baseEnumerator.Current; var value = BaseIterator.GetValueFromIndex(key); - yield return new SimpleScriptTable(new List(){key, value}); + if (value.Type != Type.Nil) + yield return new SimpleScriptTable(new List(){key, value}); } } } diff --git a/UpsilonTests/StandardLibraryTests/BasicFunctionsTests.cs b/UpsilonTests/StandardLibraryTests/BasicFunctionsTests.cs index 7273e7f..25d653d 100644 --- a/UpsilonTests/StandardLibraryTests/BasicFunctionsTests.cs +++ b/UpsilonTests/StandardLibraryTests/BasicFunctionsTests.cs @@ -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(); + Assert.Empty(script.Diagnostics.Messages); + Assert.Equal(284, result); + } } } \ No newline at end of file