Skip to content

hspec-lens-person: Rewrite test to use hspec with fail-fast. #381

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 28, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion exercises/lens-person/package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ tests:
source-dirs: test
dependencies:
- lens-person
- HUnit
- hspec
68 changes: 31 additions & 37 deletions exercises/lens-person/src/Person.hs
Original file line number Diff line number Diff line change
@@ -1,39 +1,34 @@
module Person where

import Data.Time.Calendar

data Person = Person {
_name :: Name,
_born :: Born,
_address :: Address
}

data Name = Name {
_foreNames :: String, -- Space separated
_surName :: String
}

data Born = Born {
_bornAt :: Address,
_bornOn :: Day
}

data Address = Address {
_street :: String,
_houseNumber :: Int,
_place :: String, -- Village / city
_country :: String
}

-- Valid values of Gregorian are those for which 'Data.Time.Calendar.fromGregorianValid'
-- returns Just.
data Gregorian = Gregorian {
_year :: Integer,
_month :: Int,
_day :: Int
}

-- Implement these.
module Person
( Address (..)
, Born (..)
, Name (..)
, Person (..)
, bornStreet
, renameStreets
, setBirthMonth
, setCurrentStreet
) where

import Data.Time.Calendar (Day)

data Person = Person { _name :: Name
, _born :: Born
, _address :: Address
}

data Name = Name { _foreNames :: String
, _surName :: String
}

data Born = Born { _bornAt :: Address
, _bornOn :: Day
}

data Address = Address { _street :: String
, _houseNumber :: Int
, _place :: String
, _country :: String
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah I never understood this; I just deleted it in my submission

}

bornStreet :: Born -> String
bornStreet = undefined
Expand All @@ -44,6 +39,5 @@ setCurrentStreet = undefined
setBirthMonth :: Int -> Person -> Person
setBirthMonth = undefined

-- | Transform both birth and current street names.
renameStreets :: (String -> String) -> Person -> Person
renameStreets = undefined
96 changes: 52 additions & 44 deletions exercises/lens-person/test/Tests.hs
Original file line number Diff line number Diff line change
@@ -1,59 +1,67 @@
import Data.Char (toUpper)
import Data.Time.Calendar (fromGregorian)
import System.Exit (ExitCode (..), exitWith)
import Test.HUnit (Assertion, Counts (..), Test (..),
runTestTT, (@=?))
import Data.Char (toUpper)
import Data.Time.Calendar (fromGregorian)
import Test.Hspec (Spec, describe, it, shouldBe)
import Test.Hspec.Runner (configFastFail, defaultConfig, hspecWith)

import Person (Address (..), Born (..),
Name (..), Person (..), bornStreet,
renameStreets, setBirthMonth,
setCurrentStreet)
import Person
( Address (..)
, Born (..)
, Name (..)
, Person (..)
, bornStreet
, renameStreets
, setBirthMonth
, setCurrentStreet
)

testCase :: String -> Assertion -> Test
testCase label assertion = TestLabel label (TestCase assertion)
main :: IO ()
main = hspecWith defaultConfig {configFastFail = True} specs

exitProperly :: IO Counts -> IO ()
exitProperly m = do
counts <- m
exitWith $ if failures counts /= 0 || errors counts /= 0 then ExitFailure 1 else ExitSuccess
specs :: Spec
specs = describe "lens-person" $ do

main :: IO ()
main = exitProperly $ runTestTT $ TestList
[ TestList personTests ]
-- As of 2016-09-25, there was no reference file
-- for the test cases in `exercism/x-common`.

it "bornStreet" $
(bornStreet . _born) testPerson
`shouldBe` "Longway"

it "setCurrentStreet" $
(_street . _address . setCurrentStreet "Middleroad") testPerson
`shouldBe` "Middleroad"

it "setBirthMonth" $
(_bornOn . _born . setBirthMonth 9) testPerson
`shouldBe` fromGregorian 1984 9 12

it "renameStreets birth" $
(_street . _bornAt . _born . renameStreets (map toUpper)) testPerson
`shouldBe` "LONGWAY"

it "renameStreets current" $
(_street . _address . renameStreets (map toUpper)) testPerson
`shouldBe` "SHORTLANE"

testPerson :: Person
testPerson = Person {
_name = Name {
_foreNames = "Jane Joanna",
_surName = "Doe"
},
_foreNames = "Jane Joanna",
_surName = "Doe"
},
_born = Born {
_bornAt = Address {
_street = "Longway",
_houseNumber = 1024,
_place = "Springfield",
_country = "United States"
},
_street = "Longway" ,
_houseNumber = 1024 ,
_place = "Springfield" ,
_country = "United States"
},
_bornOn = fromGregorian 1984 4 12
},
_address = Address {
_street = "Shortlane",
_houseNumber = 2,
_place = "Fallmeadow",
_country = "Canada"
_street = "Shortlane" ,
_houseNumber = 2 ,
_place = "Fallmeadow",
_country = "Canada"
}
}

personTests :: [Test]
personTests =
[ testCase "bornStreet" $
"Longway" @=? bornStreet (_born testPerson),
testCase "setCurrentStreet" $
"Middleroad" @=? (_street . _address) (setCurrentStreet "Middleroad" testPerson),
testCase "setBirthMonth" $
fromGregorian 1984 9 12 @=? (_bornOn . _born) (setBirthMonth 9 testPerson),
testCase "renameStreets birth" $
"LONGWAY" @=? (_street . _bornAt . _born) (renameStreets (map toUpper) testPerson),
testCase "renameStreets current" $
"SHORTLANE" @=? (_street . _address) (renameStreets (map toUpper) testPerson)
]