Skip to content

Commit

Permalink
Fix date filter not using context settings
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastienros committed Mar 9, 2021
1 parent 46d76d5 commit 0ef19d3
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
37 changes: 36 additions & 1 deletion Fluid.Tests/TemplateContextTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Fluid.Values;
using System;
using System.Globalization;
using System.Linq;
using System.Threading.Tasks;
using Xunit;
Expand All @@ -22,7 +24,6 @@ private static async Task Register()
templateContext.Options.MemberAccessStrategy.Register(typeof(TestClass));
}


[Fact]
public void ScopeShouldFallbackToTemplateOptions()
{
Expand All @@ -43,6 +44,40 @@ public void ScopeShouldFallbackToTemplateOptions()
Assert.Equal("o3", context.GetValue("o3").ToStringValue());
}

[Fact]
public void CustomContextShouldNotUseTemplateOptionsProperties()
{
var parser = new FluidParser();

var options = new TemplateOptions();

var context = new TemplateContext(options);
context.TimeZone = TimeZoneInfo.Utc;
context.CultureInfo = new CultureInfo("fr-FR");
context.Now = () => new DateTime(2020, 01, 01);

Assert.Equal(TimeZoneInfo.Utc, context.TimeZone);
Assert.Equal(new CultureInfo("fr-FR"), context.CultureInfo);
Assert.Equal(new DateTime(2020, 01, 01), context.Now());
}

[Fact]
public void DefaultContextShouldUseTemplateOptionsProperties()
{
var parser = new FluidParser();

var options = new TemplateOptions();
options.TimeZone = TimeZoneInfo.Utc;
options.CultureInfo = new CultureInfo("fr-FR");
options.Now = () => new DateTime(2020, 01, 01);

var context = new TemplateContext(options);

Assert.Equal(TimeZoneInfo.Utc, context.TimeZone);
Assert.Equal(new CultureInfo("fr-FR"), context.CultureInfo);
Assert.Equal(new DateTime(2020, 01, 01), context.Now());
}

[Fact]
public void UseDifferentModelsWithSameMemberName()
{
Expand Down
2 changes: 1 addition & 1 deletion Fluid/Filters/MiscFilters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ public static ValueTask<FluidValue> FormatDate(FluidValue input, FilterArguments

private static bool TryGetDateTimeInput(FluidValue input, TemplateContext context, out DateTimeOffset result)
{
result = context.Options.Now();
result = context.Now();

if (input.Type == FluidValues.String)
{
Expand Down

0 comments on commit 0ef19d3

Please sign in to comment.