{-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE TemplateHaskell #-} module Main where ------------------------------------------------------------------------------- import Control.Lens hiding (elements) import qualified Data.Configurator as C import Data.Configurator.Types import qualified Data.Map as M import Data.Maybe import Data.Monoid import Data.Text (Text) import qualified Data.Text as T import qualified Data.Text.IO as T import Data.Text.Read import Heist import Heist.Interpreted import Snap import Snap.Util.FileServe import Snap.Snaplet.Heist import System.FilePath import System.Random import Test.QuickCheck.Gen import Text.XmlHtml ------------------------------------------------------------------------------ data App = App { _heist :: Snaplet (Heist App) } makeLenses ''App instance HasHeist App where heistLens = subSnaplet heist ------------------------------------------------------------------------------ -- | Not sure whether we want these types specifically reified into an -- enumeration like this or to make them dynamic strings. At any rate, I'm -- putting it like this now to record my thoughts on some of the types needed. data GenTypes = IntT -- ^ Integers | RealT -- ^ Reals | EnumTextT -- ^ Text that comes from an enumeration. This is useful for generating -- things like first names, last names, and other lists. | ChildListT -- ^ This type would be used on a heist tag that functions as a -- runChildren-style list. deriving (Read, Show, Eq, Enum) ------------------------------------------------------------------------------ -- | Integer generation genInt :: [Text] -> Gen [Node] genInt [] = genInt' 0 100 genInt [a,b] = genInt' (read $ T.unpack a) (read $ T.unpack b) genInt _ = error "charade: invalid number of parameters to int generator" genInt' :: Int -> Int -> Gen [Node] genInt' a b = fmap ((:[]) . TextNode . T.pack . show) $ choose (a,b) ------------------------------------------------------------------------------ -- | Real generation genReal :: [Text] -> Gen [Node] genReal [] = genReal' 0 1 genReal [a,b] = genReal' (read $ T.unpack a) (read $ T.unpack b) genReal _ = error "charade: invalid number of parameters to real generator" genReal' :: Double -> Double -> Gen [Node] genReal' a b = fmap ((:[]) . TextNode . T.pack . show) $ choose (a,b) ------------------------------------------------------------------------------ -- | List generation genList :: M.Map Text [Text] -> Node -> [Text] -> Gen [Node] genList enums node [] = genList' enums node 5 genList enums node [n] = genList' enums node $ either error fst (decimal n) genList enums node [a,b] = do let minCount = either error fst (decimal a) :: Int maxCount = either error fst (decimal b) :: Int count <- choose (minCount, maxCount) genList' enums node count genList _ _ _ = error "charade: invalid number of parameters to list generator" genList' :: M.Map Text [Text] -> Node -> Int -> Gen [Node] genList' enums node count = liftM concat $ vectorOf count $ liftM concat $ mapM (fakeNode enums) (childNodes node) ------------------------------------------------------------------------------ -- | List generation lorem :: Text lorem = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum." genLorem :: [Text] -> Gen [Node] genLorem [] = return [TextNode lorem] genLorem [n] = return [TextNode $ T.intercalate " " $ replicate count lorem] where count = either error fst (decimal n) genLorem _ = error "charade: invalid number of parameters to lorem generator" ------------------------------------------------------------------------------ -- | Enum generation genEnum :: [Text] -> [Node] -> Gen [Node] genEnum [] = fmap (:[]) . elements genEnum _ = error "charade: invalid number of parameters to enum generator" ------------------------------------------------------------------------------ -- | Enum generation genDynEnum :: M.Map Text [Text] -> [Text] -> Gen [Node] genDynEnum enums [file] = genEnum [] (map TextNode $ enums M.! file) genDynEnum _ _ = error "charade: must supply an file to the enum type" ------------------------------------------------------------------------------ -- | Uses the \"fake\" attribute to determine what type of random data should -- be generated for this node. Usage might look something like this: -- -- >