Skip to content

Commit f1de9ea

Browse files
committed
add json patch lessons
1 parent 513549c commit f1de9ea

14 files changed

Lines changed: 1397 additions & 861 deletions

File tree

.github/instructions/apis.instructions.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ These instructions apply to `ILessonHost` implementations in `LearnJsonEverythin
1515

1616
- Implement `ILessonHost` with a single `Run(LessonData lesson)` method returning `string[]`.
1717
- Use `CompilationHelpers` to compile user code at runtime.
18-
- Each result string must be prefixed with either `Iconography.SuccessIcon` (pass) or `Iconography.FailIcon` (fail).
18+
- Each result string must be prefixed with either `Iconography.SuccessIcon` (pass) or `Iconography.ErrorIcon` (fail).
1919
- Keep test execution logic consistent across hosts so lesson behavior is predictable.
2020

2121
## Adding A New Host

.github/instructions/architecture.instructions.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ This repository is a Blazor WebAssembly interactive learning site for JSON tooli
2424

2525
## Target Frameworks
2626

27-
- All projects target `net8.0`.
27+
- All projects target `net10.0`.
2828
- Do not change target frameworks unless explicitly requested.
2929

3030
## Dependency Direction
Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

3-
<PropertyGroup>
4-
<OutputType>WinExe</OutputType>
5-
<TargetFramework>net10.0-windows</TargetFramework>
6-
<Nullable>enable</Nullable>
7-
<ImplicitUsings>enable</ImplicitUsings>
8-
<UseWPF>true</UseWPF>
9-
</PropertyGroup>
3+
<PropertyGroup>
4+
<OutputType>WinExe</OutputType>
5+
<TargetFramework>net10.0-windows</TargetFramework>
6+
<Nullable>enable</Nullable>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<UseWPF>true</UseWPF>
9+
</PropertyGroup>
1010

11-
<ItemGroup> <PackageReference Include="JsonLogic" Version="6.0.2" />
12-
</ItemGroup>
11+
<ItemGroup>
12+
<PackageReference Include="JsonLogic" Version="6.0.2" />
13+
</ItemGroup>
1314

14-
<ItemGroup> <ProjectReference Include="..\LearnJsonEverything\LearnJsonEverything.csproj" />
15-
</ItemGroup>
15+
<ItemGroup>
16+
<ProjectReference Include="..\LearnJsonEverything\LearnJsonEverything.csproj" />
17+
</ItemGroup>
1618

17-
</Project>
19+
</Project>

LearnJsonEverything.LessonEditor/MainWindow.xaml

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,28 @@
88
mc:Ignorable="d"
99
Title="Learn json-everything - Lesson Editor"
1010
WindowState="Maximized"
11-
SnapsToDevicePixels="True"
12-
UseLayoutRounding="True"
11+
SnapsToDevicePixels="True"
12+
UseLayoutRounding="True"
1313
TextElement.FontSize="14"
1414
x:Name="Self">
15-
<Window.Resources>
16-
<hosts:SchemaHost x:Key="SchemaHost"/>
17-
<hosts:PathHost x:Key="PathHost"/>
18-
<hosts:JsonEHost x:Key="JsonEHost"/>
19-
</Window.Resources>
20-
<TabControl>
21-
<TabItem Header="JSON Schema">
22-
<controls:Editor FileName="schema.json" LessonHost="{StaticResource SchemaHost}"/>
23-
</TabItem>
24-
<TabItem Header="JSON Path">
25-
<controls:Editor FileName="path.json" LessonHost="{StaticResource PathHost}"/>
26-
</TabItem>
27-
<TabItem Header="JSON-e">
28-
<controls:Editor FileName="json-e.json" LessonHost="{StaticResource JsonEHost}"/>
29-
</TabItem>
30-
</TabControl>
31-
</Window>
15+
<Window.Resources>
16+
<hosts:SchemaHost x:Key="SchemaHost"/>
17+
<hosts:PathHost x:Key="PathHost"/>
18+
<hosts:JsonEHost x:Key="JsonEHost"/>
19+
<hosts:JsonPatchHost x:Key="JsonPatchHost"/>
20+
</Window.Resources>
21+
<TabControl>
22+
<TabItem Header="JSON Schema">
23+
<controls:Editor FileName="schema.json" LessonHost="{StaticResource SchemaHost}"/>
24+
</TabItem>
25+
<TabItem Header="JSON Path">
26+
<controls:Editor FileName="path.json" LessonHost="{StaticResource PathHost}"/>
27+
</TabItem>
28+
<TabItem Header="JSON-e">
29+
<controls:Editor FileName="json-e.json" LessonHost="{StaticResource JsonEHost}"/>
30+
</TabItem>
31+
<TabItem Header="JSON Patch">
32+
<controls:Editor FileName="json-patch.json" LessonHost="{StaticResource JsonPatchHost}"/>
33+
</TabItem>
34+
</TabControl>
35+
</Window>

LearnJsonEverything.Tests/ProvidedSolutionTests.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,22 @@ public void JsonLogic(LessonData lesson)
103103
Assert.That(result, Does.StartWith(Iconography.SuccessIcon));
104104
}
105105
}
106+
107+
public static IEnumerable<TestCaseData> JsonPatchLessons => GetLessons("json-patch.json");
108+
109+
[TestCaseSource(nameof(JsonPatchLessons))]
110+
public void JsonPatch(LessonData lesson)
111+
{
112+
var results = new JsonPatchHost().Run(lesson);
113+
114+
foreach (var result in results)
115+
{
116+
Console.WriteLine(result);
117+
}
118+
119+
foreach (var result in results)
120+
{
121+
Assert.That(result, Does.StartWith(Iconography.SuccessIcon));
122+
}
123+
}
106124
}

LearnJsonEverything.Tests/ReferenceLoader.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Json.JsonE;
22
using Json.Logic;
33
using Json.More;
4+
using Json.Patch;
45
using Json.Path;
56
using Json.Schema;
67
using Json.Schema.Generation;
@@ -20,6 +21,7 @@ static ReferenceLoader()
2021
Load<JsonPath>();
2122
Load<JsonFunction>();
2223
Load<Rule>();
24+
Load<JsonPatch>();
2325
}
2426

2527
private static void Load<T>(){}

LearnJsonEverything/LearnJsonEverything.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
<PackageReference Include="BlazorMonaco" Version="3.4.0" />
1717
<PackageReference Include="JsonE.Net" Version="3.0.1" />
1818
<PackageReference Include="JsonLogic" Version="6.0.2" />
19+
<PackageReference Include="JsonPatch.Net" Version="5.0.2" />
1920
<PackageReference Include="JsonPath.Net" Version="3.0.2" />
2021
<PackageReference Include="JsonSchema.Net" Version="9.2.0" />
2122
<PackageReference Include="JsonSchema.Net.Generation" Version="7.3.6" />
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
@page "/json-patch"
2+
@using LearnJsonEverything.Services.Hosts
3+
4+
<Teacher LessonSource="/data/lessons/json-patch.json" Host="@_host"></Teacher>
5+
6+
@code {
7+
private readonly ILessonHost _host = new JsonPatchHost();
8+
}

LearnJsonEverything/Services/CompilationHelpers.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public static class CompilationHelpers
2020
"Json.More",
2121
"JsonE.Net",
2222
"JsonLogic",
23+
"JsonPatch.Net",
2324
"JsonPath.Net",
2425
"JsonPointer.Net",
2526
"JsonSchema.Net",
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System.Text.Json.Nodes;
2+
using Json.More;
3+
4+
namespace LearnJsonEverything.Services.Hosts;
5+
6+
public class JsonPatchHost : ILessonHost
7+
{
8+
public string[] Run(LessonData lesson)
9+
{
10+
var (runner, errors) = CompilationHelpers.GetRunner<JsonNode?>(lesson);
11+
12+
if (runner is null) return errors;
13+
14+
var results = new List<string>();
15+
16+
var correct = true;
17+
foreach (var test in lesson.Tests)
18+
{
19+
var expectedResult = test!["result"];
20+
JsonNode? result = null;
21+
var printedTest = test.Print();
22+
try
23+
{
24+
result = runner.Run(test.AsObject());
25+
}
26+
catch
27+
{
28+
// ignored
29+
}
30+
31+
var localResult = expectedResult.IsEquivalentTo(result);
32+
correct &= localResult;
33+
results.Add($"{(localResult ? Iconography.SuccessIcon : Iconography.ErrorIcon)} {printedTest}");
34+
}
35+
36+
lesson.Achieved |= correct;
37+
38+
return [.. results];
39+
}
40+
}

0 commit comments

Comments
 (0)