buuka/test/Test/Data/Buuka.hs

51 lines
1.5 KiB
Haskell
Raw Permalink Normal View History

2021-01-03 08:33:44 +02:00
{-# LANGUAGE OverloadedStrings #-}
2020-12-31 08:51:37 +02:00
module Test.Data.Buuka where
import Hedgehog
import qualified Hedgehog.Gen as Gen
import qualified Hedgehog.Range as Range
import Test.Tasty
import Test.Tasty.Hedgehog
import qualified Data.Aeson as A
import qualified Data.Foldable as F
2021-01-03 08:33:44 +02:00
import qualified Data.Text as T
2020-12-31 08:51:37 +02:00
import Data.Buuka
genUrl :: Gen URL
2021-01-03 08:33:44 +02:00
genUrl = URL . T.concat <$> sequence go
2020-12-31 08:51:37 +02:00
where
go = [ Gen.element protocols, Gen.element domains, Gen.element tlds, pure "/", genPath ]
protocols = ["http://", "https://"]
domains = ["example", "foo", "bar"]
tlds = ["com", "fi", "org", "net", "info"]
2021-01-03 08:33:44 +02:00
genPath = T.intercalate "/" <$> Gen.list (Range.linear 0 10) (Gen.element paths)
2020-12-31 08:51:37 +02:00
paths = ["foo", "bar", "asd", "xyzzy"]
genBuukaEntry :: Gen BuukaEntry
genBuukaEntry = BuukaEntry <$> genUrl <*> genTitle
where
2021-01-03 08:33:44 +02:00
genTitle = Gen.maybe (Gen.text (Range.linear 0 10) Gen.unicode)
2020-12-31 08:51:37 +02:00
genBuuka :: Gen Buuka
genBuuka = F.foldl' (flip insert) mempty <$> Gen.list (Range.linear 0 10) genBuukaEntry
prop_buukaentry_encode_decode :: Property
prop_buukaentry_encode_decode = property $ do
x <- forAll genBuukaEntry
tripping x A.encode A.eitherDecode
prop_buuka_encode_decode :: Property
prop_buuka_encode_decode = property $ do
x <- forAll genBuuka
tripping x A.encode A.eitherDecode
tests :: TestTree
tests = testGroup "Data.Buuka"
[ testProperty "Entries can be encoded and decoded" prop_buukaentry_encode_decode
, testProperty "Entire set can be encoded and decoded" prop_buuka_encode_decode
]