git.iko.soy/typeable/bloodhound
on 717c442 help sign in
63 lines 2.4 kB history raw
1{-# LANGUAGE DefaultSignatures #-}
2{-# LANGUAGE OverloadedStrings #-}
4module Test.ApproxEq where
6import Database.V5.Bloodhound
8import Test.Import
10import qualified Data.List.NonEmpty as NE
12-- | Typeclass for "equal where it matters". Use this to specify
13-- less-strict equivalence for things such as lists that can wind up
14-- in an unpredictable order
15class ApproxEq a where
16 (=~) :: a -> a -> Bool
18 showApproxEq :: a -> String
19 default showApproxEq :: (Show a) => a -> String
20 showApproxEq = show
22(==~) :: (ApproxEq a) => a -> a -> Property
23a ==~ b = counterexample (showApproxEq a ++ " !=~ " ++ showApproxEq b) (a =~ b)
25instance ApproxEq NominalDiffTime where (=~) = (==)
26instance ApproxEq Bool where (=~) = (==)
27instance ApproxEq Int where (=~) = (==)
28instance (Eq a, Show a) => ApproxEq (Maybe a) where (=~) = (==)
29instance ApproxEq Char where
30 (=~) = (==)
32instance ApproxEq NodeAttrFilter where (=~) = (==)
33instance ApproxEq NodeAttrName where (=~) = (==)
34instance (Eq a, Show a) => ApproxEq (NonEmpty a) where (=~) = (==)
35instance (ApproxEq l, Show l, ApproxEq r, Show r) => ApproxEq (Either l r) where
36 Left a =~ Left b = a =~ b
37 Right a =~ Right b = a =~ b
38 _ =~ _ = False
39 showApproxEq (Left x) = "Left " <> showApproxEq x
40 showApproxEq (Right x) = "Right " <> showApproxEq x
41instance (ApproxEq a, Show a) => ApproxEq [a] where
42 as =~ bs = and (zipWith (=~) as bs)
43instance ApproxEq ReplicaCount where (=~) = (==)
44instance ApproxEq ReplicaBounds where (=~) = (==)
45instance ApproxEq Bytes where (=~) = (==)
46instance ApproxEq AllocationPolicy where (=~) = (==)
47instance ApproxEq InitialShardCount where (=~) = (==)
48instance ApproxEq FSType where (=~) = (==)
50-- | Due to the way nodeattrfilters get serialized here, they may come
51-- out in a different order, but they are morally equivalent
52instance ApproxEq UpdatableIndexSetting where
53 RoutingAllocationInclude a =~ RoutingAllocationInclude b =
54 NE.sort a =~ NE.sort b
55 RoutingAllocationExclude a =~ RoutingAllocationExclude b =
56 NE.sort a =~ NE.sort b
57 RoutingAllocationRequire a =~ RoutingAllocationRequire b =
58 NE.sort a =~ NE.sort b
59 a =~ b = a == b
60 showApproxEq (RoutingAllocationInclude xs) = show (RoutingAllocationInclude (NE.sort xs))
61 showApproxEq (RoutingAllocationExclude xs) = show (RoutingAllocationExclude (NE.sort xs))
62 showApproxEq (RoutingAllocationRequire xs) = show (RoutingAllocationRequire (NE.sort xs))
63 showApproxEq x = show x