Skip to content

Commit bf10eae

Browse files
committed
ES: added name generation to context-based testing
1 parent 1ed368d commit bf10eae

File tree

1 file changed

+69
-46
lines changed
  • grin/src/Test/ExtendedSyntax/New

1 file changed

+69
-46
lines changed

grin/src/Test/ExtendedSyntax/New/Test.hs

Lines changed: 69 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@ module Test.ExtendedSyntax.New.Test where
44
import Data.Text (Text, pack)
55
import Data.Bifunctor (second)
66

7-
import Control.Monad (forM_)
7+
import Control.Monad (forM_, forM)
88

99
import Test.Hspec (Spec, describe)
1010

1111
import Grin.ExtendedSyntax.TH (expr)
12-
import Grin.ExtendedSyntax.Grin (Exp)
12+
import Grin.ExtendedSyntax.Grin (Exp, Name)
1313
import 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

1618
type SpecWithProg = Exp -> Spec
1719

@@ -29,8 +31,8 @@ contexts :: [TestExpContext]
2931
contexts =
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 =
3941
emptyCtx :: TestExpContext
4042
emptyCtx = ("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

5977
firstAlt :: TestExpContext
6078
firstAlt = ("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

7393
middleAlt :: TestExpContext
7494
middleAlt = ("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

87109
lastAlt :: TestExpContext
88110
lastAlt = ("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

101125
lastBindR :: TestExpContext
102126
lastBindR = ("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

Comments
 (0)