diff options
author | David Terei <davidterei@gmail.com> | 2011-07-20 11:09:03 -0700 |
---|---|---|
committer | David Terei <davidterei@gmail.com> | 2011-07-20 11:26:35 -0700 |
commit | 16514f272fb42af6e9c7674a9bd6c9dce369231f (patch) | |
tree | e4f332b45fe65e2a7a2451be5674f887b42bf199 /testsuite/tests/lib/IO/3307.hs | |
parent | ebd422aed41048476aa61dd4c520d43becd78682 (diff) | |
download | haskell-16514f272fb42af6e9c7674a9bd6c9dce369231f.tar.gz |
Move tests from tests/ghc-regress/* to just tests/*
Diffstat (limited to 'testsuite/tests/lib/IO/3307.hs')
-rw-r--r-- | testsuite/tests/lib/IO/3307.hs | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/testsuite/tests/lib/IO/3307.hs b/testsuite/tests/lib/IO/3307.hs new file mode 100644 index 0000000000..fb1a360ea2 --- /dev/null +++ b/testsuite/tests/lib/IO/3307.hs @@ -0,0 +1,52 @@ +import Control.Exception + +import System.Directory +import System.Environment +import System.IO + +import Data.Char +import Data.List + +import GHC.IO.Encoding + +main = do + hSetBuffering stdout NoBuffering + + -- 1) A file name arriving via an argument + putStrLn "Test 1" + [file] <- getArgs + print $ map ord file + readFile file >>= putStr + + -- 2) A file name arriving via getDirectoryContents + putStrLn "Test 2" + [file] <- fmap (filter ("chinese-file-" `isPrefixOf`)) $ getDirectoryContents "." + print $ map ord file + readFile file >>= putStr + + -- 3) A file name occurring literally in the program + -- The file is created with a UTF-8 file name as well, so this will only work in Windows or a + -- UTF-8 locale, or this string will be encoded in some non-UTF-8 way and won't match. + putStrLn "Test 3" + let file = "chinese-file-小说" + print $ map ord file + readFile file >>= putStr + + -- 4) A file name arriving via another file. + -- Again, the file is created with UTF-8 contents, so we read it in that encoding. + -- Once again, on non-Windows this may fail in a non-UTF-8 locale because we could encode the valid + -- filename string into a useless non-UTF-8 byte sequence. + putStrLn "Test 4" + str <- readFileAs utf8 "chinese-name" + let file = dropTrailingSpace str + print $ map ord file + readFile file >>= putStr + +readFileAs :: TextEncoding -> FilePath -> IO String +readFileAs enc fp = do + h <- openFile fp ReadMode + hSetEncoding h enc + hGetContents h + +dropTrailingSpace :: String -> String +dropTrailingSpace = reverse . dropWhile (not . isAlphaNum) . reverse |