{-# OPTIONS_GHC -fconstraint-solver-iterations=24 #-}{-# OPTIONS_GHC -Wno-orphans #-}module CssParser.Test.Arbitrary.Value whereimport CssParser.Identimport CssParser.Norm ( Norm(..) )import CssParser.Parser.Monad ( reorderErr )import CssParser.Rule.Valueimport CssParser.Rule.Type ( CssType, CssLeafType, AtomicCssType )import CssParser.Rule.TypedNum ( TypedNum(..), RawNum(..), PropValType (Mm, K), mkRawNum )import CssParser.Test.Arbitraryimport CssParser.Test.Arbitrary.Ident ()import Data.Text qualified as Tinstance Norm NthFormula where normalize = \case NthExpr e -> NthExpr . normNeg . stripParens $ reorderErr e o -> oinstance Arbitrary NthFormula where arbitrary = normalize <$> genericArbitrary shrink x = normalize <$> genericShrink xderiving via (GenericArbitrary CssType) instance Arbitrary CssTypederiving via (GenericArbitrary CssLeafType) instance Arbitrary CssLeafTypederiving via (GenericArbitrary AtomicCssType) instance Arbitrary AtomicCssTypedata Anum = IntAnum Int | PositiveIntAnum Word | FloatAnum Int Word | EAnum Int Int | NoWholeAnum Word | EAnum2 Int Word Int deriving (Eq, Generic)instance Show Anum where show = \case IntAnum x -> show x PositiveIntAnum x -> "+" <> show x FloatAnum x y -> show x <> "." <> show y EAnum x y -> show x <> "e" <> show y EAnum2 x y z -> show x <> "." <> show y <> "e" <> show z NoWholeAnum x -> "." <> show xderiving via (GenericArbitrary Anum) instance Arbitrary Anumderiving via (GenericArbitrary Important) instance Arbitrary Importantinstance Arbitrary RawNum where arbitrary = mkRawNum . show <$> (arbitrary :: Gen Anum) shrink _ = []instance Arbitrary HexColor where arbitrary = HC . pack <$> vectorOf 6 arbitraryHex shrink (HC x) | T.length x == 6 = [HC $ T.take 3 x] | otherwise = []instance Arbitrary Unsigned where arbitrary = Unsigned . RawNum . pack <$> listOf1 (elements [ '0' .. '9' ]) shrink = genericShrinkinstance Arbitrary Url where arbitrary = oneof [ pure $ Url "https://ooo.com/aoeu/style.css" , pure $ UnquotedUrl "https://ooo.com:443/aoeu/style.css?y=3&x=ok#eoeu" , pure $ UnquotedUrl "./file.css" , pure $ UnquotedUrl "/style.css" , pure $ UnquotedUrl "/../style.css" ]deriving via Ident instance Arbitrary LiteralStringderiving via (GenericArbitrary Ratio) instance Arbitrary Ratioinstance Norm PropVals where normalize (PropVals l i) = PropVals (mapInitNe embraceBinOp embraceDivOp l) iinstance Arbitrary PropVals where arbitrary = normalize <$> genericArbitrary shrink x = normalize <$> genericShrink xmapInit :: (a -> a) -> (a -> a) -> [a] -> [a]mapInit _ _ [] = []mapInit _ g [a] = [g a]mapInit f g (h:nh:t) = f h : mapInit f g (nh : t)mapInitNe :: (a -> a) -> (a -> a) -> NonEmpty a -> NonEmpty amapInitNe f g = \case h :| [] -> g h :| [] h :| l -> f h :| mapInit f g lembraceBinOp :: PropVal -> PropValembraceBinOp = \case CalcFun bop@BinOpCe {} -> CalcFun (CalcCe NoFn (CalcExprList $ bop :| [])) o -> oembraceDivOp :: PropVal -> PropValembraceDivOp = \case CalcFun bop@(BinOpCe _ DivCe _) -> CalcFun (CalcCe NoFn (CalcExprList $ bop :| [])) o -> ostripImportant :: PropVals -> PropValsstripImportant = \case PropVals l (Just _) -> PropVals l Nothing o -> oinstance Norm PropValsList where normalize (PropValsList l) = PropValsList (stripImportant <$> l)instance Arbitrary PropValsList where arbitrary = normalize <$> genericArbitrary shrink x = normalize <$> genericShrink xderiving via (GenericArbitrary PropValType) instance Arbitrary PropValTypederiving via (GenericArbitrary CalcFns) instance Arbitrary CalcFnsderiving via (GenericArbitrary CalcOp) instance Arbitrary CalcOpisD :: Char -> BoolisD x = isDigit x || x == '.'normNeg :: CalcExpr -> CalcExprnormNeg = \case o@(CalcNeg (ValCe (TypedNum (RawNum rn) pt))) -> case T.uncons rn of Just ('-', absRn) -> ValCe (TypedNum (RawNum absRn) pt) Just ('+', absRn) -> case T.uncons absRn of Just (fc, _) | isD fc -> ValCe (TypedNum (RawNum (T.cons '-' absRn)) pt) | otherwise -> ValCe (TypedNum (RawNum absRn) pt) Nothing -> o Just (fc, _) | isD fc -> ValCe (TypedNum (RawNum (T.cons '-' rn)) pt) | otherwise -> o _ -> o CalcNeg (CalcNeg x) -> normNeg x CalcNeg x -> CalcNeg $ normNeg x BinOpCe l op r -> BinOpCe (normNeg l) op (normNeg r) CalcCe f (CalcExprList l) -> CalcCe f . CalcExprList $ fmap normNeg l o -> oderiving via (GenericArbitrary CalcExpr) instance Arbitrary CalcExprderiving via (GenericArbitrary TypedNum) instance Arbitrary TypedNumderiving via (GenericArbitrary CalcExprList) instance Arbitrary CalcExprListrightMost :: PropVal -> PropValrightMost = \case Div _ y -> rightMost y o -> oinstance Norm CalcExpr where normalize = normNeginstance Norm PropVal where normalize = \case Div x y -> Div (rightMost y) (normalize x) AppFunEnum f (PropValsList (a :| [])) -> AppFun f a CalcFun ce@CalcCe {} -> CalcFun . normNeg . stripParens $ reorderErr ce CalcFun ce -> CalcFun . CalcCe CalcFn . CalcExprList . (:| []) . normNeg . stripParens $ reorderErr ce o -> oinstance Arbitrary PropVal where arbitrary = normalize <$> genericArbitrary shrink = normalize <$> genericShrinkderiving via (GenericArbitrary CommaSeparatedList) instance Arbitrary CommaSeparatedListinstance Norm AttrType where normalize = \case UnitAt K -> UnitAt Mm o -> oinstance Arbitrary AttrType where arbitrary = normalize <$> genericArbitrary shrink x = normalize <$> genericShrink xunPatternLetter :: Gen CharunPatternLetter = elements ( '?' : ['0' .. '9' ] <> ['a' .. 'f' ])unPattern :: Gen TextunPattern = do s <- unPatternLetter b <- sublistOf =<< vectorOf 3 unPatternLetter f <- maybeToList <$> elements [ Nothing, Just '0' ] p <- maybeToList <$> elements [ Nothing, Just '1' ] pure (pack $ p ++ f ++ b ++ [s])unDoublePattern :: Gen TextunDoublePattern = liftA2 (\a b -> a <> "-" <> b) unPattern unPatterninstance Arbitrary UnicodeRange where arbitrary = UnicodeRange <$> oneof [unPattern, unDoublePattern] shrink (UnicodeRange ur) = case T.dropEnd 1 ur of ur'-> case T.unsnoc ur' of Just (ur'', '-') -> shrink (UnicodeRange ur'') Just (_, _) -> [UnicodeRange ur'] Nothing -> []