Skip to content

Add Tests for provided solutions #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jun 9, 2024
Merged
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
5 changes: 3 additions & 2 deletions .github/workflows/publish-site.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ on:

jobs:
publish:
name: build, pack & publish
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
Expand All @@ -21,7 +20,9 @@ jobs:
run: dotnet restore
- name: Pre-build solution
run: dotnet build LearnJsonEverything.sln -c Release --no-restore
- name: Build
- name: Test
run: dotnet test LearnJsonEverything.sln -c Release --no-restore
- name: Publish
run: dotnet publish LearnJsonEverything/LearnJsonEverything.csproj -c Release --no-restore -o bin
- name: Add .nojekyll file
run: touch bin/wwwroot/.nojekyll
Expand Down
19 changes: 19 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: Verify solutions
on:
pull_request:

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: restore submodules
run: git submodule update --init
- name: Setup .NET Core
uses: actions/setup-dotnet@v2
with:
dotnet-version: 8.0.x
- name: Install dependencies
run: dotnet restore
- name: Test
run: dotnet test LearnJsonEverything.sln -c Release --no-restore
34 changes: 34 additions & 0 deletions LearnJsonEverything.Tests/LearnJsonEverything.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<None Include="..\LearnJsonEverything\wwwroot\data\lessons\*.yaml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="NUnit" Version="3.14.0" />
<PackageReference Include="NUnit.Analyzers" Version="3.9.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\LearnJsonEverything\LearnJsonEverything.csproj" />
</ItemGroup>

<ItemGroup>
<Using Include="NUnit.Framework" />
</ItemGroup>

</Project>
54 changes: 54 additions & 0 deletions LearnJsonEverything.Tests/ProvidedSolutionTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System.Text.Json;
using LearnJsonEverything.Services;
using LearnJsonEverything.Services.Runners;
using Yaml2JsonNode;

namespace LearnJsonEverything.Tests;

public class ProvidedSolutionTests
{
private static JsonSerializerOptions SerializerOptions =
new()
{
PropertyNameCaseInsensitive = true
};

[OneTimeSetUp]
public void Setup()
{
CompilationHelpers.TestOnly_SetReferences(ReferenceLoader.Load());
}

private static LessonPlan LoadLessonPlan(string filename)
{
var yamlText = File.ReadAllText(filename);
var yaml = YamlSerializer.Parse(yamlText);
var json = yaml.First().ToJsonNode();
return json.Deserialize<LessonPlan>(SerializerOptions)!;
}

public static IEnumerable<TestCaseData> SchemaLessons
{
get
{
var lessonPlan = LoadLessonPlan("schema.yaml");
return lessonPlan.Select(x =>
{
x.UserCode = x.Solution;
return new TestCaseData(x) { TestName = x.Title };
});
}
}

[TestCaseSource(nameof(SchemaLessons))]
public void Schema(LessonData lesson)
{
var results = SchemaRunner.Run(lesson);

foreach (var result in results)
{
Console.WriteLine(result);
Assert.That(result, Does.StartWith(Iconography.SuccessIcon));
}
}
}
43 changes: 43 additions & 0 deletions LearnJsonEverything.Tests/ReferenceLoader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System.Text.Json.Nodes;
using Json.Schema;
using Microsoft.CodeAnalysis;
using Yaml2JsonNode;

namespace LearnJsonEverything.Tests;

public static class ReferenceLoader
{
private class NullRunner : ILessonRunner<int>
{
public int Run(JsonObject context) => 0;
}

static ReferenceLoader()
{
// force some assemblies to load
SchemaRegistry.Global.Fetch = null!;
_ = YamlSerializer.Parse(string.Empty);
_ = new NullRunner();
}

public static MetadataReference[] Load()
{
var refs = AppDomain.CurrentDomain.GetAssemblies();
var assemblies = refs
.Where(x => !x.IsDynamic)
.OrderBy(x => x.FullName)
.ToArray();

var references = new MetadataReference[assemblies.Length];
int i = 0;
foreach (var assembly in assemblies)
{
Console.WriteLine($"Loading {assembly.FullName}...");
references[i] = MetadataReference.CreateFromFile(assembly.Location);
i++;
}

return references;
}

}
8 changes: 7 additions & 1 deletion LearnJsonEverything.sln
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ VisualStudioVersion = 17.9.34622.214
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LearnJsonEverything", "LearnJsonEverything\LearnJsonEverything.csproj", "{82FEC514-66E5-4034-9845-7C66B753D6FD}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LearnJsonEverything.Template", "LearnJsonEverything.Template\LearnJsonEverything.Template.csproj", "{B85D9C1F-3C5B-40E5-905B-9D5461E7E076}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LearnJsonEverything.Template", "LearnJsonEverything.Template\LearnJsonEverything.Template.csproj", "{B85D9C1F-3C5B-40E5-905B-9D5461E7E076}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LearnJsonEverything.Tests", "LearnJsonEverything.Tests\LearnJsonEverything.Tests.csproj", "{F01F40CE-A765-4DAB-85C2-39C391BAC00D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -21,6 +23,10 @@ Global
{B85D9C1F-3C5B-40E5-905B-9D5461E7E076}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B85D9C1F-3C5B-40E5-905B-9D5461E7E076}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B85D9C1F-3C5B-40E5-905B-9D5461E7E076}.Release|Any CPU.Build.0 = Release|Any CPU
{F01F40CE-A765-4DAB-85C2-39C391BAC00D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F01F40CE-A765-4DAB-85C2-39C391BAC00D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F01F40CE-A765-4DAB-85C2-39C391BAC00D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F01F40CE-A765-4DAB-85C2-39C391BAC00D}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
2 changes: 2 additions & 0 deletions LearnJsonEverything/Services/CompilationHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public static class CompilationHelpers
"Yaml2JsonNode",
];

public static void TestOnly_SetReferences(MetadataReference[] references) => _references = references;

public static async Task<MetadataReference[]?> LoadAssemblyReferences(HttpClient client)
{
if (_references is null)
Expand Down
1 change: 0 additions & 1 deletion LearnJsonEverything/Services/LessonPlan.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Collections;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;

namespace LearnJsonEverything.Services;
Expand Down
2 changes: 1 addition & 1 deletion LearnJsonEverything/wwwroot/data/lessons/schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@
}
}
solution: |-
builder.Type(SchemaValueType.String)
builder.Type(SchemaValueType.Number)
.ExclusiveMinimum(0)
.Maximum(10);
tests:
Expand Down
Loading