Skip to content

Commit

Permalink
feature: ReflectionSource and DictionarySource (#426)
Browse files Browse the repository at this point in the history
* Initial commit

* Make ReflectionSource.TypeCache static
* Control size of ReflectionSource.TypeCache, remove oldest item first
* dynamics in DictionarySource also use case-sensitivity setting
* Cache for IReadOnlyDictionary has instance scope

* Remove cache insertion tracker for NET6_0_OR_GREATER

Only before NETCore3.1, the dictionary is not ordered by insertion order.

* Add explicit EqualityComparer for creating the TypeCache dictionary

* Add comment about handling case-sensitivity
* Add test for: When there are multiple members with the same name but different case, the first member is used

* Add unit test for dynamics (ExpandoObject) case-insensitive

* refactor: Less cognitive complexity

* for DictionarySource
* for ReflectionSource

* chore: Fix indent
  • Loading branch information
axunonb committed Jul 2, 2024
1 parent 421c715 commit 2bf827f
Show file tree
Hide file tree
Showing 4 changed files with 281 additions and 135 deletions.
19 changes: 9 additions & 10 deletions src/SmartFormat.Tests/Extensions/DictionarySourceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using SmartFormat.Core.Settings;
using SmartFormat.Extensions;
using SmartFormat.Tests.TestUtils;
using SmartFormat.Utilities;

namespace SmartFormat.Tests.Extensions;

Expand Down Expand Up @@ -51,7 +50,7 @@ private static dynamic GetDynamicArgs()
d.Object = new { Prop1 = "a", Prop2 = "b", Prop3 = "c", };

return new object[] {
d,
d
};
}

Expand All @@ -76,17 +75,17 @@ public void Test_Dictionary()
}

[Test]
public void Test_Dynamic()
public void Test_Dynamic_CaseSensitive()
{
var formatter = Smart.CreateDefaultSmartFormat();
var formatter = Smart.CreateDefaultSmartFormat(new SmartSettings {CaseSensitivity = CaseSensitivityType.CaseSensitive});
formatter.AddExtensions(new DictionarySource());

var formats = new[]
var formats = new string[]
{
"Chained: {0.Numbers.One} {Numbers.Two} {Letters.A} {Object.Prop1} {Raw.X}",
"Nested: {0:{Numbers:{One} {Two}}} {Letters:{A}} {Object:{Prop1}} {Raw:{X}}"
};
var expected = new[]
var expected = new string[]
{
"Chained: 1 2 a a z",
"Nested: 1 2 a a z"
Expand All @@ -98,20 +97,20 @@ public void Test_Dynamic()
[Test]
public void Test_Dynamic_CaseInsensitive()
{
var formatter = Smart.CreateDefaultSmartFormat(new SmartSettings {CaseSensitivity = CaseSensitivityType.CaseInsensitive});
var formatter = Smart.CreateDefaultSmartFormat(new SmartSettings { CaseSensitivity = CaseSensitivityType.CaseInsensitive});
formatter.AddExtensions(new DictionarySource());

var formats = new string[]
{
"Chained: {0.Numbers.One} {Numbers.Two} {Letters.A} {Object.Prop1} {Raw.x}",
"Nested: {0:{Numbers:{One} {Two}}} {Letters:{A}} {Object:{Prop1}} {Raw:{x}}"
"Chained: {0.Numbers.ONE} {Numbers.TWO} {Letters.A} {Object.PROP1} {Raw.x}",
"Nested: {0:{Numbers:{ONE} {TWO}}} {Letters:{A}} {Object:{PROP1}} {Raw:{x}}"
};
var expected = new string[]
{
"Chained: 1 2 a a z",
"Nested: 1 2 a a z"
};
var args = (object[])GetDynamicArgs();
var args = (object[]) GetDynamicArgs();
formatter.Test(formats, args, expected);
}

Expand Down
Loading

0 comments on commit 2bf827f

Please sign in to comment.