packages feed

css-parser-0.0.2 / test / CssParser / Test / Arbitrary.hs

{-# OPTIONS_GHC -fconstraint-solver-iterations=24 #-}{-# OPTIONS_GHC -Wno-orphans #-}{-# LANGUAGE AllowAmbiguousTypes #-}module CssParser.Test.Arbitrary  ( module X  , pack  , module CssParser.Test.Arbitrary  )whereimport Data.List qualified as Limport Data.HashSet ( fromList, member, HashSet )import Data.Text (pack, tails, inits)import Data.Text qualified as Timport CssParser.At.MediaQuery ( MediaType )import CssParser.Prelude as Ximport CssParser.Show ( CssShow(toCssText), CslNe(..), Embraced(..), SslNe(..) )import Test.QuickCheck as Ximport Test.QuickCheck.Gen as Ximport Test.QuickCheck.Instances as X ()import Test.QuickCheck.Arbitrary.Generic as XenumDomain :: forall a. (Enum a, Bounded a) => [a]enumDomain = enumFromTo (minBound @a) (maxBound @a)arbitraryLetter :: Gen ChararbitraryLetter = elements [ 'a' .. 'z' ]arbitraryHex :: Gen ChararbitraryHex = elements $ ['0' .. '9'] <> [ 'a' .. 'f' ] <> [ 'A' .. 'F' ]arbitraryWord :: Gen TextarbitraryWord = pack <$> listOf1 arbitraryLetterkeywords :: HashSet Textkeywords = fromList $! initL <> fmap toTxt (enumDomain @MediaType)  where    toTxt = toStrict . toCssText    initL =      T.words "not of or and only src false true from to url x y d r"      <>      T.words "cx cy rx ry is dir odd even open min max clamp calc"      <>      T.words "sin cos var past part"arbitraryIdent :: Gen TextarbitraryIdent = do  i <- arbitraryText fl nl  if i == "$"    then pure "$a"    else     if i `member` keywords       then pure $ i <> "_"       else pure i  where    fl = "$_" <> ['a' .. 'z']    nl = drop 1 fl <> ['-', '0' .. '9']arbitraryText :: String -> String -> Gen TextarbitraryText fl nl = do  fc <- elements fl  pack . (fc :) <$> listOf (elements nl)shrinkText :: Text -> [Text]shrinkText = liftA2 (zipWith (<>)) inits (tails . T.drop 1)shrinkIdent :: Text -> [Text]shrinkIdent t    | T.length t < 2 = []    | otherwise = L.filter isGood $ shrinkText t  where    isGood s = s `isMissing` keywords &&      (case T.uncons s of         Nothing -> False         Just (l, _) -> isLetter l || l == '_')    isMissing a b = not (a `member` b)instance Arbitrary a => Arbitrary (Embraced a) where  arbitrary = Embraced <$> arbitrary  shrink (Embraced a) =  Embraced <$> shrink ainstance Arbitrary a => Arbitrary (CslNe a) where  arbitrary = CslNe <$> arbitrary  shrink (CslNe a) = CslNe <$> shrink ainstance Arbitrary a => Arbitrary (SslNe a) where  arbitrary = SslNe <$> arbitrary  shrink (SslNe a) = SslNe <$> shrink a