File: IO.hs

package info (click to toggle)
haskell-serialise 0.2.6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 564 kB
  • sloc: haskell: 6,809; makefile: 6
file content (40 lines) | stat: -rw-r--r-- 1,197 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
{-# LANGUAGE CPP #-}
module Tests.IO
  ( testTree -- :: TestTree
  ) where
import           System.FilePath
import           System.Directory  (removeFile)
import           Control.Exception (bracket)

import           Test.Tasty
import           Test.Tasty.HUnit

import           Codec.Serialise

--------------------------------------------------------------------------------
-- Tests and properties

test_encodeAndDecodeFile :: Assertion
test_encodeAndDecodeFile =
  let path = ("tests" </> "io_test1.cbor")
  in withDeleteFile path $ do
    let val = Just True
    writeFileSerialise path val
    val' <- readFileDeserialise path :: IO (Maybe Bool)
    val @=? val'

--------------------------------------------------------------------------------
-- TestTree API

testTree :: TestTree
testTree = testGroup "IO tests"
  [ testCase "file encode/decode roundtrip" test_encodeAndDecodeFile
  ]

--------------------------------------------------------------------------------
-- Utilities

-- | Run an action, and be sure to delete the specified @'FilePath'@ when
-- finished.
withDeleteFile :: FilePath -> IO a -> IO a
withDeleteFile f k = bracket (return ()) (const $ removeFile f) (const k)