File: NewtypeSpec.hs

package info (click to toggle)
haskell-newtype-generics 0.6.2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 100 kB
  • sloc: haskell: 299; makefile: 5
file content (58 lines) | stat: -rw-r--r-- 1,674 bytes parent folder | download | duplicates (4)
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
{-# LANGUAGE NoMonomorphismRestriction #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeFamilies #-}

module Control.NewtypeSpec where

import Prelude

import Data.Monoid
import Control.Newtype.Generics
import GHC.Generics

import Test.Hspec

newtype TestNewType = TestNewType Int deriving (Eq,Show,Generic)

instance Newtype TestNewType

{-# ANN spec ("HLint: ignore Redundant do"::String) #-}
spec :: Spec
spec = describe "Newtype test" $ do
  let four = 4 :: Int
      five = 5 :: Int
      noth = Nothing  :: Maybe String
  it "pack" $ do
    (pack True :: All)              `shouldBe` All True
    (pack True :: Any)              `shouldBe` Any True
    (pack (Just five) :: First Int) `shouldBe` First (Just 5)

  it "unpack" $ do
    unpack (Any False)          `shouldBe` False
    unpack (First (Just five))  `shouldBe` Just five
    unpack (Last noth)          `shouldBe` Nothing
    unpack (TestNewType five)   `shouldBe` five

  it "op" $ do
    op All (All True)  `shouldBe` True
    op Any (Any False) `shouldBe` False
    op Sum (Sum five)  `shouldBe` five

  it "under" $ do
    let sumLess (Sum x) = Sum (x - 1)
        firstN  (_)     = First Nothing
    under Sum   sumLess five        `shouldBe` four
    under First firstN  (Just five) `shouldBe` (Nothing :: Maybe Int)

  it "over" $ do
    over Sum     (+1) (Sum     four) `shouldBe` Sum five
    over Product (+1) (Product four) `shouldBe` Product five

  it "under2" $ do
    under2 Sum (<>) four five `shouldBe` 9

  it "over2" $ do
    over2 TestNewType (+) (TestNewType four) (TestNewType five) `shouldBe` TestNewType 9