Skip to content

Commit 513549c

Browse files
committed
add json logic lessons
1 parent 3e57f61 commit 513549c

17 files changed

Lines changed: 359 additions & 25 deletions

File tree

.github/instructions/lesson-writing.instructions.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ Each lesson is an object in the lesson plan array with these fields:
5151
### Instructions
5252
- State exactly what the user must do, in one or two sentences.
5353
- Use imperative language: "Parse the path…", "Write a query that…", "Return null if…".
54-
- Name the required library action in the task (method/class/option/exception behavior) so the objective is implementation-focused rather than concept-only.
54+
- Name the required library action at the capability level (method family/class/option/exception behavior) so the objective is implementation-focused rather than concept-only.
55+
- Do not reveal the full implementation path in the task text. Avoid naming the exact operator or single method call when that would make the solution trivial.
5556
- Do not explain how — that belongs in the background.
5657
- Do not refer to the testing framework or expected test mechanics in user-facing task text.
5758

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
*.userosscache
1111
*.sln.docstates
1212
.github/instructions/personal-preferences.instructions.md
13+
.notes
1314

1415
# User-specific files (MonoDevelop/Xamarin Studio)
1516
*.userprefs

LearnJsonEverything.LessonEditor/LearnJsonEverything.LessonEditor.csproj

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88
<UseWPF>true</UseWPF>
99
</PropertyGroup>
1010

11-
<ItemGroup>
12-
<ProjectReference Include="..\LearnJsonEverything\LearnJsonEverything.csproj" />
11+
<ItemGroup> <PackageReference Include="JsonLogic" Version="6.0.2" />
12+
</ItemGroup>
13+
14+
<ItemGroup> <ProjectReference Include="..\LearnJsonEverything\LearnJsonEverything.csproj" />
1315
</ItemGroup>
1416

1517
</Project>

LearnJsonEverything.LessonEditor/ReferenceLoader.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Json.JsonE;
2+
using Json.Logic;
23
using Json.More;
34
using Json.Path;
45
using Json.Schema;
@@ -18,6 +19,7 @@ static ReferenceLoader()
1819
Load<MinimumAttribute>();
1920
Load<JsonPath>();
2021
Load<JsonFunction>();
22+
Load<Rule>();
2123
}
2224

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

LearnJsonEverything.Tests/ProvidedSolutionTests.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,22 @@ public void JsonE(LessonData lesson)
8585
Assert.That(result, Does.StartWith(Iconography.SuccessIcon));
8686
}
8787
}
88+
89+
public static IEnumerable<TestCaseData> JsonLogicLessons => GetLessons("json-logic.json");
90+
91+
[TestCaseSource(nameof(JsonLogicLessons))]
92+
public void JsonLogic(LessonData lesson)
93+
{
94+
var results = new JsonLogicHost().Run(lesson);
95+
96+
foreach (var result in results)
97+
{
98+
Console.WriteLine(result);
99+
}
100+
101+
foreach (var result in results)
102+
{
103+
Assert.That(result, Does.StartWith(Iconography.SuccessIcon));
104+
}
105+
}
88106
}

LearnJsonEverything.Tests/ReferenceLoader.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Json.JsonE;
2+
using Json.Logic;
23
using Json.More;
34
using Json.Path;
45
using Json.Schema;
@@ -18,6 +19,7 @@ static ReferenceLoader()
1819
Load<MinimumAttribute>();
1920
Load<JsonPath>();
2021
Load<JsonFunction>();
22+
Load<Rule>();
2123
}
2224

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

LearnJsonEverything/LearnJsonEverything.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
<PackageReference Include="Blazored.LocalStorage" Version="4.5.0" />
1616
<PackageReference Include="BlazorMonaco" Version="3.4.0" />
1717
<PackageReference Include="JsonE.Net" Version="3.0.1" />
18+
<PackageReference Include="JsonLogic" Version="6.0.2" />
1819
<PackageReference Include="JsonPath.Net" Version="3.0.2" />
1920
<PackageReference Include="JsonSchema.Net" Version="9.2.0" />
2021
<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-logic"
2+
@using LearnJsonEverything.Services.Hosts
3+
4+
<Teacher LessonSource="/data/lessons/json-logic.json" Host="@_host"></Teacher>
5+
6+
@code {
7+
private readonly ILessonHost _host = new JsonLogicHost();
8+
}

LearnJsonEverything/Services/CompilationHelpers.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using Microsoft.CodeAnalysis.CSharp;
44
using System.Reflection;
55
using System.Runtime.Loader;
6+
using Microsoft.CodeAnalysis.Emit;
67
using static LearnJsonEverything.Services.Iconography;
78

89
namespace LearnJsonEverything.Services;
@@ -18,6 +19,7 @@ public static class CompilationHelpers
1819
[
1920
"Json.More",
2021
"JsonE.Net",
22+
"JsonLogic",
2123
"JsonPath.Net",
2224
"JsonPointer.Net",
2325
"JsonSchema.Net",
@@ -95,7 +97,16 @@ public static (ILessonRunner<T>?, string[]) GetRunner<T>(LessonData lesson)
9597
using var dllStream = new MemoryStream();
9698
using var pdbStream = new MemoryStream();
9799
using var xmlStream = new MemoryStream();
98-
var emitResult = compilation.Emit(dllStream, pdbStream, xmlStream);
100+
EmitResult emitResult;
101+
if (IsBrowserRuntime)
102+
{
103+
// Avoid debugger-agent assertions in WASM by not emitting debug symbols.
104+
emitResult = compilation.Emit(dllStream, xmlDocumentationStream: xmlStream);
105+
}
106+
else
107+
{
108+
emitResult = compilation.Emit(dllStream, pdbStream, xmlStream);
109+
}
99110
if (!emitResult.Success)
100111
{
101112
var diagnostics = new List<string>();
@@ -114,8 +125,9 @@ public static (ILessonRunner<T>?, string[]) GetRunner<T>(LessonData lesson)
114125
}
115126

116127
dllStream.Position = 0;
117-
pdbStream.Position = 0;
118128
xmlStream.Position = 0;
129+
if (!IsBrowserRuntime)
130+
pdbStream.Position = 0;
119131

120132
#pragma warning disable IL2026
121133
#pragma warning disable IL2072
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 JsonLogicHost : 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+
// ignore
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)