Skip to content

Commit 731418d

Browse files
authored
Combine all tests into a unified test suite (#435)
* Convert LazyHClose to a proper test suite * Combine all tests into a unified test suite
1 parent fb738ea commit 731418d

File tree

7 files changed

+99
-113
lines changed

7 files changed

+99
-113
lines changed

bytestring.cabal

Lines changed: 22 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -134,66 +134,35 @@ library
134134
includes: fpstring.h
135135
install-includes: fpstring.h
136136

137-
test-suite prop-compiled
137+
test-suite bytestring-tests
138138
type: exitcode-stdio-1.0
139-
main-is: Properties.hs
140-
other-modules: QuickCheckUtils
139+
main-is: Main.hs
140+
other-modules: Builder
141+
Data.ByteString.Builder.Prim.TestUtils
142+
Data.ByteString.Builder.Prim.Tests
143+
Data.ByteString.Builder.Tests
144+
IsValidUtf8
145+
LazyHClose
146+
Lift
147+
Properties
141148
Properties.ByteString
142149
Properties.ByteStringChar8
143150
Properties.ByteStringLazy
144151
Properties.ByteStringLazyChar8
145-
hs-source-dirs: tests
146-
include-dirs: tests/Properties
147-
build-depends: base, bytestring, ghc-prim, deepseq,
148-
tasty, tasty-quickcheck
149-
ghc-options: -fwarn-unused-binds
150-
-threaded -rtsopts
151-
default-language: Haskell2010
152-
153-
test-suite lazy-hclose
154-
type: exitcode-stdio-1.0
155-
main-is: LazyHClose.hs
156-
hs-source-dirs: tests
157-
build-depends: base, bytestring, ghc-prim, deepseq,
158-
tasty, tasty-quickcheck
159-
ghc-options: -fwarn-unused-binds
160-
-threaded -rtsopts
161-
default-language: Haskell2010
162-
163-
test-suite test-builder
164-
type: exitcode-stdio-1.0
165-
hs-source-dirs: tests/builder
166-
main-is: TestSuite.hs
167-
other-modules: Data.ByteString.Builder.Prim.TestUtils
168-
Data.ByteString.Builder.Prim.Tests
169-
Data.ByteString.Builder.Tests
170-
build-depends: base, bytestring, ghc-prim,
152+
QuickCheckUtils
153+
hs-source-dirs: tests,
154+
tests/builder
155+
build-depends: base,
156+
bytestring,
171157
deepseq,
172-
transformers >= 0.3,
158+
ghc-prim,
159+
QuickCheck,
173160
tasty,
174-
tasty-quickcheck
175-
ghc-options: -Wall -fwarn-tabs -threaded -rtsopts
176-
default-language: Haskell2010
177-
178-
test-suite bytestring-th
179-
type: exitcode-stdio-1.0
180-
hs-source-dirs: tests
181-
main-is: bytestring-th.hs
182-
other-extensions: TemplateHaskell
183-
build-depends: base, bytestring, template-haskell, tasty, tasty-quickcheck
184-
ghc-options: -Wall -fwarn-tabs -threaded -rtsopts
185-
default-language: Haskell2010
186-
187-
test-suite is-valid-utf8
188-
type: exitcode-stdio-1.0
189-
hs-source-dirs: tests/is-valid-utf8
190-
main-is: Main.hs
191-
build-depends: base,
192-
bytestring,
193-
tasty,
194-
tasty-quickcheck,
195-
QuickCheck
196-
ghc-options: -Wall -fwarn-tabs -threaded -rtsopts
161+
tasty-quickcheck,
162+
template-haskell,
163+
transformers >= 0.3
164+
ghc-options: -fwarn-unused-binds
165+
-threaded -rtsopts
197166
default-language: Haskell2010
198167

199168
benchmark bytestring-bench

tests/builder/TestSuite.hs renamed to tests/Builder.hs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
1-
module Main where
1+
module Builder (testSuite) where
22

33
import qualified Data.ByteString.Builder.Tests
44
import qualified Data.ByteString.Builder.Prim.Tests
5-
import Test.Tasty (defaultMain, TestTree, testGroup)
5+
import Test.Tasty (TestTree, testGroup)
66

7-
main :: IO ()
8-
main = defaultMain $ testGroup "All" tests
9-
10-
tests :: [TestTree]
11-
tests =
7+
testSuite :: TestTree
8+
testSuite = testGroup "Builder"
129
[ testGroup "Data.ByteString.Builder"
1310
Data.ByteString.Builder.Tests.tests
1411

tests/is-valid-utf8/Main.hs renamed to tests/IsValidUtf8.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{-# LANGUAGE LambdaCase #-}
22

3-
module Main (main) where
3+
module IsValidUtf8 (testSuite) where
44

55
import Data.Bits (shiftR, (.&.), shiftL)
66
import Data.ByteString (ByteString)
@@ -12,11 +12,11 @@ import Test.QuickCheck (Property, forAll, (===))
1212
import Test.QuickCheck.Arbitrary (Arbitrary (arbitrary, shrink))
1313
import Test.QuickCheck.Gen (oneof, Gen, choose, vectorOf, listOf1, sized, resize,
1414
elements)
15-
import Test.Tasty (defaultMain, testGroup, adjustOption, TestTree)
15+
import Test.Tasty (testGroup, adjustOption, TestTree)
1616
import Test.Tasty.QuickCheck (testProperty, QuickCheckTests)
1717

18-
main :: IO ()
19-
main = defaultMain . testGroup "UTF-8 validation" $ [
18+
testSuite :: TestTree
19+
testSuite = testGroup "UTF-8 validation" $ [
2020
adjustOption (max testCount) . testProperty "Valid UTF-8" $ goValid,
2121
adjustOption (max testCount) . testProperty "Invalid UTF-8" $ goInvalid,
2222
testGroup "Regressions" checkRegressions

tests/LazyHClose.hs

Lines changed: 43 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,64 @@
1-
module Main (main) where
1+
module LazyHClose (testSuite) where
22

33
import Control.Monad (void, forM_)
44
import Data.ByteString.Internal (toForeignPtr)
55
import Foreign.C.String (withCString)
66
import Foreign.ForeignPtr (finalizeForeignPtr)
77
import System.IO (openFile, openTempFile, hClose, hPutStrLn, IOMode(..))
88
import System.Posix.Internals (c_unlink)
9+
import Test.Tasty (TestTree, testGroup, withResource)
10+
import Test.Tasty.QuickCheck (testProperty, ioProperty)
911

1012
import qualified Data.ByteString as S
1113
import qualified Data.ByteString.Char8 as S8
1214
import qualified Data.ByteString.Lazy as L
1315
import qualified Data.ByteString.Lazy.Char8 as L8
1416

15-
main :: IO ()
16-
main = do
17-
let n = 1000
18-
(fn, h) <- openTempFile "." "lazy-hclose-test.tmp"
19-
hPutStrLn h "x"
20-
hClose h
17+
n :: Int
18+
n = 1000
2119

22-
------------------------------------------------------------------------
23-
-- readFile tests
20+
testSuite :: TestTree
21+
testSuite = withResource
22+
(do (fn, h) <- openTempFile "." "lazy-hclose-test.tmp"; hPutStrLn h "x"; hClose h; pure fn)
23+
removeFile $ \fn' ->
24+
testGroup "LazyHClose"
25+
[ testProperty "Testing resource leaks for Strict.readFile" $ ioProperty $
26+
forM_ [1..n] $ const $ do
27+
fn <- fn'
28+
r <- S.readFile fn
29+
appendFile fn "" -- will fail, if fn has not been closed yet
2430

25-
putStrLn "Testing resource leaks for Strict.readFile"
26-
forM_ [1..n] $ const $ do
27-
r <- S.readFile fn
28-
appendFile fn "" -- will fail, if fn has not been closed yet
31+
, testProperty "Testing resource leaks for Lazy.readFile" $ ioProperty $
32+
forM_ [1..n] $ const $ do
33+
fn <- fn'
34+
r <- L.readFile fn
35+
L.length r `seq` return ()
36+
appendFile fn "" -- will fail, if fn has not been closed yet
2937

30-
putStrLn "Testing resource leaks for Lazy.readFile"
31-
forM_ [1..n] $ const $ do
32-
r <- L.readFile fn
33-
L.length r `seq` return ()
34-
appendFile fn "" -- will fail, if fn has not been closed yet
38+
, testProperty "Testing resource leaks when converting lazy to strict" $ ioProperty $
39+
forM_ [1..n] $ const $ do
40+
fn <- fn'
41+
let release c = finalizeForeignPtr fp where (fp,_,_) = toForeignPtr c
42+
r <- L.readFile fn
43+
mapM_ release (L.toChunks r)
44+
appendFile fn "" -- will fail, if fn has not been closed yet
3545

36-
-- manage the resources explicitly.
37-
putStrLn "Testing resource leaks when converting lazy to strict"
38-
forM_ [1..n] $ const $ do
39-
let release c = finalizeForeignPtr fp where (fp,_,_) = toForeignPtr c
40-
r <- L.readFile fn
41-
mapM_ release (L.toChunks r)
42-
appendFile fn "" -- will fail, if fn has not been closed yet
46+
, testProperty "Testing strict hGetContents" $ ioProperty $
47+
forM_ [1..n] $ const $ do
48+
fn <- fn'
49+
h <- openFile fn ReadMode
50+
r <- S.hGetContents h
51+
S.last r `seq` return ()
52+
appendFile fn "" -- will fail, if fn has not been closed yet
4353

44-
------------------------------------------------------------------------
45-
-- hGetContents tests
46-
47-
putStrLn "Testing strict hGetContents"
48-
forM_ [1..n] $ const $ do
49-
h <- openFile fn ReadMode
50-
r <- S.hGetContents h
51-
S.last r `seq` return ()
52-
appendFile fn "" -- will fail, if fn has not been closed yet
53-
54-
putStrLn "Testing lazy hGetContents"
55-
forM_ [1..n] $ const $ do
56-
h <- openFile fn ReadMode
57-
r <- L.hGetContents h
58-
L.last r `seq` return ()
59-
appendFile fn "" -- will fail, if fn has not been closed yet
60-
61-
removeFile fn
54+
, testProperty "Testing lazy hGetContents" $ ioProperty $
55+
forM_ [1..n] $ const $ do
56+
fn <- fn'
57+
h <- openFile fn ReadMode
58+
r <- L.hGetContents h
59+
L.last r `seq` return ()
60+
appendFile fn "" -- will fail, if fn has not been closed yet
61+
]
6262

6363
removeFile :: String -> IO ()
6464
removeFile fn = void $ withCString fn c_unlink

tests/bytestring-th.hs renamed to tests/Lift.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
{-# LANGUAGE CPP #-}
22
{-# LANGUAGE TemplateHaskell #-}
33
{-# LANGUAGE OverloadedStrings #-}
4-
module Main (main) where
4+
module Lift (testSuite) where
55

6-
import Test.Tasty (defaultMain, testGroup)
6+
import Test.Tasty (TestTree, testGroup)
77
import Test.Tasty.QuickCheck (testProperty, (===))
88
import qualified Data.ByteString as BS
99
import qualified Data.ByteString.Lazy as LBS
1010
import qualified Data.ByteString.Short as SBS
1111
import qualified Language.Haskell.TH.Syntax as TH
1212

13-
main :: IO ()
14-
main = defaultMain $ testGroup "bytestring-th"
13+
testSuite :: TestTree
14+
testSuite = testGroup "Lift"
1515
[ testGroup "strict"
1616
[ testProperty "normal" $
1717
let bs = "foobar" :: BS.ByteString in

tests/Main.hs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
module Main (main) where
2+
3+
import Test.Tasty
4+
5+
import qualified Builder
6+
import qualified IsValidUtf8
7+
import qualified LazyHClose
8+
import qualified Lift
9+
import qualified Properties
10+
11+
main :: IO ()
12+
main = defaultMain $ testGroup "All"
13+
[ Builder.testSuite
14+
, IsValidUtf8.testSuite
15+
, LazyHClose.testSuite
16+
, Lift.testSuite
17+
, Properties.testSuite
18+
]

tests/Properties.hs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
{-# LANGUAGE MagicHash #-}
33
{-# LANGUAGE UnboxedTuples #-}
44

5+
module Properties (testSuite) where
6+
57
import Foreign.C.String (withCString)
68
import Foreign.Storable
79
import Foreign.ForeignPtr
@@ -472,8 +474,8 @@ explosiveTail = (`L.append` error "Tail of this byte string is undefined!")
472474
------------------------------------------------------------------------
473475
-- The entry point
474476

475-
main :: IO ()
476-
main = defaultMain $ testGroup "All"
477+
testSuite :: TestTree
478+
testSuite = testGroup "Properties"
477479
[ testGroup "StrictWord8" PropBS.tests
478480
, testGroup "StrictChar8" PropBS8.tests
479481
, testGroup "LazyWord8" PropBL.tests

0 commit comments

Comments
 (0)