Add helpers in Lookup and return a Maybe from lookup. (#84)

This commit is contained in:
Connor Prussin
2017-10-23 18:50:27 -04:00
committed by GitHub
parent f38c5987ee
commit 133b98c9c6
8 changed files with 90 additions and 53 deletions

View File

@@ -3,6 +3,7 @@ module HTTPure.HeadersSpec where
import Prelude
import Control.Monad.Eff.Class as EffClass
import Data.Maybe as Maybe
import Data.Tuple as Tuple
import Test.Spec as Spec
@@ -16,14 +17,14 @@ lookupSpec :: SpecHelpers.Test
lookupSpec = Spec.describe "lookup" do
Spec.describe "when the string is in the header set" do
Spec.describe "when searching with lowercase" do
Spec.it "is the string" do
Headers.header "x-test" "test" !! "x-test" ?= "test"
Spec.it "is Just the string" do
Headers.header "x-test" "test" !! "x-test" ?= Maybe.Just "test"
Spec.describe "when searching with uppercase" do
Spec.it "is the string" do
Headers.header "x-test" "test" !! "X-Test" ?= "test"
Spec.it "is Just the string" do
Headers.header "x-test" "test" !! "X-Test" ?= Maybe.Just "test"
Spec.describe "when the string is not in the header set" do
Spec.it "is an empty string" do
Headers.empty !! "X-Test" ?= ""
Spec.it "is Nothing" do
((Headers.empty !! "X-Test") :: Maybe.Maybe String) ?= Maybe.Nothing
showSpec :: SpecHelpers.Test
showSpec = Spec.describe "show" do

View File

@@ -2,36 +2,57 @@ module HTTPure.LookupSpec where
import Prelude
import Data.Maybe as Maybe
import Data.StrMap as StrMap
import Test.Spec as Spec
import HTTPure.Lookup ((!!))
import HTTPure.Lookup ((!!), (!@), (!?))
import HTTPure.SpecHelpers as SpecHelpers
import HTTPure.SpecHelpers ((?=))
atSpec :: SpecHelpers.Test
atSpec = Spec.describe "at" do
Spec.describe "when the lookup returns a Just" do
Spec.it "is the value inside the Just" do
[ "one", "two", "three" ] !@ 1 ?= "two"
Spec.describe "when the lookup returns a Nothing" do
Spec.it "is mempty" do
[ "one", "two", "three" ] !@ 4 ?= ""
hasSpec :: SpecHelpers.Test
hasSpec = Spec.describe "has" do
Spec.describe "when the lookup returns a Just" do
Spec.it "is true" do
[ "one", "two", "three" ] !? 1 ?= true
Spec.describe "when the lookup returns a Nothing" do
Spec.it "is false" do
[ "one", "two", "three" ] !? 4 ?= false
lookupArraySpec :: SpecHelpers.Test
lookupArraySpec = Spec.describe "lookupArray" do
Spec.describe "when the index is in bounds" do
Spec.it "is the segment at the index" do
[ "one", "two", "three" ] !! 1 ?= "two"
Spec.it "is Just the value at the index" do
[ "one", "two", "three" ] !! 1 ?= Maybe.Just "two"
Spec.describe "when the index is out of bounds" do
Spec.it "is an empty monoid" do
[ "one", "two", "three" ] !! 4 ?= ""
Spec.it "is Nothing" do
(([ "one", "two", "three" ] !! 4) :: Maybe.Maybe String) ?= Maybe.Nothing
lookupStrMapSpec :: SpecHelpers.Test
lookupStrMapSpec = Spec.describe "lookupStrMap" do
Spec.describe "when the key is in the StrMap" do
Spec.it "is the value at the given key" do
mockStrMap !! "foo" ?= "bar"
Spec.it "is Just the value at the given key" do
mockStrMap !! "foo" ?= Maybe.Just "bar"
Spec.describe "when the key is not in the StrMap" do
Spec.it "is an empty monoid" do
mockStrMap !! "baz" ?= ""
Spec.it "is Nothing" do
((mockStrMap !! "baz") :: Maybe.Maybe String) ?= Maybe.Nothing
where
mockStrMap = StrMap.singleton "foo" "bar"
lookupSpec :: SpecHelpers.Test
lookupSpec = Spec.describe "Lookup" do
atSpec
hasSpec
lookupArraySpec
lookupStrMapSpec