packages feed

text-show-1: tests/Spec/System/IOSpec.hs

{-# LANGUAGE CPP #-}

{-|
Module:      Spec.System.IOSpec
Copyright:   (C) 2014-2015 Ryan Scott
License:     BSD-style (see the file LICENSE)
Maintainer:  Ryan Scott
Stability:   Provisional
Portability: GHC

@hspec@ tests for data types in the "System.IO" module.
-}
module Spec.System.IOSpec (main, spec) where

#if MIN_VERSION_base(4,4,0)
import GHC.IO.Encoding.Failure (CodingFailureMode)
import GHC.IO.Encoding.Types (CodingProgress)
#endif

import Instances.System.IO ()

import Prelude ()
import Prelude.Compat

import Spec.Utils (ioProperty, prop_matchesShow)

import System.IO (BufferMode, IOMode, HandlePosn, Newline,
                  NewlineMode, SeekMode, Handle, mkTextEncoding)

import Test.Hspec (Spec, describe, hspec, parallel)
import Test.Hspec.QuickCheck (prop)
import Test.QuickCheck (Property, generate, oneof)

main :: IO ()
main = hspec spec

spec :: Spec
spec = parallel $ do
    describe "Handle" $
        prop "Show instance" (prop_matchesShow :: Int -> Handle -> Bool)
    describe "IOMode" $
        prop "Show instance" (prop_matchesShow :: Int -> IOMode -> Bool)
    describe "BufferMode" $
        prop "Show instance" (prop_matchesShow :: Int -> BufferMode -> Bool)
    describe "HandlePosn" $
        prop "Show instance" (prop_matchesShow :: Int -> HandlePosn -> Bool)
    describe "SeekMode" $
        prop "Show instance" (prop_matchesShow :: Int -> SeekMode -> Bool)
    describe "TextEncoding" $
        prop "Show instance" prop_showTextEncoding
#if MIN_VERSION_base(4,4,0)
    describe "CodingProgress" $
        prop "Show instance" (prop_matchesShow :: Int -> CodingProgress -> Bool)
    describe "CodingFailureMode" $
        prop "Show instance" (prop_matchesShow :: Int -> CodingFailureMode -> Bool)
#endif
    describe "Newline" $
        prop "Show instance" (prop_matchesShow :: Int -> Newline -> Bool)
    describe "NewlineMode" $
        prop "Show instance" (prop_matchesShow :: Int -> NewlineMode -> Bool)

-- | Verifies the 'Show' instance for 'TextEncoding' is accurate.
prop_showTextEncoding :: Int -> Property
prop_showTextEncoding p = ioProperty $ do
    -- Based on this description:
    -- http://hackage.haskell.org/package/base-4.7.0.2/docs/System-IO.html#v:mkTextEncoding
    utf <- generate . oneof $ map pure [ "UTF-8"
                                       , "UTF-16", "UTF-16BE", "UTF-16LE"
                                       , "UTF-32", "UTF-32BE", "UTF-32LE"
                                       ]
    tenc <- mkTextEncoding utf
    pure $ prop_matchesShow p tenc