Skip to content

Commit 5aadce3

Browse files
authored
Merge pull request #1 from gregsdennis/tests
Add Tests for provided solutions
2 parents 36f2c01 + d34b369 commit 5aadce3

File tree

9 files changed

+163
-5
lines changed

9 files changed

+163
-5
lines changed

.github/workflows/publish-site.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ on:
77

88
jobs:
99
publish:
10-
name: build, pack & publish
1110
runs-on: ubuntu-latest
1211
steps:
1312
- uses: actions/checkout@v3
@@ -21,7 +20,9 @@ jobs:
2120
run: dotnet restore
2221
- name: Pre-build solution
2322
run: dotnet build LearnJsonEverything.sln -c Release --no-restore
24-
- name: Build
23+
- name: Test
24+
run: dotnet test LearnJsonEverything.sln -c Release --no-restore
25+
- name: Publish
2526
run: dotnet publish LearnJsonEverything/LearnJsonEverything.csproj -c Release --no-restore -o bin
2627
- name: Add .nojekyll file
2728
run: touch bin/wwwroot/.nojekyll

.github/workflows/test.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
name: Verify solutions
2+
on:
3+
pull_request:
4+
5+
jobs:
6+
test:
7+
runs-on: ubuntu-latest
8+
steps:
9+
- uses: actions/checkout@v3
10+
- name: restore submodules
11+
run: git submodule update --init
12+
- name: Setup .NET Core
13+
uses: actions/setup-dotnet@v2
14+
with:
15+
dotnet-version: 8.0.x
16+
- name: Install dependencies
17+
run: dotnet restore
18+
- name: Test
19+
run: dotnet test LearnJsonEverything.sln -c Release --no-restore
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
8+
<IsPackable>false</IsPackable>
9+
<IsTestProject>true</IsTestProject>
10+
</PropertyGroup>
11+
12+
<ItemGroup>
13+
<None Include="..\LearnJsonEverything\wwwroot\data\lessons\*.yaml">
14+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
15+
</None>
16+
</ItemGroup>
17+
18+
<ItemGroup>
19+
<PackageReference Include="coverlet.collector" Version="6.0.0" />
20+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
21+
<PackageReference Include="NUnit" Version="3.14.0" />
22+
<PackageReference Include="NUnit.Analyzers" Version="3.9.0" />
23+
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
24+
</ItemGroup>
25+
26+
<ItemGroup>
27+
<ProjectReference Include="..\LearnJsonEverything\LearnJsonEverything.csproj" />
28+
</ItemGroup>
29+
30+
<ItemGroup>
31+
<Using Include="NUnit.Framework" />
32+
</ItemGroup>
33+
34+
</Project>
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using System.Text.Json;
2+
using LearnJsonEverything.Services;
3+
using LearnJsonEverything.Services.Runners;
4+
using Yaml2JsonNode;
5+
6+
namespace LearnJsonEverything.Tests;
7+
8+
public class ProvidedSolutionTests
9+
{
10+
private static JsonSerializerOptions SerializerOptions =
11+
new()
12+
{
13+
PropertyNameCaseInsensitive = true
14+
};
15+
16+
[OneTimeSetUp]
17+
public void Setup()
18+
{
19+
CompilationHelpers.TestOnly_SetReferences(ReferenceLoader.Load());
20+
}
21+
22+
private static LessonPlan LoadLessonPlan(string filename)
23+
{
24+
var yamlText = File.ReadAllText(filename);
25+
var yaml = YamlSerializer.Parse(yamlText);
26+
var json = yaml.First().ToJsonNode();
27+
return json.Deserialize<LessonPlan>(SerializerOptions)!;
28+
}
29+
30+
public static IEnumerable<TestCaseData> SchemaLessons
31+
{
32+
get
33+
{
34+
var lessonPlan = LoadLessonPlan("schema.yaml");
35+
return lessonPlan.Select(x =>
36+
{
37+
x.UserCode = x.Solution;
38+
return new TestCaseData(x) { TestName = x.Title };
39+
});
40+
}
41+
}
42+
43+
[TestCaseSource(nameof(SchemaLessons))]
44+
public void Schema(LessonData lesson)
45+
{
46+
var results = SchemaRunner.Run(lesson);
47+
48+
foreach (var result in results)
49+
{
50+
Console.WriteLine(result);
51+
Assert.That(result, Does.StartWith(Iconography.SuccessIcon));
52+
}
53+
}
54+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using System.Text.Json.Nodes;
2+
using Json.Schema;
3+
using Microsoft.CodeAnalysis;
4+
using Yaml2JsonNode;
5+
6+
namespace LearnJsonEverything.Tests;
7+
8+
public static class ReferenceLoader
9+
{
10+
private class NullRunner : ILessonRunner<int>
11+
{
12+
public int Run(JsonObject context) => 0;
13+
}
14+
15+
static ReferenceLoader()
16+
{
17+
// force some assemblies to load
18+
SchemaRegistry.Global.Fetch = null!;
19+
_ = YamlSerializer.Parse(string.Empty);
20+
_ = new NullRunner();
21+
}
22+
23+
public static MetadataReference[] Load()
24+
{
25+
var refs = AppDomain.CurrentDomain.GetAssemblies();
26+
var assemblies = refs
27+
.Where(x => !x.IsDynamic)
28+
.OrderBy(x => x.FullName)
29+
.ToArray();
30+
31+
var references = new MetadataReference[assemblies.Length];
32+
int i = 0;
33+
foreach (var assembly in assemblies)
34+
{
35+
Console.WriteLine($"Loading {assembly.FullName}...");
36+
references[i] = MetadataReference.CreateFromFile(assembly.Location);
37+
i++;
38+
}
39+
40+
return references;
41+
}
42+
43+
}

LearnJsonEverything.sln

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ VisualStudioVersion = 17.9.34622.214
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LearnJsonEverything", "LearnJsonEverything\LearnJsonEverything.csproj", "{82FEC514-66E5-4034-9845-7C66B753D6FD}"
77
EndProject
8-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LearnJsonEverything.Template", "LearnJsonEverything.Template\LearnJsonEverything.Template.csproj", "{B85D9C1F-3C5B-40E5-905B-9D5461E7E076}"
8+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LearnJsonEverything.Template", "LearnJsonEverything.Template\LearnJsonEverything.Template.csproj", "{B85D9C1F-3C5B-40E5-905B-9D5461E7E076}"
9+
EndProject
10+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LearnJsonEverything.Tests", "LearnJsonEverything.Tests\LearnJsonEverything.Tests.csproj", "{F01F40CE-A765-4DAB-85C2-39C391BAC00D}"
911
EndProject
1012
Global
1113
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -21,6 +23,10 @@ Global
2123
{B85D9C1F-3C5B-40E5-905B-9D5461E7E076}.Debug|Any CPU.Build.0 = Debug|Any CPU
2224
{B85D9C1F-3C5B-40E5-905B-9D5461E7E076}.Release|Any CPU.ActiveCfg = Release|Any CPU
2325
{B85D9C1F-3C5B-40E5-905B-9D5461E7E076}.Release|Any CPU.Build.0 = Release|Any CPU
26+
{F01F40CE-A765-4DAB-85C2-39C391BAC00D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
27+
{F01F40CE-A765-4DAB-85C2-39C391BAC00D}.Debug|Any CPU.Build.0 = Debug|Any CPU
28+
{F01F40CE-A765-4DAB-85C2-39C391BAC00D}.Release|Any CPU.ActiveCfg = Release|Any CPU
29+
{F01F40CE-A765-4DAB-85C2-39C391BAC00D}.Release|Any CPU.Build.0 = Release|Any CPU
2430
EndGlobalSection
2531
GlobalSection(SolutionProperties) = preSolution
2632
HideSolutionNode = FALSE

LearnJsonEverything/Services/CompilationHelpers.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ public static class CompilationHelpers
1919
"Yaml2JsonNode",
2020
];
2121

22+
public static void TestOnly_SetReferences(MetadataReference[] references) => _references = references;
23+
2224
public static async Task<MetadataReference[]?> LoadAssemblyReferences(HttpClient client)
2325
{
2426
if (_references is null)

LearnJsonEverything/Services/LessonPlan.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System.Collections;
22
using System.Text.Json;
3-
using System.Text.Json.Nodes;
43
using System.Text.Json.Serialization;
54

65
namespace LearnJsonEverything.Services;

LearnJsonEverything/wwwroot/data/lessons/schema.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@
200200
}
201201
}
202202
solution: |-
203-
builder.Type(SchemaValueType.String)
203+
builder.Type(SchemaValueType.Number)
204204
.ExclusiveMinimum(0)
205205
.Maximum(10);
206206
tests:

0 commit comments

Comments
 (0)