From 71dfc6eec11949aee3d190b95e084eefc6acc024 Mon Sep 17 00:00:00 2001 From: John Efford Date: Sun, 18 Jun 2023 12:07:48 +0100 Subject: [PATCH] Add examples showcasing attributes in CSharp --- Hedgehog.Xunit.sln | 6 ++++ ...itiveAndNegativeGeneratorContainerTypes.cs | 36 +++++++++++++++++++ .../PositiveAndNegativeSimpleAttribute.cs | 28 +++++++++++++++ ...dNegativeUtilizingIntegerRangeAttribute.cs | 24 +++++++++++++ .../Usings.cs | 1 + ...ribute-based-parameters-comparision.csproj | 29 +++++++++++++++ 6 files changed, 124 insertions(+) create mode 100644 examples/csharp-attribute-based-parameters-comparision/PositiveAndNegativeGeneratorContainerTypes.cs create mode 100644 examples/csharp-attribute-based-parameters-comparision/PositiveAndNegativeSimpleAttribute.cs create mode 100644 examples/csharp-attribute-based-parameters-comparision/PositiveAndNegativeUtilizingIntegerRangeAttribute.cs create mode 100644 examples/csharp-attribute-based-parameters-comparision/Usings.cs create mode 100644 examples/csharp-attribute-based-parameters-comparision/csharp-attribute-based-parameters-comparision.csproj diff --git a/Hedgehog.Xunit.sln b/Hedgehog.Xunit.sln index 52a4d81..41aa603 100644 --- a/Hedgehog.Xunit.sln +++ b/Hedgehog.Xunit.sln @@ -13,6 +13,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution readme.md = readme.md EndProjectSection EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "csharp-attribute-based-parameters-comparision", "examples\csharp-attribute-based-parameters-comparision\csharp-attribute-based-parameters-comparision.csproj", "{2048061B-0561-4297-A02C-A12A263177A5}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -27,6 +29,10 @@ Global {63A8D184-519E-4061-8A74-F1EACAF3B0D5}.Debug|Any CPU.Build.0 = Debug|Any CPU {63A8D184-519E-4061-8A74-F1EACAF3B0D5}.Release|Any CPU.ActiveCfg = Release|Any CPU {63A8D184-519E-4061-8A74-F1EACAF3B0D5}.Release|Any CPU.Build.0 = Release|Any CPU + {2048061B-0561-4297-A02C-A12A263177A5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {2048061B-0561-4297-A02C-A12A263177A5}.Debug|Any CPU.Build.0 = Debug|Any CPU + {2048061B-0561-4297-A02C-A12A263177A5}.Release|Any CPU.ActiveCfg = Release|Any CPU + {2048061B-0561-4297-A02C-A12A263177A5}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/examples/csharp-attribute-based-parameters-comparision/PositiveAndNegativeGeneratorContainerTypes.cs b/examples/csharp-attribute-based-parameters-comparision/PositiveAndNegativeGeneratorContainerTypes.cs new file mode 100644 index 0000000..1fcb2b2 --- /dev/null +++ b/examples/csharp-attribute-based-parameters-comparision/PositiveAndNegativeGeneratorContainerTypes.cs @@ -0,0 +1,36 @@ +using Hedgehog; +using Hedgehog.Linq; +using Hedgehog.Xunit; +using Gen = Hedgehog.Linq.Gen; +using Range = Hedgehog.Linq.Range; + + +namespace csharp_attribute_based_parameters_comparision; + +public record PositiveInt(int Value); +public record NegativeInt( int Value ); + +public class Generators +{ + public static Gen GenPositiveInt => + from x in Gen.Int32(Range.Constant(1, Int32.MaxValue)) + select new PositiveInt(x); + + public static Gen GenNegativeInt => + from x in Gen.Int32(Range.Constant(Int32.MinValue, -1)) + select new NegativeInt(x); + + public static AutoGenConfig _ => GenX.defaults + .WithGenerator(GenPositiveInt) + .WithGenerator(GenNegativeInt); +} + +public class PositiveAndNegativeGeneratorContainerTypes +{ + [Property(typeof(Generators))] + public bool ResultOfAddingPositiveAndNegativeLessThanPositive( + PositiveInt positive, + NegativeInt negative) + => positive.Value + negative.Value < positive.Value; + +} diff --git a/examples/csharp-attribute-based-parameters-comparision/PositiveAndNegativeSimpleAttribute.cs b/examples/csharp-attribute-based-parameters-comparision/PositiveAndNegativeSimpleAttribute.cs new file mode 100644 index 0000000..8b4054b --- /dev/null +++ b/examples/csharp-attribute-based-parameters-comparision/PositiveAndNegativeSimpleAttribute.cs @@ -0,0 +1,28 @@ +using Hedgehog; +using Hedgehog.Xunit; +using Gen = Hedgehog.Linq.Gen; +using Range = Hedgehog.Linq.Range; + +namespace csharp_attribute_based_parameters_comparision; + +public class Int32Range : ParameterGeneraterBaseType +{ + private readonly int _min; + private readonly int _max; + + public Int32Range(int min, int max) + { + _min = min; + _max = max; + } + public override Gen Generator => Gen.Int32(Range.Constant(_min, _max)); +} + +public class PositiveAndNegativeWithAttributes +{ + [Property] + public bool ResultOfAddingPositiveAndNegativeLessThanPositive( + [Int32Range(1, Int32.MaxValue)] int positive, + [Int32Range(Int32.MinValue, -1 )] int negative) + => positive + negative < positive; +} diff --git a/examples/csharp-attribute-based-parameters-comparision/PositiveAndNegativeUtilizingIntegerRangeAttribute.cs b/examples/csharp-attribute-based-parameters-comparision/PositiveAndNegativeUtilizingIntegerRangeAttribute.cs new file mode 100644 index 0000000..87d2b25 --- /dev/null +++ b/examples/csharp-attribute-based-parameters-comparision/PositiveAndNegativeUtilizingIntegerRangeAttribute.cs @@ -0,0 +1,24 @@ +using Hedgehog; +using Hedgehog.Xunit; +using Gen = Hedgehog.Linq.Gen; +using Range = Hedgehog.Linq.Range; + +namespace csharp_attribute_based_parameters_comparision; + +public class Negative : ParameterGeneraterBaseType +{ + public override Gen Generator => Gen.Int32(Range.Constant(Int32.MinValue, -1)); +} +public class Positive : ParameterGeneraterBaseType +{ + public override Gen Generator => Gen.Int32(Range.Constant(1, Int32.MaxValue)); +} + +public class PositiveAndNegativeUtilizingIntegerRangeAttribute +{ + [Property] + public bool ResultOfAddingPositiveAndNegativeLessThanPositive( + [Positive] int positive, + [Negative] int negative) + => positive + negative < positive; +} diff --git a/examples/csharp-attribute-based-parameters-comparision/Usings.cs b/examples/csharp-attribute-based-parameters-comparision/Usings.cs new file mode 100644 index 0000000..8c927eb --- /dev/null +++ b/examples/csharp-attribute-based-parameters-comparision/Usings.cs @@ -0,0 +1 @@ +global using Xunit; \ No newline at end of file diff --git a/examples/csharp-attribute-based-parameters-comparision/csharp-attribute-based-parameters-comparision.csproj b/examples/csharp-attribute-based-parameters-comparision/csharp-attribute-based-parameters-comparision.csproj new file mode 100644 index 0000000..34ba708 --- /dev/null +++ b/examples/csharp-attribute-based-parameters-comparision/csharp-attribute-based-parameters-comparision.csproj @@ -0,0 +1,29 @@ + + + + net7.0 + csharp_attribute_based_parameters_comparision + enable + enable + + false + + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + + + + +