Skip to content
Open
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
19 changes: 9 additions & 10 deletions source/Handlebars.Extension.Test/Handlebars.Extension.Test.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netcoreapp2.1;netcoreapp3.1</TargetFrameworks>
<TargetFrameworks>netcoreapp3.1;net6.0</TargetFrameworks>
<TargetFrameworks Condition=" '$(OS)' == 'Windows_NT'">$(TargetFrameworks);net472</TargetFrameworks>
<ProjectGuid>1956A22F-7B26-4747-8125-7EAC0B94665D</ProjectGuid>
<RootNamespace>HandlebarsDotNet.Extension.Test</RootNamespace>
Expand All @@ -10,7 +10,7 @@
<GenerateDocumentationFile>false</GenerateDocumentationFile>
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
</PropertyGroup>

<PropertyGroup>
<NoWarn>0618;1701</NoWarn>
</PropertyGroup>
Expand All @@ -27,15 +27,15 @@
<PropertyGroup Condition="'$(TargetFramework)'=='net452'">
<DefineConstants>$(DefineConstants);netFramework</DefineConstants>
</PropertyGroup>

<PropertyGroup Condition="'$(TargetFramework)'=='netcoreapp2.1'">
<DefineConstants>$(DefineConstants);netcoreapp;netstandard</DefineConstants>
</PropertyGroup>

<PropertyGroup Condition="'$(TargetFramework)'=='netcoreapp3.1'">
<DefineConstants>$(DefineConstants);netcoreapp;netstandard</DefineConstants>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="1.2.1">
<PrivateAssets>all</PrivateAssets>
Expand All @@ -45,23 +45,22 @@
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1" />
</ItemGroup>


<ItemGroup Condition="'$(TargetFramework)'=='net46' or '$(TargetFramework)'=='net461' or '$(TargetFramework)'=='net472' or '$(TargetFramework)'=='net452'">
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0" />
</ItemGroup>


<ItemGroup Condition="'$(TargetFramework)'=='netcoreapp3.1' or '$(TargetFramework)'=='netcoreapp2.1'">
<ItemGroup Condition="'$(TargetFramework)'=='netcoreapp2.1' or '$(TargetFramework)'=='netcoreapp3.1' or '$(TargetFramework)'=='net6.0'">
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.1" />
<PackageReference Include="System.Collections.NonGeneric" Version="4.3.0" />
</ItemGroup>

