{-# OPTIONS_GHC -Wno-orphans #-}{-# LANGUAGE FlexibleInstances #-}{-# LANGUAGE OverloadedStrings #-}module GHCup.ArbitraryTypes whereimport GHCup.Typesimport GHCup.Types.Stackimport GHCup.Types.JSONimport Data.ByteString ( ByteString )import Data.Char ( toLower )import Data.Versionsimport Data.List.NonEmptyimport Data.Time.Calendar ( Day(..) )import Test.QuickCheckimport Test.QuickCheck.Arbitrary.ADT ( ToADTArbitrary )import Test.QuickCheck.Arbitrary.Genericimport URI.ByteStringimport qualified Data.Map.Strict as Mimport qualified Data.Text as Timport qualified Data.Text.Encoding as Eimport qualified Data.Text.Lazy as T ( toStrict )import qualified Data.Text.Lazy.Builder as Bimport qualified Data.Text.Lazy.Builder.Int as Binstance Arbitrary ToolVersionSpec where arbitrary = genericArbitrary shrink = genericShrinkinstance Arbitrary GHCupDownloads where arbitrary = genericArbitrary shrink = genericShrinkinstance Arbitrary Rev where arbitrary = genericArbitrary shrink = genericShrinkinstance Arbitrary RevisionSpec where arbitrary = genericArbitrary shrink = genericShrinkinstance Arbitrary VersionMetadata where arbitrary = genericArbitrary shrink = genericShrinkinstance Arbitrary ArchitectureSpec where arbitrary = genericArbitrary shrink = genericShrinkinstance Arbitrary PlatformSpec where arbitrary = genericArbitrary shrink = genericShrinkinstance Arbitrary PlatformVersionSpec where arbitrary = genericArbitrary shrink = genericShrink ----------------- --[ utilities ]-- -----------------intToText :: Integral a => a -> T.TextintToText = T.toStrict . B.toLazyText . B.decimalgenVer :: Gen (Int, Int, Int)genVer = (\x y z -> (getPositive x, getPositive y, getPositive z)) <$> arbitrary <*> arbitrary <*> arbitraryinstance ToADTArbitrary ToolDescriptioninstance ToADTArbitrary ToolInfoinstance ToADTArbitrary GHCupInfo ---------------------- --[ base arbitrary ]-- ----------------------instance Arbitrary T.Text where arbitrary = fmap T.pack $ listOf $ elements ['a' .. 'z'] shrink xs = T.pack <$> shrink (T.unpack xs)instance Arbitrary (NonEmpty Word) where arbitrary = fmap fromList $ listOf1 arbitrary-- utf8 encoded bytestringinstance Arbitrary ByteString where arbitrary = fmap (E.encodeUtf8 . T.pack) $ listOf $ elements ['a' .. 'z'] --------------------- --[ uri arbitrary ]-- ---------------------instance Arbitrary Scheme where arbitrary = elements [ Scheme "http", Scheme "https" ]instance Arbitrary Host where arbitrary = genericArbitrary shrink = genericShrinkinstance Arbitrary Port where arbitrary = genericArbitrary shrink = genericShrinkinstance Arbitrary Day where arbitrary = ModifiedJulianDay <$> choose (-100000, 100000)instance Arbitrary (URIRef Absolute) where arbitrary = URI <$> arbitrary <*> pure Nothing <*> arbitrary <*> pure (Query []) <*> pure Nothing ------------------------- --[ version arbitrary ]-- -------------------------instance Arbitrary Mess where arbitrary = do (x, y, z) <- genVer pure $ either (error . show) id $ mess (intToText x <> "." <> intToText y <> "." <> intToText z)instance Arbitrary Version where arbitrary = do (x, y, z) <- genVer pure $ either (error . show) id $ version (intToText x <> "." <> intToText y <> "." <> intToText z)instance Arbitrary SemVer where arbitrary = do (x, y, z) <- genVer pure $ either (error . show) id $ semver (intToText x <> "." <> intToText y <> "." <> intToText z)instance Arbitrary PVP where arbitrary = do (x, y, z) <- genVer pure $ either (error . show) id $ pvp (intToText x <> "." <> intToText y <> "." <> intToText z)instance Arbitrary Versioning where arbitrary = Ideal <$> arbitrary ----------------------- --[ ghcup arbitrary ]-- -----------------------instance Arbitrary ToolDescription where arbitrary = genericArbitrary shrink = genericShrinkinstance Arbitrary ToolInfo where arbitrary = genericArbitrary shrink = genericShrinkinstance Arbitrary EnvUnion where arbitrary = genericArbitrary shrink = genericShrinkinstance Arbitrary EnvSpec where arbitrary = genericArbitrary shrink = genericShrinkinstance Arbitrary ConfigSpec where arbitrary = genericArbitrary shrink = genericShrinkinstance Arbitrary MakeSpec where arbitrary = genericArbitrary shrink = genericShrinkinstance Arbitrary InstallationSpecInput where arbitrary = genericArbitrary shrink = genericShrinkinstance Arbitrary (NonEmpty InstallFileRule) where arbitrary = genericArbitrary shrink = genericShrinkinstance Arbitrary InstallFileRule where arbitrary = suchThat genericArbitrary pred' where pred' (InstallFileRule source dest) = safePath source && maybe True safePath dest pred' (InstallFilePatternRule fps) = all safePath fps shrink = genericShrinkinstance Arbitrary (SymlinkSpec String) where arbitrary = suchThat genericArbitrary pred' where pred' SymlinkSpec{..} = safePath _slTarget && safeFilename _slLinkName && maybe True safeFilename _slSetName shrink = genericShrinkinstance Arbitrary SymlinkInputSpec where arbitrary = suchThat genericArbitrary pred' where pred' SymlinkInputSpec{..} = safePath _slTarget && safeFilename _slLinkName && maybe True safeFilename _slSetName pred' SymlinkPatternSpec{..} = all safePath _slTargetPattern && all safePath _slTargetPatternIgnore && safeFilename _slLinkName && maybe True safeFilename _slSetName shrink = genericShrinkinstance Arbitrary Requirements where arbitrary = genericArbitrary shrink = genericShrinkinstance Arbitrary GHCup.Types.DownloadInfo where arbitrary = genericArbitrary shrink = genericShrinkinstance Arbitrary GHCup.Types.Stack.DownloadInfo where arbitrary = genericArbitrary shrink = genericShrinkinstance Arbitrary LinuxDistro where arbitrary = oneof (pure <$> allDistros)instance Arbitrary Platform where arbitrary = genericArbitrary shrink = genericShrinkinstance Arbitrary Tag where arbitrary = genericArbitrary shrink = genericShrinkinstance Arbitrary Architecture where arbitrary = genericArbitrary shrink = genericShrinkinstance Arbitrary VersionInfo where arbitrary = genericArbitrary shrink = genericShrinkinstance Arbitrary VersionRange where arbitrary = genericArbitrary shrink = genericShrinkinstance Arbitrary UserSettings where arbitrary = genericArbitrary shrink = genericShrinkinstance Arbitrary PagerConfig where arbitrary = genericArbitrary shrink = genericShrinkinstance Arbitrary PlatformRequest where arbitrary = genericArbitrary shrink = genericShrinkinstance Arbitrary MetaMode where arbitrary = genericArbitrary shrink = genericShrinkinstance Arbitrary GPGSetting where arbitrary = genericArbitrary shrink = genericShrinkinstance Arbitrary Verbosity where arbitrary = genericArbitrary shrink = genericShrinkinstance Arbitrary URLSource where arbitrary = genericArbitrary shrink = genericShrinkinstance Arbitrary KeepDirs where arbitrary = genericArbitrary shrink = genericShrinkinstance Arbitrary Downloader where arbitrary = genericArbitrary shrink = genericShrinkinstance Arbitrary SetupInfo where arbitrary = genericArbitrary shrink = genericShrinkinstance Arbitrary UserKeyBindings where arbitrary = genericArbitrary shrink = genericShrinkinstance Arbitrary NewURLSource where arbitrary = genericArbitrary shrink = genericShrinkinstance Arbitrary ChannelAlias where arbitrary = genericArbitrary shrink = genericShrinkinstance Arbitrary KeyCombination where arbitrary = genericArbitrary shrink = genericShrinkinstance Arbitrary Key where arbitrary = genericArbitrary shrink = genericShrinkinstance Arbitrary Modifier where arbitrary = genericArbitrary shrink = genericShrinkinstance Arbitrary GHCDownloadInfo where arbitrary = genericArbitrary shrink = genericShrinkinstance Arbitrary ProcessSpec where arbitrary = genericArbitrary shrink = genericShrinkinstance Arbitrary UserInfo where arbitrary = genericArbitrary shrink = genericShrinkinstance Arbitrary Authority where arbitrary = genericArbitrary shrink = genericShrinkinstance Arbitrary DownloadMirror where arbitrary = genericArbitrary shrink = genericShrinkinstance Arbitrary DownloadMirrors where arbitrary = genericArbitrary shrink = genericShrinkinstance Arbitrary VersionedDownloadInfo where arbitrary = genericArbitrary shrink = genericShrinkinstance Arbitrary (NonEmpty VersionCmp) where arbitrary = genericArbitrary shrink = genericShrinkinstance Arbitrary VersionCmp where arbitrary = genericArbitrary shrink = genericShrinkinstance Arbitrary TarDir where arbitrary = genericArbitrary shrink = genericShrinkinstance Arbitrary Tool where arbitrary = flip suchThat pred' $ do ASCIIString str <- arbitrary pure $ Tool (toLower <$> str) where pred' = safeToolname shrink = genericShrinkinstance Arbitrary GHCupInfo where arbitrary = genericArbitrary shrink = genericShrinkinstance Arbitrary TargetVersion where arbitrary = suchThat (TargetVersion Nothing <$> arbitrary) pred' where pred' = safeVersion shrink = genericShrink-- our maps are nested... the default size easily blows up most ppls raminstance {-# OVERLAPS #-} Arbitrary v => Arbitrary (M.Map Tool v) where arbitrary = resize 8 $ M.fromList <$> arbitraryinstance {-# OVERLAPS #-} Arbitrary v => Arbitrary (M.Map (Maybe Version) v) where arbitrary = resize 8 $ M.fromList <$> arbitraryinstance {-# OVERLAPS #-} Arbitrary v => Arbitrary (M.Map Platform v) where arbitrary = resize 8 $ M.fromList <$> arbitraryinstance {-# OVERLAPS #-} Arbitrary v => Arbitrary (MapIgnoreUnknownKeys Platform v) where arbitrary = resize 8 $ MapIgnoreUnknownKeys . M.fromList <$> arbitraryinstance {-# OVERLAPS #-} Arbitrary v => Arbitrary (MapIgnoreUnknownKeys Architecture v) where arbitrary = resize 8 $ MapIgnoreUnknownKeys . M.fromList <$> arbitraryinstance {-# OVERLAPS #-} Arbitrary v => Arbitrary (M.Map (Maybe Versioning) v) where arbitrary = resize 8 $ M.fromList <$> arbitrary