File tree Expand file tree Collapse file tree
tests/Hedgehog.Linq.Tests Expand file tree Collapse file tree Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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}
You can’t perform that action at this time.
0 commit comments