Skip to content

Commit

Permalink
Test/QuickCheck/All.hs: fix 'quickCheckAll' to read haskell sources i…
Browse files Browse the repository at this point in the history
…n UTF-8

Today I've got an Agda build failure on a box running
LANG=C locale (UTF-8 incapable).

    examples/Heap.hs:151:10:
        Exception when trying to run compile-time code:
          examples/Heap.hs: hGetContents: invalid argument (invalid byte sequence)
          Code: quickCheckAll
        In the expression: $quickCheckAll
        In an equation for `main': main = $quickCheckAll

It's a common error of a 'readFile' assuming locale input.
Fixed to read in UTF-8 in recent GHCs.

Signed-off-by: Sergei Trofimovich <slyfox@gentoo.org>
  • Loading branch information
Sergei Trofimovich committed Apr 27, 2014
1 parent 9c6635e commit ccf15ee
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
17 changes: 16 additions & 1 deletion Test/QuickCheck/All.hs
Expand Up @@ -22,6 +22,8 @@ import Data.Char
import Data.List
import Control.Monad

import qualified System.IO as S

-- | Test a polymorphic property, defaulting all type variables to 'Integer'.
--
-- Invoke as @$('polyQuickCheck' 'prop)@, where @prop@ is a property.
Expand Down Expand Up @@ -103,7 +105,7 @@ forAllProperties :: Q Exp -- :: (Property -> IO Result) -> IO Bool
forAllProperties = do
Loc { loc_filename = filename } <- location
when (filename == "<interactive>") $ error "don't run this interactively"
ls <- runIO (fmap lines (readFile filename))
ls <- runIO (fmap lines (readUTF8File filename))
let prefixes = map (takeWhile (\c -> isAlphaNum c || c == '_') . dropWhile (\c -> isSpace c || c == '>')) ls
idents = nubBy (\x y -> snd x == snd y) (filter (("prop_" `isPrefixOf`) . snd) (zip [1..] prefixes))
warning x = reportWarning ("Name " ++ x ++ " found in source file but was not in scope")
Expand All @@ -115,6 +117,19 @@ forAllProperties = do
else return []
[| runQuickCheckAll $(fmap (ListE . concat) (mapM quickCheckOne idents)) |]

readUTF8File name = S.openFile name S.ReadMode >>=
set_utf8_io_enc >>=
S.hGetContents

-- Deal with UTF-8 input and output.
set_utf8_io_enc :: S.Handle -> IO S.Handle
#if __GLASGOW_HASKELL__ > 611
-- possibly if MIN_VERSION_base(4,2,0)
set_utf8_io_enc h = do S.hSetEncoding h S.utf8; return h
#else
set_utf8_io_enc h = return h
#endif

-- | Test all properties in the current module.
-- The name of the property must begin with @prop_@.
-- Polymorphic properties will be defaulted to 'Integer'.
Expand Down
2 changes: 2 additions & 0 deletions examples/Heap.hs
Expand Up @@ -146,6 +146,8 @@ instance (Ord a, Arbitrary a) => Arbitrary (Heap a) where
-- main

return []
-- quickCheckAll reads this file and treats it as UTF-8
-- Here is a bait to test: Привет!
main = $(quickCheckAll)

--------------------------------------------------------------------------
Expand Down

0 comments on commit ccf15ee

Please sign in to comment.