Skip to content

Commit

Permalink
Add examples showcasing attributes in CSharp
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnEffo committed Jun 18, 2023
1 parent 4667514 commit 71dfc6e
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 0 deletions.
6 changes: 6 additions & 0 deletions Hedgehog.Xunit.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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<PositiveInt> GenPositiveInt =>
from x in Gen.Int32(Range.Constant(1, Int32.MaxValue))
select new PositiveInt(x);

public static Gen<NegativeInt> 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;

}
Original file line number Diff line number Diff line change
@@ -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<int>
{
private readonly int _min;
private readonly int _max;

public Int32Range(int min, int max)
{
_min = min;
_max = max;
}
public override Gen<int> 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;
}
Original file line number Diff line number Diff line change
@@ -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<int>
{
public override Gen<int> Generator => Gen.Int32(Range.Constant(Int32.MinValue, -1));
}
public class Positive : ParameterGeneraterBaseType<int>
{
public override Gen<int> Generator => Gen.Int32(Range.Constant(1, Int32.MaxValue));
}

public class PositiveAndNegativeUtilizingIntegerRangeAttribute
{
[Property]
public bool ResultOfAddingPositiveAndNegativeLessThanPositive(
[Positive] int positive,
[Negative] int negative)
=> positive + negative < positive;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
global using Xunit;
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<RootNamespace>csharp_attribute_based_parameters_comparision</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.1.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Hedgehog.Xunit\Hedgehog.Xunit.fsproj" />
</ItemGroup>

</Project>

0 comments on commit 71dfc6e

Please sign in to comment.