Skip to content

Commit

Permalink
Switch to Hspec for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
mrkkrp committed Sep 10, 2016
1 parent 63a4489 commit 6423c52
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 98 deletions.
12 changes: 5 additions & 7 deletions htaglib.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -71,23 +71,21 @@ library
default-language: Haskell2010

test-suite tests
main-is: Main.hs
main-is: Spec.hs
hs-source-dirs: tests
type: exitcode-stdio-1.0
if flag(dev)
ghc-options: -O0 -Wall -Werror
else
ghc-options: -O2 -Wall
other-modules: Getter
, Setter
, Util
other-modules: Sound.HTagLib.GetterSpec
, Sound.HTagLib.SetterSpec
, Sound.HTagLib.Test.Util
build-depends: base >= 4.6 && < 5.0
, HUnit >= 1.2 && < 1.4
, directory >= 1.2 && < 1.3
, filepath >= 1.4 && < 2.0
, hspec >= 2.0 && < 3.0
, htaglib >= 1.0
, test-framework >= 0.6 && < 1.0
, test-framework-hunit >= 0.2 && < 0.4
default-extensions:
CPP
default-language: Haskell2010
Expand Down
43 changes: 0 additions & 43 deletions tests/Main.hs

This file was deleted.

25 changes: 12 additions & 13 deletions tests/Getter.hs → tests/Sound/HTagLib/GetterSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -30,31 +30,30 @@
-- ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
-- POSSIBILITY OF SUCH DAMAGE.

module Getter (tests) where

import Test.Framework
import Test.HUnit hiding (Test, path)
module Sound.HTagLib.GetterSpec (spec) where

import Sound.HTagLib
import Util
import Sound.HTagLib.Test.Util
import Test.Hspec

#if !MIN_VERSION_base(4,8,0)
import Control.Applicative ((<$>))
#endif

tests :: Test
tests = testGroup "Getters" $
fmap (caseWithFile (const simpleGetter)) fileList ++
fmap (caseWithFile specializedGetter) fileList
spec :: Spec
spec =
describe "getters" $ do
mapM_ (withFile $ const simpleGetter) fileList
mapM_ (withFile specializedGetter) fileList

simpleGetter :: AudioTags -> Assertion
simpleGetter :: AudioTags -> Expectation
simpleGetter tags = do
let path = atFileName tags
extracted <- getTags path (id <$> sampleGetter path)
extracted `cfbr` tags
extracted `shouldMatchTags` tags

specializedGetter :: FileType -> AudioTags -> Assertion
specializedGetter :: FileType -> AudioTags -> Expectation
specializedGetter t tags = do
let path = atFileName tags
extracted <- getTags' path t (id <$> sampleGetter path)
extracted `cfbr` tags
extracted `shouldMatchTags` tags
30 changes: 14 additions & 16 deletions tests/Setter.hs → tests/Sound/HTagLib/SetterSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,23 @@

{-# LANGUAGE OverloadedStrings #-}

module Setter (tests) where
module Sound.HTagLib.SetterSpec (spec) where

import Sound.HTagLib
import Sound.HTagLib.Test.Util
import System.Directory (getTemporaryDirectory, copyFile)
import System.FilePath ((</>), takeFileName)

import Test.Framework
import Test.HUnit hiding (Test, path)

import Sound.HTagLib
import Util
import Test.Hspec

#if !MIN_VERSION_base(4,8,0)
import Control.Applicative ((<$>))
#endif

tests :: Test
tests = testGroup "Setters" $
fmap (caseWithFile (const simpleSetter)) fileList ++
fmap (caseWithFile specializedGetter) fileList
spec :: Spec
spec =
describe "setters" $ do
mapM_ (withFile $ const simpleSetter) fileList
mapM_ (withFile specializedSetter) fileList

dupeFile :: FilePath -> IO FilePath
dupeFile path = do
Expand All @@ -68,18 +66,18 @@ updateSampleTags tags = tags
, atYear = mkYear 2056
, atTrackNumber = mkTrackNumber 8 }

simpleSetter :: AudioTags -> Assertion
simpleSetter :: AudioTags -> Expectation
simpleSetter tags = do
let path = atFileName tags
dupe <- dupeFile path
setTags dupe Nothing sampleSetter
extracted <- getTags dupe (sampleGetter dupe)
extracted `cfbr` updateSampleTags (tags { atFileName = dupe })
extracted `shouldMatchTags` updateSampleTags (tags { atFileName = dupe })

specializedGetter :: FileType -> AudioTags -> Assertion
specializedGetter t tags = do
specializedSetter :: FileType -> AudioTags -> Expectation
specializedSetter t tags = do
let path = atFileName tags
dupe <- dupeFile path
setTags' dupe Nothing t sampleSetter
extracted <- getTags dupe (sampleGetter dupe)
extracted `cfbr` updateSampleTags (tags { atFileName = dupe })
extracted `shouldMatchTags` updateSampleTags (tags { atFileName = dupe })
37 changes: 18 additions & 19 deletions tests/Util.hs → tests/Sound/HTagLib/Test/Util.hs
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,19 @@

{-# LANGUAGE OverloadedStrings #-}

module Util
module Sound.HTagLib.Test.Util
( AudioTags (..)
, sampleGetter
, sampleSetter
, fileList
, caseWithFile
, cfbr )
, withFile
, shouldMatchTags )
where

import Data.Maybe (fromJust)
import Data.Monoid
import Sound.HTagLib

import Test.Framework
import Test.Framework.Providers.HUnit (testCase)
import Test.HUnit hiding (Test, path)
import Test.Hspec

#if !MIN_VERSION_base(4,8,0)
import Control.Applicative ((<$>), (<*>), pure)
Expand Down Expand Up @@ -118,24 +115,26 @@ fileList =
{ atBitRate = fromJust $ mkBitRate 136
, atFileName = "audio-samples/sample.mp3" }) ]

caseWithFile
:: (FileType -> AudioTags -> Assertion)
-- | Call given function with provided data.

withFile
:: (FileType -> AudioTags -> Expectation)
-> (FileType, AudioTags)
-> Test
caseWithFile f (t, tags) = testCase name (f t tags)
-> Spec
withFile f (t, tags) = it name (f t tags)
where name = "using file: " ++ show (atFileName tags) ++ " (" ++ show t ++ ")"

-- | Create 'Assertion' that two collections of tags match. However, if bit
-- rate of the first is zero (which is the case with older versions of
-- | Create an expectation that two collections of tags match. However, if
-- bit rate of the first is zero (which is the case with older versions of
-- TagLib when it's used with such short files as our samples), allow bit
-- rate values differ.

cfbr
shouldMatchTags
:: AudioTags -- ^ Tags to test
-> AudioTags -- ^ Correct tags to test against
-> Assertion
cfbr n tags =
-> Expectation
shouldMatchTags given expected =
let zeroBitRate = fromJust (mkBitRate 0)
in if atBitRate n == zeroBitRate
then n @?= tags { atBitRate = zeroBitRate }
else n @?= tags
in if atBitRate given == zeroBitRate
then given `shouldBe` expected { atBitRate = zeroBitRate }
else given `shouldBe` expected
1 change: 1 addition & 0 deletions tests/Spec.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{-# OPTIONS_GHC -F -pgmF hspec-discover #-}

0 comments on commit 6423c52

Please sign in to comment.