Skip to content

Instantly share code, notes, and snippets.

@szabba szabba/.gitignore
Last active Sep 5, 2016

Embed
What would you like to do?
Elm BDD API sketch
elm-stuff/
module BDD exposing (..)
import String
import Expect exposing (Expectation)
import State exposing (State)
type alias Spec a =
State Trace a
type alias Trace =
{ messages : List String
, result : Expectation
}
given : String -> a -> Spec a
given msg x =
log ("GIVEN " ++ msg) |> andThen (\() -> State.state x)
when : String -> Spec ()
when msg =
log ("WHEN " ++ msg)
then' : String -> Expectation -> Spec ()
then' msg expectation =
log ("THEN " ++ msg) |> andThen (assert expectation |> always)
assert : Expectation -> Spec ()
assert expectation =
State.modify
<| \({ result } as trace) ->
result
|> Expect.getFailure
|> Maybe.map (always trace)
|> Maybe.withDefault { trace | result = expectation }
log : String -> Spec ()
log msg =
State.modify
<| \({ result, messages } as trace) ->
case result |> Expect.getFailure of
Just _ ->
trace
Nothing ->
{ trace | messages = msg :: messages }
map : (a -> b) -> Spec a -> Spec b
map =
State.map
andThen : (a -> Spec b) -> Spec a -> Spec b
andThen =
flip State.andThen
run : Spec a -> Expectation
run spec =
let
{ messages, result } =
spec
|> State.finalState { messages = [], result = Expect.pass }
in
result
|> Expect.getFailure
|> Maybe.map
(\{ message } ->
messages
|> List.reverse
|> (::) message
|> List.reverse
|> String.join "\n"
|> Expect.fail
)
|> Maybe.withDefault Expect.pass
{
"version": "1.0.0",
"summary": "helpful summary of your project, less than 80 characters",
"repository": "https://github.com/user/project.git",
"license": "BSD3",
"source-directories": [
"."
],
"exposed-modules": [],
"dependencies": {
"elm-community/elm-test": "2.0.1 <= v < 3.0.0",
"elm-lang/core": "4.0.5 <= v < 5.0.0",
"folkertdev/elm-state": "1.0.0 <= v < 2.0.0"
},
"elm-version": "0.17.0 <= v < 0.18.0"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.