Skip to content

Commit 92c2143

Browse files
committed
use full test object; reset scroll
1 parent 64a60d6 commit 92c2143

File tree

9 files changed

+242
-65
lines changed

9 files changed

+242
-65
lines changed

LearnJsonEverything/Services/InstructionsBuilder.cs

+43-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
using System.Text.Json;
1+
using System.Text;
22
using System.Text.Json.Nodes;
3+
using Humanizer;
34
using Json.More;
45

56
namespace LearnJsonEverything.Services;
@@ -66,14 +67,46 @@ private static string BuildApiLink(LessonData lesson) =>
6667

6768
private static string BuildTestList(JsonArray testData)
6869
{
69-
var tests = testData.Deserialize(SerializerContext.Default.SchemaTestArray)!;
70-
string[] lines =
71-
[
72-
"| Instance | Is Valid |",
73-
"|:-|:-:|",
74-
.. tests.Select(test => $"|`{test.Instance.AsJsonString()}`|{test.IsValid}|")
75-
];
76-
77-
return string.Join(Environment.NewLine, lines);
70+
var lines = new List<string>();
71+
72+
var sample = testData[0]!.AsObject();
73+
lines.Add($"|{string.Join("|", sample.Select(x => x.Key.Pascalize().Humanize()))}|");
74+
lines.Add($"|{string.Join("|", sample.Select(_ => ":-"))}|");
75+
76+
foreach (var test in testData)
77+
{
78+
var lineContent = new StringBuilder("|");
79+
foreach (var kvp in sample)
80+
{
81+
lineContent.Append(MaybeCode(test![kvp.Key], kvp.Key));
82+
lineContent.Append('|');
83+
}
84+
85+
lines.Add(lineContent.ToString());
86+
}
87+
88+
return string.Join(Environment.NewLine, lines);
89+
90+
//string[] lines =
91+
// [
92+
// "| Instance | Is Valid |",
93+
// "|:-|:-:|",
94+
// .. testData.Select(test => $"|`{test!["instance"]!.Print()}`|{test["isValid"]!.Print()}|")
95+
// ];
96+
97+
// return string.Join(Environment.NewLine, lines);
7898
}
99+
100+
private static readonly string[] KeysToFormat =
101+
[
102+
"instance",
103+
"format"
104+
];
105+
106+
private static string MaybeCode(JsonNode? node, string key)
107+
{
108+
if (KeysToFormat.Contains(key)) return $"`{node.Print()}`";
109+
110+
return node?.Print();
111+
}
79112
}

LearnJsonEverything/Services/LessonData.cs

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ public class LessonData
88
{
99
public int Index { get; set; }
1010
public Guid Id { get; set; }
11+
public bool Skip { get; set; }
1112
public string Title { get; set; }
1213
public string Background { get; set; }
1314
public string Docs { get; set; }

LearnJsonEverything/Services/LessonPlan.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public class LessonPlanJsonConverter : JsonConverter<LessonPlan>
6161
var lessonData = JsonSerializer.Deserialize<LessonData[]>(ref reader, options)!;
6262
#pragma warning restore IL2026 // Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code
6363

64-
return new LessonPlan(lessonData);
64+
return new LessonPlan(lessonData.Where(x => !x.Skip).ToArray());
6565
}
6666

6767
public override void Write(Utf8JsonWriter writer, LessonPlan value, JsonSerializerOptions options)
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,28 @@
1-
using System.Text.Json;
2-
using System.Text.Json.Nodes;
3-
using Json.More;
4-
using Json.Schema;
1+
using Json.Schema;
52

63
namespace LearnJsonEverything.Services.Runners;
74

