Skip to content

Commit

Permalink
Merge pull request #247 from adam-becker/split-modules
Browse files Browse the repository at this point in the history
Split Property.fs across multiple files
  • Loading branch information
moodmosaic committed Jan 20, 2021
2 parents 42311bb + 0904a52 commit 01e4952
Show file tree
Hide file tree
Showing 10 changed files with 242 additions and 235 deletions.
6 changes: 5 additions & 1 deletion src/Hedgehog/AutoOpen.fs
@@ -1,4 +1,8 @@
[<AutoOpen>]
module internal AutoOpen

let flip f b a = f a b
let inline always (a : 'a) (_ : 'b) : 'a =
a

let inline flip (f : 'a -> 'b -> 'c) (b : 'b) (a : 'a) : 'c =
f a b
4 changes: 4 additions & 0 deletions src/Hedgehog/Hedgehog.fsproj
Expand Up @@ -32,6 +32,10 @@ https://github.com/hedgehogqa/fsharp-hedgehog/blob/master/doc/tutorial.md
<Compile Include="Random.fs" />
<Compile Include="Shrink.fs" />
<Compile Include="Gen.fs" />
<Compile Include="Journal.fs" />
<Compile Include="Tuple.fs" />
<Compile Include="Outcome.fs" />
<Compile Include="Report.fs" />
<Compile Include="Property.fs" />
<Compile Include="Linq\Gen.fs" />
<Compile Include="Linq\Property.fs" />
Expand Down
36 changes: 36 additions & 0 deletions src/Hedgehog/Journal.fs
@@ -0,0 +1,36 @@
namespace Hedgehog

[<Struct>]
type Journal =
| Journal of seq<unit -> string>

module Journal =

/// Creates a journal from a sequence of entries.
let ofSeq (entries : seq<unit -> string>) : Journal =
Journal entries

/// Evaluates a single entry, returning it's message.
let private evalEntry (f : unit -> string) : string =
f()

/// Evaluates all entries in the journal, returning their messages.
let eval (Journal entries : Journal) : seq<string> =
Seq.map evalEntry entries

/// Represents a journal with no entries.
let empty : Journal =
ofSeq []

/// Creates a single entry journal from a given message.
let singletonMessage (message : string) : Journal =
ofSeq [ fun () -> message ]

/// Creates a single entry journal from a given entry.
let singleton (entry : unit -> string) : Journal =
ofSeq [ entry ]

/// Creates a journal composed of entries from two journals.
let append (Journal xs) (Journal ys) : Journal =
Seq.append xs ys
|> ofSeq
8 changes: 4 additions & 4 deletions src/Hedgehog/Linq/Property.fs
Expand Up @@ -23,11 +23,11 @@ type Property = private Property of Property<unit> with
Property.ofBool value
|> Property

static member FromGen (gen : Gen<Journal * Result<'T>>) : Property<'T> =
static member FromGen (gen : Gen<Journal * Outcome<'T>>) : Property<'T> =
Property.ofGen gen

static member FromResult (result : Result<'T>) : Property<'T> =
Property.ofResult result
static member FromOutcome (result : Outcome<'T>) : Property<'T> =
Property.ofOutcome result

static member FromThrowing (throwingFunc : Action<'T>, arg : 'T) : Property =
Property.ofThrowing throwingFunc.Invoke arg
Expand All @@ -54,7 +54,7 @@ type Property = private Property of Property<unit> with
type PropertyExtensions private () =

[<Extension>]
static member ToGen (property : Property<'T>) : Gen<Journal * Result<'T>> =
static member ToGen (property : Property<'T>) : Gen<Journal * Outcome<'T>> =
Property.toGen property

[<Extension>]
Expand Down
44 changes: 44 additions & 0 deletions src/Hedgehog/Outcome.fs
@@ -0,0 +1,44 @@
namespace Hedgehog

type Outcome<'a> =
| Failure
| Discard
| Success of 'a

module Outcome =

let cata result failure discard success =
match result with
| Failure ->
failure()
| Discard ->
discard()
| Success(x) ->
success(x)

[<CompiledName("Map")>]
let map (f : 'a -> 'b) (result : Outcome<'a>) : Outcome<'b> =
cata result
(always Failure)
(always Discard)
(f >> Success)

[<CompiledName("Filter")>]
let filter (f : 'a -> bool) (result : Outcome<'a>) : Outcome<'a> =
let successOrDiscard x =
if f x then
Success(x)
else
Discard

cata result
(always Failure)
(always Discard)
successOrDiscard

[<CompiledName("IsFailure")>]
let isFailure (result : Outcome<'a>) : bool =
cata result
(always true)
(always false)
(always false)

0 comments on commit 01e4952

Please sign in to comment.