Skip to content

Commit c81da30

Browse files
committed
Add a nicer test case (by @edsko)
1 parent 39fecd0 commit c81da30

File tree

2 files changed

+45
-15
lines changed

2 files changed

+45
-15
lines changed

haskell-src-meta/src/Language/Haskell/Meta/Syntax/Translate.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -601,10 +601,10 @@ instance ToDec (Exts.Decl l) where
601601

602602
toDec (Exts.AnnPragma _ ann) = TH.PragmaD (TH.AnnP (target ann) (expann ann))
603603
where
604-
target (Exts.Ann _ n _) = TH.ValueAnnotation (toName n)
604+
target (Exts.Ann _ n _) = TH.ValueAnnotation (toName n)
605605
target (Exts.TypeAnn _ n _) = TH.TypeAnnotation (toName n)
606606
target (Exts.ModuleAnn _ _) = TH.ModuleAnnotation
607-
expann (Exts.Ann _ _ e) = toExp e
607+
expann (Exts.Ann _ _ e) = toExp e
608608
expann (Exts.TypeAnn _ _ e) = toExp e
609609
expann (Exts.ModuleAnn _ e) = toExp e
610610

haskell-src-meta/tests/Main.hs

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
1+
{-# LANGUAGE RankNTypes #-}
2+
{-# LANGUAGE ScopedTypeVariables #-}
3+
14
module Main where
25

3-
import qualified Control.Monad.Fail as Fail
4-
import qualified Language.Haskell.Exts as Exts
5-
import qualified Language.Haskell.Exts.Extension as Extension
6-
import qualified Language.Haskell.Exts.Parser as Parser
6+
import qualified Control.Monad.Fail as Fail
7+
import Data.Data (Data, cast, gfoldl)
8+
import Data.Functor.Const
9+
(Const (Const, getConst))
10+
import qualified Language.Haskell.Exts as Exts
11+
import qualified Language.Haskell.Exts.Extension as Extension
12+
import qualified Language.Haskell.Exts.Parser as Parser
713
import Language.Haskell.Meta.Parse
814
import Language.Haskell.Meta.Syntax.Translate
9-
import qualified Language.Haskell.TH as TH
10-
import Test.HUnit (Assertion, (@?=))
15+
import qualified Language.Haskell.TH as TH
16+
import Test.HUnit (Assertion, (@?=))
1117
import Test.Tasty
1218
(TestTree, defaultMain, testGroup)
13-
import Test.Tasty.HUnit (testCase)
19+
import Test.Tasty.HUnit (testCase)
1420

1521
type Test = TestTree
1622

@@ -27,14 +33,38 @@ derivingClausesTest :: Test
2733
derivingClausesTest = testCase "Deriving clauses preserved" $
2834
roundTripDecls "data Foo = Foo deriving (A, B, C)"
2935

30-
-- Very ugly test, illustrating incorrect order of promoted type tuples reconstructed into TH
3136
orderInTypeTuples :: Test
32-
orderInTypeTuples = testCase "Ensure that type tuples reconstructed in proper order" $ do
33-
let Parser.ParseOk parsed = show . toExp <$> Parser.parseExpWithMode mode "getField @'(\"a\", \"b\", \"c\") localVar"
34-
parsed @?= expected
37+
orderInTypeTuples =
38+
testCase "Ensure that type tuples reconstructed in proper order" $ do
39+
expected @?= actual
40+
where
41+
expected :: [TH.TyLit]
42+
expected = collectAll (toExp parsed)
43+
actual = [TH.StrTyLit "a", TH.StrTyLit "b"]
44+
45+
parsed :: Exts.Exp Exts.SrcSpanInfo
46+
parsed = case Exts.parseExpWithMode mode "foo @'(\"a\", \"b\")" of
47+
Exts.ParseOk v -> v
48+
e -> error $ show e
49+
mode :: Exts.ParseMode
50+
mode = Exts.defaultParseMode {
51+
Exts.extensions = [
52+
Exts.EnableExtension Exts.TypeApplications
53+
, Exts.EnableExtension Exts.DataKinds
54+
]
55+
}
56+
57+
collectAll :: (Data a, Data b) => a -> [b]
58+
collectAll = ($ []) . go
3559
where
36-
mode = Parser.defaultParseMode { Parser.extensions = [Extension.EnableExtension Extension.TypeApplications, Extension.EnableExtension Extension.DataKinds ] }
37-
expected = "(AppE (AppTypeE (VarE getField) (AppT (AppT (AppT (PromotedTupleT 3) (LitT (StrTyLit \"a\"))) (LitT (StrTyLit \"b\"))) (LitT (StrTyLit \"c\")))) (VarE localVar))"
60+
go :: forall a b. (Data a, Data b) => a -> [b] -> [b]
61+
go = \x ->
62+
case cast x of
63+
Just x' -> (x' :)
64+
Nothing -> getConst $ gfoldl ap (const $ Const id) x
65+
where
66+
ap :: Data x => Const ([b] -> [b]) (x -> y) -> x -> Const ([b] -> [b]) y
67+
ap (Const acc) x = Const $ acc . go x
3868

3969
typeAppMode :: Exts.ParseMode
4070
typeAppMode = Parser.defaultParseMode { Parser.extensions = [Extension.EnableExtension Extension.TypeApplications] }

0 commit comments

Comments
 (0)