Skip to content

Commit d453457

Browse files
committed
Add C# counterpart for Property.ignoreResult
1 parent 20c6e4b commit d453457

2 files changed

Lines changed: 57 additions & 0 deletions

File tree

src/Hedgehog/Linq/Property.fs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,26 @@ type PropertyExtensions private () =
9696
static member TryWith (property : Property<'T>, onError : Func<exn, Property<'T>>) : Property<'T> =
9797
Property.tryWith onError.Invoke property
9898

99+
/// <summary>
100+
/// Discards the result of a property, converting it to <see cref="Property"/>.
101+
/// This is useful when using assertion libraries that return non-unit types (e.g., fluent assertions).
102+
/// Assertions that throw exceptions will still cause the property to fail.
103+
/// </summary>
104+
/// <param name="property">The property whose result should be ignored.</param>
105+
/// <returns>A property that produces unit.</returns>
106+
/// <example>
107+
/// <code>
108+
/// var property =
109+
/// from x in Gen.Int32(Range.Linear(0, 100)).ForAll()
110+
/// select x.Should().Be(42); // Returns IAssertion
111+
///
112+
/// property.IgnoreResult().Check(); // Convert to <see cref="Property"/> and run
113+
/// </code>
114+
/// </example>
115+
[<Extension>]
116+
static member IgnoreResult (property : Property<'T>) : Property =
117+
property |> Property.ignoreResult |> Property
118+
99119
[<Extension>]
100120
static member Report (property : Property) : Report =
101121
let (Property property) = property

tests/Hedgehog.Linq.Tests/LinqTests.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,5 +302,42 @@ from subset in Gen.SubsetOf(distinctOriginal).ForAll()
302302

303303
prop.Check();
304304
}
305+
306+
// Test record to simulate fluent assertion library
307+
private record AssertionResult(bool Success);
308+
309+
private static AssertionResult ShouldBe(int expected, int actual)
310+
{
311+
if (actual == expected)
312+
return new AssertionResult(true);
313+
throw new Exception($"Expected {expected} but got {actual}");
314+
}
315+
316+
[Fact]
317+
public void IgnoreResult_ConvertsNonUnitPropertyToUnit()
318+
{
319+
var testRan = false;
320+
var property =
321+
from x in Gen.Int32(Range.Constant(42, 42)).ForAll()
322+
let _ = testRan = true
323+
select ShouldBe(42, x); // Returns AssertionResult, not unit
324+
325+
// Should compile and run
326+
property.IgnoreResult().Check();
327+
328+
Assert.True(testRan);
329+
}
330+
331+
[Fact]
332+
public void IgnoreResult_PreservesFailures()
333+
{
334+
var property =
335+
from x in Gen.Int32(Range.Constant(42, 42)).ForAll()
336+
select ShouldBe(99, x); // Will throw
337+
338+
var report = property.IgnoreResult().Report();
339+
340+
Assert.True(report.Status.IsFailed);
341+
}
305342
}
306343
}

0 commit comments

Comments
 (0)