Skip to content

Commit

Permalink
- Monadic Lambdas in Generic context. Closes lymar/hastache/lymar#8
Browse files Browse the repository at this point in the history
- Add EclipseFP trash to .gitignore
  • Loading branch information
akaspin committed Oct 29, 2011
1 parent 0ecc7bf commit 9086a21
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 5 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
/dist
/.project
/.hsproject
/.dist-scion
9 changes: 6 additions & 3 deletions Text/Hastache/Context.hs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ UPPER
esrever
@
-}
mkGenericContext :: (Monad m, Data a) => a -> MuContext m
mkGenericContext :: (Monad m, Data a, Typeable1 m) => a -> MuContext m
mkGenericContext val = toGenTemp val ~> convertGenTempToContext

data TD m =
Expand All @@ -100,12 +100,12 @@ data TD m =
| TUnknown
deriving (Show)

toGenTemp :: (Data a, Monad m) => a -> TD m
toGenTemp :: (Data a, Monad m, Typeable1 m) => a -> TD m
toGenTemp a = zip fields (gmapQ procField a) ~> TObj
where
fields = toConstr a ~> constrFields

procField :: (Data a, Monad m) => a -> TD m
procField :: (Data a, Monad m, Typeable1 m) => a -> TD m
procField =
obj
`ext1Q` list
Expand All @@ -131,11 +131,14 @@ procField =
`extQ` (\(i::Bool) -> MuBool i ~> TSimple)
`extQ` muLambdaBS
`extQ` muLambdaS
`extQ` muLambdaBSIO
where
obj a = case dataTypeRep (dataTypeOf a) of
AlgRep [c] -> toGenTemp a
_ -> TUnknown
list a = map procField a ~> TList
muLambdaBSIO :: (BS.ByteString -> m BS.ByteString) -> TD m
muLambdaBSIO f = MuLambdaM f ~> TSimple
muLambdaBS :: (BS.ByteString -> BS.ByteString) -> TD m
muLambdaBS f = MuLambda f ~> TSimple
muLambdaS :: (String -> String) -> TD m
Expand Down
39 changes: 37 additions & 2 deletions tests/test.hs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE DeriveDataTypeable, DeriveFunctor #-}
module Tests where

import Control.Monad
import Control.Monad.Writer
import Control.Monad.Writer.Lazy (liftIO, MonadIO)
import Data.Char
import Data.Data
import Data.Generics
Expand Down Expand Up @@ -334,6 +335,7 @@ nestedContextTest = do
\ * elem 2. top\n\
\"

-- Up-level context from nested block (Generic version)
data TopData = TopData {
topDataTop :: String,
topDataItems :: [NestedData]
Expand All @@ -345,7 +347,6 @@ data NestedData = NestedData {
}
deriving (Data, Typeable)

-- Up-level context from nested block (Generic version)
nestedGenericContextTest = do
res <- hastacheStr defaultConfig (encodeStr template) context
assertEqualStr "result correctness" (decodeStrLBS res) testRes
Expand Down Expand Up @@ -374,6 +375,39 @@ nestedGenericContextTest = do
\Nested variable : NESTED_TWO\n\
\"

-- Modadic lamda in generic context

data CtxWithLambdaM = CtxWithLambdaM {
lMone :: String,
lMLamb :: BS.ByteString -> BS.ByteString,
lMLambM :: BS.ByteString -> IO BS.ByteString
} deriving (Data, Typeable)

genericContextLamdaMTest = do
res <- hastacheStr defaultConfig (encodeStr template) context
assertEqualStr "result correctness" (decodeStrLBS res) testRes
where
template = "\
\{{lMone}}\
\{{#lMLamb}}\n\
\In normal lambda\n\
\{{/lMLamb}}\n\
\{{#lMLambM}}\n\
\In IO lambda\n\
\{{/lMLambM}}\n\n\
\"
context = mkGenericContext $ CtxWithLambdaM {
lMone = "ONE",
lMLamb = BS.reverse,
lMLambM = (\s -> do
return $ BS.reverse s)
}
testRes = "\
\ONE\n\
\adbmal lamron nI\n\
\adbmal OI nI\n\
\"

tests = TestList [
TestLabel "Comments test" (TestCase commentsTest)
,TestLabel "Variables test" (TestCase variablesTest)
Expand All @@ -387,6 +421,7 @@ tests = TestList [
,TestLabel "Generic context test" (TestCase genericContextTest)
,TestLabel "Nested context test" (TestCase nestedContextTest)
,TestLabel "Nested generic context test" (TestCase nestedGenericContextTest)
,TestLabel "Generic context LambdaM" (TestCase genericContextLamdaMTest)
]

main = do
Expand Down

0 comments on commit 9086a21

Please sign in to comment.