Skip to content
Merged
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
12 changes: 12 additions & 0 deletions Fluid.Tests/TemplateContextTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,18 @@ public void ShouldUseTemplateOptionsStringComparer()
Assert.Equal("insert", context.GetValue("pageState").ToStringValue());
}

[Fact]
public void ShouldUseTemplateOptionsStringComparerWithCaseSensitive()
{
var options = new TemplateOptions { ModelNamesComparer = StringComparer.Ordinal };
var context = new TemplateContext(options);
context.SetValue("case", "lower");
context.SetValue("CASE", "upper");
context.SetValue("Case", "mixed");

Assert.Equal("lowerupper", context.GetValue("case").ToStringValue() + context.GetValue("CASE").ToStringValue());
}

private class TestClass
{
public string Name { get; set; }
Expand Down
21 changes: 11 additions & 10 deletions Fluid/Scope.cs
Original file line number Diff line number Diff line change
@@ -1,33 +1,34 @@
using System.Runtime.CompilerServices;
using Fluid.Values;
using System.Runtime.CompilerServices;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why?!!

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

VS moved it?


namespace Fluid
{
public sealed class Scope
{
private Dictionary<string, FluidValue> _properties;
private readonly bool _forLoopScope;
private readonly StringComparer _stringComparer;

public Scope() : this(null)
public Scope() : this(null, false, null)
{
}

public Scope(Scope parent)
public Scope(Scope parent) : this(parent, false, null)
{
Parent = parent;
}

public Scope(Scope parent, bool forLoopScope)
public Scope(Scope parent, bool forLoopScope, StringComparer stringComparer = null)
{
if (forLoopScope && parent == null) ExceptionHelper.ThrowArgumentNullException(nameof(parent));

// For loops are also ordinal by default
_stringComparer = stringComparer ?? StringComparer.Ordinal;

Parent = parent;

// A ForLoop scope reads and writes its values in the parent scope.
// Internal accessors to the inner properties grant access to the local properties.
_forLoopScope = forLoopScope;

// ForLoop scopes are ordinal since the properties are keywords: "forloop"
_properties = new Dictionary<string, FluidValue>(StringComparer.OrdinalIgnoreCase);
}

/// <summary>
Expand Down Expand Up @@ -114,7 +115,7 @@ public void SetValue(string name, FluidValue value)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void SetOwnValue(string name, FluidValue value)
{
_properties ??= new Dictionary<string, FluidValue>(Parent?._properties?.Comparer ?? TemplateOptions.Default.ModelNamesComparer);
_properties ??= new Dictionary<string, FluidValue>(Parent?._properties?.Comparer ?? _stringComparer);
_properties[name] = value ?? NilValue.Instance;
}

Expand All @@ -137,4 +138,4 @@ public void CopyTo(Scope scope)
}
}
}
}
}
6 changes: 4 additions & 2 deletions Fluid/TemplateContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,18 @@ public TemplateContext(object model, TemplateOptions options, bool allowModelMem
/// <param name="modelNamesComparer">An optional <see cref="StringComparer"/> instance used when comparing model names.</param>
public TemplateContext(TemplateOptions options, StringComparer modelNamesComparer = null)
{
modelNamesComparer ??= options.ModelNamesComparer;

Options = options;
LocalScope = new Scope(options.Scope);
LocalScope = new Scope(options.Scope, forLoopScope: false, modelNamesComparer);
RootScope = LocalScope;
CultureInfo = options.CultureInfo;
TimeZone = options.TimeZone;
Captured = options.Captured;
Assigned = options.Assigned;
Now = options.Now;
MaxSteps = options.MaxSteps;
ModelNamesComparer = modelNamesComparer ?? options.ModelNamesComparer;
ModelNamesComparer = modelNamesComparer;
}

/// <summary>
Expand Down