Skip to content

Instantly share code, notes, and snippets.

@nushio3
Last active February 4, 2016 02:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nushio3/4a10f3c0092295696daf to your computer and use it in GitHub Desktop.
Save nushio3/4a10f3c0092295696daf to your computer and use it in GitHub Desktop.
~$ stack runhaskell myshow.hs
Run from outside a project, using implicit global project config
Using resolver: lts-5.1 from implicit global project's config file: /home/nushio/.stack/global-project/stack.yaml
individual representations test:
ushow "\28450\&6" == "漢6": [OK]
ushow "\na\ri\ta \22269\38555\31354\28207" == "\na\ri\ta 国際空港": [Failed]
expected: "\"\\na\\ri\\ta \22269\38555\31354\28207\""
but got: "\"\na\ri\ta \22269\38555\31354\28207\""
ushow "\SOH\SO\&H" == "\SOH\SO\&H": [Failed]
expected: "\"\\SOH\\SO\\&H\""
but got: "\"\SOH\SO\&H\""
read . ushow == id:
read . ushow == id, for String: [Failed]
*** Failed! Exception: 'Prelude.read: no parse' (after 16 tests and 4 shrinks):
"\""
(used seed 1488425704269779283)
read . read . ushow . ushow == id, for String: [Failed]
*** Failed! Exception: 'Prelude.read: no parse' (after 1 test):
""
(used seed 7384686312765896853)
read . ushow == id, for some crazy Unicode type: [Failed]
*** Failed! Exception: 'Prelude.read: no parse' (after 19 tests and 2 shrinks):
"\""
(used seed -7705745822685319130)
read . ushow == id, for some crazy Unicode type: [Failed]
*** Failed! Exception: 'Prelude.read: no parse' (after 28 tests and 6 shrinks):
""
"\\"
(used seed -2988398841461806209)
read . ushow == id, for compound type: [Failed]
*** Failed! Exception: 'Prelude.read: no parse' (after 7 tests and 6 shrinks):
Left ["\""]
(used seed -9135237574203273231)
Properties Test Cases Total
Passed 0 1 1
Failed 5 2 7
Total 5 3 8
{-# 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)
ushow :: Show a => a -> String
ushow x = go (show x) where
go :: String -> String
go [] = []
go s@(x:xs) = case x of
'\"' -> '\"' : str ++ "\"" ++ go rest
'\'' -> '\'' : char : '\'' : go rest'
_ -> x : go xs
where
(str :: String, rest):_ = reads s
(char :: Char, rest'):_ = reads s
data T試6験 = Å4 { すけろく :: String} deriving (Eq, Ord, Show, Read)
data T試7験 = String :\& String deriving (Eq, Ord, Show, Read)
-- Derived instance of Read and Show produced by GHC
-- does not satisfy Haskell 2010 spec, for the moment (ghc 7.10.3).
-- Therefore, we must regretfully skip this test.
data T試8験 = 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"
[
"漢6" `ushowTo` "\"漢6\""
, "\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 . 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))
]
]
main :: IO ()
main = defaultMain tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment