Main.hs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. {-# LANGUAGE ScopedTypeVariables #-}
  2. module Main where
  3. import Data.Finitary.Optics (reindexed, tighter)
  4. import Data.Int (Int16, Int8)
  5. import Data.Word (Word8)
  6. import Hedgehog ((===), discard, forAll)
  7. import Hedgehog.Gen (bool, choice, int8, word8)
  8. import Hedgehog.Range (constantBounded)
  9. import Optics.AffineFold (preview)
  10. import Optics.Getter (view)
  11. import Optics.Iso (Iso')
  12. import Optics.Prism (Prism')
  13. import Optics.Review (review)
  14. import Test.Hspec (describe, hspec, it)
  15. import Test.Hspec.Hedgehog (hedgehog)
  16. main :: IO ()
  17. main = hspec $ do
  18. describe "tighter" $ do
  19. it "should follow the review-preview law" . hedgehog $ do
  20. x <- forAll . word8 $ constantBounded
  21. let t :: Prism' Int16 Word8 = tighter
  22. (preview t . review t $ x) === Just x
  23. it "should follow the preview-review law" . hedgehog $ do
  24. x <- forAll . choice $ [Left <$> bool, Right <$> int8 constantBounded]
  25. let t :: Prism' (Either Bool Int8) Word8 = tighter
  26. case preview t x of
  27. Nothing -> discard
  28. Just y -> review t y === x
  29. it "should preserve ordering via review" . hedgehog $ do
  30. x <- forAll . word8 $ constantBounded
  31. y <- forAll . word8 $ constantBounded
  32. let t :: Prism' Int16 Word8 = tighter
  33. compare x y === compare (review t x) (review t y)
  34. it "should preserve ordering via preview" . hedgehog $ do
  35. x <- forAll . choice $ [Left <$> bool, Right <$> int8 constantBounded]
  36. y <- forAll . choice $ [Left <$> bool, Right <$> int8 constantBounded]
  37. let t :: Prism' (Either Bool Int8) Word8 = tighter
  38. case (preview t x, preview t y) of
  39. (Just x', Just y') -> compare x y === compare x' y'
  40. _ -> discard
  41. describe "reindexed" $ do
  42. it "should follow the iso laws" . hedgehog $ do
  43. x <- forAll . word8 $ constantBounded
  44. let i :: Iso' Word8 Int8 = reindexed
  45. (review i . view i $ x) === x
  46. it "should preserve ordering" . hedgehog $ do
  47. x <- forAll . word8 $ constantBounded
  48. y <- forAll . word8 $ constantBounded
  49. let i :: Iso' Word8 Int8 = reindexed
  50. compare x y === compare (view i x) (view i y)