|
| 1 | +import Data.Char (toUpper) |
| 2 | +import Data.Time.Calendar (fromGregorian) |
| 3 | +import System.Exit (ExitCode (..), exitWith) |
| 4 | +import Test.HUnit (Assertion, Counts (..), Test (..), |
| 5 | + runTestTT, (@=?)) |
| 6 | + |
| 7 | +import Person (Address (..), Born (..), |
| 8 | + Name (..), Person (..), bornStreet, |
| 9 | + renameStreets, setBirthMonth, |
| 10 | + setCurrentStreet) |
| 11 | + |
| 12 | +testCase :: String -> Assertion -> Test |
| 13 | +testCase label assertion = TestLabel label (TestCase assertion) |
| 14 | + |
| 15 | +exitProperly :: IO Counts -> IO () |
| 16 | +exitProperly m = do |
| 17 | + counts <- m |
| 18 | + exitWith $ if failures counts /= 0 || errors counts /= 0 then ExitFailure 1 else ExitSuccess |
| 19 | + |
| 20 | +main :: IO () |
| 21 | +main = exitProperly $ runTestTT $ TestList |
| 22 | + [ TestList personTests ] |
| 23 | + |
| 24 | +testPerson :: Person |
| 25 | +testPerson = Person { |
| 26 | + _name = Name { |
| 27 | + _foreNames = "Jane Joanna", |
| 28 | + _surName = "Doe" |
| 29 | + }, |
| 30 | + _born = Born { |
| 31 | + _bornAt = Address { |
| 32 | + _street = "Longway", |
| 33 | + _houseNumber = 1024, |
| 34 | + _place = "Springfield", |
| 35 | + _country = "United States" |
| 36 | + }, |
| 37 | + _bornOn = fromGregorian 1984 4 12 |
| 38 | + }, |
| 39 | + _address = Address { |
| 40 | + _street = "Shortlane", |
| 41 | + _houseNumber = 2, |
| 42 | + _place = "Fallmeadow", |
| 43 | + _country = "Canada" |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | +personTests :: [Test] |
| 48 | +personTests = |
| 49 | + [ testCase "bornStreet" $ |
| 50 | + "Longway" @=? bornStreet (_born testPerson), |
| 51 | + testCase "setCurrentStreet" $ |
| 52 | + "Middleroad" @=? (_street . _address) (setCurrentStreet "Middleroad" testPerson), |
| 53 | + testCase "setBirthMonth" $ |
| 54 | + fromGregorian 1984 9 12 @=? (_bornOn . _born) (setBirthMonth 9 testPerson), |
| 55 | + testCase "renameStreets birth" $ |
| 56 | + "LONGWAY" @=? (_street . _bornAt . _born) (renameStreets (map toUpper) testPerson), |
| 57 | + testCase "renameStreets current" $ |
| 58 | + "SHORTLANE" @=? (_street . _address) (renameStreets (map toUpper) testPerson) |
| 59 | + ] |
0 commit comments