-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
When ThrowOnUnresolvedBindingExpression is False, do not throw when U…
…ndefinedBindingResult but use default value (#95) * When ThrowOnUnresolvedBindingExpression is False, do not throw when UndefinedBindingResult but use default value * tst * tst * Equal_WhenInputIsUnresolvedBinding_And_ThrowOnUnresolvedBindingExpressionIsFalse_ShouldReturnEmpty Equal_WhenInputIsUnresolvedBinding_And_ThrowOnUnresolvedBindingExpressionIsTrue_ShouldThrow
- Loading branch information
Showing
8 changed files
with
236 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 16 additions & 17 deletions
33
src/Handlebars.Net.Helpers.Core/Parsers/StringValueParser.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,26 @@ | ||
using System.Globalization; | ||
|
||
namespace HandlebarsDotNet.Helpers.Parsers | ||
namespace HandlebarsDotNet.Helpers.Parsers; | ||
|
||
public static class StringValueParser | ||
{ | ||
public static class StringValueParser | ||
public static object Parse(IHandlebars context, string valueAsString) | ||
{ | ||
public static object Parse(IHandlebars context, string valueAsString) | ||
if (int.TryParse(valueAsString, NumberStyles.Any, context.Configuration.FormatProvider, out var valueAsInt)) | ||
{ | ||
if (int.TryParse(valueAsString, NumberStyles.Any, context.Configuration.FormatProvider, out int valueAsInt)) | ||
{ | ||
return valueAsInt; | ||
} | ||
return valueAsInt; | ||
} | ||
|
||
if (long.TryParse(valueAsString, NumberStyles.Any, context.Configuration.FormatProvider, out long valueAsLong)) | ||
{ | ||
return valueAsLong; | ||
} | ||
if (long.TryParse(valueAsString, NumberStyles.Any, context.Configuration.FormatProvider, out var valueAsLong)) | ||
{ | ||
return valueAsLong; | ||
} | ||
|
||
if (double.TryParse(valueAsString, NumberStyles.Any, context.Configuration.FormatProvider, out double valueAsDouble)) | ||
{ | ||
return valueAsDouble; | ||
} | ||
|
||
return valueAsString; | ||
if (double.TryParse(valueAsString, NumberStyles.Any, context.Configuration.FormatProvider, out var valueAsDouble)) | ||
{ | ||
return valueAsDouble; | ||
} | ||
|
||
return valueAsString; | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/Handlebars.Net.Helpers.Core/Utils/DefaultValueCache.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System.Collections.Concurrent; | ||
using System.Reflection; | ||
|
||
namespace HandlebarsDotNet.Helpers.Utils; | ||
|
||
internal static class DefaultValueCache | ||
{ | ||
private static readonly ConcurrentDictionary<Type, object?> Cache = new(); | ||
|
||
public static object? GetDefaultValue(Type type) | ||
{ | ||
// Try to get the value from the cache | ||
if (!Cache.TryGetValue(type, out var value)) | ||
{ | ||
// Value not found in cache, compute and add to cache | ||
value = CreateDefaultValue(type); | ||
Cache.TryAdd(type, value); | ||
} | ||
|
||
return value; | ||
} | ||
|
||
private static object? CreateDefaultValue(Type type) | ||
{ | ||
// Uses reflection to create a generic method call | ||
return typeof(DefaultValueCreator<>) | ||
.MakeGenericType(type) | ||
.GetMethod("GetDefault")! | ||
.Invoke(null, null); | ||
} | ||
|
||
private static class DefaultValueCreator<T> | ||
{ | ||
public static T? GetDefault() => default(T); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
test/Handlebars.Net.Helpers.Tests/Helpers/Parsers/StringValueParserTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
using System.Globalization; | ||
using FluentAssertions; | ||
using HandlebarsDotNet.Helpers.Parsers; | ||
using Moq; | ||
using Xunit; | ||
|
||
namespace HandlebarsDotNet.Helpers.Tests.Helpers.Parsers; | ||
|
||
public class StringValueParserTests | ||
{ | ||
private readonly Mock<IHandlebars> _handlebarsContext; | ||
|
||
public StringValueParserTests() | ||
{ | ||
var configuration = new HandlebarsConfiguration | ||
{ | ||
FormatProvider = CultureInfo.InvariantCulture | ||
}; | ||
|
||
_handlebarsContext = new Mock<IHandlebars>(); | ||
_handlebarsContext.SetupGet(c => c.Configuration).Returns(configuration); | ||
} | ||
|
||
[Theory] | ||
[InlineData("123", 123)] | ||
[InlineData("-1", -1)] | ||
[InlineData("2147483647", int.MaxValue)] | ||
[InlineData("-2147483648", int.MinValue)] | ||
public void Parse_ShouldReturnInt_WhenInputIsInt(string input, int expected) | ||
{ | ||
var result = StringValueParser.Parse(_handlebarsContext.Object, input); | ||
result.Should().BeOfType<int>().And.Be(expected); | ||
} | ||
|
||
[Theory] | ||
[InlineData("9223372036854775807", long.MaxValue)] | ||
[InlineData("-9223372036854775808", long.MinValue)] | ||
public void Parse_ShouldReturnLong_WhenInputIsLong(string input, long expected) | ||
{ | ||
var result = StringValueParser.Parse(_handlebarsContext.Object, input); | ||
result.Should().BeOfType<long>().And.Be(expected); | ||
} | ||
|
||
[Theory] | ||
[InlineData("123.456", 123.456)] | ||
[InlineData("0.0001", 0.0001)] | ||
[InlineData("-1.42", -1.42)] | ||
public void Parse_ShouldReturnDouble_WhenInputIsDouble(string input, double expected) | ||
{ | ||
var result = StringValueParser.Parse(_handlebarsContext.Object, input); | ||
result.Should().BeOfType<double>().And.Be(expected); | ||
} | ||
|
||
[Theory] | ||
[InlineData("")] | ||
[InlineData("test")] | ||
[InlineData("123test")] | ||
public void Parse_ShouldReturnString_WhenInputIsNonNumericString(string input) | ||
{ | ||
var result = StringValueParser.Parse(_handlebarsContext.Object, input); | ||
result.Should().BeOfType<string>().And.Be(input); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters