Skip to content

Commit

Permalink
Use Hspec instead of HUnit
Browse files Browse the repository at this point in the history
I'm not sure why, but HUnit's @?= doesn't support Testable in QuickCheck, while Hspec's shouldBe does.
  • Loading branch information
igrep committed Dec 22, 2019
1 parent f0222ab commit 9e698ae
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 46 deletions.
2 changes: 1 addition & 1 deletion stack.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# Specifies the GHC version and set of packages available (e.g., lts-3.5, nightly-2015-09-21, ghc-7.10.2)
#resolver: lts-5.1
resolver: lts-12.4
resolver: lts-14.18

# Local packages, usually specified by relative directory name
packages:
Expand Down
85 changes: 44 additions & 41 deletions test/Spec.hs
Original file line number Diff line number Diff line change
@@ -1,55 +1,58 @@
{-# LANGUAGE ScopedTypeVariables #-}


import Test.Framework (defaultMain, testGroup)
import Test.Framework.Providers.API (Test)
import Test.Framework.Providers.HUnit (testCase)
import Test.Framework.Providers.QuickCheck2 (testProperty)
import Test.HUnit.Base hiding (Test)
import Test.Hspec
import Test.Hspec.QuickCheck

import Text.Show.Unicode
import Text.Show.Unicode

data T6= Å4 { すけろく :: String} deriving (Eq, Ord, Show, Read)
data T7= String :@\& String deriving (Eq, Ord, Show, Read)
data T8= String :@\& String deriving (Eq, Ord, Show, Read)


ushowTo :: Show a => a -> String -> Test
ushowTo f t = testCase ("ushow " ++ show f ++ " == " ++ t) $ t @=? ushow f

tests :: [Test]
tests =
[ testGroup "individual representations test"
[ "صباح الخير" `ushowTo` "\"صباح الخير\""
, "😆💕>λ\\=🐘" `ushowTo` "\"😆💕>λ\\\\=🐘\""
, "漢6" `ushowTo` "\"漢6\""
, "\32\&7" `ushowTo` "\" 7\""
, "\n" `ushowTo` "\"\\n行\""
, "下一站\na\ri\ta国际机场" `ushowTo` "\"下一站\\na\\ri\\ta国际机场\""
, "\SOH\SO\&H" `ushowTo` "\"\\SOH\\SO\\&H\""
]

, testGroup "read . ushow == id"
[ testProperty "read . ushow == id, for String" $
\str -> read (ushow str) == (str :: String)
, testProperty "read . ushow == id, for Char" $
\x -> read (ushow x) == (x :: Char)
, testProperty "read . ushow == id, for [(Char,())]" $
\x -> read (ushow x) == (x :: [(Char,())])
, testProperty "read . read . ushow . ushow == id, for String" $
\str -> (read $ read $ ushow $ ushow str) == (str :: String)
, testProperty "read . ushow == id, for some crazy Unicode type" $
\str -> let v = Å4 str in read (ushow v) == v
, testProperty "read . ushow == id, for some crazy Unicode type" $
\a b -> let v = a :@\& b in read (ushow v) == v
, testProperty "read . ushow == id, for some crazy Unicode type" $
\a b -> let v = a :@\& b in read (show v) == v
, testProperty "read . ushow == id, for compound type" $
\str -> read (ushow str) == (str :: Either [String] (String,String))
]
]
ushowTo :: Show a => a -> String -> Spec
ushowTo f t = it ("ushow " ++ show f ++ " == " ++ t) $ t `shouldBe` ushow f

spec :: Spec
spec =
describe "individual representations test" $ do
describe "individual representations test" $ do
"صباح الخير" `ushowTo` "\"صباح الخير\""
"😆💕>λ\\=🐘" `ushowTo` "\"😆💕>λ\\\\=🐘\""
"漢6" `ushowTo` "\"漢6\""
"\32\&7" `ushowTo` "\" 7\""
"\n" `ushowTo` "\"\\n行\""
"下一站\na\ri\ta国际机场" `ushowTo` "\"下一站\\na\\ri\\ta国际机场\""
"\SOH\SO\&H" `ushowTo` "\"\\SOH\\SO\\&H\""

describe "read . ushow == id" $ do
prop "read . ushow == id, for String" $
\str -> read (ushow str) `shouldBe` (str :: String)

prop "read . ushow == id, for Char" $
\x -> read (ushow x) `shouldBe` (x :: Char)

prop "read . ushow == id, for [(Char,())]" $
\x -> read (ushow x) `shouldBe` (x :: [(Char,())])

prop "read . read . ushow . ushow == id, for String" $
\str -> (read $ read $ ushow $ ushow str) `shouldBe` (str :: String)

prop "read . ushow == id, for some crazy Unicode type" $
\str -> let v = Å4 str in read (ushow v) `shouldBe` v

prop "read . ushow == id, for some crazy Unicode type" $
\a b -> let v = a :@\& b in read (ushow v) `shouldBe` v

prop "read . ushow == id, for some crazy Unicode type" $
\a b -> let v = a :@\& b in read (show v) `shouldBe` v

prop "read . ushow == id, for compound type" $
\str -> read (ushow str) `shouldBe` (str :: Either [String] (String,String))

main :: IO ()
main = do
print $ "hoge" :@\& "huga"
putStrLn $ ushow $ "hoge" :@\& "huga"
defaultMain tests
hspec spec
5 changes: 1 addition & 4 deletions unicode-show.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,9 @@ test-suite unicode-show-test
hs-source-dirs: test
main-is: Spec.hs
build-depends: base
, HUnit
, hspec
, QuickCheck
, unicode-show
, test-framework
, test-framework-hunit
, test-framework-quickcheck2

ghc-options: -threaded -rtsopts -with-rtsopts=-N
default-language: Haskell2010
Expand Down

0 comments on commit 9e698ae

Please sign in to comment.