Skip to content

Commit

Permalink
fix: Add Lenient System.Text StringEnumConverters for non-nullable Ve…
Browse files Browse the repository at this point in the history
…rwendungszweck+Gasqualität (never really worked before!); Bump MSTest.TestAdapter from 3.5.2 to 3.6.0 (#527)

* Bump MSTest.TestAdapter from 3.5.2 to 3.6.0

Bumps [MSTest.TestAdapter](https://github.com/microsoft/testfx) from 3.5.2 to 3.6.0.
- [Release notes](https://github.com/microsoft/testfx/releases)
- [Changelog](https://github.com/microsoft/testfx/blob/main/docs/Changelog.md)
- [Commits](microsoft/testfx@v3.5.2...v3.6.0)

---
updated-dependencies:
- dependency-name: MSTest.TestAdapter
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <[email protected]>

* fix method name

* shit never worked before!

was für eine ätzende kombination von verschiedenen fehlern

* more test coverage

---------

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: konstantin <[email protected]>
Co-authored-by: Konstantin <[email protected]>
  • Loading branch information
3 people authored Oct 7, 2024
1 parent 25c7f7b commit 42c3fe1
Show file tree
Hide file tree
Showing 7 changed files with 444 additions and 285 deletions.
51 changes: 48 additions & 3 deletions BO4E/meta/LenientConverters/GasqualitaetStringEnumConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
namespace BO4E.meta.LenientConverters;

/// <summary>
/// converts 'HGAS' to Enum Member Gasqualitaet.H_GAS (and 'LGAS' to Gasqualitaet.L_GAS)
/// converts 'HGAS' to Enum Member Gasqualitaet.H_GAS (and 'LGAS' to Gasqualitaet.L_GAS).
/// For non-nullable see <seealso cref="SystemTextGasqualitaetStringEnumConverter"/>.
/// </summary>
public class SystemTextGasqualitaetStringEnumConverter
public class SystemTextNullableGasqualitaetStringEnumConverter
: System.Text.Json.Serialization.JsonConverter<Gasqualitaet?>
{
/// <inheritdoc />
Expand Down Expand Up @@ -57,6 +58,50 @@ System.Text.Json.JsonSerializerOptions options
}
}

/// <summary>
/// converts 'HGAS' to Enum Member Gasqualitaet.H_GAS (and 'LGAS' to Gasqualitaet.L_GAS) for non-nullable Gasqualitaet.
/// <seealso cref="SystemTextNullableGasqualitaetStringEnumConverter"/>
/// </summary>
public class SystemTextGasqualitaetStringEnumConverter
: System.Text.Json.Serialization.JsonConverter<Gasqualitaet>
{
/// <inheritdoc />
public override Gasqualitaet Read(
ref System.Text.Json.Utf8JsonReader reader,
Type typeToConvert,
System.Text.Json.JsonSerializerOptions options
)
{
if (reader.TokenType == System.Text.Json.JsonTokenType.Number)
{
var integerValue = reader.GetInt64();
return (Gasqualitaet)Enum.ToObject(typeof(Gasqualitaet), integerValue);
}
string enumString = reader.GetString();

return enumString?.ToUpper() switch
{
"HGAS" => Gasqualitaet.H_GAS,
"LGAS" => Gasqualitaet.L_GAS,
_ => Enum.TryParse(enumString?.ToUpper(), out Gasqualitaet result)
? result
: throw new System.Text.Json.JsonException(
$"Invalid value for {typeToConvert}: {enumString}"
),
};
}

/// <inheritdoc />
public override void Write(
System.Text.Json.Utf8JsonWriter writer,
Gasqualitaet value,
System.Text.Json.JsonSerializerOptions options
)
{
writer.WriteStringValue(value.ToString());
}
}

/// <summary>
/// Converts 'HGAS' to Enum Member Gasqualitaet.H_GAS (and 'LGAS' to Gasqualitaet.L_GAS)
/// </summary>
Expand All @@ -70,7 +115,7 @@ System.Text.Json.JsonSerializerOptions options
/// };
/// </code>
/// </remarks>
/// <remarks><seealso cref="SystemTextGasqualitaetStringEnumConverter"/></remarks>
/// <remarks><seealso cref="SystemTextNullableGasqualitaetStringEnumConverter"/></remarks>
public class NewtonsoftGasqualitaetStringEnumConverter
: Newtonsoft.Json.JsonConverter<Gasqualitaet?>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ HashSet<string> userPropertiesWhiteList
settings.Converters.Add(new AutoNumberToStringConverter());
settings.Converters.Add(new VertragsConverter());
settings.Converters.Add(new EnergiemengeConverter());
settings.Converters.Add(new SystemTextNullableVerwendungszweckStringEnumConverter());
settings.Converters.Add(new SystemTextVerwendungszweckStringEnumConverter());
settings.Converters.Add(new SystemTextNullableGasqualitaetStringEnumConverter());
settings.Converters.Add(new SystemTextGasqualitaetStringEnumConverter());
settings.Converters.Add(new LenientSystemTextJsonStringToBoolConverter());
settings.Converters.Add(new StringNullableEnumConverter());
Expand Down
58 changes: 55 additions & 3 deletions BO4E/meta/LenientConverters/VerwendungszweckStringEnumConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
namespace BO4E.meta.LenientConverters;

/// <summary>
/// Converts 'MEHRMINDERMBENGENABRECHNUNG' (with typo!) to Enum Member <see cref="Verwendungszweck.MEHRMINDERMENGENABRECHNUNG"/>.
/// Converts 'MEHRMINDERMBENGENABRECHNUNG' (with typo!) to Enum Member <see cref="Verwendungszweck.MEHRMINDERMENGENABRECHNUNG"/> for nullable Verwendungszweck.
/// <seealso cref="SystemTextVerwendungszweckStringEnumConverter"/> for non-nullable Verwendungszweck.
/// </summary>
public class SystemTextVerwendungszweckStringEnumConverter
public class SystemTextNullableVerwendungszweckStringEnumConverter
: System.Text.Json.Serialization.JsonConverter<Verwendungszweck?>
{
/// <inheritdoc />
Expand Down Expand Up @@ -65,6 +66,57 @@ System.Text.Json.JsonSerializerOptions options
}
}

/// <summary>
/// Converts 'MEHRMINDERMBENGENABRECHNUNG' (with typo!) to Enum Member <see cref="Verwendungszweck.MEHRMINDERMENGENABRECHNUNG"/> for non-nullable Verwendungszweck.
/// See <seealso cref="SystemTextNullableVerwendungszweckStringEnumConverter"/> for nullable Verwendungszweck.
/// </summary>
public class SystemTextVerwendungszweckStringEnumConverter
: System.Text.Json.Serialization.JsonConverter<Verwendungszweck>
{
/// <inheritdoc />
public override Verwendungszweck Read(
ref System.Text.Json.Utf8JsonReader reader,
Type typeToConvert,
System.Text.Json.JsonSerializerOptions options
)
{
if (reader.TokenType == System.Text.Json.JsonTokenType.Number)
{
var integerValue = reader.GetInt64();
return (Verwendungszweck)Enum.ToObject(typeof(Verwendungszweck), integerValue);
}

if (reader.TokenType == System.Text.Json.JsonTokenType.String)
{
string enumString = reader.GetString();

return enumString switch
{
"MEHRMINDERMBENGENABRECHNUNG" => Verwendungszweck.MEHRMINDERMENGENABRECHNUNG,
_ => Enum.TryParse<Verwendungszweck>(enumString, true, out var result)
? result
: throw new System.Text.Json.JsonException(
$"Invalid value for {typeToConvert.Name}: {enumString}"
),
};
}

throw new System.Text.Json.JsonException(
$"Unexpected token parsing {typeToConvert.Name}. Expected String or Number, got {reader.TokenType}."
);
}

/// <inheritdoc />
public override void Write(
System.Text.Json.Utf8JsonWriter writer,
Verwendungszweck value,
System.Text.Json.JsonSerializerOptions options
)
{
writer.WriteStringValue(value.ToString());
}
}

/// <summary>
/// converts 'MEHRMINDERMBENGENABRECHNUNG' (with typo!) to Enum Member <see cref="Verwendungszweck.MEHRMINDERMENGENABRECHNUNG"/>
/// </summary>
Expand All @@ -78,7 +130,7 @@ System.Text.Json.JsonSerializerOptions options
/// };
/// </code>
/// </remarks>
/// <remarks><seealso cref="SystemTextVerwendungszweckStringEnumConverter"/></remarks>
/// <remarks><seealso cref="SystemTextNullableVerwendungszweckStringEnumConverter"/></remarks>
public class NewtonsoftVerwendungszweckStringEnumConverter
: Newtonsoft.Json.JsonConverter<Verwendungszweck?>
{
Expand Down
192 changes: 96 additions & 96 deletions BO4ETestProject/TestBO4E.csproj
Original file line number Diff line number Diff line change
@@ -1,96 +1,96 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>

<IsPackable>false</IsPackable>

<ApplicationIcon />

<OutputType>Library</OutputType>

<StartupObject />

<RootNamespace>TestBO4E-dotnet</RootNamespace>

<LangVersion>12</LangVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="FluentAssertions" Version="6.12.1" />
<PackageReference Include="JsonDiffPatch.Net" Version="2.3.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="MSTest.TestAdapter" Version="3.5.2" />
<PackageReference Include="MSTest.TestFramework" Version="3.6.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>

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

<ItemGroup>
<None Update="bo4eURITests\ansprechpartner_vorname.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="bo4eURITests\ansprechpartner_vorname_null.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="bo4eURITests\marktlokation_simple.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="bo4eURITests\marktteilnehmer.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="BoMapperTests\geschaeftspartner_non_lenient_enumlist.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="BoMapperTests\marktlokation_with_typenamehandling.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="BoMapperTests\rechnung.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="BoMapperTests\energiemenge_empty_verbrauch.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="BoMapperTests\energiemenge_lenient_datetime.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="BoMapperTests\geschaeftspartner_lenient_enumlist.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="BoMapperTests\marktlokation_broken_gasqualitaet.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="BoMapperTests\messlokation_userProps.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="BoMapperTests\messlokation_hf_sap.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="BoMapperTests\marktlokation_simple.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="BoMapperTests\Vertrag_lenient_String.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="testjsons\weatherforecast.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="testjsons\vertrag_numeric_versionstruktur.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="testjsons\zähler2.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="testjsons\zähler.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>

<IsPackable>false</IsPackable>

<ApplicationIcon />

<OutputType>Library</OutputType>

<StartupObject />

<RootNamespace>TestBO4E-dotnet</RootNamespace>

<LangVersion>12</LangVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="FluentAssertions" Version="6.12.1" />
<PackageReference Include="JsonDiffPatch.Net" Version="2.3.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="MSTest.TestAdapter" Version="3.6.0" />
<PackageReference Include="MSTest.TestFramework" Version="3.6.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>

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

<ItemGroup>
<None Update="bo4eURITests\ansprechpartner_vorname.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="bo4eURITests\ansprechpartner_vorname_null.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="bo4eURITests\marktlokation_simple.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="bo4eURITests\marktteilnehmer.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="BoMapperTests\geschaeftspartner_non_lenient_enumlist.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="BoMapperTests\marktlokation_with_typenamehandling.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="BoMapperTests\rechnung.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="BoMapperTests\energiemenge_empty_verbrauch.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="BoMapperTests\energiemenge_lenient_datetime.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="BoMapperTests\geschaeftspartner_lenient_enumlist.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="BoMapperTests\marktlokation_broken_gasqualitaet.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="BoMapperTests\messlokation_userProps.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="BoMapperTests\messlokation_hf_sap.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="BoMapperTests\marktlokation_simple.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="BoMapperTests\Vertrag_lenient_String.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="testjsons\weatherforecast.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="testjsons\vertrag_numeric_versionstruktur.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="testjsons\zähler2.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="testjsons\zähler.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
Loading

0 comments on commit 42c3fe1

Please sign in to comment.