brian-watkins / elm-spec / Spec.Claim

A claim is a function that maps a subject to a verdict.


type alias Claim a =
a -> Verdict

Represents a function from a subject to a verdict.


type Verdict
    = Accept
    | Reject Spec.Report.Report

The result of applying a claim to a subject.

A claim about a subject will either be accepted or rejected.

Basic Claims

isEqual : (a -> String) -> a -> Claim a

Claim that the subject is equal to the provided value.

The first argument to this function converts a value of some type to a string, which is used to produce readable messages if the claim is rejected. Instead of providing this string-generating function each time, I suggest adding a helper function that just uses the Debug.toString function like so:

equals : a -> Claim a
equals =
  Spec.Claim.isEqual Debug.toString

Then just use your equals function whenever you need to claim that a subject is equal to some value.

isTrue : Claim Basics.Bool

Claim that the subject is True

isFalse : Claim Basics.Bool

Claim that the subject is False

Claims about Strings

isStringContaining : Basics.Int -> String -> Claim String

Claim that the subject contains the given string the given number of times.

For example,

"some funny string"
  |> isStringContaining 2 "fun"

would be rejected, since it contains fun only once.

Claims about Lists

isListWhere : List (Claim a) -> Claim (List a)

Claim that the subject is a list where the following claims are satisfied:

For example:

[ 1, 2, 3 ]
  |> Spec.Claim.isListWhere
      [ Spec.Claim.isEqual Debug.toString 1
      , Spec.Claim.isEqual Debug.toString 27
      , Spec.Claim.isEqual Debug.toString 3
      ]

would result in a rejected claim, since 2 is not equal to 27.

isListWhereItemAt : Basics.Int -> Claim a -> Claim (List a)

Claim that the subject is a list such that:

isListWithLength : Basics.Int -> Claim (List a)

Claim that the subject is a list with the given length.

Claims about Maybe types

isSomething : Claim (Maybe a)

Claim that the subject is the Just case of the Maybe type.

isSomethingWhere : Claim a -> Claim (Maybe a)

Claim that the subject is the Just case of the Maybe type and that the associated value satisfies the given claim.

For example,

Just "apple"
  |> isSomethingWhere (isStringContaining 1 "cheese")

would be rejected.

isNothing : Claim (Maybe a)

Claim that the subject is the Nothing case of the Maybe type.

Working with Claims

satisfying : List (Claim a) -> Claim a

Combine multiple claims into one.

If any of the claims is rejected, then the combined claim is rejected.

specifyThat : (a -> b) -> Claim b -> Claim a

Claim that a value derived from the subject satisfies the given claim.

For example, the following claim:

{ x = 27, y = 31 }
  |> specifyThat .y (isEqual Debug.toString 31)

would be accepted.

mapRejection : (Spec.Report.Report -> Spec.Report.Report) -> Verdict -> Verdict

Modify the report associated with a rejected verdict; otherwise, do nothing.