File: GenericSpec.hs

package info (click to toggle)
haskell-genvalidity 1.1.1.0-1
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 128 kB
  • sloc: haskell: 942; makefile: 9
file content (105 lines) | stat: -rw-r--r-- 2,981 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE ScopedTypeVariables #-}

module Data.GenValidity.GenericSpec
  ( spec,
  )
where

import Control.Monad
import Data.GenValidity
import Data.Proxy
import Data.Typeable
import GHC.Generics (Generic, Rep)
import Test.Hspec
import Test.QuickCheck

spec :: Spec
spec = do
  describe "genValidStructurally" $ do
    genValidstructurallySpec (Proxy :: Proxy Bool)
    genValidstructurallySpec (Proxy :: Proxy Ordering)
    genValidstructurallySpec (Proxy :: Proxy (Maybe Double))
    genValidstructurallySpec (Proxy :: Proxy (Either Double Ordering))
    genValidstructurallySpec (Proxy :: Proxy MyType)
  describe "shrinkValidStructurally" $ do
    shrinkValidstructurallySpec (Proxy :: Proxy Bool)
    shrinkValidstructurallySpec (Proxy :: Proxy Ordering)
    shrinkValidstructurallySpec (Proxy :: Proxy (Maybe Double))
    shrinkValidstructurallySpec (Proxy :: Proxy (Either Double Ordering))
    shrinkValidstructurallySpec (Proxy :: Proxy MyType)

genValidstructurallySpec ::
  forall a.
  (Validity a, Show a, Typeable a, Generic a, GGenValid (Rep a)) =>
  Proxy a ->
  Spec
genValidstructurallySpec proxy =
  it (unwords ["only generates valid", "\"" ++ nameOf proxy ++ "\"s"]) $
    forAll (genValidStructurally :: Gen a) $ \a ->
      case prettyValidate a of
        Right _ -> return ()
        Left err ->
          expectationFailure $
            unlines
              [ "'validate' reported this value to be invalid: ",
                show a,
                "with explanation",
                err,
                ""
              ]

shrinkValidstructurallySpec ::
  forall a.
  ( Show a,
    Eq a,
    Typeable a,
    Generic a,
    GenValid a,
    GValidRecursivelyShrink (Rep a),
    GValidSubterms (Rep a) a
  ) =>
  Proxy a ->
  Spec
shrinkValidstructurallySpec proxy = do
  it (unwords ["only shrinks to valid", "\"" ++ nameOf proxy ++ "\"s"]) $
    forAll (genValid :: Gen a) $ \a ->
      forM_ (shrinkValidStructurally a) $ \subA ->
        case prettyValidate subA of
          Right _ -> return ()
          Left err ->
            expectationFailure $
              unlines
                [ "'validate' reported this value to be invalid: ",
                  show subA,
                  "with explanation",
                  err,
                  "but it should have been valid from shrinking"
                ]
  it
    ( unwords
        ["never shrinks to itself for valid", "\"" ++ nameOf proxy ++ "\"s"]
    )
    $ forAll (genValid :: Gen a)
    $ \a ->
      forM_ (shrinkValidStructurally a) $ \subA ->
        when (subA == a) $
          expectationFailure $
            unlines [show a, "was shrunk to itself."]

nameOf ::
  forall a.
  (Typeable a) =>
  Proxy a ->
  String
nameOf = show . typeRep

data MyType
  = MyType Double Ordering
  deriving (Show, Eq, Generic, Typeable)

instance Validity MyType

instance GenValid MyType