mirror of
https://github.com/MindWorkAI/AI-Studio.git
synced 2026-11-04 06:47:21 +00:00
Added a test which checks that all JSON options of the app resolve through generated contracts only
This commit is contained in:
2 files changed
+56
No files matched your search
@@ -0,0 +1,49 @@
|
|||||||
|
using System.Text.Json;
|
||||||
|
using System.Text.Json.Serialization;
|
||||||
|
|
||||||
|
using AIStudio.TrimSmokeTest;
|
||||||
|
|
||||||
|
namespace AIStudio.Tests.Serialization;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Checks that all options of the app get their contracts from source generation, never from reflection.
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// MWAIS0015 refuses options without a TypeInfoResolver while compiling, but cannot tell what the resolver does.
|
||||||
|
/// Only contexts throw for a type they do not know. Any other resolver, e.g., a DefaultJsonTypeInfoResolver which
|
||||||
|
/// a helper adds to the chain, builds the missing contract by reflection instead, and in a published build, the
|
||||||
|
/// code it needs for that may be gone.
|
||||||
|
/// </remarks>
|
||||||
|
[TestFixture]
|
||||||
|
public sealed class JsonOptionsResolverTests
|
||||||
|
{
|
||||||
|
private static IEnumerable<TestCaseData> AllOptions()
|
||||||
|
{
|
||||||
|
TestHost.PrepareStaticState();
|
||||||
|
return AppJsonOptions.FindAll().Select(options => new TestCaseData(options.Value).SetArgDisplayNames(options.Key));
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestCaseSource(nameof(AllOptions))]
|
||||||
|
public void TheOptionsResolveWithContextsOnly(JsonSerializerOptions options)
|
||||||
|
{
|
||||||
|
using (Assert.EnterMultipleScope())
|
||||||
|
{
|
||||||
|
Assert.That(options.TypeInfoResolverChain, Is.Not.Empty);
|
||||||
|
Assert.That(options.TypeInfoResolverChain, Is.All.InstanceOf<JsonSerializerContext>());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void FieldsAndPropertiesOfTheAppAreFound()
|
||||||
|
{
|
||||||
|
TestHost.PrepareStaticState();
|
||||||
|
var names = AppJsonOptions.FindAll().Keys.ToList();
|
||||||
|
|
||||||
|
using (Assert.EnterMultipleScope())
|
||||||
|
{
|
||||||
|
Assert.That(names, Does.Contain("SettingsManager.JSON_OPTIONS"));
|
||||||
|
Assert.That(names, Does.Contain("VisualBriefingJson.Canonical"));
|
||||||
|
Assert.That(names, Has.None.Contains("k__BackingField"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -45,6 +45,13 @@
|
|||||||
<ProjectReference Include="..\SourceGeneratedMappings\SourceGeneratedMappings.csproj" />
|
<ProjectReference Include="..\SourceGeneratedMappings\SourceGeneratedMappings.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
<!--
|
||||||
|
Compiled here as well, so that the tests check the same options of the app which the trim smoke test finds.
|
||||||
|
-->
|
||||||
|
<ItemGroup>
|
||||||
|
<Compile Include="..\TrimSmokeTest\AppJsonOptions.cs" Link="Serialization\AppJsonOptions.cs" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Using Include="NUnit.Framework" />
|
<Using Include="NUnit.Framework" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|||||||
Reference in new issue
Block a user