Revise tests

Link to fuzz test docs and Fuzzer docs

Move part9 to be part12

Update part11

Update 12, and some other Mains

Rearrange things, drop 2 modules

Add a new part12

Fix READMEs

Move some things up a directory

Update part11

Use ! []

Update parts7-9

Fix part12g

Swap part11 and part12

Fix readmes for part11 and part12

Add HtmlRunner to part8

Update part8 and part9 READMEs

rm part10/test
This commit is contained in:
Richard Feldman
2016-06-26 01:00:27 -07:00
parent 00e641d186
commit b557ee0842
45 changed files with 481 additions and 883 deletions

View File

@@ -6,7 +6,6 @@ import Html.Events exposing (..)
import Auth
import Json.Decode exposing (Decoder)
import Json.Decode.Pipeline exposing (..)
import Json.Encode
getQueryString : String -> String
@@ -97,8 +96,8 @@ type Msg
= Search
| SetQuery String
| DeleteById ResultId
| SetResults (List SearchResult)
| SetErrorMessage (Maybe String)
| HandleSearchResponse (List SearchResult)
| HandleSearchError (Maybe String)
| DoNothing
@@ -111,11 +110,11 @@ update searchFeed msg model =
SetQuery query ->
( { model | query = query }, Cmd.none )
SetResults results ->
HandleSearchResponse results ->
( { model | results = results }, Cmd.none )
SetErrorMessage errorMessage ->
( { model | errorMessage = errorMessage }, Cmd.none )
HandleSearchError error ->
( { model | errorMessage = error }, Cmd.none )
DeleteById idToHide ->
let
@@ -132,10 +131,11 @@ update searchFeed msg model =
( model, Cmd.none )
decodeGithubResponse : Json.Encode.Value -> Msg
decodeGithubResponse : Json.Decode.Value -> Msg
decodeGithubResponse value =
-- TODO use Json.Decode.DecodeValue to decode the response into an Action.
--
-- Hint: look at ElmHub.elm, specifically the definition of Action and
-- the deefinition of responseDecoder
SetErrorMessage (Just "TODO decode the response!")
case Json.Decode.decodeValue responseDecoder value of
Ok results ->
HandleSearchResponse results
Err err ->
HandleSearchError (Just err)

View File

@@ -19,10 +19,10 @@ decodeResponse : Json.Decode.Value -> Msg
decodeResponse json =
case Json.Decode.decodeValue responseDecoder json of
Err err ->
SetErrorMessage (Just err)
HandleSearchError (Just err)
Ok results ->
SetResults results
HandleSearchResponse results
port githubSearch : String -> Cmd msg

View File

@@ -21,12 +21,29 @@ elm-live Main.elm --open --output=elm.js
## Running Tests
First do this:
```bash
cd test
elm-package install
elm-test Test.elm
```
Then do either (or both!) of the following:
#### Running tests on the command line
```bash
elm-test NodeRunner.elm
```
#### Running tests in a browser
```bash
elm-reactor
```
Then visit [localhost:8000](http://localhost:8000) and choose `Html.elm`.
## References
* [Using Elm packages](https://github.com/elm-lang/elm-package/blob/master/README.md#basic-usage)

16
part8/test/HtmlRunner.elm Normal file
View File

@@ -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

19
part8/test/NodeRunner.elm Normal file
View File

@@ -0,0 +1,19 @@
port module Main exposing (..)
import Tests
import Test.Runner.Node as Runner
import Json.Decode exposing (Value)
-- To run this:
--
-- cd into part8/test
-- elm-test NodeRunner.elm
main : Program Never
main =
Runner.run emit Tests.all
port emit : ( String, Value ) -> Cmd msg

View File

@@ -1,55 +1,70 @@
port module Main exposing (..)
module Tests exposing (..)
import Test exposing (..)
import Fuzz exposing (..)
import Expect exposing (Expectation)
import ElmHub exposing (responseDecoder)
import Json.Decode exposing (decodeString, Value)
import Test.Runner.Node as Runner
import String
main : Program Never
main =
describe "Decoding responses from GitHub"
[ test "they can decode empty responses"
all : Test
all =
describe "GitHub Response Decoder"
[ test "it results in an Err for invalid JSON"
<| \() ->
let
emptyResponse =
"""{ "items": [] }"""
in
Expect.equal (Ok [])
(decodeString responseDecoder emptyResponse)
, test "they can decode responses with results in them"
<| \() ->
let
response =
"""{ "items": [
/* TODO: dummy JSON goes here */
] }"""
in
Expect.equal
(Ok
[ { id = 5, name = "foo", stars = 42 }
, { id = 3, name = "bar", stars = 77 }
]
)
(decodeString responseDecoder response)
, test "they result in an error for invalid JSON"
<| \() ->
let
response =
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/3.0.0/Result
-- Result docs: http://package.elm-lang.org/packages/elm-lang/core/4.0.1/Result
False
in
Expect.true "Expected decoding an invalid response to return an Err."
(isErrorResult (decodeString responseDecoder response))
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)
]
|> Runner.run emit
port emit : ( String, Value ) -> Cmd msg

View File

@@ -9,12 +9,13 @@
],
"exposed-modules": [],
"dependencies": {
"project-fuzzball/test": "2.0.1 <= v < 3.0.0",
"project-fuzzball/node": "1.0.2 <= v < 2.0.0",
"NoRedInk/elm-decode-pipeline": "1.1.2 <= v < 2.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"
"evancz/elm-http": "3.0.1 <= v < 4.0.0",
"project-fuzzball/node": "1.0.2 <= v < 2.0.0",
"project-fuzzball/test": "1.0.1 <= v < 2.0.0",
"project-fuzzball/test-runner": "1.0.1 <= v < 2.0.0"
},
"elm-version": "0.17.0 <= v < 0.18.0"
}
}