Skip to content

Commit

Permalink
Handling Nulls in Currency Filter (#477)
Browse files Browse the repository at this point in the history
  • Loading branch information
AnushaKaraka committed Apr 13, 2022
1 parent c9aff49 commit 491f941
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/DotLiquid.Tests/StandardFilterTests.cs
Expand Up @@ -714,6 +714,26 @@ public void TestEuroCurrencyFromString(object input, string expected, string lan
Assert.AreEqual(expected, StandardFilters.Currency(context: _contextV20, input: input, languageTag: languageTag));
}

[Test]
[TestCase(null)]
[TestCase("")]
public void TestNullOrEmptyInputCurrency(string input)
{
// Set the thread culture and test for backward compatibility
using (CultureHelper.SetCulture("en-US"))
{
Helper.AssertTemplateResult(
expected: string.Empty,
template: "{{ input | currency: 'de-DE' }}",
localVariables: Hash.FromAnonymousObject(new { input = input }));
}

// _contextV20 is initialized with InvariantCulture
Assert.AreEqual(
expected: input,
actual: StandardFilters.Currency(context: _contextV20, input: input, languageTag: "de-DE"));
}

[Test]
public void TestMalformedCurrency()
{
Expand Down
3 changes: 3 additions & 0 deletions src/DotLiquid/StandardFilters.cs
Expand Up @@ -359,6 +359,9 @@ public static string Rstrip(string input)
/// <seealso href="https://shopify.dev/api/liquid/filters/money-filters#money">Shopify Money filter</seealso>
public static string Currency(Context context, object input, string languageTag = null)
{
// Check for null input, return null
if (input == null) return null;

// Check for null only, allow an empty string as it represent the InvariantCulture
var culture = languageTag == null ? context.CurrentCulture : new CultureInfo(languageTag.Trim());

Expand Down

0 comments on commit 491f941

Please sign in to comment.