diff --git a/Fluid.Tests/DictionaryDictionaryFluidIndexableTests.cs b/Fluid.Tests/DictionaryDictionaryFluidIndexableTests.cs new file mode 100644 index 00000000..9d13352f --- /dev/null +++ b/Fluid.Tests/DictionaryDictionaryFluidIndexableTests.cs @@ -0,0 +1,51 @@ +using Fluid.Values; +using System.Collections.Generic; +using System.Linq; +using Xunit; + +namespace Fluid.Tests; + +public class DictionaryDictionaryFluidIndexableTests +{ + [Fact] + public void CountShouldHaveTheSameLengthAsKeysWhenKeyInLong() + { + var items = new Dictionary() + { + {1, "a"}, + {2, "b"}, + {3, "c"}, + {4, "d"}, + {5, "e"}, + }; + + var value = FluidValue.Create(items, new TemplateOptions()); + + var castedValue = value.ToObjectValue() as IFluidIndexable; + + Assert.NotNull(castedValue); + + Assert.Equal(items.Count, castedValue.Keys.Count()); + } + + [Fact] + public void CountShouldHaveTheSameLengthAsKeysWhenKeyInString() + { + var items = new Dictionary() + { + {"1", "a"}, + {"2", "b"}, + {"3", "c"}, + {"4", "d"}, + {"5", "e"}, + }; + + var value = FluidValue.Create(items, new TemplateOptions()); + + var castedValue = value.ToObjectValue() as IFluidIndexable; + + Assert.NotNull(castedValue); + + Assert.Equal(items.Count, castedValue.Keys.Count()); + } +} diff --git a/Fluid/Values/DictionaryDictionaryFluidIndexable.cs b/Fluid/Values/DictionaryDictionaryFluidIndexable.cs index 69e4fa79..faf6333d 100644 --- a/Fluid/Values/DictionaryDictionaryFluidIndexable.cs +++ b/Fluid/Values/DictionaryDictionaryFluidIndexable.cs @@ -23,7 +23,11 @@ public IEnumerable Keys foreach (var key in _dictionary.Keys) { // Only handle string keys since this is what TryGetValue returns - if (key is string) + if (key is string str) + { + yield return str; + } + else { yield return key.ToString(); }