Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correctly convert dictionary keys to string #605

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions Fluid.Tests/DictionaryDictionaryFluidIndexableTests.cs
Original file line number Diff line number Diff line change
@@ -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<long, string>()
{
{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<string, string>()
{
{"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());
}
}
6 changes: 5 additions & 1 deletion Fluid/Values/DictionaryDictionaryFluidIndexable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ public IEnumerable<string> 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();
}
Expand Down