Skip to content

Instantly share code, notes, and snippets.

@szabba
Last active September 5, 2016 14:49
Show Gist options
  • Save szabba/558b45a8b5d74cc5406838954c9193b6 to your computer and use it in GitHub Desktop.
Save szabba/558b45a8b5d74cc5406838954c9193b6 to your computer and use it in GitHub Desktop.
Elm BDD API sketch
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