Skip to content

Commit

Permalink
readmeCSharp more or less done
Browse files Browse the repository at this point in the history
  • Loading branch information
dharmaturtle committed Jul 4, 2023
1 parent 9915cfd commit aecd795
Show file tree
Hide file tree
Showing 3 changed files with 356 additions and 302 deletions.
159 changes: 90 additions & 69 deletions examples/Hedgehog.Xunit.Examples.CSharp/DocumentationSamples.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,29 @@
using Hedgehog.Linq;
using Hedgehog.Xunit;
using Microsoft.FSharp.Core;
using static Hedgehog.Linq.Property;
using Gen = Linq.Gen;
using Property = Linq.Property;
using Range = Linq.Range;

public class DocumentationSamples
{
[Fact]
public void Reversing_a_list_twice_yields_the_original_list()
{
var gen = GenX.auto<List<int>>();
var prop = from xs in Property.ForAll(gen)
let testList = Enumerable.Reverse(xs).Reverse().ToList()
select Assert.Equal(xs, testList);
prop.Check();
}

[Property]
public void Reversing_a_list_twice_yields_the_original_list_with_xunit(List<int> xs)
{
var testList = Enumerable.Reverse(xs).Reverse().ToList();
Assert.Equal(xs, testList);
}

private readonly ITestOutputHelper _output;

public DocumentationSamples(ITestOutputHelper output)
Expand All @@ -29,120 +46,126 @@ public void Can_generate_an_int(
[Property(Skip = "For documentation purposes")]
public bool Will_fail(bool value) => value;

//[Property]
[Property(Skip = "For documentation purposes")]
public void Will_fail_by_assertion(
bool value) =>
Assert.True(value, "All booleans values should be true!");

//[Property]
[Property(Skip = "Problem with async method")]
public async Task<bool> Async_with_exception_shrinks(int i)
public async Task Task_with_exception_shrinks(int i)
{
await Task.Delay(100);
//Assert.True(i +i == 1);
return true;
if (i > 10) throw new Exception("whoops");
}

//[Property]
[Property(Skip = "Problem with Result")]
public FSharpResult<int, string> Result_with_Error_shrinks(int i) =>
i < 10
? FSharpResult<int, string>.NewOk(i)
: FSharpResult<int, string>.NewError("humbug!");

//[Property]
[Property(Skip = "For documentation purposes")]
public Property<bool> Returning_a_failing_property_bool_with_an_external_number_gen_fails_and_shrinks(int i) =>
from fifty in Property.ForAll(Hedgehog.Gen.constant(50))
select i <= fifty;

public class AutoGenConfigContainer
{
public static AutoGenConfig _ =>
GenX.defaults.WithGenerator(Gen.Int32(Range.FromValue(13)));
}

[Property(tests: 3)]
public void This_runs_3_times() => _output.WriteLine($"Test run");

[Property(Shrinks = 0, Skip = "For documentation purposes")]
public void No_shrinks_occur(int i)
{
if (i > 50)
{
throw new Exception("oops");
}
}

[Fact]
public void Can_create_some()
{
var t = FSharpOption<int>.Some(5);
}

[Property(typeof(AutoGenConfigContainer))]
public bool This_test_passes_because_always_13(
int i) =>
i == 13;
public bool This_test_passes_because_always_13(int i) => i == 13;

public class ConfigWithArgs
{
public static AutoGenConfig _(
string word,
int number) =>
GenX.defaults
.WithGenerator(Hedgehog.Gen.constant(word))
.WithGenerator(Hedgehog.Gen.constant(number));
.WithGenerator(Hedgehog.Gen.constant(word))
.WithGenerator(Hedgehog.Gen.constant(number));
}

[Property(AutoGenConfig = typeof(ConfigWithArgs), AutoGenConfigArgs = new object[] { "foo", 13 })]
public bool This_also_passes(string s, int i) =>
s == "foo" && i == 13;
s == "foo" && i == 13;

internal static Task FooAsync()
[Property(tests: 3)]
public void This_runs_3_times() => _output.WriteLine($"Test run");

[Property(Shrinks = 0, Skip = "For documentation purposes")]
public void No_shrinks_occur(int i)
{
return Task.Delay(100);
if (i > 50)
{
throw new Exception("oops");
}
}

[Property(Size = 2)]
public void i_mostly_ranges_between_neg_1_and_1(int i) => _output.WriteLine(i.ToString());

[Property]
public async Task Async_Task_property(
int i)
[Recheck("44_13097736474433561873_6153509253234735533_")]
public bool this_passes(int i) => i == 12345;

public class Five : GenAttribute<int>
{
await FooAsync();
Assert.StrictEqual(i, i);
public override Gen<int> Generator => Gen.Int32(Range.FromValue(5));
}

[Property]
public Task Task_property(
int i)
public bool Can_set_parameter_as_5(
[Five] int five) =>
five == 5;

public class ConstInt : GenAttribute<int>
{
Assert.StrictEqual(i, i);
return Task.Delay(100);
private readonly int _i;
public ConstInt(int i)
{
_i = i;
}
public override Gen<int> Generator => Gen.Int32(Range.FromValue(_i));
}

[Property]
public async Task<bool> Async_boolean(bool i)
[Property(typeof(AutoGenConfigContainer))]
public bool GenAttribute_overrides_Property_AutoGenConfig(int thirteen, [ConstInt(6)] int six) =>
thirteen == 13 && six == 6;

[Properties(Tests = 13, AutoGenConfig = typeof(AutoGenConfigContainer))]
public class __
{
await FooAsync();
return i || !i;

[Property(AutoGenConfig = typeof(AutoGenConfigContainer), Tests = 2718, Skip = "just because")]
public void Not_sure_why_youd_do_this_but_okay() { }
}

[Property]
public async Task<bool> Task_boolean(bool i)
public class Int13
{
await Task.Delay(100);
return i || !i;
public static AutoGenConfig _ =>
GenX.defaults.WithGenerator(Gen.Int32(Range.FromValue(13)));
}

[Fact]
public void Reversing_a_list_twice_yields_the_original_list()
public class PropertyInt13Attribute : PropertyAttribute
{
var gen = GenX.auto<List<int>>();
var prop = from data in ForAll(gen)
let testList = Enumerable.Reverse(data).Reverse().ToList()
select Assert.Equivalent(data, testList, true);
prop.Check();
public PropertyInt13Attribute() : base(typeof(Int13)) { }
}

[Property]
public void Reversing_a_list_twice_yields_the_original_list_with_xunit(List<int> xs)
[PropertyInt13]
public bool This_passes(int i) => i == 13;

public class PropertiesInt13Attribute : PropertiesAttribute
{
var testList = Enumerable.Reverse(xs).ToList();
Assert.Equivalent(xs, testList, true);
public PropertiesInt13Attribute() : base(typeof(Int13)) { }
}

[Property]
[Recheck("44_13097736474433561873_6153509253234735533_")]
public bool this_passes_now(int i) =>
i == 12345;
[PropertiesInt13]
public class ___
{
[Property]
public bool This_also_passes(int i) => i == 13;
}
}


Expand All @@ -169,5 +192,3 @@ public bool this_passes_passes_and_runs_twice(
int i) =>
i == 2718;
}


6 changes: 3 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@ let ``Reversing a list twice yields the original list, with Hedgehog.Xunit`` (xs
## Documentation

`Hedgehog.Xunit` provides the following attributes:
* [`Property`](#-property)
* [`[<Property>]`](#-property)
Extends xUnit's `Fact` to call Hedgehog's `property`.
* [`Properties`](#-properties)
* [`[<Properties>]`](#-properties)
Configures all [`Property`](#-property) tagged tests in a module or class.
* [`GenAttribute`](#-genattribute)
Set a parameter's generator.
* [`Recheck`](#-recheck)
* [`[<Recheck>]`](#-recheck)
Run a test with a specific `Size` and `Seed`.

### 🔖 `[<Property>]`
Expand Down
Loading

0 comments on commit aecd795

Please sign in to comment.