brian-watkins/elm-spec/1.0.0

NOTE: the latest version is 3.3.2

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 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 -> a -> Verdict

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 Bool

Claim that the subject is True

isFalse : Claim Bool

Claim that the subject is False

Claims about Lists

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

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

  • the subject has the same length as the provided list
  • for each item in the subject, that item satisfies the corresponding claim in the provided list.

For example:

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

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

isListWhereItemAt : Int -> Claim a -> List a -> Verdict

Claim that the subject is a list such that:

  • there is an item at the given index
  • that item satisfies the given claim
isListWithLength : Int -> List a -> Verdict

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

Claims about Maybe types

isSomething : Maybe a -> Verdict

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

isNothing : Maybe a -> Verdict

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

Combining Claims

satisfying : List (Claim a) -> a -> Verdict

Combine multiple claims into one.

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