diff --git a/Directory.Build.props b/Directory.Build.props index 091ef215..4c5e4697 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -12,7 +12,7 @@ https://github.com/PawelGerr/Thinktecture.Runtime.Extensions false Thinktecture - net7.0;net8.0 + net7.0 13.0 true enable diff --git a/samples/Directory.Build.props b/samples/Directory.Build.props index 8830293d..9b43e343 100644 --- a/samples/Directory.Build.props +++ b/samples/Directory.Build.props @@ -7,7 +7,7 @@ - net9.0 + net9.0 $(NoWarn);CS1591;CA1052;CA1062;CA1303;CA1707;CA1801;CA2000;NU1903 false disable diff --git a/src/Thinktecture.Runtime.Extensions.EntityFrameworkCore8/Thinktecture.Runtime.Extensions.EntityFrameworkCore8.csproj b/src/Thinktecture.Runtime.Extensions.EntityFrameworkCore8/Thinktecture.Runtime.Extensions.EntityFrameworkCore8.csproj index c82d49f5..c5af9e96 100644 --- a/src/Thinktecture.Runtime.Extensions.EntityFrameworkCore8/Thinktecture.Runtime.Extensions.EntityFrameworkCore8.csproj +++ b/src/Thinktecture.Runtime.Extensions.EntityFrameworkCore8/Thinktecture.Runtime.Extensions.EntityFrameworkCore8.csproj @@ -4,7 +4,7 @@ Extends Entity Framework Core to support some components from Thinktecture.Runtime.Extensions. common;EntityFrameworkCore $(DefineConstants);COMPLEX_TYPES - net8.0 + net8.0 diff --git a/src/Thinktecture.Runtime.Extensions.EntityFrameworkCore9/Thinktecture.Runtime.Extensions.EntityFrameworkCore9.csproj b/src/Thinktecture.Runtime.Extensions.EntityFrameworkCore9/Thinktecture.Runtime.Extensions.EntityFrameworkCore9.csproj index f6804548..94c077b6 100644 --- a/src/Thinktecture.Runtime.Extensions.EntityFrameworkCore9/Thinktecture.Runtime.Extensions.EntityFrameworkCore9.csproj +++ b/src/Thinktecture.Runtime.Extensions.EntityFrameworkCore9/Thinktecture.Runtime.Extensions.EntityFrameworkCore9.csproj @@ -4,7 +4,7 @@ Extends Entity Framework Core to support some components from Thinktecture.Runtime.Extensions. common;EntityFrameworkCore $(DefineConstants);COMPLEX_TYPES - net8.0 + net8.0 diff --git a/src/Thinktecture.Runtime.Extensions.SourceGenerator/CodeAnalysis/AdHocUnions/AdHocUnionCodeGenerator.cs b/src/Thinktecture.Runtime.Extensions.SourceGenerator/CodeAnalysis/AdHocUnions/AdHocUnionCodeGenerator.cs index 5dab5261..6c6f417d 100644 --- a/src/Thinktecture.Runtime.Extensions.SourceGenerator/CodeAnalysis/AdHocUnions/AdHocUnionCodeGenerator.cs +++ b/src/Thinktecture.Runtime.Extensions.SourceGenerator/CodeAnalysis/AdHocUnions/AdHocUnionCodeGenerator.cs @@ -447,7 +447,17 @@ private void GenerateSwitchForAction(bool withState, bool isPartially) _sb.Append(" = null"); } - _sb.Append(@") + _sb.Append(")"); + + if (withState) + { + _sb.Append(@" +#if NET9_0_OR_GREATER + where TState : allows ref struct +#endif"); + } + + _sb.Append(@" {"); GenerateIndexBasedActionSwitchBody(withState, isPartially); @@ -598,6 +608,17 @@ private void GenerateSwitchForFunc(bool withState, bool isPartially) } _sb.Append(@") +#if NET9_0_OR_GREATER + where TResult : allows ref struct"); + + if (withState) + { + _sb.Append(@" + where TState : allows ref struct"); + } + + _sb.Append(@" +#endif {"); GenerateIndexBasedFuncSwitchBody(withState, isPartially); @@ -723,6 +744,9 @@ private void GenerateMap(bool isPartially) } _sb.Append(@") +#if NET9_0_OR_GREATER + where TResult : allows ref struct +#endif {"); GenerateIndexBasedMapSwitchBody(isPartially); diff --git a/src/Thinktecture.Runtime.Extensions.SourceGenerator/CodeAnalysis/SmartEnums/SmartEnumCodeGenerator.cs b/src/Thinktecture.Runtime.Extensions.SourceGenerator/CodeAnalysis/SmartEnums/SmartEnumCodeGenerator.cs index 2fbad4f9..437c57ca 100644 --- a/src/Thinktecture.Runtime.Extensions.SourceGenerator/CodeAnalysis/SmartEnums/SmartEnumCodeGenerator.cs +++ b/src/Thinktecture.Runtime.Extensions.SourceGenerator/CodeAnalysis/SmartEnums/SmartEnumCodeGenerator.cs @@ -354,7 +354,17 @@ private void GenerateSwitchForAction(bool withState, bool isPartially) _sb.Append(" = null"); } - _sb.Append(@") + _sb.Append(")"); + + if (withState) + { + _sb.Append(@" +#if NET9_0_OR_GREATER + where TState : allows ref struct +#endif"); + } + + _sb.Append(@" {"); if (_state.Settings.IsValidatable) @@ -554,6 +564,17 @@ private void GenerateSwitchForFunc(bool withState, bool isPartially) } _sb.Append(@") +#if NET9_0_OR_GREATER + where TResult : allows ref struct"); + + if (withState) + { + _sb.Append(@" + where TState : allows ref struct"); + } + + _sb.Append(@" +#endif {"); if (_state.Settings.IsValidatable) @@ -724,6 +745,9 @@ private void GenerateMap(bool isPartially) } _sb.Append(@") +#if NET9_0_OR_GREATER + where TResult : allows ref struct +#endif {"); if (_state.Settings.IsValidatable) diff --git a/src/Thinktecture.Runtime.Extensions.SourceGenerator/CodeAnalysis/Unions/UnionCodeGenerator.cs b/src/Thinktecture.Runtime.Extensions.SourceGenerator/CodeAnalysis/Unions/UnionCodeGenerator.cs index c8389ff3..1ef593ca 100644 --- a/src/Thinktecture.Runtime.Extensions.SourceGenerator/CodeAnalysis/Unions/UnionCodeGenerator.cs +++ b/src/Thinktecture.Runtime.Extensions.SourceGenerator/CodeAnalysis/Unions/UnionCodeGenerator.cs @@ -250,7 +250,17 @@ private void GenerateSwitchForAction(bool withState, bool isPartially) _sb.Append(" = null"); } - _sb.Append(@") + _sb.Append(")"); + + if (withState) + { + _sb.Append(@" +#if NET9_0_OR_GREATER + where TState : allows ref struct +#endif"); + } + + _sb.Append(@" {"); GenerateIndexBasedActionSwitchBody(withState, isPartially); @@ -387,6 +397,17 @@ private void GenerateSwitchForFunc(bool withState, bool isPartially) } _sb.Append(@") +#if NET9_0_OR_GREATER + where TResult : allows ref struct"); + + if (withState) + { + _sb.Append(@" + where TState : allows ref struct"); + } + + _sb.Append(@" +#endif {"); GenerateIndexBasedFuncSwitchBody(withState, isPartially); @@ -499,6 +520,9 @@ private void GenerateMap(bool isPartially) } _sb.Append(@") +#if NET9_0_OR_GREATER + where TResult : allows ref struct +#endif {"); GenerateIndexBasedMapSwitchBody(isPartially); diff --git a/src/Thinktecture.Runtime.Extensions.SourceGenerator/Thinktecture.Runtime.Extensions.SourceGenerator.csproj b/src/Thinktecture.Runtime.Extensions.SourceGenerator/Thinktecture.Runtime.Extensions.SourceGenerator.csproj index e04c4d01..eb8d0e28 100644 --- a/src/Thinktecture.Runtime.Extensions.SourceGenerator/Thinktecture.Runtime.Extensions.SourceGenerator.csproj +++ b/src/Thinktecture.Runtime.Extensions.SourceGenerator/Thinktecture.Runtime.Extensions.SourceGenerator.csproj @@ -1,7 +1,6 @@ Source generators for Thinktecture.Runtime.Extensions. - netstandard2.0 sourcegenerator $(NoWarn);CS1591;RS2008;NU5128 diff --git a/src/Thinktecture.Runtime.Extensions/Argument.cs b/src/Thinktecture.Runtime.Extensions/Argument.cs index f17730d0..a7077d49 100644 --- a/src/Thinktecture.Runtime.Extensions/Argument.cs +++ b/src/Thinktecture.Runtime.Extensions/Argument.cs @@ -5,6 +5,9 @@ namespace Thinktecture; /// /// The type of the parameter. public readonly ref struct Argument +#if NET9_0_OR_GREATER + where T : allows ref struct +#endif { /// /// Indication whether the argument is set (explicitly). diff --git a/src/Thinktecture.Runtime.Extensions/Thinktecture.Runtime.Extensions.csproj b/src/Thinktecture.Runtime.Extensions/Thinktecture.Runtime.Extensions.csproj index 3242b852..131556ff 100644 --- a/src/Thinktecture.Runtime.Extensions/Thinktecture.Runtime.Extensions.csproj +++ b/src/Thinktecture.Runtime.Extensions/Thinktecture.Runtime.Extensions.csproj @@ -3,6 +3,8 @@ Provides an easy way to implement Smart Enums and Value Objects. smart-enum;value-object; + + net7.0;net9.0; diff --git a/test/Directory.Build.props b/test/Directory.Build.props index 079b3763..58a60b15 100644 --- a/test/Directory.Build.props +++ b/test/Directory.Build.props @@ -13,6 +13,7 @@ $(NoWarn);CS1591;CA1707;NU1903;NETSDK1206;NU1904 Thinktecture.Runtime.Tests true + net7.0;net8.0;net9.0 diff --git a/test/Thinktecture.Runtime.Extensions.EntityFrameworkCore8.Tests/Thinktecture.Runtime.Extensions.EntityFrameworkCore8.Tests.csproj b/test/Thinktecture.Runtime.Extensions.EntityFrameworkCore8.Tests/Thinktecture.Runtime.Extensions.EntityFrameworkCore8.Tests.csproj index 0837df25..45e09771 100644 --- a/test/Thinktecture.Runtime.Extensions.EntityFrameworkCore8.Tests/Thinktecture.Runtime.Extensions.EntityFrameworkCore8.Tests.csproj +++ b/test/Thinktecture.Runtime.Extensions.EntityFrameworkCore8.Tests/Thinktecture.Runtime.Extensions.EntityFrameworkCore8.Tests.csproj @@ -2,7 +2,7 @@ $(DefineConstants);COMPLEX_TYPES - net8.0 + net8.0;net9.0 diff --git a/test/Thinktecture.Runtime.Extensions.EntityFrameworkCore9.Tests/Thinktecture.Runtime.Extensions.EntityFrameworkCore9.Tests.csproj b/test/Thinktecture.Runtime.Extensions.EntityFrameworkCore9.Tests/Thinktecture.Runtime.Extensions.EntityFrameworkCore9.Tests.csproj index 30d2198e..f7863f78 100644 --- a/test/Thinktecture.Runtime.Extensions.EntityFrameworkCore9.Tests/Thinktecture.Runtime.Extensions.EntityFrameworkCore9.Tests.csproj +++ b/test/Thinktecture.Runtime.Extensions.EntityFrameworkCore9.Tests/Thinktecture.Runtime.Extensions.EntityFrameworkCore9.Tests.csproj @@ -2,7 +2,7 @@ $(DefineConstants);COMPLEX_TYPES - net8.0 + net8.0;net9.0 diff --git a/test/Thinktecture.Runtime.Extensions.SourceGenerator.Tests/SourceGeneratorTests/AdHocUnionSourceGeneratorTests.cs b/test/Thinktecture.Runtime.Extensions.SourceGenerator.Tests/SourceGeneratorTests/AdHocUnionSourceGeneratorTests.cs index cfd05252..cdb42f03 100644 --- a/test/Thinktecture.Runtime.Extensions.SourceGenerator.Tests/SourceGeneratorTests/AdHocUnionSourceGeneratorTests.cs +++ b/test/Thinktecture.Runtime.Extensions.SourceGenerator.Tests/SourceGeneratorTests/AdHocUnionSourceGeneratorTests.cs @@ -133,6 +133,9 @@ public void Switch( TState state, global::System.Action @string, global::System.Action @int32) + #if NET9_0_OR_GREATER + where TState : allows ref struct + #endif { switch (this._valueIndex) { @@ -155,6 +158,9 @@ public void Switch( public TResult Switch( global::System.Func @string, global::System.Func @int32) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + #endif { switch (this._valueIndex) { @@ -177,6 +183,10 @@ public TResult Switch( TState state, global::System.Func @string, global::System.Func @int32) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + where TState : allows ref struct + #endif { switch (this._valueIndex) { @@ -197,6 +207,9 @@ public TResult Switch( public TResult Map( TResult @string, TResult @int32) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + #endif { switch (this._valueIndex) { @@ -451,6 +464,9 @@ public void Switch( TState state, global::System.Action @string, global::System.Action @int32) + #if NET9_0_OR_GREATER + where TState : allows ref struct + #endif { switch (this._valueIndex) { @@ -476,6 +492,9 @@ public void Switch( public TResult Switch( global::System.Func @string, global::System.Func @int32) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + #endif { switch (this._valueIndex) { @@ -501,6 +520,10 @@ public TResult Switch( TState state, global::System.Func @string, global::System.Func @int32) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + where TState : allows ref struct + #endif { switch (this._valueIndex) { @@ -524,6 +547,9 @@ public TResult Switch( public TResult Map( TResult @string, TResult @int32) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + #endif { switch (this._valueIndex) { @@ -772,6 +798,9 @@ public void Switch( TState state, global::System.Action @string, global::System.Action @int32) + #if NET9_0_OR_GREATER + where TState : allows ref struct + #endif { switch (this._valueIndex) { @@ -797,6 +826,9 @@ public void Switch( public TResult Switch( global::System.Func @string, global::System.Func @int32) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + #endif { switch (this._valueIndex) { @@ -822,6 +854,10 @@ public TResult Switch( TState state, global::System.Func @string, global::System.Func @int32) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + where TState : allows ref struct + #endif { switch (this._valueIndex) { @@ -845,6 +881,9 @@ public TResult Switch( public TResult Map( TResult @string, TResult @int32) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + #endif { switch (this._valueIndex) { @@ -1088,6 +1127,9 @@ public void Switch( TState state, global::System.Action @string, global::System.Action @int32) + #if NET9_0_OR_GREATER + where TState : allows ref struct + #endif { switch (this._valueIndex) { @@ -1110,6 +1152,9 @@ public void Switch( public TResult Switch( global::System.Func @string, global::System.Func @int32) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + #endif { switch (this._valueIndex) { @@ -1132,6 +1177,10 @@ public TResult Switch( TState state, global::System.Func @string, global::System.Func @int32) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + where TState : allows ref struct + #endif { switch (this._valueIndex) { @@ -1152,6 +1201,9 @@ public TResult Switch( public TResult Map( TResult @string, TResult @int32) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + #endif { switch (this._valueIndex) { @@ -1379,6 +1431,9 @@ public void Switch( TState state, global::System.Action @string, global::System.Action @int32) + #if NET9_0_OR_GREATER + where TState : allows ref struct + #endif { switch (this._valueIndex) { @@ -1401,6 +1456,9 @@ public void Switch( public TResult Switch( global::System.Func @string, global::System.Func @int32) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + #endif { switch (this._valueIndex) { @@ -1423,6 +1481,10 @@ public TResult Switch( TState state, global::System.Func @string, global::System.Func @int32) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + where TState : allows ref struct + #endif { switch (this._valueIndex) { @@ -1443,6 +1505,9 @@ public TResult Switch( public TResult Map( TResult @string, TResult @int32) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + #endif { switch (this._valueIndex) { @@ -1722,6 +1787,9 @@ public void Switch( TState state, global::System.Action @string, global::System.Action @int32) + #if NET9_0_OR_GREATER + where TState : allows ref struct + #endif { switch (this._valueIndex) { @@ -1748,6 +1816,9 @@ public void SwitchPartially( global::System.Action? @default = null, global::System.Action? @string = null, global::System.Action? @int32 = null) + #if NET9_0_OR_GREATER + where TState : allows ref struct + #endif { switch (this._valueIndex) { @@ -1778,6 +1849,9 @@ public void SwitchPartially( public TResult Switch( global::System.Func @string, global::System.Func @int32) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + #endif { switch (this._valueIndex) { @@ -1800,6 +1874,9 @@ public TResult SwitchPartially( global::System.Func @default, global::System.Func? @string = null, global::System.Func? @int32 = null) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + #endif { switch (this._valueIndex) { @@ -1830,6 +1907,10 @@ public TResult Switch( TState state, global::System.Func @string, global::System.Func @int32) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + where TState : allows ref struct + #endif { switch (this._valueIndex) { @@ -1854,6 +1935,10 @@ public TResult SwitchPartially( global::System.Func @default, global::System.Func? @string = null, global::System.Func? @int32 = null) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + where TState : allows ref struct + #endif { switch (this._valueIndex) { @@ -1882,6 +1967,9 @@ public TResult SwitchPartially( public TResult Map( TResult @string, TResult @int32) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + #endif { switch (this._valueIndex) { @@ -2129,6 +2217,9 @@ public void Switch( TState state, global::System.Action @string, global::System.Action @int32) + #if NET9_0_OR_GREATER + where TState : allows ref struct + #endif { switch (this._valueIndex) { @@ -2151,6 +2242,9 @@ public void Switch( public TResult Switch( global::System.Func @string, global::System.Func @int32) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + #endif { switch (this._valueIndex) { @@ -2173,6 +2267,10 @@ public TResult Switch( TState state, global::System.Func @string, global::System.Func @int32) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + where TState : allows ref struct + #endif { switch (this._valueIndex) { @@ -2193,6 +2291,9 @@ public TResult Switch( public TResult Map( TResult @string, TResult @int32) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + #endif { switch (this._valueIndex) { @@ -2215,6 +2316,9 @@ public TResult MapPartially( TResult @default, global::Thinktecture.Argument @string = default, global::Thinktecture.Argument @int32 = default) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + #endif { switch (this._valueIndex) { @@ -2470,6 +2574,9 @@ public void Switch( TState state, global::System.Action @string, global::System.Action @int32) + #if NET9_0_OR_GREATER + where TState : allows ref struct + #endif { switch (this._valueIndex) { @@ -2492,6 +2599,9 @@ public void Switch( public TResult Switch( global::System.Func @string, global::System.Func @int32) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + #endif { switch (this._valueIndex) { @@ -2514,6 +2624,10 @@ public TResult Switch( TState state, global::System.Func @string, global::System.Func @int32) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + where TState : allows ref struct + #endif { switch (this._valueIndex) { @@ -2737,6 +2851,9 @@ public TestUnion(int @int32) public TResult Map( TResult @string, TResult @int32) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + #endif { switch (this._valueIndex) { @@ -2984,6 +3101,9 @@ public void Switch( TState state, global::System.Action @string, global::System.Action @int32) + #if NET9_0_OR_GREATER + where TState : allows ref struct + #endif { switch (this._valueIndex) { @@ -3006,6 +3126,9 @@ public void Switch( public TResult Switch( global::System.Func @string, global::System.Func @int32) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + #endif { switch (this._valueIndex) { @@ -3028,6 +3151,10 @@ public TResult Switch( TState state, global::System.Func @string, global::System.Func @int32) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + where TState : allows ref struct + #endif { switch (this._valueIndex) { @@ -3048,6 +3175,9 @@ public TResult Switch( public TResult Map( TResult @string, TResult @int32) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + #endif { switch (this._valueIndex) { @@ -3284,6 +3414,9 @@ public void Switch( TState state, global::System.Action @string, global::System.Action @int32) + #if NET9_0_OR_GREATER + where TState : allows ref struct + #endif { switch (this._valueIndex) { @@ -3306,6 +3439,9 @@ public void Switch( public TResult Switch( global::System.Func @string, global::System.Func @int32) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + #endif { switch (this._valueIndex) { @@ -3328,6 +3464,10 @@ public TResult Switch( TState state, global::System.Func @string, global::System.Func @int32) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + where TState : allows ref struct + #endif { switch (this._valueIndex) { @@ -3348,6 +3488,9 @@ public TResult Switch( public TResult Map( TResult @string, TResult @int32) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + #endif { switch (this._valueIndex) { @@ -3595,6 +3738,9 @@ public void Switch( TState state, global::System.Action @string, global::System.Action @nullableInt32) + #if NET9_0_OR_GREATER + where TState : allows ref struct + #endif { switch (this._valueIndex) { @@ -3617,6 +3763,9 @@ public void Switch( public TResult Switch( global::System.Func @string, global::System.Func @nullableInt32) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + #endif { switch (this._valueIndex) { @@ -3639,6 +3788,10 @@ public TResult Switch( TState state, global::System.Func @string, global::System.Func @nullableInt32) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + where TState : allows ref struct + #endif { switch (this._valueIndex) { @@ -3659,6 +3812,9 @@ public TResult Switch( public TResult Map( TResult @string, TResult @nullableInt32) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + #endif { switch (this._valueIndex) { @@ -3906,6 +4062,9 @@ public void Switch( TState state, global::System.Action @text, global::System.Action @number) + #if NET9_0_OR_GREATER + where TState : allows ref struct + #endif { switch (this._valueIndex) { @@ -3928,6 +4087,9 @@ public void Switch( public TResult Switch( global::System.Func @text, global::System.Func @number) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + #endif { switch (this._valueIndex) { @@ -3950,6 +4112,10 @@ public TResult Switch( TState state, global::System.Func @text, global::System.Func @number) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + where TState : allows ref struct + #endif { switch (this._valueIndex) { @@ -3970,6 +4136,9 @@ public TResult Switch( public TResult Map( TResult @text, TResult @number) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + #endif { switch (this._valueIndex) { @@ -4307,6 +4476,9 @@ public void Switch( global::System.Action @boolean, global::System.Action @guid, global::System.Action @char) + #if NET9_0_OR_GREATER + where TState : allows ref struct + #endif { switch (this._valueIndex) { @@ -4344,6 +4516,9 @@ public TResult Switch( global::System.Func @boolean, global::System.Func @guid, global::System.Func @char) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + #endif { switch (this._valueIndex) { @@ -4378,6 +4553,10 @@ public TResult Switch( global::System.Func @boolean, global::System.Func @guid, global::System.Func @char) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + where TState : allows ref struct + #endif { switch (this._valueIndex) { @@ -4410,6 +4589,9 @@ public TResult Map( TResult @boolean, TResult @guid, TResult @char) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + #endif { switch (this._valueIndex) { @@ -4735,6 +4917,9 @@ public void Switch( TState state, global::System.Action @stringArray, global::System.Action @int32) + #if NET9_0_OR_GREATER + where TState : allows ref struct + #endif { switch (this._valueIndex) { @@ -4757,6 +4942,9 @@ public void Switch( public TResult Switch( global::System.Func @stringArray, global::System.Func @int32) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + #endif { switch (this._valueIndex) { @@ -4779,6 +4967,10 @@ public TResult Switch( TState state, global::System.Func @stringArray, global::System.Func @int32) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + where TState : allows ref struct + #endif { switch (this._valueIndex) { @@ -4799,6 +4991,9 @@ public TResult Switch( public TResult Map( TResult @stringArray, TResult @int32) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + #endif { switch (this._valueIndex) { @@ -5139,6 +5334,9 @@ public void Switch( global::System.Action @string2, global::System.Action @string3, global::System.Action @nullableInt32) + #if NET9_0_OR_GREATER + where TState : allows ref struct + #endif { switch (this._valueIndex) { @@ -5176,6 +5374,9 @@ public TResult Switch( global::System.Func @string2, global::System.Func @string3, global::System.Func @nullableInt32) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + #endif { switch (this._valueIndex) { @@ -5210,6 +5411,10 @@ public TResult Switch( global::System.Func @string2, global::System.Func @string3, global::System.Func @nullableInt32) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + where TState : allows ref struct + #endif { switch (this._valueIndex) { @@ -5242,6 +5447,9 @@ public TResult Map( TResult @string2, TResult @string3, TResult @nullableInt32) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + #endif { switch (this._valueIndex) { diff --git a/test/Thinktecture.Runtime.Extensions.SourceGenerator.Tests/SourceGeneratorTests/EnumSourceGeneratorTests.cs b/test/Thinktecture.Runtime.Extensions.SourceGenerator.Tests/SourceGeneratorTests/EnumSourceGeneratorTests.cs index 7c465634..33144d0b 100644 --- a/test/Thinktecture.Runtime.Extensions.SourceGenerator.Tests/SourceGeneratorTests/EnumSourceGeneratorTests.cs +++ b/test/Thinktecture.Runtime.Extensions.SourceGenerator.Tests/SourceGeneratorTests/EnumSourceGeneratorTests.cs @@ -270,6 +270,9 @@ public void Switch( TState state, global::System.Action @item1, global::System.Action @item2) + #if NET9_0_OR_GREATER + where TState : allows ref struct + #endif { switch (_itemIndex.Value) { @@ -292,6 +295,9 @@ public void Switch( public TResult Switch( global::System.Func @item1, global::System.Func @item2) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + #endif { switch (_itemIndex.Value) { @@ -314,6 +320,10 @@ public TResult Switch( TState state, global::System.Func @item1, global::System.Func @item2) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + where TState : allows ref struct + #endif { switch (_itemIndex.Value) { @@ -334,6 +344,9 @@ public TResult Switch( public TResult Map( TResult @item1, TResult @item2) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + #endif { switch (_itemIndex.Value) { @@ -589,6 +602,9 @@ public void Switch( TState state, global::System.Action @item1, global::System.Action @item2) + #if NET9_0_OR_GREATER + where TState : allows ref struct + #endif { switch (_itemIndex.Value) { @@ -611,6 +627,9 @@ public void Switch( public TResult Switch( global::System.Func @item1, global::System.Func @item2) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + #endif { switch (_itemIndex.Value) { @@ -633,6 +652,10 @@ public TResult Switch( TState state, global::System.Func @item1, global::System.Func @item2) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + where TState : allows ref struct + #endif { switch (_itemIndex.Value) { @@ -653,6 +676,9 @@ public TResult Switch( public TResult Map( TResult @item1, TResult @item2) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + #endif { switch (_itemIndex.Value) { @@ -895,6 +921,9 @@ public void Switch( TState state, global::System.Action @item1, global::System.Action @item2) + #if NET9_0_OR_GREATER + where TState : allows ref struct + #endif { switch (_itemIndex.Value) { @@ -917,6 +946,9 @@ public void Switch( public TResult Switch( global::System.Func @item1, global::System.Func @item2) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + #endif { switch (_itemIndex.Value) { @@ -939,6 +971,10 @@ public TResult Switch( TState state, global::System.Func @item1, global::System.Func @item2) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + where TState : allows ref struct + #endif { switch (_itemIndex.Value) { @@ -959,6 +995,9 @@ public TResult Switch( public TResult Map( TResult @item1, TResult @item2) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + #endif { switch (_itemIndex.Value) { @@ -1655,6 +1694,9 @@ public void Switch( TState state, global::System.Action @item1, global::System.Action @item2) + #if NET9_0_OR_GREATER + where TState : allows ref struct + #endif { switch (_itemIndex.Value) { @@ -1681,6 +1723,9 @@ public void SwitchPartially( global::System.Action? @default = null, global::System.Action? @item1 = null, global::System.Action? @item2 = null) + #if NET9_0_OR_GREATER + where TState : allows ref struct + #endif { switch (_itemIndex.Value) { @@ -1711,6 +1756,9 @@ public void SwitchPartially( public TResult Switch( global::System.Func @item1, global::System.Func @item2) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + #endif { switch (_itemIndex.Value) { @@ -1733,6 +1781,9 @@ public TResult SwitchPartially( global::System.Func @default, global::System.Func? @item1 = null, global::System.Func? @item2 = null) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + #endif { switch (_itemIndex.Value) { @@ -1763,6 +1814,10 @@ public TResult Switch( TState state, global::System.Func @item1, global::System.Func @item2) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + where TState : allows ref struct + #endif { switch (_itemIndex.Value) { @@ -1787,6 +1842,10 @@ public TResult SwitchPartially( global::System.Func @default, global::System.Func? @item1 = null, global::System.Func? @item2 = null) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + where TState : allows ref struct + #endif { switch (_itemIndex.Value) { @@ -1815,6 +1874,9 @@ public TResult SwitchPartially( public TResult Map( TResult @item1, TResult @item2) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + #endif { switch (_itemIndex.Value) { @@ -1837,6 +1899,9 @@ public TResult MapPartially( TResult @default, global::Thinktecture.Argument @item1 = default, global::Thinktecture.Argument @item2 = default) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + #endif { switch (_itemIndex.Value) { @@ -1992,6 +2057,9 @@ public void Switch( TState state, global::System.Action @item1, global::System.Action @item2) + #if NET9_0_OR_GREATER + where TState : allows ref struct + #endif { switch (_itemIndex.Value) { @@ -2014,6 +2082,9 @@ public void Switch( public TResult Switch( global::System.Func @item1, global::System.Func @item2) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + #endif { switch (_itemIndex.Value) { @@ -2036,6 +2107,10 @@ public TResult Switch( TState state, global::System.Func @item1, global::System.Func @item2) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + where TState : allows ref struct + #endif { switch (_itemIndex.Value) { @@ -2056,6 +2131,9 @@ public TResult Switch( public TResult Map( TResult @item1, TResult @item2) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + #endif { switch (_itemIndex.Value) { @@ -2386,6 +2464,9 @@ public void Switch( TState state, global::System.Action @item1, global::System.Action @item2) + #if NET9_0_OR_GREATER + where TState : allows ref struct + #endif { switch (_itemIndex.Value) { @@ -2412,6 +2493,9 @@ public void SwitchPartially( global::System.Action? @default = null, global::System.Action? @item1 = null, global::System.Action? @item2 = null) + #if NET9_0_OR_GREATER + where TState : allows ref struct + #endif { switch (_itemIndex.Value) { @@ -2442,6 +2526,9 @@ public void SwitchPartially( public TResult Switch( global::System.Func @item1, global::System.Func @item2) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + #endif { switch (_itemIndex.Value) { @@ -2464,6 +2551,9 @@ public TResult SwitchPartially( global::System.Func @default, global::System.Func? @item1 = null, global::System.Func? @item2 = null) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + #endif { switch (_itemIndex.Value) { @@ -2494,6 +2584,10 @@ public TResult Switch( TState state, global::System.Func @item1, global::System.Func @item2) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + where TState : allows ref struct + #endif { switch (_itemIndex.Value) { @@ -2518,6 +2612,10 @@ public TResult SwitchPartially( global::System.Func @default, global::System.Func? @item1 = null, global::System.Func? @item2 = null) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + where TState : allows ref struct + #endif { switch (_itemIndex.Value) { @@ -2546,6 +2644,9 @@ public TResult SwitchPartially( public TResult Map( TResult @item1, TResult @item2) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + #endif { switch (_itemIndex.Value) { @@ -2568,6 +2669,9 @@ public TResult MapPartially( TResult @default, global::Thinktecture.Argument @item1 = default, global::Thinktecture.Argument @item2 = default) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + #endif { switch (_itemIndex.Value) { @@ -2883,6 +2987,9 @@ public override string ToString() public TResult Map( TResult @item1, TResult @item2) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + #endif { switch (_itemIndex.Value) { @@ -2905,6 +3012,9 @@ public TResult MapPartially( TResult @default, global::Thinktecture.Argument @item1 = default, global::Thinktecture.Argument @item2 = default) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + #endif { switch (_itemIndex.Value) { @@ -3233,6 +3343,9 @@ public void Switch( TState state, global::System.Action @item1, global::System.Action @item2) + #if NET9_0_OR_GREATER + where TState : allows ref struct + #endif { switch (_itemIndex.Value) { @@ -3259,6 +3372,9 @@ public void SwitchPartially( global::System.Action? @default = null, global::System.Action? @item1 = null, global::System.Action? @item2 = null) + #if NET9_0_OR_GREATER + where TState : allows ref struct + #endif { switch (_itemIndex.Value) { @@ -3289,6 +3405,9 @@ public void SwitchPartially( public TResult Switch( global::System.Func @item1, global::System.Func @item2) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + #endif { switch (_itemIndex.Value) { @@ -3311,6 +3430,9 @@ public TResult SwitchPartially( global::System.Func @default, global::System.Func? @item1 = null, global::System.Func? @item2 = null) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + #endif { switch (_itemIndex.Value) { @@ -3341,6 +3463,10 @@ public TResult Switch( TState state, global::System.Func @item1, global::System.Func @item2) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + where TState : allows ref struct + #endif { switch (_itemIndex.Value) { @@ -3365,6 +3491,10 @@ public TResult SwitchPartially( global::System.Func @default, global::System.Func? @item1 = null, global::System.Func? @item2 = null) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + where TState : allows ref struct + #endif { switch (_itemIndex.Value) { @@ -3737,6 +3867,9 @@ public void Switch( TState state, global::System.Action @item1, global::System.Action @item2) + #if NET9_0_OR_GREATER + where TState : allows ref struct + #endif { switch (_itemIndex.Value) { @@ -3763,6 +3896,9 @@ public void SwitchPartially( global::System.Action? @default = null, global::System.Action? @item1 = null, global::System.Action? @item2 = null) + #if NET9_0_OR_GREATER + where TState : allows ref struct + #endif { switch (_itemIndex.Value) { @@ -3793,6 +3929,9 @@ public void SwitchPartially( public TResult Switch( global::System.Func @item1, global::System.Func @item2) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + #endif { switch (_itemIndex.Value) { @@ -3815,6 +3954,9 @@ public TResult SwitchPartially( global::System.Func @default, global::System.Func? @item1 = null, global::System.Func? @item2 = null) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + #endif { switch (_itemIndex.Value) { @@ -3845,6 +3987,10 @@ public TResult Switch( TState state, global::System.Func @item1, global::System.Func @item2) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + where TState : allows ref struct + #endif { switch (_itemIndex.Value) { @@ -3869,6 +4015,10 @@ public TResult SwitchPartially( global::System.Func @default, global::System.Func? @item1 = null, global::System.Func? @item2 = null) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + where TState : allows ref struct + #endif { switch (_itemIndex.Value) { @@ -3897,6 +4047,9 @@ public TResult SwitchPartially( public TResult Map( TResult @item1, TResult @item2) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + #endif { switch (_itemIndex.Value) { @@ -3919,6 +4072,9 @@ public TResult MapPartially( TResult @default, global::Thinktecture.Argument @item1 = default, global::Thinktecture.Argument @item2 = default) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + #endif { switch (_itemIndex.Value) { @@ -4237,6 +4393,9 @@ public void Switch( TState state, global::System.Action @item1, global::System.Action @item2) + #if NET9_0_OR_GREATER + where TState : allows ref struct + #endif { switch (_itemIndex.Value) { @@ -4263,6 +4422,9 @@ public void SwitchPartially( global::System.Action? @default = null, global::System.Action? @item1 = null, global::System.Action? @item2 = null) + #if NET9_0_OR_GREATER + where TState : allows ref struct + #endif { switch (_itemIndex.Value) { @@ -4293,6 +4455,9 @@ public void SwitchPartially( public TResult Switch( global::System.Func @item1, global::System.Func @item2) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + #endif { switch (_itemIndex.Value) { @@ -4315,6 +4480,9 @@ public TResult SwitchPartially( global::System.Func @default, global::System.Func? @item1 = null, global::System.Func? @item2 = null) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + #endif { switch (_itemIndex.Value) { @@ -4345,6 +4513,10 @@ public TResult Switch( TState state, global::System.Func @item1, global::System.Func @item2) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + where TState : allows ref struct + #endif { switch (_itemIndex.Value) { @@ -4369,6 +4541,10 @@ public TResult SwitchPartially( global::System.Func @default, global::System.Func? @item1 = null, global::System.Func? @item2 = null) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + where TState : allows ref struct + #endif { switch (_itemIndex.Value) { @@ -4397,6 +4573,9 @@ public TResult SwitchPartially( public TResult Map( TResult @item1, TResult @item2) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + #endif { switch (_itemIndex.Value) { @@ -4419,6 +4598,9 @@ public TResult MapPartially( TResult @default, global::Thinktecture.Argument @item1 = default, global::Thinktecture.Argument @item2 = default) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + #endif { switch (_itemIndex.Value) { @@ -5003,6 +5185,9 @@ public void Switch( global::System.Action @item_decimal_2, global::System.Action @item_derived_1, global::System.Action @item_derived_2) + #if NET9_0_OR_GREATER + where TState : allows ref struct + #endif { switch (_itemIndex.Value) { @@ -5054,6 +5239,9 @@ public void SwitchPartially( global::System.Action? @item_decimal_2 = null, global::System.Action? @item_derived_1 = null, global::System.Action? @item_derived_2 = null) + #if NET9_0_OR_GREATER + where TState : allows ref struct + #endif { switch (_itemIndex.Value) { @@ -5124,6 +5312,9 @@ public TResult Switch( global::System.Func @item_decimal_2, global::System.Func @item_derived_1, global::System.Func @item_derived_2) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + #endif { switch (_itemIndex.Value) { @@ -5166,6 +5357,9 @@ public TResult SwitchPartially( global::System.Func? @item_decimal_2 = null, global::System.Func? @item_derived_1 = null, global::System.Func? @item_derived_2 = null) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + #endif { switch (_itemIndex.Value) { @@ -5231,6 +5425,10 @@ public TResult Switch( global::System.Func @item_decimal_2, global::System.Func @item_derived_1, global::System.Func @item_derived_2) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + where TState : allows ref struct + #endif { switch (_itemIndex.Value) { @@ -5275,6 +5473,10 @@ public TResult SwitchPartially( global::System.Func? @item_decimal_2 = null, global::System.Func? @item_derived_1 = null, global::System.Func? @item_derived_2 = null) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + where TState : allows ref struct + #endif { switch (_itemIndex.Value) { @@ -5338,6 +5540,9 @@ public TResult Map( TResult @item_decimal_2, TResult @item_derived_1, TResult @item_derived_2) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + #endif { switch (_itemIndex.Value) { @@ -5380,6 +5585,9 @@ public TResult MapPartially( global::Thinktecture.Argument @item_decimal_2 = default, global::Thinktecture.Argument @item_derived_1 = default, global::Thinktecture.Argument @item_derived_2 = default) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + #endif { switch (_itemIndex.Value) { @@ -5840,6 +6048,9 @@ public void Switch( global::System.Action invalid, global::System.Action @item1, global::System.Action @item2) + #if NET9_0_OR_GREATER + where TState : allows ref struct + #endif { if (!this.IsValid) { @@ -5874,6 +6085,9 @@ public void SwitchPartially( global::System.Action? invalid = null, global::System.Action? @item1 = null, global::System.Action? @item2 = null) + #if NET9_0_OR_GREATER + where TState : allows ref struct + #endif { if (!this.IsValid) { @@ -5918,6 +6132,9 @@ public TResult Switch( global::System.Func invalid, global::System.Func @item1, global::System.Func @item2) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + #endif { if (!this.IsValid) { @@ -5947,6 +6164,9 @@ public TResult SwitchPartially( global::System.Func? invalid = null, global::System.Func? @item1 = null, global::System.Func? @item2 = null) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + #endif { if (!this.IsValid) { @@ -5987,6 +6207,10 @@ public TResult Switch( global::System.Func invalid, global::System.Func @item1, global::System.Func @item2) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + where TState : allows ref struct + #endif { if (!this.IsValid) { @@ -6018,6 +6242,10 @@ public TResult SwitchPartially( global::System.Func? invalid = null, global::System.Func? @item1 = null, global::System.Func? @item2 = null) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + where TState : allows ref struct + #endif { if (!this.IsValid) { @@ -6056,6 +6284,9 @@ public TResult Map( TResult invalid, TResult @item1, TResult @item2) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + #endif { if (!this.IsValid) return invalid; @@ -6083,6 +6314,9 @@ public TResult MapPartially( global::Thinktecture.Argument invalid = default, global::Thinktecture.Argument @item1 = default, global::Thinktecture.Argument @item2 = default) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + #endif { if (!this.IsValid) return invalid.IsSet ? invalid.Value : @default; @@ -6485,6 +6719,9 @@ public void Switch( global::System.Action invalid, global::System.Action @item1, global::System.Action @item2) + #if NET9_0_OR_GREATER + where TState : allows ref struct + #endif { if (!this.IsValid) { @@ -6519,6 +6756,9 @@ public void SwitchPartially( global::System.Action? invalid = null, global::System.Action? @item1 = null, global::System.Action? @item2 = null) + #if NET9_0_OR_GREATER + where TState : allows ref struct + #endif { if (!this.IsValid) { @@ -6563,6 +6803,9 @@ public TResult Switch( global::System.Func invalid, global::System.Func @item1, global::System.Func @item2) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + #endif { if (!this.IsValid) { @@ -6592,6 +6835,9 @@ public TResult SwitchPartially( global::System.Func? invalid = null, global::System.Func? @item1 = null, global::System.Func? @item2 = null) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + #endif { if (!this.IsValid) { @@ -6632,6 +6878,10 @@ public TResult Switch( global::System.Func invalid, global::System.Func @item1, global::System.Func @item2) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + where TState : allows ref struct + #endif { if (!this.IsValid) { @@ -6663,6 +6913,10 @@ public TResult SwitchPartially( global::System.Func? invalid = null, global::System.Func? @item1 = null, global::System.Func? @item2 = null) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + where TState : allows ref struct + #endif { if (!this.IsValid) { @@ -6701,6 +6955,9 @@ public TResult Map( TResult invalid, TResult @item1, TResult @item2) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + #endif { if (!this.IsValid) return invalid; @@ -6728,6 +6985,9 @@ public TResult MapPartially( global::Thinktecture.Argument invalid = default, global::Thinktecture.Argument @item1 = default, global::Thinktecture.Argument @item2 = default) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + #endif { if (!this.IsValid) return invalid.IsSet ? invalid.Value : @default; @@ -7307,6 +7567,9 @@ public void Switch( global::System.Action invalid, global::System.Action @item1, global::System.Action @item2) + #if NET9_0_OR_GREATER + where TState : allows ref struct + #endif { if (!this.IsValid) { @@ -7341,6 +7604,9 @@ public void SwitchPartially( global::System.Action? invalid = null, global::System.Action? @item1 = null, global::System.Action? @item2 = null) + #if NET9_0_OR_GREATER + where TState : allows ref struct + #endif { if (!this.IsValid) { @@ -7385,6 +7651,9 @@ public TResult Switch( global::System.Func invalid, global::System.Func @item1, global::System.Func @item2) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + #endif { if (!this.IsValid) { @@ -7414,6 +7683,9 @@ public TResult SwitchPartially( global::System.Func? invalid = null, global::System.Func? @item1 = null, global::System.Func? @item2 = null) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + #endif { if (!this.IsValid) { @@ -7454,6 +7726,10 @@ public TResult Switch( global::System.Func invalid, global::System.Func @item1, global::System.Func @item2) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + where TState : allows ref struct + #endif { if (!this.IsValid) { @@ -7485,6 +7761,10 @@ public TResult SwitchPartially( global::System.Func? invalid = null, global::System.Func? @item1 = null, global::System.Func? @item2 = null) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + where TState : allows ref struct + #endif { if (!this.IsValid) { @@ -7523,6 +7803,9 @@ public TResult Map( TResult invalid, TResult @item1, TResult @item2) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + #endif { if (!this.IsValid) return invalid; @@ -7550,6 +7833,9 @@ public TResult MapPartially( global::Thinktecture.Argument invalid = default, global::Thinktecture.Argument @item1 = default, global::Thinktecture.Argument @item2 = default) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + #endif { if (!this.IsValid) return invalid.IsSet ? invalid.Value : @default; @@ -8117,6 +8403,9 @@ public void Switch( TState state, global::System.Action @item1, global::System.Action @item2) + #if NET9_0_OR_GREATER + where TState : allows ref struct + #endif { switch (_itemIndex.Value) { @@ -8143,6 +8432,9 @@ public void SwitchPartially( global::System.Action? @default = null, global::System.Action? @item1 = null, global::System.Action? @item2 = null) + #if NET9_0_OR_GREATER + where TState : allows ref struct + #endif { switch (_itemIndex.Value) { @@ -8173,6 +8465,9 @@ public void SwitchPartially( public TResult Switch( global::System.Func @item1, global::System.Func @item2) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + #endif { switch (_itemIndex.Value) { @@ -8195,6 +8490,9 @@ public TResult SwitchPartially( global::System.Func @default, global::System.Func? @item1 = null, global::System.Func? @item2 = null) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + #endif { switch (_itemIndex.Value) { @@ -8225,6 +8523,10 @@ public TResult Switch( TState state, global::System.Func @item1, global::System.Func @item2) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + where TState : allows ref struct + #endif { switch (_itemIndex.Value) { @@ -8249,6 +8551,10 @@ public TResult SwitchPartially( global::System.Func @default, global::System.Func? @item1 = null, global::System.Func? @item2 = null) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + where TState : allows ref struct + #endif { switch (_itemIndex.Value) { @@ -8277,6 +8583,9 @@ public TResult SwitchPartially( public TResult Map( TResult @item1, TResult @item2) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + #endif { switch (_itemIndex.Value) { @@ -8299,6 +8608,9 @@ public TResult MapPartially( TResult @default, global::Thinktecture.Argument @item1 = default, global::Thinktecture.Argument @item2 = default) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + #endif { switch (_itemIndex.Value) { @@ -8617,6 +8929,9 @@ public void Switch( TState state, global::System.Action @item1, global::System.Action @item2) + #if NET9_0_OR_GREATER + where TState : allows ref struct + #endif { switch (_itemIndex.Value) { @@ -8643,6 +8958,9 @@ public void SwitchPartially( global::System.Action? @default = null, global::System.Action? @item1 = null, global::System.Action? @item2 = null) + #if NET9_0_OR_GREATER + where TState : allows ref struct + #endif { switch (_itemIndex.Value) { @@ -8673,6 +8991,9 @@ public void SwitchPartially( public TResult Switch( global::System.Func @item1, global::System.Func @item2) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + #endif { switch (_itemIndex.Value) { @@ -8695,6 +9016,9 @@ public TResult SwitchPartially( global::System.Func @default, global::System.Func? @item1 = null, global::System.Func? @item2 = null) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + #endif { switch (_itemIndex.Value) { @@ -8725,6 +9049,10 @@ public TResult Switch( TState state, global::System.Func @item1, global::System.Func @item2) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + where TState : allows ref struct + #endif { switch (_itemIndex.Value) { @@ -8749,6 +9077,10 @@ public TResult SwitchPartially( global::System.Func @default, global::System.Func? @item1 = null, global::System.Func? @item2 = null) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + where TState : allows ref struct + #endif { switch (_itemIndex.Value) { @@ -8777,6 +9109,9 @@ public TResult SwitchPartially( public TResult Map( TResult @item1, TResult @item2) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + #endif { switch (_itemIndex.Value) { @@ -8799,6 +9134,9 @@ public TResult MapPartially( TResult @default, global::Thinktecture.Argument @item1 = default, global::Thinktecture.Argument @item2 = default) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + #endif { switch (_itemIndex.Value) { @@ -9151,6 +9489,9 @@ public void Switch( TState state, global::System.Action @item1, global::System.Action @item2) + #if NET9_0_OR_GREATER + where TState : allows ref struct + #endif { switch (_itemIndex.Value) { @@ -9173,6 +9514,9 @@ public void Switch( public TResult Switch( global::System.Func @item1, global::System.Func @item2) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + #endif { switch (_itemIndex.Value) { @@ -9195,6 +9539,10 @@ public TResult Switch( TState state, global::System.Func @item1, global::System.Func @item2) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + where TState : allows ref struct + #endif { switch (_itemIndex.Value) { @@ -9215,6 +9563,9 @@ public TResult Switch( public TResult Map( TResult @item1, TResult @item2) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + #endif { switch (_itemIndex.Value) { diff --git a/test/Thinktecture.Runtime.Extensions.SourceGenerator.Tests/SourceGeneratorTests/UnionSourceGeneratorTests.cs b/test/Thinktecture.Runtime.Extensions.SourceGenerator.Tests/SourceGeneratorTests/UnionSourceGeneratorTests.cs index ed91f49a..7d84813c 100644 --- a/test/Thinktecture.Runtime.Extensions.SourceGenerator.Tests/SourceGeneratorTests/UnionSourceGeneratorTests.cs +++ b/test/Thinktecture.Runtime.Extensions.SourceGenerator.Tests/SourceGeneratorTests/UnionSourceGeneratorTests.cs @@ -82,6 +82,9 @@ public void Switch( TState state, global::System.Action.Success> @success, global::System.Action.Failure> @failure) + #if NET9_0_OR_GREATER + where TState : allows ref struct + #endif { switch (this) { @@ -104,6 +107,9 @@ public void Switch( public TResult Switch( global::System.Func.Success, TResult> @success, global::System.Func.Failure, TResult> @failure) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + #endif { switch (this) { @@ -126,6 +132,10 @@ public TResult Switch( TState state, global::System.Func.Success, TResult> @success, global::System.Func.Failure, TResult> @failure) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + where TState : allows ref struct + #endif { switch (this) { @@ -146,6 +156,9 @@ public TResult Switch( public TResult Map( TResult @success, TResult @failure) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + #endif { switch (this) { @@ -230,6 +243,9 @@ public void Switch( TState state, global::System.Action @child1, global::System.Action @child2) + #if NET9_0_OR_GREATER + where TState : allows ref struct + #endif { switch (this) { @@ -252,6 +268,9 @@ public void Switch( public TResult Switch( global::System.Func @child1, global::System.Func @child2) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + #endif { switch (this) { @@ -274,6 +293,10 @@ public TResult Switch( TState state, global::System.Func @child1, global::System.Func @child2) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + where TState : allows ref struct + #endif { switch (this) { @@ -294,6 +317,9 @@ public TResult Switch( public TResult Map( TResult @child1, TResult @child2) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + #endif { switch (this) { @@ -374,6 +400,9 @@ public void Switch( TState state, global::System.Action @child1, global::System.Action @child2) + #if NET9_0_OR_GREATER + where TState : allows ref struct + #endif { switch (this) { @@ -396,6 +425,9 @@ public void Switch( public TResult Switch( global::System.Func @child1, global::System.Func @child2) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + #endif { switch (this) { @@ -418,6 +450,10 @@ public TResult Switch( TState state, global::System.Func @child1, global::System.Func @child2) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + where TState : allows ref struct + #endif { switch (this) { @@ -438,6 +474,9 @@ public TResult Switch( public TResult Map( TResult @child1, TResult @child2) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + #endif { switch (this) { @@ -528,6 +567,9 @@ public void Switch( global::System.Action @child1, global::System.Action @child2, global::System.Action @child2Child1) + #if NET9_0_OR_GREATER + where TState : allows ref struct + #endif { switch (this) { @@ -555,6 +597,9 @@ public TResult Switch( global::System.Func @child1, global::System.Func @child2, global::System.Func @child2Child1) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + #endif { switch (this) { @@ -581,6 +626,10 @@ public TResult Switch( global::System.Func @child1, global::System.Func @child2, global::System.Func @child2Child1) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + where TState : allows ref struct + #endif { switch (this) { @@ -605,6 +654,9 @@ public TResult Map( TResult @child1, TResult @child2, TResult @child2Child1) + #if NET9_0_OR_GREATER + where TResult : allows ref struct + #endif { switch (this) { diff --git a/test/Thinktecture.Runtime.Extensions.Tests.Shared/TestRefStruct.cs b/test/Thinktecture.Runtime.Extensions.Tests.Shared/TestRefStruct.cs new file mode 100644 index 00000000..ad4d3c7e --- /dev/null +++ b/test/Thinktecture.Runtime.Extensions.Tests.Shared/TestRefStruct.cs @@ -0,0 +1,11 @@ +namespace Thinktecture.Runtime.Tests; + +public ref struct TestRefStruct +{ + public object Value; + + public TestRefStruct(object value) + { + Value = value; + } +} diff --git a/test/Thinktecture.Runtime.Extensions.Tests/AdHocUnionTests/Map.cs b/test/Thinktecture.Runtime.Extensions.Tests/AdHocUnionTests/Map.cs index 3bde1dd1..52de834a 100644 --- a/test/Thinktecture.Runtime.Extensions.Tests/AdHocUnionTests/Map.cs +++ b/test/Thinktecture.Runtime.Extensions.Tests/AdHocUnionTests/Map.cs @@ -26,6 +26,26 @@ public void Should_use_correct_arg_having_2_values(int index, object expected) calledActionOn.Should().Be(expected); } +#if NET9_0_OR_GREATER + [Theory] + [InlineData(1, "text")] + [InlineData(2, 42)] + public void Should_use_correct_arg_having_2_values_returning_ref_struct(int index, object expected) + { + var value = index switch + { + 1 => new TestUnion_class_string_int("text"), + 2 => new TestUnion_class_string_int(42), + _ => throw new Exception() + }; + + var calledActionOn = value.Map(@string: new TestRefStruct("text"), + int32: new TestRefStruct(42)); + + calledActionOn.Value.Should().Be(expected); + } +#endif + [Theory] [InlineData(1, "text")] [InlineData(2, 42)] diff --git a/test/Thinktecture.Runtime.Extensions.Tests/AdHocUnionTests/MapPartially.cs b/test/Thinktecture.Runtime.Extensions.Tests/AdHocUnionTests/MapPartially.cs index 4bd0c4cb..1b64cb7a 100644 --- a/test/Thinktecture.Runtime.Extensions.Tests/AdHocUnionTests/MapPartially.cs +++ b/test/Thinktecture.Runtime.Extensions.Tests/AdHocUnionTests/MapPartially.cs @@ -27,6 +27,27 @@ public void Should_use_correct_arg_having_2_types(int index, object expected) calledActionOn.Should().Be(expected); } +#if NET9_0_OR_GREATER + [Theory] + [InlineData(1, "text")] + [InlineData(2, 42)] + public void Should_use_correct_arg_having_2_types_returning_ref_struct(int index, object expected) + { + var value = index switch + { + 1 => new TestUnion_class_string_int("text"), + 2 => new TestUnion_class_string_int(42), + _ => throw new Exception() + }; + + var calledActionOn = value.MapPartially(@default: new TestRefStruct("default"), + @string: new TestRefStruct("text"), + int32: new TestRefStruct(42)); + + calledActionOn.Value.Should().Be(expected); + } +#endif + [Theory] [InlineData(1, "default")] [InlineData(2, 42)] diff --git a/test/Thinktecture.Runtime.Extensions.Tests/AdHocUnionTests/Switch.cs b/test/Thinktecture.Runtime.Extensions.Tests/AdHocUnionTests/Switch.cs new file mode 100644 index 00000000..7127160b --- /dev/null +++ b/test/Thinktecture.Runtime.Extensions.Tests/AdHocUnionTests/Switch.cs @@ -0,0 +1,813 @@ +using System; +using Thinktecture.Runtime.Tests.TestAdHocUnions; + +namespace Thinktecture.Runtime.Tests.AdHocUnionTests; + +// ReSharper disable once InconsistentNaming +public class Switch +{ + public class HavingClass + { + public class WithAction + { + [Theory] + [InlineData(1, "text")] + [InlineData(2, 42)] + public void Should_use_correct_arg_having_2_values(int index, object expected) + { + var value = index switch + { + 1 => new TestUnion_class_string_int("text"), + 2 => new TestUnion_class_string_int(42), + _ => throw new Exception() + }; + object calledActionOn = null; + + value.Switch(@string: v => + { + calledActionOn = v; + }, + int32: v => + { + calledActionOn = v; + }); + + calledActionOn.Should().Be(expected); + } + + [Theory] + [InlineData(1, "text")] + [InlineData(2, 42)] + [InlineData(3, true)] + public void Should_use_correct_arg_having_3_values(int index, object expected) + { + var value = index switch + { + 1 => new TestUnion_class_string_int_bool("text"), + 2 => new TestUnion_class_string_int_bool(42), + 3 => new TestUnion_class_string_int_bool(true), + _ => throw new Exception() + }; + object calledActionOn = null; + + value.Switch(@string: v => + { + calledActionOn = v; + }, + int32: v => + { + calledActionOn = v; + }, + boolean: v => + { + calledActionOn = v; + }); + + calledActionOn.Should().Be(expected); + } + + [Theory] + [InlineData(1, "text")] + [InlineData(2, 42)] + [InlineData(3, true)] + [InlineData(4, "2A986EEB-1B82-46F8-A7F3-401ADC22BE33")] + public void Should_use_correct_arg_having_4_values(int index, object expected) + { + var value = index switch + { + 1 => new TestUnion_class_string_int_bool_guid("text"), + 2 => new TestUnion_class_string_int_bool_guid(42), + 3 => new TestUnion_class_string_int_bool_guid(true), + 4 => new TestUnion_class_string_int_bool_guid(new Guid("2A986EEB-1B82-46F8-A7F3-401ADC22BE33")), + _ => throw new Exception() + }; + object calledActionOn = null; + + value.Switch(@string: v => + { + calledActionOn = v; + }, + int32: v => + { + calledActionOn = v; + }, + boolean: v => + { + calledActionOn = v; + }, + guid: v => + { + calledActionOn = v; + }); + + calledActionOn.Should().Be(index == 4 ? new Guid((string)expected) : expected); + } + + [Theory] + [InlineData(1, "text")] + [InlineData(2, 42)] + [InlineData(3, true)] + [InlineData(4, "2A986EEB-1B82-46F8-A7F3-401ADC22BE33")] + [InlineData(5, 'A')] + public void Should_use_correct_arg_having_5_values(int index, object expected) + { + var value = index switch + { + 1 => new TestUnion_class_string_int_bool_guid_char("text"), + 2 => new TestUnion_class_string_int_bool_guid_char(42), + 3 => new TestUnion_class_string_int_bool_guid_char(true), + 4 => new TestUnion_class_string_int_bool_guid_char(new Guid("2A986EEB-1B82-46F8-A7F3-401ADC22BE33")), + 5 => new TestUnion_class_string_int_bool_guid_char('A'), + _ => throw new Exception() + }; + object calledActionOn = null; + + value.Switch(@string: v => + { + calledActionOn = v; + }, + int32: v => + { + calledActionOn = v; + }, + boolean: v => + { + calledActionOn = v; + }, + guid: v => + { + calledActionOn = v; + }, + @char: v => + { + calledActionOn = v; + }); + + calledActionOn.Should().Be(index == 4 ? new Guid((string)expected) : expected); + } + + [Theory] + [InlineData(1, "text")] + [InlineData(2, 42)] + [InlineData(3, "text2")] + [InlineData(4, "text3")] + [InlineData(5, 43)] + public void Should_use_correct_arg_having_5_values_with_duplicates(int index, object expected) + { + var value = index switch + { + 1 => TestUnion_class_with_same_types.CreateText("text"), + 2 => new TestUnion_class_with_same_types(42), + 3 => TestUnion_class_with_same_types.CreateString2("text2"), + 4 => TestUnion_class_with_same_types.CreateString3("text3"), + 5 => new TestUnion_class_with_same_types((int?)43), + _ => throw new Exception() + }; + object calledActionOn = null; + + value.Switch(text: v => + { + calledActionOn = v; + }, + int32: v => + { + calledActionOn = v; + }, + string2: v => + { + calledActionOn = v; + }, + string3: v => + { + calledActionOn = v; + }, + nullableInt32: v => + { + calledActionOn = v; + }); + + calledActionOn.Should().Be(expected); + } + } + + public class WithActionAndState + { + [Theory] + [InlineData(1, "text")] + [InlineData(2, 42)] + public void Should_pass_context_having_2_types(int index, object expected) + { + var value = index switch + { + 1 => new TestUnion_class_string_int("text"), + 2 => new TestUnion_class_string_int(42), + _ => throw new Exception() + }; + + var state = new object(); + + object calledActionOn = null; + + value.Switch(state, + @string: (s, v) => + { + s.Should().Be(s); + calledActionOn = v; + }, + int32: (s, v) => + { + s.Should().Be(state); + calledActionOn = v; + }); + + calledActionOn.Should().Be(expected); + } + +#if NET9_0_OR_GREATER + [Theory] + [InlineData(1, "text")] + [InlineData(2, 42)] + public void Should_pass_context_having_2_types_and_ref_struct(int index, object expected) + { + var value = index switch + { + 1 => new TestUnion_class_string_int("text"), + 2 => new TestUnion_class_string_int(42), + _ => throw new Exception() + }; + + var state = new TestRefStruct(42); + + object calledActionOn = null; + + value.Switch(state, + @string: (s, v) => + { + s.Value.Should().Be(42); + calledActionOn = v; + }, + int32: (s, v) => + { + s.Value.Should().Be(42); + calledActionOn = v; + }); + + calledActionOn.Should().Be(expected); + } +#endif + + [Theory] + [InlineData(1, "text")] + [InlineData(2, 42)] + [InlineData(3, true)] + public void Should_pass_context_having_3_types(int index, object expected) + { + var value = index switch + { + 1 => new TestUnion_class_string_int_bool("text"), + 2 => new TestUnion_class_string_int_bool(42), + 3 => new TestUnion_class_string_int_bool(true), + _ => throw new Exception() + }; + + var state = new object(); + + object calledActionOn = null; + + value.Switch(state, + @string: (s, v) => + { + s.Should().Be(state); + calledActionOn = v; + }, + int32: (s, v) => + { + s.Should().Be(state); + calledActionOn = v; + }, + boolean: (s, v) => + { + s.Should().Be(state); + calledActionOn = v; + }); + + calledActionOn.Should().Be(expected); + } + + [Theory] + [InlineData(1, "text")] + [InlineData(2, 42)] + [InlineData(3, true)] + [InlineData(4, "3E85ABD4-621A-4F58-8926-A842D71BB230")] + public void Should_pass_context_having_4_types(int index, object expected) + { + var value = index switch + { + 1 => new TestUnion_class_string_int_bool_guid("text"), + 2 => new TestUnion_class_string_int_bool_guid(42), + 3 => new TestUnion_class_string_int_bool_guid(true), + 4 => new TestUnion_class_string_int_bool_guid(new Guid("3E85ABD4-621A-4F58-8926-A842D71BB230")), + _ => throw new Exception() + }; + + var state = new object(); + + object calledActionOn = null; + + value.Switch(state, + @string: (s, v) => + { + s.Should().Be(state); + calledActionOn = v; + }, + int32: (s, v) => + { + s.Should().Be(state); + calledActionOn = v; + }, + boolean: (s, v) => + { + s.Should().Be(state); + calledActionOn = v; + }, + guid: (s, v) => + { + s.Should().Be(state); + calledActionOn = v; + }); + + calledActionOn.Should().Be(index == 4 ? new Guid((string)expected) : expected); + } + + [Theory] + [InlineData(1, "text")] + [InlineData(2, 42)] + [InlineData(3, true)] + [InlineData(4, "3E85ABD4-621A-4F58-8926-A842D71BB230")] + [InlineData(5, 'A')] + public void Should_pass_context_having_5_types(int index, object expected) + { + var value = index switch + { + 1 => new TestUnion_class_string_int_bool_guid_char("text"), + 2 => new TestUnion_class_string_int_bool_guid_char(42), + 3 => new TestUnion_class_string_int_bool_guid_char(true), + 4 => new TestUnion_class_string_int_bool_guid_char(new Guid("3E85ABD4-621A-4F58-8926-A842D71BB230")), + 5 => new TestUnion_class_string_int_bool_guid_char('A'), + _ => throw new Exception() + }; + + var state = new object(); + + object calledActionOn = null; + + value.Switch(state, + @string: (s, v) => + { + s.Should().Be(state); + calledActionOn = v; + }, + int32: (s, v) => + { + s.Should().Be(state); + calledActionOn = v; + }, + boolean: (s, v) => + { + s.Should().Be(state); + calledActionOn = v; + }, + guid: (s, v) => + { + s.Should().Be(state); + calledActionOn = v; + }, + @char: (s, v) => + { + s.Should().Be(state); + calledActionOn = v; + }); + + calledActionOn.Should().Be(index == 4 ? new Guid((string)expected) : expected); + } + } + + public class WithFunc + { + [Theory] + [InlineData(1, "text")] + [InlineData(2, 42)] + public void Should_call_correct_arg_having_2_types(int index, object expected) + { + var value = index switch + { + 1 => new TestUnion_class_string_int("text"), + 2 => new TestUnion_class_string_int(42), + _ => throw new Exception() + }; + + var calledActionOn = value.Switch(@string: v => (object)v, + int32: v => v); + + calledActionOn.Should().Be(expected); + } + +#if NET9_0_OR_GREATER + [Theory] + [InlineData(1, "text")] + [InlineData(2, 42)] + public void Should_call_correct_arg_having_2_types_returning_ref_struct(int index, object expected) + { + var value = index switch + { + 1 => new TestUnion_class_string_int("text"), + 2 => new TestUnion_class_string_int(42), + _ => throw new Exception() + }; + + var calledActionOn = value.Switch(@string: v => new TestRefStruct(v), + int32: v => new TestRefStruct(v)); + + calledActionOn.Value.Should().Be(expected); + } +#endif + + [Theory] + [InlineData(1, "text")] + [InlineData(2, 42)] + [InlineData(3, true)] + public void Should_call_correct_arg_having_3_types(int index, object expected) + { + var value = index switch + { + 1 => new TestUnion_class_string_int_bool("text"), + 2 => new TestUnion_class_string_int_bool(42), + 3 => new TestUnion_class_string_int_bool(true), + _ => throw new Exception() + }; + + var calledActionOn = value.Switch(@string: v => (object)v, + int32: v => v, + boolean: v => v); + + calledActionOn.Should().Be(expected); + } + + [Theory] + [InlineData(1, "text")] + [InlineData(2, 42)] + [InlineData(3, true)] + [InlineData(4, "F8002E79-5465-4797-AD3F-A6503ADF066E")] + public void Should_call_correct_arg_having_4_types(int index, object expected) + { + var value = index switch + { + 1 => new TestUnion_class_string_int_bool_guid("text"), + 2 => new TestUnion_class_string_int_bool_guid(42), + 3 => new TestUnion_class_string_int_bool_guid(true), + 4 => new TestUnion_class_string_int_bool_guid(new Guid("F8002E79-5465-4797-AD3F-A6503ADF066E")), + _ => throw new Exception() + }; + + var calledActionOn = value.Switch(@string: v => (object)v, + int32: v => v, + boolean: v => v, + guid: v => v); + + calledActionOn.Should().Be(index == 4 ? new Guid((string)expected) : expected); + } + + [Theory] + [InlineData(1, "text")] + [InlineData(2, 42)] + [InlineData(3, true)] + [InlineData(4, "F8002E79-5465-4797-AD3F-A6503ADF066E")] + [InlineData(5, 'A')] + public void Should_call_correct_arg_having_5_types(int index, object expected) + { + var value = index switch + { + 1 => new TestUnion_class_string_int_bool_guid_char("text"), + 2 => new TestUnion_class_string_int_bool_guid_char(42), + 3 => new TestUnion_class_string_int_bool_guid_char(true), + 4 => new TestUnion_class_string_int_bool_guid_char(new Guid("F8002E79-5465-4797-AD3F-A6503ADF066E")), + 5 => new TestUnion_class_string_int_bool_guid_char('A'), + _ => throw new Exception() + }; + + var calledActionOn = value.Switch(@string: v => (object)v, + int32: v => v, + boolean: v => v, + guid: v => v, + @char: v => v); + + calledActionOn.Should().Be(index == 4 ? new Guid((string)expected) : expected); + } + } + + public class WithFuncAndContext + { + [Theory] + [InlineData(1, "text")] + [InlineData(2, 42)] + public void Should_pass_context_having_2_types(int index, object expected) + { + var value = index switch + { + 1 => new TestUnion_class_string_int("text"), + 2 => new TestUnion_class_string_int(42), + _ => throw new Exception() + }; + + var state = new object(); + var calledActionOn = value.Switch(state, + @string: (s, v) => + { + s.Should().Be(state); + return (object)v; + }, + int32: (s, v) => + { + s.Should().Be(state); + return v; + }); + + calledActionOn.Should().Be(expected); + } + +#if NET9_0_OR_GREATER + [Theory] + [InlineData(1, "text")] + [InlineData(2, 42)] + public void Should_pass_context_having_2_types_and_ref_struct(int index, object expected) + { + var value = index switch + { + 1 => new TestUnion_class_string_int("text"), + 2 => new TestUnion_class_string_int(42), + _ => throw new Exception() + }; + + var state = new TestRefStruct(42); + + var calledActionOn = value.Switch(state, + @string: (s, v) => + { + s.Value.Should().Be(42); + return new TestRefStruct(v); + }, + int32: (s, v) => + { + s.Value.Should().Be(42); + return new TestRefStruct(v); + }); + + calledActionOn.Value.Should().Be(expected); + } +#endif + + [Theory] + [InlineData(1, "text")] + [InlineData(2, 42)] + [InlineData(3, true)] + public void Should_pass_context_having_3_types(int index, object expected) + { + var value = index switch + { + 1 => new TestUnion_class_string_int_bool("text"), + 2 => new TestUnion_class_string_int_bool(42), + 3 => new TestUnion_class_string_int_bool(true), + _ => throw new Exception() + }; + + var state = new object(); + var calledActionOn = value.Switch(state, + @string: (s, v) => + { + s.Should().Be(state); + return (object)v; + }, + int32: (s, v) => + { + s.Should().Be(state); + return v; + }, + boolean: (s, v) => + { + s.Should().Be(state); + return v; + }); + + calledActionOn.Should().Be(expected); + } + + [Theory] + [InlineData(1, "text")] + [InlineData(2, 42)] + [InlineData(3, true)] + [InlineData(4, "6EF10862-7FC4-4AEB-BC92-21E798AC54D0")] + public void Should_pass_context_having_4_types(int index, object expected) + { + var value = index switch + { + 1 => new TestUnion_class_string_int_bool_guid("text"), + 2 => new TestUnion_class_string_int_bool_guid(42), + 3 => new TestUnion_class_string_int_bool_guid(true), + 4 => new TestUnion_class_string_int_bool_guid(new Guid("6EF10862-7FC4-4AEB-BC92-21E798AC54D0")), + _ => throw new Exception() + }; + + var state = new object(); + var calledActionOn = value.Switch(state, + @string: (s, v) => + { + s.Should().Be(state); + return (object)v; + }, + int32: (s, v) => + { + s.Should().Be(state); + return v; + }, + boolean: (s, v) => + { + s.Should().Be(state); + return v; + }, + guid: (s, v) => + { + s.Should().Be(state); + return v; + }); + + calledActionOn.Should().Be(index == 4 ? new Guid((string)expected) : expected); + } + + [Theory] + [InlineData(1, "text")] + [InlineData(2, 42)] + [InlineData(3, true)] + [InlineData(4, "6EF10862-7FC4-4AEB-BC92-21E798AC54D0")] + [InlineData(5, 'A')] + public void Should_pass_context_having_5_types(int index, object expected) + { + var value = index switch + { + 1 => new TestUnion_class_string_int_bool_guid_char("text"), + 2 => new TestUnion_class_string_int_bool_guid_char(42), + 3 => new TestUnion_class_string_int_bool_guid_char(true), + 4 => new TestUnion_class_string_int_bool_guid_char(new Guid("6EF10862-7FC4-4AEB-BC92-21E798AC54D0")), + 5 => new TestUnion_class_string_int_bool_guid_char('A'), + _ => throw new Exception() + }; + + var state = new object(); + var calledActionOn = value.Switch(state, + @string: (s, v) => + { + s.Should().Be(state); + return (object)v; + }, + int32: (s, v) => + { + s.Should().Be(state); + return v; + }, + boolean: (s, v) => + { + s.Should().Be(state); + return v; + }, + guid: (s, v) => + { + s.Should().Be(state); + return v; + }, + @char: (s, v) => + { + s.Should().Be(state); + return v; + }); + + calledActionOn.Should().Be(index == 4 ? new Guid((string)expected) : expected); + } + } + } + + public class HavingStruct + { + public class WithAction + { + [Theory] + [InlineData(1, "text")] + [InlineData(2, 42)] + public void Should_use_correct_arg_having_2_values(int index, object expected) + { + var value = index switch + { + 1 => new TestUnion_struct_string_int("text"), + 2 => new TestUnion_struct_string_int(42), + _ => throw new Exception() + }; + object calledActionOn = null; + + value.Switch(@string: v => + { + calledActionOn = v; + }, + int32: v => + { + calledActionOn = v; + }); + + calledActionOn.Should().Be(expected); + } + } + + public class WithActionAndState + { + [Theory] + [InlineData(1, "text")] + [InlineData(2, 42)] + public void Should_pass_context_having_2_types(int index, object expected) + { + var value = index switch + { + 1 => new TestUnion_struct_string_int("text"), + 2 => new TestUnion_struct_string_int(42), + _ => throw new Exception() + }; + + var state = new object(); + + object calledActionOn = null; + + value.Switch(state, + @string: (s, v) => + { + s.Should().Be(state); + calledActionOn = v; + }, + int32: (s, v) => + { + s.Should().Be(state); + calledActionOn = v; + }); + + calledActionOn.Should().Be(expected); + } + } + + public class WithFunc + { + [Theory] + [InlineData(1, "text")] + [InlineData(2, 42)] + public void Should_call_correct_arg_having_2_types(int index, object expected) + { + var value = index switch + { + 1 => new TestUnion_struct_string_int("text"), + 2 => new TestUnion_struct_string_int(42), + _ => throw new Exception() + }; + + var calledActionOn = value.Switch(@string: v => (object)v, + int32: v => v); + + calledActionOn.Should().Be(expected); + } + } + + public class WithFuncAndContext + { + [Theory] + [InlineData(1, "text")] + [InlineData(2, 42)] + public void Should_pass_context_having_2_types(int index, object expected) + { + var value = index switch + { + 1 => new TestUnion_struct_string_int("text"), + 2 => new TestUnion_struct_string_int(42), + _ => throw new Exception() + }; + + var state = new object(); + var calledActionOn = value.Switch(state, + @string: (s, v) => + { + s.Should().Be(state); + return (object)v; + }, + int32: (s, v) => + { + s.Should().Be(state); + return v; + }); + + calledActionOn.Should().Be(expected); + } + } + } +} diff --git a/test/Thinktecture.Runtime.Extensions.Tests/AdHocUnionTests/SwitchPartially.cs b/test/Thinktecture.Runtime.Extensions.Tests/AdHocUnionTests/SwitchPartially.cs index aa9e3c2b..f3cb38d0 100644 --- a/test/Thinktecture.Runtime.Extensions.Tests/AdHocUnionTests/SwitchPartially.cs +++ b/test/Thinktecture.Runtime.Extensions.Tests/AdHocUnionTests/SwitchPartially.cs @@ -401,6 +401,38 @@ public void Should_pass_state_having_2_types(int index, object expected) calledActionOn.Should().Be(expected); } +#if NET9_0_OR_GREATER + [Theory] + [InlineData(1, "text")] + [InlineData(2, 42)] + public void Should_pass_state_having_2_types_and_ref_struct(int index, object expected) + { + var value = index switch + { + 1 => new TestUnion_class_string_int("text"), + 2 => new TestUnion_class_string_int(42), + _ => throw new Exception() + }; + + var state = new TestRefStruct(42); + object calledActionOn = null; + + value.SwitchPartially(state, + @string: (s, v) => + { + s.Value.Should().Be(42); + calledActionOn = v; + }, + int32: (s, v) => + { + s.Value.Should().Be(42); + calledActionOn = v; + }); + + calledActionOn.Should().Be(expected); + } +#endif + [Theory] [InlineData(1, "text")] [InlineData(2, 42)] @@ -419,19 +451,19 @@ public void Should_pass_state_having_3_types(int index, object expected) object calledActionOn = null; value.SwitchPartially(state, - @string: (ctx, v) => + @string: (s, v) => { - ctx.Should().Be(state); + s.Should().Be(state); calledActionOn = v; }, - int32: (ctx, v) => + int32: (s, v) => { - ctx.Should().Be(state); + s.Should().Be(state); calledActionOn = v; }, - boolean: (ctx, v) => + boolean: (s, v) => { - ctx.Should().Be(state); + s.Should().Be(state); calledActionOn = v; }); @@ -458,24 +490,24 @@ public void Should_pass_state_having_5_types(int index, object expected) object calledActionOn = null; value.SwitchPartially(state, - @string: (ctx, v) => + @string: (s, v) => { - ctx.Should().Be(state); + s.Should().Be(state); calledActionOn = v; }, - int32: (ctx, v) => + int32: (s, v) => { - ctx.Should().Be(state); + s.Should().Be(state); calledActionOn = v; }, - boolean: (ctx, v) => + boolean: (s, v) => { - ctx.Should().Be(state); + s.Should().Be(state); calledActionOn = v; }, - guid: (ctx, v) => + guid: (s, v) => { - ctx.Should().Be(state); + s.Should().Be(state); calledActionOn = v; }); @@ -498,14 +530,14 @@ public void Should_pass_state_to_default_having_2_types(int index, object expect object calledActionOn = null; value.SwitchPartially(state, - @default: (ctx, v) => + @default: (s, v) => { - ctx.Should().Be(state); + s.Should().Be(state); calledActionOn = $"default:{v}"; }, - int32: (ctx, v) => + int32: (s, v) => { - ctx.Should().Be(state); + s.Should().Be(state); calledActionOn = v; }); @@ -530,19 +562,19 @@ public void Should_pass_state_to_default_having_3_types(int index, object expect object calledActionOn = null; value.SwitchPartially(state, - @default: (ctx, v) => + @default: (s, v) => { - ctx.Should().Be(state); + s.Should().Be(state); calledActionOn = $"default:{v}"; }, - int32: (ctx, v) => + int32: (s, v) => { - ctx.Should().Be(state); + s.Should().Be(state); calledActionOn = v; }, - boolean: (ctx, v) => + boolean: (s, v) => { - ctx.Should().Be(state); + s.Should().Be(state); calledActionOn = v; }); @@ -569,24 +601,24 @@ public void Should_pass_state_to_default_having_4_types(int index, object expect object calledActionOn = null; value.SwitchPartially(state, - @default: (ctx, v) => + @default: (s, v) => { - ctx.Should().Be(state); + s.Should().Be(state); calledActionOn = $"default:{v}"; }, - int32: (ctx, v) => + int32: (s, v) => { - ctx.Should().Be(state); + s.Should().Be(state); calledActionOn = v; }, - boolean: (ctx, v) => + boolean: (s, v) => { - ctx.Should().Be(state); + s.Should().Be(state); calledActionOn = v; }, - guid: (ctx, v) => + guid: (s, v) => { - ctx.Should().Be(state); + s.Should().Be(state); calledActionOn = v; }); @@ -615,29 +647,29 @@ public void Should_pass_state_to_default_having_5_types(int index, object expect object calledActionOn = null; value.SwitchPartially(state, - @default: (ctx, v) => + @default: (s, v) => { - ctx.Should().Be(state); + s.Should().Be(state); calledActionOn = $"default:{v}"; }, - int32: (ctx, v) => + int32: (s, v) => { - ctx.Should().Be(state); + s.Should().Be(state); calledActionOn = v; }, - boolean: (ctx, v) => + boolean: (s, v) => { - ctx.Should().Be(state); + s.Should().Be(state); calledActionOn = v; }, - guid: (ctx, v) => + guid: (s, v) => { - ctx.Should().Be(state); + s.Should().Be(state); calledActionOn = v; }, - @char: (ctx, v) => + @char: (s, v) => { - ctx.Should().Be(state); + s.Should().Be(state); calledActionOn = v; }); @@ -666,6 +698,27 @@ public void Should_use_correct_arg_having_2_types(int index, object expected) calledActionOn.Should().Be(expected); } +#if NET9_0_OR_GREATER + [Theory] + [InlineData(1, "text")] + [InlineData(2, 42)] + public void Should_use_correct_arg_having_2_types_returning_ref_struct(int index, object expected) + { + var value = index switch + { + 1 => new TestUnion_class_string_int("text"), + 2 => new TestUnion_class_string_int(42), + _ => throw new Exception() + }; + + var calledActionOn = value.SwitchPartially(@default: v => new TestRefStruct($"default:{v}"), + @string: v => new TestRefStruct(v), + int32: v => new TestRefStruct(v)); + + calledActionOn.Value.Should().Be(expected); + } +#endif + [Theory] [InlineData(1, "text")] [InlineData(2, 42)] @@ -849,25 +902,61 @@ public void Should_pass_state_having_2_types(int index, object expected) var state = new object(); var calledActionOn = value.SwitchPartially(state, - @default: (ctx, v) => + @default: (s, v) => { - ctx.Should().Be(state); + s.Should().Be(state); return (object)$"default:{v}"; }, - @string: (ctx, v) => + @string: (s, v) => { - ctx.Should().Be(state); + s.Should().Be(state); return v; }, - int32: (ctx, v) => + int32: (s, v) => { - ctx.Should().Be(state); + s.Should().Be(state); return v; }); calledActionOn.Should().Be(expected); } +#if NET9_0_OR_GREATER + [Theory] + [InlineData(1, "text")] + [InlineData(2, 42)] + public void Should_pass_state_having_2_types_and_ref_struct(int index, object expected) + { + var value = index switch + { + 1 => new TestUnion_class_string_int("text"), + 2 => new TestUnion_class_string_int(42), + _ => throw new Exception() + }; + + var state = new TestRefStruct(42); + + var calledActionOn = value.SwitchPartially(state, + @default: (s, v) => + { + s.Value.Should().Be(42); + return new TestRefStruct($"default:{v}"); + }, + @string: (s, v) => + { + s.Value.Should().Be(42); + return new TestRefStruct(v); + }, + int32: (s, v) => + { + s.Value.Should().Be(42); + return new TestRefStruct(v); + }); + + calledActionOn.Value.Should().Be(expected); + } +#endif + [Theory] [InlineData(1, "text")] [InlineData(2, 42)] @@ -885,24 +974,24 @@ public void Should_pass_state_having_3_types(int index, object expected) var state = new object(); var calledActionOn = value.SwitchPartially(state, - @default: (ctx, v) => + @default: (s, v) => { - ctx.Should().Be(state); + s.Should().Be(state); return (object)$"default:{v}"; }, - @string: (ctx, v) => + @string: (s, v) => { - ctx.Should().Be(state); + s.Should().Be(state); return v; }, - int32: (ctx, v) => + int32: (s, v) => { - ctx.Should().Be(state); + s.Should().Be(state); return v; }, - boolean: (ctx, v) => + boolean: (s, v) => { - ctx.Should().Be(state); + s.Should().Be(state); return v; }); @@ -928,29 +1017,29 @@ public void Should_pass_state_having_4_types(int index, object expected) var state = new object(); var calledActionOn = value.SwitchPartially(state, - @default: (ctx, v) => + @default: (s, v) => { - ctx.Should().Be(state); + s.Should().Be(state); return (object)$"default:{v}"; }, - @string: (ctx, v) => + @string: (s, v) => { - ctx.Should().Be(state); + s.Should().Be(state); return v; }, - int32: (ctx, v) => + int32: (s, v) => { - ctx.Should().Be(state); + s.Should().Be(state); return v; }, - boolean: (ctx, v) => + boolean: (s, v) => { - ctx.Should().Be(state); + s.Should().Be(state); return v; }, - guid: (ctx, v) => + guid: (s, v) => { - ctx.Should().Be(state); + s.Should().Be(state); return v; }); @@ -978,34 +1067,34 @@ public void Should_pass_state_having_5_types(int index, object expected) var state = new object(); var calledActionOn = value.SwitchPartially(state, - @default: (ctx, v) => + @default: (s, v) => { - ctx.Should().Be(state); + s.Should().Be(state); return (object)$"default:{v}"; }, - @string: (ctx, v) => + @string: (s, v) => { - ctx.Should().Be(state); + s.Should().Be(state); return v; }, - int32: (ctx, v) => + int32: (s, v) => { - ctx.Should().Be(state); + s.Should().Be(state); return v; }, - boolean: (ctx, v) => + boolean: (s, v) => { - ctx.Should().Be(state); + s.Should().Be(state); return v; }, - guid: (ctx, v) => + guid: (s, v) => { - ctx.Should().Be(state); + s.Should().Be(state); return v; }, - @char: (ctx, v) => + @char: (s, v) => { - ctx.Should().Be(state); + s.Should().Be(state); return v; }); @@ -1027,14 +1116,14 @@ public void Should_pass_state_to_default_having_2_types(int index, object expect var state = new object(); var calledActionOn = value.SwitchPartially(state, - @default: (ctx, v) => + @default: (s, v) => { - ctx.Should().Be(state); + s.Should().Be(state); return (object)$"default:{v}"; }, - int32: (ctx, v) => + int32: (s, v) => { - ctx.Should().Be(state); + s.Should().Be(state); return v; }); @@ -1058,19 +1147,19 @@ public void Should_pass_state_to_default_having_3_types(int index, object expect var state = new object(); var calledActionOn = value.SwitchPartially(state, - @default: (ctx, v) => + @default: (s, v) => { - ctx.Should().Be(state); + s.Should().Be(state); return (object)$"default:{v}"; }, - int32: (ctx, v) => + int32: (s, v) => { - ctx.Should().Be(state); + s.Should().Be(state); return v; }, - boolean: (ctx, v) => + boolean: (s, v) => { - ctx.Should().Be(state); + s.Should().Be(state); return v; }); @@ -1096,24 +1185,24 @@ public void Should_pass_state_to_default_having_4_types(int index, object expect var state = new object(); var calledActionOn = value.SwitchPartially(state, - @default: (ctx, v) => + @default: (s, v) => { - ctx.Should().Be(state); + s.Should().Be(state); return (object)$"default:{v}"; }, - int32: (ctx, v) => + int32: (s, v) => { - ctx.Should().Be(state); + s.Should().Be(state); return v; }, - boolean: (ctx, v) => + boolean: (s, v) => { - ctx.Should().Be(state); + s.Should().Be(state); return v; }, - guid: (ctx, v) => + guid: (s, v) => { - ctx.Should().Be(state); + s.Should().Be(state); return v; }); @@ -1141,29 +1230,29 @@ public void Should_pass_state_to_default_having_5_types(int index, object expect var state = new object(); var calledActionOn = value.SwitchPartially(state, - @default: (ctx, v) => + @default: (s, v) => { - ctx.Should().Be(state); + s.Should().Be(state); return (object)$"default:{v}"; }, - int32: (ctx, v) => + int32: (s, v) => { - ctx.Should().Be(state); + s.Should().Be(state); return v; }, - boolean: (ctx, v) => + boolean: (s, v) => { - ctx.Should().Be(state); + s.Should().Be(state); return v; }, - guid: (ctx, v) => + guid: (s, v) => { - ctx.Should().Be(state); + s.Should().Be(state); return v; }, - @char: (ctx, v) => + @char: (s, v) => { - ctx.Should().Be(state); + s.Should().Be(state); return v; }); @@ -1245,14 +1334,14 @@ public void Should_pass_state_having_2_types(int index, object expected) object calledActionOn = null; value.SwitchPartially(state, - @string: (ctx, v) => + @string: (s, v) => { - ctx.Should().Be(state); + s.Should().Be(state); calledActionOn = v; }, - int32: (ctx, v) => + int32: (s, v) => { - ctx.Should().Be(state); + s.Should().Be(state); calledActionOn = v; }); @@ -1275,14 +1364,14 @@ public void Should_pass_state_to_default_having_2_types(int index, object expect object calledActionOn = null; value.SwitchPartially(state, - @default: (ctx, v) => + @default: (s, v) => { - ctx.Should().Be(state); + s.Should().Be(state); calledActionOn = $"default:{v}"; }, - int32: (ctx, v) => + int32: (s, v) => { - ctx.Should().Be(state); + s.Should().Be(state); calledActionOn = v; }); @@ -1347,19 +1436,19 @@ public void Should_pass_state_having_2_types(int index, object expected) var state = new object(); var calledActionOn = value.SwitchPartially(state, - @default: (ctx, v) => + @default: (s, v) => { - ctx.Should().Be(state); + s.Should().Be(state); return (object)$"default:{v}"; }, - @string: (ctx, v) => + @string: (s, v) => { - ctx.Should().Be(state); + s.Should().Be(state); return v; }, - int32: (ctx, v) => + int32: (s, v) => { - ctx.Should().Be(state); + s.Should().Be(state); return v; }); @@ -1381,14 +1470,14 @@ public void Should_pass_state_to_default_having_2_types(int index, object expect var state = new object(); var calledActionOn = value.SwitchPartially(state, - @default: (ctx, v) => + @default: (s, v) => { - ctx.Should().Be(state); + s.Should().Be(state); return (object)$"default:{v}"; }, - int32: (ctx, v) => + int32: (s, v) => { - ctx.Should().Be(state); + s.Should().Be(state); return v; }); diff --git a/test/Thinktecture.Runtime.Extensions.Tests/EnumTests/Map.cs b/test/Thinktecture.Runtime.Extensions.Tests/EnumTests/Map.cs index 74980f58..a64cd834 100644 --- a/test/Thinktecture.Runtime.Extensions.Tests/EnumTests/Map.cs +++ b/test/Thinktecture.Runtime.Extensions.Tests/EnumTests/Map.cs @@ -18,6 +18,16 @@ public void Should_return_correct_item() .Should().Be(2); } +#if NET9_0_OR_GREATER + [Fact] + public void Should_return_ref_struct() + { + ValidTestEnum.Item1.Map(item1: new TestRefStruct(1), + item2: new TestRefStruct(2)) + .Value.Should().Be(1); + } +#endif + [Theory] [InlineData(0)] [InlineData(1)] diff --git a/test/Thinktecture.Runtime.Extensions.Tests/EnumTests/MapPartially.cs b/test/Thinktecture.Runtime.Extensions.Tests/EnumTests/MapPartially.cs index 87806956..6b21d3e0 100644 --- a/test/Thinktecture.Runtime.Extensions.Tests/EnumTests/MapPartially.cs +++ b/test/Thinktecture.Runtime.Extensions.Tests/EnumTests/MapPartially.cs @@ -31,6 +31,17 @@ public void Should_return_correct_item() .Should().Be(0); } +#if NET9_0_OR_GREATER + [Fact] + public void Should_return_ref_struct() + { + ValidTestEnum.Item1.MapPartially(@default: new TestRefStruct(0), + item1: new TestRefStruct(1), + item2: new TestRefStruct(2)) + .Value.Should().Be(1); + } +#endif + [Theory] [InlineData(-1)] [InlineData(1)] diff --git a/test/Thinktecture.Runtime.Extensions.Tests/EnumTests/Switch.cs b/test/Thinktecture.Runtime.Extensions.Tests/EnumTests/Switch.cs index 04bc4137..315901bb 100644 --- a/test/Thinktecture.Runtime.Extensions.Tests/EnumTests/Switch.cs +++ b/test/Thinktecture.Runtime.Extensions.Tests/EnumTests/Switch.cs @@ -129,6 +129,32 @@ public void Should_pass_state() calledActionOn.Should().Be(ValidTestEnum.Item1); } +#if NET9_0_OR_GREATER + [Fact] + public void Should_pass_state_having_ref_struct() + { + ValidTestEnum calledActionOn = null; + + var obj = new TestRefStruct(42); + + ValidTestEnum.Item1.Switch(obj, + item1: o => + { + o.Value.Should().Be(42); + + calledActionOn = ValidTestEnum.Item1; + }, + item2: o => + { + o.Value.Should().Be(42); + + calledActionOn = ValidTestEnum.Item2; + }); + + calledActionOn.Should().Be(ValidTestEnum.Item1); + } +#endif + [Theory] [InlineData(true)] [InlineData(false)] @@ -242,6 +268,16 @@ public void Should_call_correct_arg() .Should().Be(ValidTestEnum.Item1); } +#if NET9_0_OR_GREATER + [Fact] + public void Should_call_correct_arg_and_return_ref_struct() + { + ValidTestEnum.Item1.Switch(item1: () => new TestRefStruct(1), + item2: () => new TestRefStruct(2)) + .Value.Should().Be(1); + } +#endif + [Theory] [InlineData(true)] [InlineData(false)] @@ -303,6 +339,29 @@ public void Should_pass_state() .Should().Be(ValidTestEnum.Item1); } +#if NET9_0_OR_GREATER + [Fact] + public void Should_pass_state_having_ref_struct() + { + var obj = new TestRefStruct(42); + + ValidTestEnum.Item1.Switch(obj, + item1: o => + { + o.Value.Should().Be(42); + + return new TestRefStruct(1); + }, + item2: o => + { + o.Value.Should().Be(42); + + return new TestRefStruct(2); + }) + .Value.Should().Be(1); + } +#endif + [Theory] [InlineData(true)] [InlineData(false)] diff --git a/test/Thinktecture.Runtime.Extensions.Tests/EnumTests/SwitchPartially.cs b/test/Thinktecture.Runtime.Extensions.Tests/EnumTests/SwitchPartially.cs index 710103af..2dc2ca1c 100644 --- a/test/Thinktecture.Runtime.Extensions.Tests/EnumTests/SwitchPartially.cs +++ b/test/Thinktecture.Runtime.Extensions.Tests/EnumTests/SwitchPartially.cs @@ -216,6 +216,32 @@ public void Should_pass_state() calledActionOn.Should().Be(ValidTestEnum.Item1); } +#if NET9_0_OR_GREATER + [Fact] + public void Should_pass_state_having_ref_struct() + { + ValidTestEnum calledActionOn = null; + + var obj = new TestRefStruct(42); + + ValidTestEnum.Item1.SwitchPartially(obj, + item1: o => + { + o.Value.Should().Be(42); + + calledActionOn = ValidTestEnum.Item1; + }, + item2: o => + { + o.Value.Should().Be(42); + + calledActionOn = ValidTestEnum.Item2; + }); + + calledActionOn.Should().Be(ValidTestEnum.Item1); + } +#endif + [Fact] public void Should_pass_state_to_default() { @@ -558,6 +584,35 @@ public void Should_pass_state() .Should().Be(ValidTestEnum.Item1); } +#if NET9_0_OR_GREATER + [Fact] + public void Should_pass_state_having_ref_struct() + { + var obj = new TestRefStruct(42); + + ValidTestEnum.Item1.SwitchPartially(obj, + @default: (o, item) => + { + o.Value.Should().Be(42); + + return new TestRefStruct(-1); + }, + item1: o => + { + o.Value.Should().Be(42); + + return new TestRefStruct(1); + }, + item2: o => + { + o.Value.Should().Be(42); + + return new TestRefStruct(2); + }) + .Value.Should().Be(1); + } +#endif + [Fact] public void Should_pass_state_to_default() { diff --git a/test/Thinktecture.Runtime.Extensions.Tests/UnionTests/Map.cs b/test/Thinktecture.Runtime.Extensions.Tests/UnionTests/Map.cs index 771abc91..3c5c1940 100644 --- a/test/Thinktecture.Runtime.Extensions.Tests/UnionTests/Map.cs +++ b/test/Thinktecture.Runtime.Extensions.Tests/UnionTests/Map.cs @@ -26,6 +26,26 @@ public void Should_use_correct_arg_having_2_values(int index, string expected) calledActionOn.Should().Be(expected); } + +#if NET9_0_OR_GREATER + [Theory] + [InlineData(1, "1")] + [InlineData(2, "2")] + public void Should_use_correct_arg_having_2_values_and_return_ref_struct(int index, string expected) + { + var value = index switch + { + 1 => (TestUnion)new TestUnion.Child1("1"), + 2 => new TestUnion.Child2("2"), + _ => throw new Exception() + }; + + var calledActionOn = value.Map(child1: new TestRefStruct("1"), + child2: new TestRefStruct("2")); + + calledActionOn.Value.Should().Be(expected); + } +#endif } public class HavingRecord diff --git a/test/Thinktecture.Runtime.Extensions.Tests/UnionTests/MapPartially.cs b/test/Thinktecture.Runtime.Extensions.Tests/UnionTests/MapPartially.cs index 48003bf5..2de58dba 100644 --- a/test/Thinktecture.Runtime.Extensions.Tests/UnionTests/MapPartially.cs +++ b/test/Thinktecture.Runtime.Extensions.Tests/UnionTests/MapPartially.cs @@ -28,6 +28,27 @@ public void Should_use_correct_arg_having_2_types(int index, string expected) calledActionOn.Should().Be(expected); } +#if NET9_0_OR_GREATER + [Theory] + [InlineData(1, "1")] + [InlineData(2, "2")] + public void Should_use_correct_arg_having_2_types_and_return_ref_struct(int index, string expected) + { + var value = index switch + { + 1 => (TestUnion)new TestUnion.Child1("1"), + 2 => new TestUnion.Child2("2"), + _ => throw new Exception() + }; + + var calledActionOn = value.MapPartially(@default: new TestRefStruct("default"), + child1: new TestRefStruct("1"), + child2: new TestRefStruct("2")); + + calledActionOn.Value.Should().Be(expected); + } +#endif + [Theory] [InlineData(1, "default")] [InlineData(2, "2")] diff --git a/test/Thinktecture.Runtime.Extensions.Tests/UnionTests/Switch.cs b/test/Thinktecture.Runtime.Extensions.Tests/UnionTests/Switch.cs index 54727432..94ad5677 100644 --- a/test/Thinktecture.Runtime.Extensions.Tests/UnionTests/Switch.cs +++ b/test/Thinktecture.Runtime.Extensions.Tests/UnionTests/Switch.cs @@ -68,6 +68,39 @@ public void Should_pass_state_having_2_types(int index, string expected) calledActionOn.Should().Be(expected); } + +#if NET9_0_OR_GREATER + [Theory] + [InlineData(1, "1")] + [InlineData(2, "2")] + public void Should_pass_state_having_2_types_and_ref_struct(int index, string expected) + { + var value = index switch + { + 1 => (TestUnion)new TestUnion.Child1("1"), + 2 => new TestUnion.Child1("2"), + _ => throw new Exception() + }; + + var state = new TestRefStruct(42); + + string calledActionOn = null; + + value.Switch(state, + child1: (s, v) => + { + s.Value.Should().Be(42); + calledActionOn = v.Name; + }, + child2: (s, v) => + { + s.Value.Should().Be(42); + calledActionOn = v.Name; + }); + + calledActionOn.Should().Be(expected); + } +#endif } public class WithFunc @@ -89,6 +122,26 @@ public void Should_call_correct_arg_having_2_types(int index, string expected) result.Should().Be(expected); } + +#if NET9_0_OR_GREATER + [Theory] + [InlineData(1, "1")] + [InlineData(2, "2")] + public void Should_call_correct_arg_having_2_types_returning_ref_struct(int index, string expected) + { + var value = index switch + { + 1 => (TestUnion)new TestUnion.Child1("1"), + 2 => new TestUnion.Child1("2"), + _ => throw new Exception() + }; + + var result = value.Switch(child1: v => new TestRefStruct(v.Name), + child2: v => new TestRefStruct(v.Name)); + + result.Value.Should().Be(expected); + } +#endif } public class WithFuncAndContext @@ -107,19 +160,49 @@ public void Should_pass_state_having_2_types(int index, string expected) var state = new object(); var calledActionOn = value.Switch(state, - child1: (ctx, v) => + child1: (s, v) => { - ctx.Should().Be(state); + s.Should().Be(state); return v.Name; }, - child2: (ctx, v) => + child2: (s, v) => { - ctx.Should().Be(state); + s.Should().Be(state); return v.Name; }); calledActionOn.Should().Be(expected); } + +#if NET9_0_OR_GREATER + [Theory] + [InlineData(1, "1")] + [InlineData(2, "2")] + public void Should_pass_state_having_2_types_and_ref_struct(int index, string expected) + { + var value = index switch + { + 1 => (TestUnion)new TestUnion.Child1("1"), + 2 => new TestUnion.Child1("2"), + _ => throw new Exception() + }; + + var state = new TestRefStruct(42); + var calledActionOn = value.Switch(state, + child1: (s, v) => + { + s.Value.Should().Be(42); + return new TestRefStruct(v.Name); + }, + child2: (s, v) => + { + s.Value.Should().Be(42); + return new TestRefStruct(v.Name); + }); + + calledActionOn.Value.Should().Be(expected); + } +#endif } } @@ -224,14 +307,14 @@ public void Should_pass_state_having_2_types(int index, string expected) var state = new object(); var calledActionOn = value.Switch(state, - child1: (ctx, v) => + child1: (s, v) => { - ctx.Should().Be(state); + s.Should().Be(state); return v.Name; }, - child2: (ctx, v) => + child2: (s, v) => { - ctx.Should().Be(state); + s.Should().Be(state); return v.Name; }); diff --git a/test/Thinktecture.Runtime.Extensions.Tests/UnionTests/SwitchPartially.cs b/test/Thinktecture.Runtime.Extensions.Tests/UnionTests/SwitchPartially.cs index 8e2b0d79..37eac8d0 100644 --- a/test/Thinktecture.Runtime.Extensions.Tests/UnionTests/SwitchPartially.cs +++ b/test/Thinktecture.Runtime.Extensions.Tests/UnionTests/SwitchPartially.cs @@ -93,6 +93,38 @@ public void Should_pass_state_having_2_types(int index, string expected) calledActionOn.Should().Be(expected); } +#if NET9_0_OR_GREATER + [Theory] + [InlineData(1, "1")] + [InlineData(2, "2")] + public void Should_pass_state_having_2_types_and_ref_struct(int index, string expected) + { + var value = index switch + { + 1 => (TestUnion)new TestUnion.Child1("1"), + 2 => new TestUnion.Child2("2"), + _ => throw new Exception() + }; + + var state = new TestRefStruct(42); + string calledActionOn = null; + + value.SwitchPartially(state, + child1: (s, v) => + { + s.Value.Should().Be(42); + calledActionOn = v.Name; + }, + child2: (s, v) => + { + s.Value.Should().Be(42); + calledActionOn = v.Name; + }); + + calledActionOn.Should().Be(expected); + } +#endif + [Theory] [InlineData(1, "default:1")] [InlineData(2, "2")] @@ -145,6 +177,27 @@ public void Should_use_correct_arg_having_2_types(int index, string expected) calledActionOn.Should().Be(expected); } +#if NET9_0_OR_GREATER + [Theory] + [InlineData(1, "1")] + [InlineData(2, "2")] + public void Should_use_correct_arg_having_2_types_returning_ref_struct(int index, string expected) + { + var value = index switch + { + 1 => (TestUnion)new TestUnion.Child1("1"), + 2 => new TestUnion.Child2("2"), + _ => throw new Exception() + }; + + var calledActionOn = value.SwitchPartially(@default: v => new TestRefStruct($"default:{v.Name}"), + child1: v => new TestRefStruct(v.Name), + child2: v => new TestRefStruct(v.Name)); + + calledActionOn.Value.Should().Be(expected); + } +#endif + [Theory] [InlineData(1, "default:1")] [InlineData(2, "2")]