Skip to content

Commit

Permalink
Add snapshot test for en locale schema (#518)
Browse files Browse the repository at this point in the history
* Add snapshot test for en locale schema

* Enable JObject sorting

* Use net472 for Bogus.Tests.csproj
set .editorconfig and .gitattributes for Verify
update docs on net472 requirement
  • Loading branch information
bchavez committed Dec 21, 2023
1 parent fe8594b commit bd43a1e
Show file tree
Hide file tree
Showing 6 changed files with 10,596 additions and 2 deletions.
10 changes: 10 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,13 @@ indent_size = 4
indent_style = space
indent_size = 2


# Verify settings
[*.{received,verified}.{txt,xml,json}]
charset = "utf-8-bom"
end_of_line = lf
indent_size = unset
indent_style = unset
insert_final_newline = false
tab_width = unset
trim_trailing_whitespace = false
6 changes: 6 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,9 @@
*.vbproj merge=union
*.fsproj merge=union
*.dbproj merge=union


*.verified.txt text eol=lf working-tree-encoding=UTF-8
*.verified.xml text eol=lf working-tree-encoding=UTF-8
*.verified.json text eol=lf working-tree-encoding=UTF-8

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1179,7 +1179,7 @@ The following section is only useful for people looking to contribute to **Bogus
The minimum requirements to build **Bogus** from source code are as follows:
* **Windows 11** or later.
* [**Git for Windows**](https://git-scm.com/downloads) `v2.39.2` or later.
* [**.NET Framework**](https://dotnet.microsoft.com/download/dotnet-framework) `v4.7.1`.
* [**.NET Framework**](https://dotnet.microsoft.com/download/dotnet-framework) `v4.7.2`.
* **.NET SDK**
* LTS [`SDK v8.0.100`](https://dotnet.microsoft.com/download/dotnet/8.0)
* LTS [`SDK v6.0.417`](https://dotnet.microsoft.com/download/dotnet/6.0)
Expand Down
3 changes: 2 additions & 1 deletion Source/Bogus.Tests/Bogus.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net471;net6.0;net8.0</TargetFrameworks>
<TargetFrameworks>net472;net6.0;net8.0</TargetFrameworks>
<AssemblyOriginatorKeyFile></AssemblyOriginatorKeyFile>
<SignAssembly>false</SignAssembly>
</PropertyGroup>
Expand All @@ -10,6 +10,7 @@
<PackageReference Include="morelinq" Version="3.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Rant" Version="3.0.530" />
<PackageReference Include="Verify.Xunit" Version="22.8.0" />
<PackageReference Include="xunit" Version="2.6.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.5">
<PrivateAssets>all</PrivateAssets>
Expand Down
78 changes: 78 additions & 0 deletions Source/Bogus.Tests/SchemaTests/EnLocaleSchemaTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#if NET6_0_OR_GREATER
using Argon;
using System;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using VerifyTests;
using VerifyXunit;
using Xunit;
using static VerifyXunit.Verifier;

namespace Bogus.Tests.SchemaTests;


public static class ModuleInit
{
[ModuleInitializer]
public static void Init()
{
VerifierSettings.SortJsonObjects();
}
}


[UsesVerify]
public class EnLocaleSchemaTests
{
[Fact]
public Task ensure_wellknown_en_locale_schema()
{
var localeJson = File.ReadAllText("../../../../Bogus/data/en.locale.json");

var enLocale = JToken.Parse(localeJson);

var settings = new VerifySettings();

settings.AddExtraSettings(jss => jss.ContractResolver = new InterceptedContractResolver(jss.ContractResolver));

return Verify(enLocale, settings);
}
}

public class InterceptedContractResolver : IContractResolver
{
private readonly IContractResolver defaultResolver;

public InterceptedContractResolver(IContractResolver defaultResolver)
{
this.defaultResolver = defaultResolver;
}

public JsonNameTable GetNameTable()
{
return defaultResolver.GetNameTable();
}

public JsonContract ResolveContract(Type type)
{
var contract = this.defaultResolver.ResolveContract(type);
if( contract is JsonDictionaryContract jdc )
{
var defaultIntercept = jdc.InterceptSerializeItem;
jdc.InterceptSerializeItem = (key, val) => {
if( val is JArray arr && arr.Children().OfType<JValue>().Any() )
{
var children = arr.Children();
var first = children.First();
return InterceptResult.Replace($"[Array {first.Type}; {children.Count()}]");
}
return defaultIntercept(key, val);
};
}
return contract;
}
}
#endif
Loading

0 comments on commit bd43a1e

Please sign in to comment.