From eb26be4478055668a3db34d93cdbafd2fc5319a8 Mon Sep 17 00:00:00 2001 From: Mike Alhayek Date: Mon, 20 Nov 2023 16:06:57 -0800 Subject: [PATCH] Correctly convert dictionary keys to string --- ...DictionaryDictionaryFluidIndexableTests.cs | 51 +++++++++++++++++++ .../DictionaryDictionaryFluidIndexable.cs | 6 ++- 2 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 Fluid.Tests/DictionaryDictionaryFluidIndexableTests.cs 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(); }