@@ -4,14 +4,16 @@ module Test.ExtendedSyntax.New.Test where
44import Data.Text (Text , pack )
55import Data.Bifunctor (second )
66
7- import Control.Monad (forM_ )
7+ import Control.Monad (forM_ , forM )
88
99import Test.Hspec (Spec , describe )
1010
1111import Grin.ExtendedSyntax.TH (expr )
12- import Grin.ExtendedSyntax.Grin (Exp )
12+ import Grin.ExtendedSyntax.Grin (Exp , Name )
1313import Grin.ExtendedSyntax.TypeEnv (TypeEnv , emptyTypeEnv )
14- import Grin.ExtendedSyntax.Pretty (PP (.. ))
14+ import Grin.ExtendedSyntax.Pretty (Pretty , PP (.. ))
15+
16+ import Transformations.ExtendedSyntax.Names (evalNameM , deriveNewName )
1517
1618type SpecWithProg = Exp -> Spec
1719
@@ -29,8 +31,8 @@ contexts :: [TestExpContext]
2931contexts =
3032 [ emptyCtx
3133 , lastBindR
32- , bindL 0
33- , lastBindL 0
34+ , bindL
35+ , lastBindL
3436 , firstAlt
3537 , middleAlt
3638 , lastAlt
@@ -39,71 +41,92 @@ contexts =
3941emptyCtx :: TestExpContext
4042emptyCtx = (" empty" , id )
4143
42- bindL :: Int -> TestExpContext
43- bindL (pack . show -> n) = (" bind left" , second tr) where
44- tr (exprText -> e) = [expr |
45- fb$n <- do
46- $e
44+ -- NOTE: These contexts contain some names. Make sure not to use these in your test code!
45+
46+ deriveNames :: Exp -> [Name ]
47+ deriveNames e = fst <$> evalNameM e $ do
48+ forM [1 .. ] $ \ _ -> deriveNewName " ctxVar"
49+
50+ deriveNamesAsText :: Exp -> [Text ]
51+ deriveNamesAsText = map toText . deriveNames
52+
53+ toText :: Pretty a => a -> Text
54+ toText = pack . show . PP
55+
56+ bindL :: TestExpContext
57+ bindL = (" bind left" , second tr) where
58+ tr e
59+ | (v: _) <- deriveNamesAsText e
60+ , eText <- toText e = [expr |
61+ $v <- do
62+ $eText
4763 pure ()
4864 |]
4965
50- lastBindL :: Int -> TestExpContext
51- lastBindL (pack . show -> n) = (" last bind left" , second tr) where
52- tr (exprText -> e) = [expr |
53- md$n <- do
54- __1 <- pure ()
55- $e
66+ lastBindL :: TestExpContext
67+ lastBindL = (" last bind left" , second tr) where
68+ tr e
69+ | (v1: v2: _) <- deriveNamesAsText e
70+ , eText <- toText e = [expr |
71+ $v1 <- do
72+ $v2 <- pure ()
73+ $eText
5674 pure ()
5775 |]
5876
5977firstAlt :: TestExpContext
6078firstAlt = (" first alt" , second tr) where
61- tr (exprText -> e) = [expr |
62- __1 <- pure 1
63- case __1 of
64- 1 @ __2 ->
65- __x <- pure ()
66- $e
67- 2 @ __3 ->
79+ tr e
80+ | (scrut: v1: v2: v3: x: _) <- deriveNamesAsText e
81+ , eText <- toText e = [expr |
82+ $scrut <- pure 1
83+ case $scrut of
84+ 1 @ $v1 ->
85+ $x <- pure ()
86+ $eText
87+ 2 @ $v2 ->
6888 pure ()
69- 3 @ __4 ->
89+ 3 @ $v3 ->
7090 pure ()
7191 |]
7292
7393middleAlt :: TestExpContext
7494middleAlt = (" middle alt" , second tr) where
75- tr (exprText -> e) = [expr |
76- __1 <- pure 1
77- case __1 of
78- 1 @ __2 ->
95+ tr e
96+ | (scrut: v1: v2: v3: x: _) <- deriveNamesAsText e
97+ , eText <- toText e = [expr |
98+ $scrut <- pure 1
99+ case $scrut of
100+ 1 @ $v1 ->
79101 pure ()
80- 2 @ __3 ->
81- __x <- pure ()
82- $e
83- 3 @ __4 ->
102+ 2 @ $v2 ->
103+ $x <- pure ()
104+ $eText
105+ 3 @ $v3 ->
84106 pure ()
85107 |]
86108
87109lastAlt :: TestExpContext
88110lastAlt = (" last alt" , second tr) where
89- tr (exprText -> e) = [expr |
90- __1 <- pure 1
91- case __1 of
92- 1 @ __2 ->
111+ tr e
112+ | (scrut: v1: v2: v3: x: _) <- deriveNamesAsText e
113+ , eText <- toText e = [expr |
114+ $scrut <- pure 1
115+ case $scrut of
116+ 1 @ $v1 ->
93117 pure ()
94- 2 @ __3 ->
118+ 2 @ $v2 ->
95119 pure ()
96- 3 @ __4 ->
97- __x <- pure ()
98- $e
120+ 3 @ $v3 ->
121+ $x <- pure ()
122+ $eText
99123 |]
100124
101125lastBindR :: TestExpContext
102126lastBindR = (" last bind right" , second tr) where
103- tr (exprText -> e) = [expr |
104- __1 <- pure ()
105- $e
127+ tr e
128+ | (v: _) <- deriveNamesAsText e
129+ , eText <- toText e = [expr |
130+ $v <- pure ()
131+ $eText
106132 |]
107-
108- exprText :: Exp -> Text
109- exprText = pack . show . PP
0 commit comments