<ItemGroup>
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Handlebars.Extension\Handlebars.Extension.csproj" />
</ItemGroup>
Expand Down
75 changes: 41 additions & 34 deletions source/Handlebars.Extension.Test/JsonTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,17 @@ namespace HandlebarsDotNet.Extension.Test
{
public class JsonTests
{
public delegate object JsonModelFactory(string json);

public class EnvGenerator : IEnumerable<object[]>
{
private readonly List<IHandlebars> _data = new List<IHandlebars>
private readonly List<(IHandlebars, JsonModelFactory)> _data = new List<(IHandlebars, JsonModelFactory)>
{
Handlebars.Create(new HandlebarsConfiguration().UseJson())
(Handlebars.Create(new HandlebarsConfiguration().UseJson()), json => JsonDocument.Parse(json)),
(Handlebars.Create(new HandlebarsConfiguration().UseJson()), json => System.Text.Json.Nodes.JsonNode.Parse(json)),
};

public IEnumerator<object[]> GetEnumerator() => _data.Select(o => new object[] { o }).GetEnumerator();
public IEnumerator<object[]> GetEnumerator() => _data.Select(item => new object[] { item.Item1, item.Item2 }).GetEnumerator();

IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}
Expand Down Expand Up @@ -51,9 +54,9 @@ public void ValueTypes(string value)

[Theory]
[ClassData(typeof(EnvGenerator))]
public void JsonTestIfTruthy(IHandlebars handlebars)
public void JsonTestIfTruthy(IHandlebars handlebars, JsonModelFactory jsonModelFactory)
{
var model = JsonDocument.Parse("{\"truthy\":true}");
var model = jsonModelFactory("{\"truthy\":true}");

var source = "{{#if truthy}}{{truthy}}{{/if}}";

Expand All @@ -66,9 +69,9 @@ public void JsonTestIfTruthy(IHandlebars handlebars)

[Theory]
[ClassData(typeof(EnvGenerator))]
public void JsonTestIfFalsy(IHandlebars handlebars)
public void JsonTestIfFalsy(IHandlebars handlebars, JsonModelFactory jsonModelFactory)
{
var model = JsonDocument.Parse("{\"falsy\":false}");
var model = jsonModelFactory("{\"falsy\":false}");

var source = "{{#if (not falsy)}}{{falsy}}{{/if}}";

Expand All @@ -82,9 +85,9 @@ public void JsonTestIfFalsy(IHandlebars handlebars)

[Theory]
[ClassData(typeof(EnvGenerator))]
public void JsonTestIfFalsyMissingField(IHandlebars handlebars)
public void JsonTestIfFalsyMissingField(IHandlebars handlebars, JsonModelFactory jsonModelFactory)
{
var model = JsonDocument.Parse("{\"myfield\":\"test1\"}");
var model = jsonModelFactory("{\"myfield\":\"test1\"}");

var source = "{{myfield}}{{#if mymissingfield}}{{mymissingfield}}{{/if}}";

Expand All @@ -97,9 +100,9 @@ public void JsonTestIfFalsyMissingField(IHandlebars handlebars)

[Theory]
[ClassData(typeof(EnvGenerator))]
public void JsonTestIfFalsyValue(IHandlebars handlebars)
public void JsonTestIfFalsyValue(IHandlebars handlebars, JsonModelFactory jsonModelFactory)
{
var model = JsonDocument.Parse("{\"myfield\":\"test1\",\"falsy\":null}");
var model = jsonModelFactory("{\"myfield\":\"test1\",\"falsy\":null}");

var source = "{{myfield}}{{#if falsy}}{{falsy}}{{/if}}";

Expand All @@ -112,9 +115,9 @@ public void JsonTestIfFalsyValue(IHandlebars handlebars)

[Theory]
[ClassData(typeof(EnvGenerator))]
public void ArrayIterator(IHandlebars handlebars)
public void ArrayIterator(IHandlebars handlebars, JsonModelFactory jsonModelFactory)
{
var model = JsonDocument.Parse("[{\"Key\": \"Key1\", \"Value\": \"Val1\"},{\"Key\": \"Key2\", \"Value\": \"Val2\"}]");
var model = jsonModelFactory("[{\"Key\": \"Key1\", \"Value\": \"Val1\"},{\"Key\": \"Key2\", \"Value\": \"Val2\"}]");

var source = "{{#each this}}{{Key}}{{Value}}{{/each}}";

Expand All @@ -127,9 +130,9 @@ public void ArrayIterator(IHandlebars handlebars)

[Theory]
[ClassData(typeof(EnvGenerator))]
public void ArrayIteratorProperties(IHandlebars handlebars)
public void ArrayIteratorProperties(IHandlebars handlebars, JsonModelFactory jsonModelFactory)
{
var model = JsonDocument.Parse("[{\"Key\": \"Key1\", \"Value\": \"Val1\"},{\"Key\": \"Key2\", \"Value\": \"Val2\"}]");
var model = jsonModelFactory("[{\"Key\": \"Key1\", \"Value\": \"Val1\"},{\"Key\": \"Key2\", \"Value\": \"Val2\"}]");

var source = "{{#each this}}{{@index}}-{{@first}}-{{@last}}-{{@value.Key}}-{{@value.Value}};{{/each}}";

Expand All @@ -142,9 +145,9 @@ public void ArrayIteratorProperties(IHandlebars handlebars)

[Theory]
[ClassData(typeof(EnvGenerator))]
public void ArrayIndexProperties(IHandlebars handlebars)
public void ArrayIndexProperties(IHandlebars handlebars, JsonModelFactory jsonModelFactory)
{
var model = JsonDocument.Parse("[\"Index0\", \"Index1\"]");
var model = jsonModelFactory("[\"Index0\", \"Index1\"]");

var source = "{{@root.1}}";

Expand All @@ -157,9 +160,9 @@ public void ArrayIndexProperties(IHandlebars handlebars)

[Theory]
[ClassData(typeof(EnvGenerator))]
public void ArrayIndexPropertiesNested(IHandlebars handlebars)
public void ArrayIndexPropertiesNested(IHandlebars handlebars, JsonModelFactory jsonModelFactory)
{
var model = JsonDocument.Parse("[{}, {\"Items\": [\"Index0\", \"Index1\"]}]");
var model = jsonModelFactory("[{}, {\"Items\": [\"Index0\", \"Index1\"]}]");

var source = "{{@root.1.Items.1}}";

Expand All @@ -172,9 +175,9 @@ public void ArrayIndexPropertiesNested(IHandlebars handlebars)

[Theory]
[ClassData(typeof(EnvGenerator))]
public void ArrayCount(IHandlebars handlebars)
public void ArrayCount(IHandlebars handlebars, JsonModelFactory jsonModelFactory)
{
var model = JsonDocument.Parse("[{\"Key\": \"Key1\", \"Value\": \"Val1\"},{\"Key\": \"Key2\", \"Value\": \"Val2\"}]");
var model = jsonModelFactory("[{\"Key\": \"Key1\", \"Value\": \"Val1\"},{\"Key\": \"Key2\", \"Value\": \"Val2\"}]");

var source = "{{this.Count}} = {{this.Length}}";

Expand All @@ -187,9 +190,9 @@ public void ArrayCount(IHandlebars handlebars)

[Theory]
[ClassData(typeof(EnvGenerator))]
public void ArrayListProperties(IHandlebars handlebars)
public void ArrayListProperties(IHandlebars handlebars, JsonModelFactory jsonModelFactory)
{
var model = JsonDocument.Parse("[{\"Key\": \"Key1\", \"Value\": \"Val1\"},{\"Key\": \"Key2\", \"Value\": \"Val2\"}]");
var model = jsonModelFactory("[{\"Key\": \"Key1\", \"Value\": \"Val1\"},{\"Key\": \"Key2\", \"Value\": \"Val2\"}]");

var source = "{{listProperties this}}";

Expand All @@ -203,8 +206,9 @@ public void ArrayListProperties(IHandlebars handlebars)

[Theory]
[ClassData(typeof(EnvGenerator))]
public void ObjectIterator(IHandlebars handlebars){
var model = JsonDocument.Parse("{\"Key1\": \"Val1\", \"Key2\": \"Val2\"}");
public void ObjectIterator(IHandlebars handlebars, JsonModelFactory jsonModelFactory)
{
var model = jsonModelFactory("{\"Key1\": \"Val1\", \"Key2\": \"Val2\"}");

var source = "{{#each this}}{{@key}}{{@value}}{{/each}}";

Expand All @@ -217,8 +221,9 @@ public void ObjectIterator(IHandlebars handlebars){

[Theory]
[ClassData(typeof(EnvGenerator))]
public void ObjectIteratorProperties(IHandlebars handlebars){
var model = JsonDocument.Parse("{\"Key1\": \"Val1\", \"Key2\": \"Val2\"}");
public void ObjectIteratorProperties(IHandlebars handlebars, JsonModelFactory jsonModelFactory)
{
var model = jsonModelFactory("{\"Key1\": \"Val1\", \"Key2\": \"Val2\"}");

var source = "{{#each this}}{{@index}}-{{@first}}-{{@last}}-{{@key}}-{{@value}};{{/each}}";

Expand All @@ -231,8 +236,9 @@ public void ObjectIteratorProperties(IHandlebars handlebars){

[Theory]
[ClassData(typeof(EnvGenerator))]
public void ObjectListProperties(IHandlebars handlebars){
var model = JsonDocument.Parse("{\"Key1\": \"Val1\", \"Key2\": \"Val2\"}");
public void ObjectListProperties(IHandlebars handlebars, JsonModelFactory jsonModelFactory)
{
var model = jsonModelFactory("{\"Key1\": \"Val1\", \"Key2\": \"Val2\"}");

var source = "{{ListProperties this}}";
handlebars.RegisterHelper(new ListPropertiesHelper());
Expand All @@ -246,8 +252,9 @@ public void ObjectListProperties(IHandlebars handlebars){

[Theory]
[ClassData(typeof(EnvGenerator))]
public void ObjectIteratorPropertiesWithLast(IHandlebars handlebars){
var model = JsonDocument.Parse("{\"Key1\": \"Val1\", \"Key2\": \"Val2\"}");
public void ObjectIteratorPropertiesWithLast(IHandlebars handlebars, JsonModelFactory jsonModelFactory)
{
var model = jsonModelFactory("{\"Key1\": \"Val1\", \"Key2\": \"Val2\"}");

var source = "{{#each this}}{{@index}}-{{@first}}-{{@last}}-{{@key}}-{{@value}};{{/each}}";

Expand All @@ -261,7 +268,7 @@ public void ObjectIteratorPropertiesWithLast(IHandlebars handlebars){

[Theory]
[ClassData(typeof(EnvGenerator))]
public void WithParentIndexJsonNet(IHandlebars handlebars)
public void WithParentIndexJsonNet(IHandlebars handlebars, JsonModelFactory jsonModelFactory)
{
var source = @"
{{#each level1}}
Expand Down Expand Up @@ -328,9 +335,9 @@ public void WithParentIndexJsonNet(IHandlebars handlebars)
}
};

var json = JsonDocument.Parse(JsonSerializer.Serialize(data));
var model = jsonModelFactory(JsonSerializer.Serialize(data));

var result = template(json);
var result = template(model);

const string expected = @"
id=0
Expand Down
40 changes: 21 additions & 19 deletions source/Handlebars.Extension.sln
Original file line number Diff line number Diff line change
@@ -1,42 +1,44 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26403.3
# Visual Studio Version 17
VisualStudioVersion = 17.9.34714.143
MinimumVisualStudioVersion = 15.0.26124.0
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{E9AC0BCD-C060-4634-BBBB-636167C809B4}"
ProjectSection(SolutionItems) = preProject
..\README.md = ..\README.md
Directory.Build.props = Directory.Build.props
..\README.md = ..\README.md
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Handlebars.Extension", "Handlebars.Extension\Handlebars.Extension.csproj", "{9822C7B8-7E51-42BC-9A49-72A10491B202}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Handlebars.Extension", "Handlebars.Extension\Handlebars.Extension.csproj", "{25080858-B620-4985-8AEF-E135324081B3}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Handlebars.Extension.Test", "Handlebars.Extension.Test\Handlebars.Extension.Test.csproj", "{6BA232A6-8C4D-4C7D-BD75-1844FE9774AF}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Handlebars.Extension.Test", "Handlebars.Extension.Test\Handlebars.Extension.Test.csproj", "{1956A22F-7B26-4747-8125-7EAC0B94665D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Handlebars.Extension.Benchmark", "Handlebars.Extension.Benchmark\Handlebars.Extension.Benchmark.csproj", "{B335E9C5-7DD3-416D-89CC-8D48D89DB628}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Handlebars.Extension.Benchmark", "Handlebars.Extension.Benchmark\Handlebars.Extension.Benchmark.csproj", "{95ECC7A5-0A42-4DAF-A546-20522A3F3CF5}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{9822C7B8-7E51-42BC-9A49-72A10491B202}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9822C7B8-7E51-42BC-9A49-72A10491B202}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9822C7B8-7E51-42BC-9A49-72A10491B202}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9822C7B8-7E51-42BC-9A49-72A10491B202}.Release|Any CPU.Build.0 = Release|Any CPU
{6BA232A6-8C4D-4C7D-BD75-1844FE9774AF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6BA232A6-8C4D-4C7D-BD75-1844FE9774AF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6BA232A6-8C4D-4C7D-BD75-1844FE9774AF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6BA232A6-8C4D-4C7D-BD75-1844FE9774AF}.Release|Any CPU.Build.0 = Release|Any CPU
{B335E9C5-7DD3-416D-89CC-8D48D89DB628}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B335E9C5-7DD3-416D-89CC-8D48D89DB628}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B335E9C5-7DD3-416D-89CC-8D48D89DB628}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B335E9C5-7DD3-416D-89CC-8D48D89DB628}.Release|Any CPU.Build.0 = Release|Any CPU
{25080858-B620-4985-8AEF-E135324081B3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{25080858-B620-4985-8AEF-E135324081B3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{25080858-B620-4985-8AEF-E135324081B3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{25080858-B620-4985-8AEF-E135324081B3}.Release|Any CPU.Build.0 = Release|Any CPU
{1956A22F-7B26-4747-8125-7EAC0B94665D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1956A22F-7B26-4747-8125-7EAC0B94665D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1956A22F-7B26-4747-8125-7EAC0B94665D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1956A22F-7B26-4747-8125-7EAC0B94665D}.Release|Any CPU.Build.0 = Release|Any CPU
{95ECC7A5-0A42-4DAF-A546-20522A3F3CF5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{95ECC7A5-0A42-4DAF-A546-20522A3F3CF5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{95ECC7A5-0A42-4DAF-A546-20522A3F3CF5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{95ECC7A5-0A42-4DAF-A546-20522A3F3CF5}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {A684E42D-246D-4CF7-B79C-34C49A10B35F}
EndGlobalSection
GlobalSection(MonoDevelopProperties) = preSolution
Policies = $0
$0.TextStylePolicy = $1
Expand Down
26 changes: 26 additions & 0 deletions source/Handlebars.Extension/CountMemberAliasProvider.JsonNode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using System.Text.Json.Nodes;
using HandlebarsDotNet.PathStructure;

namespace HandlebarsDotNet.Extension.Json
{
public partial class CountMemberAliasProvider : IMemberAliasProvider<JsonNode>
{
public bool TryGetMemberByAlias(JsonNode instance, Type targetType, ChainSegment memberAlias, out object? value)
{
if (!EqualsIgnoreCase("count", memberAlias) && !EqualsIgnoreCase("length", memberAlias))
{
value = null;
return false;
}
if (!(instance is JsonArray jsonArray))
{
value = null;
return false;
}

value = jsonArray.Count;
return true;
}
}
}
10 changes: 5 additions & 5 deletions source/Handlebars.Extension/CountMemberAliasProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace HandlebarsDotNet.Extension.Json
{
public class CountMemberAliasProvider : IMemberAliasProvider<JsonElement>
public partial class CountMemberAliasProvider : IMemberAliasProvider<JsonElement>
{
public bool TryGetMemberByAlias(JsonElement instance, Type targetType, ChainSegment memberAlias, out object? value)
{
Expand All @@ -22,11 +22,11 @@ public bool TryGetMemberByAlias(JsonElement instance, Type targetType, ChainSegm

value = instance.GetArrayLength();
return true;
}

static bool EqualsIgnoreCase(string a, ChainSegment b)
{
return string.Equals(a, b.TrimmedValue, StringComparison.OrdinalIgnoreCase);
}
private static bool EqualsIgnoreCase(string a, ChainSegment b)
{
return string.Equals(a, b.TrimmedValue, StringComparison.OrdinalIgnoreCase);
}
}
}
Loading