85
public static class SchemaRunner
96
{
10-
public class SchemaTest
11-
{
12-
public JsonNode? Instance { get; set; }
13-
public bool IsValid { get; set; }
14-
}
15-
167
public static string[] Run(LessonData lesson)
178
{
189
var (runner, errors) = CompilationHelpers.GetRunner<EvaluationResults>(lesson);
1910

2011
if (runner is null) return errors;
2112

22-
var tests = lesson.Tests.Deserialize(SerializerContext.Default.SchemaTestArray)!;
2313
var results = new List<string>();
2414

2515
var correct = true;
26-
foreach (var test in tests)
16+
foreach (var test in lesson.Tests)
2717
{
28-
var result = runner.Run(new JsonObject { ["instance"] = test.Instance });
29-
correct &= test.IsValid == result.IsValid;
30-
results.Add($"{(test.IsValid == result.IsValid ? Iconography.SuccessIcon : Iconography.ErrorIcon)} {test.Instance.AsJsonString()}");
18+
var expectedValidity = test!["isValid"]!.GetValue<bool>();
19+
var result = runner.Run(test.AsObject());
20+
correct &= expectedValidity == result.IsValid;
21+
results.Add($"{(expectedValidity == result.IsValid ? Iconography.SuccessIcon : Iconography.ErrorIcon)} {test["instance"]!.Print()}");
3122
}
3223

3324
lesson.Achieved |= correct;
3425

3526
return [.. results];
3627
}
37-
}
28+
}

LearnJsonEverything/Services/SerializationHelpers.cs

+13-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,25 @@
1-
using LearnJsonEverything.Services.Runners;
1+
using System.Text.Encodings.Web;
2+
using System.Text.Json;
23
using Microsoft.CodeAnalysis.CSharp;
34
using System.Text.Json.Nodes;
45
using System.Text.Json.Serialization;
6+
using Json.More;
57
using Json.Schema;
68

79
namespace LearnJsonEverything.Services;
810

911
public static class SerializationHelpers
1012
{
13+
private static readonly JsonSerializerOptions _writeOptions =
14+
new()
15+
{
16+
TypeInfoResolverChain = { SerializerContext.Default },
17+
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
18+
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
19+
};
20+
21+
public static string Print(this JsonNode? node) => node.AsJsonString(_writeOptions);
22+
1123
public static string ToLiteral(this string valueTextForCompiler)
1224
{
1325
var formatted = SymbolDisplay.FormatLiteral(valueTextForCompiler, true);
@@ -22,6 +34,5 @@ public static string ToLiteral(this string valueTextForCompiler)
2234
[JsonSerializable(typeof(LessonPlan))]
2335
[JsonSerializable(typeof(LessonData[]))]
2436
[JsonSerializable(typeof(SchemaSaveData[]))]
25-
[JsonSerializable(typeof(SchemaRunner.SchemaTest[]))]
2637
[JsonSourceGenerationOptions(WriteIndented = true, PropertyNameCaseInsensitive = true)]
2738
internal partial class SerializerContext : JsonSerializerContext;

LearnJsonEverything/Shared/Teacher.razor

+4-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
</div>
3131
</div>
3232
<div class="col-4 d-flex h-100">
33-
<div class="grid-panel flex-grow-1 scroll">
33+
<div id="instructions-panel" class="grid-panel flex-grow-1 scroll">
3434
<MarkdownSpan Content="@Instructions"></MarkdownSpan>
3535
</div>
3636
</div>
@@ -84,6 +84,7 @@
8484
var userCode = await _codeEditor.GetValue();
8585

8686
await _outputEditor.SetValue(string.Empty);
87+
await _outputEditor.SetLanguageAsync("json", JsRuntime);
8788

8889
_currentLesson!.UserCode = userCode;
8990
var results = SchemaRunner.Run(_currentLesson);
@@ -95,7 +96,7 @@
9596
catch (Exception e)
9697
{
9798
await _outputEditor.SetLanguageAsync("text", JsRuntime);
98-
await _outputEditor.SetValue(e.Message);
99+
await _outputEditor.SetValue($"An exception occurred while executing user code:\n\n{e.GetType().Name}: {e.Message}\n{e.StackTrace}");
99100
}
100101
}
101102

@@ -124,6 +125,7 @@
124125
await _codeEditor.SetValue(_currentLesson.UserCode ?? string.Empty);
125126
await _outputEditor.SetValue(string.Empty);
126127
UpdateNavigation();
128+
await JsRuntime.InvokeVoidAsync("BlazorScrollToTopOfElement", "instructions-panel");
127129
}
128130

129131
private void UpdateNavigation()

LearnJsonEverything/wwwroot/css/app.css

+4
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ a,
9595
border-style: solid;
9696
}
9797

98+
.monaco-editor {
99+
position: absolute !important;
100+
}
101+
98102
#blazor-error-ui {
99103
background-color: #1e1e1e;
100104
bottom: 0;

0 commit comments

Comments
 (0)