From b5e7011d2b372437e0d75c59c815a67ad660c652 Mon Sep 17 00:00:00 2001 From: Richard Feldman Date: Fri, 19 Aug 2016 04:55:44 -0700 Subject: [PATCH] Upgrade part8 and part9 to latest elm-test --- part8/README.md | 2 +- part9/README.md | 14 +++----- part9/tests/HtmlRunner.elm | 16 +++++++++ part9/tests/Main.elm | 18 ++++++++++ part9/tests/Tests.elm | 70 ++++++++++++++++++++++++++++++++++++ part9/tests/elm-package.json | 21 +++++++++++ 6 files changed, 130 insertions(+), 11 deletions(-) create mode 100644 part9/tests/HtmlRunner.elm create mode 100644 part9/tests/Main.elm create mode 100644 part9/tests/Tests.elm create mode 100644 part9/tests/elm-package.json diff --git a/part8/README.md b/part8/README.md index 8057645..e388cbe 100644 --- a/part8/README.md +++ b/part8/README.md @@ -36,7 +36,7 @@ cd tests elm-reactor ``` -Then visit [localhost:8000](http://localhost:8000) and choose `Html.elm`. +Then visit [localhost:8000](http://localhost:8000) and choose `HtmlRunner.elm`. ## References diff --git a/part9/README.md b/part9/README.md index 4eac4a0..8835c0b 100644 --- a/part9/README.md +++ b/part9/README.md @@ -21,25 +21,19 @@ elm-live Main.elm --open --output=elm.js ## Running Tests -First do this: - -```bash -cd test -elm-package install -``` - -Then do either (or both!) of the following: +Do either (or both!) of the following: #### Running tests on the command line ```bash -elm-test NodeRunner.elm +elm-test ``` #### Running tests in a browser ```bash +cd tests elm-reactor ``` -Then visit [localhost:8000](http://localhost:8000) and choose `Html.elm`. +Then visit [localhost:8000](http://localhost:8000) and choose `HtmlRunner.elm`. diff --git a/part9/tests/HtmlRunner.elm b/part9/tests/HtmlRunner.elm new file mode 100644 index 0000000..24ba2cb --- /dev/null +++ b/part9/tests/HtmlRunner.elm @@ -0,0 +1,16 @@ +module HtmlRunner exposing (..) + +import Tests +import Test.Runner.Html as Runner + + +-- To run this: +-- +-- cd into part8/test +-- elm-reactor +-- navigate to HtmlRunner.elm + + +main : Program Never +main = + Runner.run Tests.all diff --git a/part9/tests/Main.elm b/part9/tests/Main.elm new file mode 100644 index 0000000..80906ca --- /dev/null +++ b/part9/tests/Main.elm @@ -0,0 +1,18 @@ +port module Main exposing (..) + +import Tests +import Test.Runner.Node as Runner +import Json.Decode exposing (Value) + + +-- To run this: +-- +-- elm-test + + +main : Program Value +main = + Runner.run emit Tests.all + + +port emit : ( String, Value ) -> Cmd msg diff --git a/part9/tests/Tests.elm b/part9/tests/Tests.elm new file mode 100644 index 0000000..8263b8c --- /dev/null +++ b/part9/tests/Tests.elm @@ -0,0 +1,70 @@ +module Tests exposing (..) + +import Test exposing (..) +import Fuzz exposing (..) +import Expect exposing (Expectation) +import ElmHub exposing (responseDecoder) +import Json.Decode exposing (decodeString, Value) +import String + + +all : Test +all = + describe "GitHub Response Decoder" + [ test "it results in an Err for invalid JSON" + <| \() -> + let + json = + """{ "pizza": [] }""" + + isErrorResult result = + -- TODO return True if the given Result is an Err of some sort, + -- and False if it is an Ok of some sort. + -- + -- Result docs: http://package.elm-lang.org/packages/elm-lang/core/4.0.1/Result + False + in + json + |> decodeString responseDecoder + |> isErrorResult + |> Expect.true "Expected decoding an invalid response to return an Err." + , test "it successfully decodes a valid response" + <| \() -> + """{ "items": [ + /* TODO: put JSON here! */ + ] }""" + |> decodeString responseDecoder + |> Expect.equal + (Ok + [ { id = 5, name = "foo", stars = 42 } + , { id = 3, name = "bar", stars = 77 } + ] + ) + , test "it decodes one SearchResult for each 'item' in the JSON" + <| \() -> + let + -- TODO convert this to a fuzz test that generates a random + -- list of ids instead of this hardcoded list of three ids. + -- + -- fuzz test docs: http://package.elm-lang.org/packages/project-fuzzball/test/2.0.1/Test#fuzz + -- Fuzzer docs: http://package.elm-lang.org/packages/project-fuzzball/test/2.0.1/Fuzz + ids = + [ 12, 5, 76 ] + + jsonFromId id = + """{"id": """ ++ toString id ++ """, "full_name": "foo", "stargazers_count": 42}""" + + jsonItems = + String.join ", " (List.map jsonFromId ids) + + json = + """{ "items": [""" ++ jsonItems ++ """] }""" + in + case decodeString responseDecoder json of + Ok results -> + List.length results + |> Expect.equal (List.length ids) + + Err err -> + Expect.fail ("JSON decoding failed unexpectedly: " ++ err) + ] diff --git a/part9/tests/elm-package.json b/part9/tests/elm-package.json new file mode 100644 index 0000000..460cf01 --- /dev/null +++ b/part9/tests/elm-package.json @@ -0,0 +1,21 @@ +{ + "version": "1.0.0", + "summary": "Like GitHub, but for Elm stuff.", + "repository": "https://github.com/rtfeldman/elm-workshop.git", + "license": "BSD-3-Clause", + "source-directories": [ + ".", + ".." + ], + "exposed-modules": [], + "dependencies": { + "NoRedInk/elm-decode-pipeline": "1.1.2 <= v < 2.0.0", + "elm-community/elm-test": "2.0.1 <= v < 3.0.0", + "elm-lang/core": "4.0.1 <= v < 5.0.0", + "elm-lang/html": "1.0.0 <= v < 2.0.0", + "evancz/elm-http": "3.0.1 <= v < 4.0.0", + "rtfeldman/html-test-runner": "1.0.0 <= v < 2.0.0", + "rtfeldman/node-test-runner": "2.0.0 <= v < 3.0.0" + }, + "elm-version": "0.17.0 <= v < 0.18.0" +}