File: OrphansSpec.hs

package info (click to toggle)
haskell-base-orphans 0.9.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 188 kB
  • sloc: haskell: 1,688; makefile: 2
file content (43 lines) | stat: -rw-r--r-- 1,148 bytes parent folder | download | duplicates (3)
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
41
42
43
{-# LANGUAGE CPP #-}
module Control.Applicative.OrphansSpec (main, spec) where

import Test.Hspec
import Control.Applicative
import Data.Orphans ()
import Data.Monoid
import Prelude

-- simplest one to use
newtype Identity a = Identity { runIdentity :: a }

instance Functor Identity where
  fmap f = Identity . f . runIdentity

instance Applicative Identity where
  pure     = Identity
  Identity f <*> x = f <$> x

instance Monad Identity where
#if !(MIN_VERSION_base(4,11,0))
  return = Identity
#endif
  m >>= k  = k (runIdentity m)

main :: IO ()
main = hspec spec

spec :: Spec
spec = do
  describe "Monoid (Const a b)" $ do
    it "mempty returns an empty const" $
      getConst (mempty :: (Const String Int)) `shouldBe` ""
    it "mappends const part" $
      getConst ((Const "aaa" :: Const String Int) `mappend` (Const "bbb" :: Const String Int))
        `shouldBe` "aaabbb"

  describe "Monad (WrappedMonad m)" $
    it "allows to use a Monad interface in a WrappedMonad" $
      (runIdentity . unwrapMonad
        $  (WrapMonad (return 1 :: Identity Int))
        >> (WrapMonad (return 2 :: Identity Int)))
        `shouldBe` (2::Int)