You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Statements/declarations (function declaration can optionally be introduced by 'let'):
66
+
Function declarations (optionally introduced by /let/):
119
67
120
68
>>> let tuple x = (x,x)
69
+
>>> let one=1;two=2
121
70
>>> triple x = (x,x,x)
71
+
72
+
Any other declaration:
73
+
122
74
>>> data TertiumDatur = Truly | Falsely | Other deriving Show
123
75
>>> class Display a where display :: a -> String
124
76
>>> instance Display TertiumDatur where display = show
125
77
126
-
Type and Kind directives (as in ghci):
78
+
Definitions are available to following tests in the __same__ comment.
79
+
80
+
If you want definitions to be available to all tests in the module, define a setup section:
81
+
82
+
@
83
+
-- $setup
84
+
-- >>> eleven = 11
85
+
@
86
+
87
+
/eleven/ is now available to any test.
88
+
89
+
== Type and Kind directives
127
90
128
91
>>> :type Truly
129
92
Truly :: TertiumDatur
@@ -143,82 +106,67 @@ TertiumDatur :: *
143
106
N + M + 1 :: Nat
144
107
= 42
145
108
146
-
Properties:
147
-
148
-
prop> \(l::[Int]) -> reverse (reverse l) == l
149
-
+++ OK, passed 100 tests.
150
-
151
-
prop> \(l::[Bool]) -> reverse l == l
152
-
*** Failed! Falsified (after 8 tests and 1 shrink):
153
-
[False,True]
154
-
155
-
And finally expressions:
109
+
== Expressions
156
110
157
111
>>> tuple 2
158
112
>>> triple 3
159
113
>>> display Other
160
114
(2,2)
161
115
(3,3,3)
162
116
"Other"
163
-
-}
164
-
165
-
-- Though the Eval plugin functionality is quite similar to that of <https://hackage.haskell.org/package/doctest Doctest>, some features are not supported.
166
117
167
-
{-
168
-
Capturing stdout:
118
+
IO expressions can also be evaluated but their output to stdout/stderr is NOT captured:
169
119
170
120
>>> print "foo"
171
121
()
172
-
-}
173
122
174
-
{- |
175
-
Matching arbitrary output
123
+
== Properties
176
124
177
-
>>> [1..5]
178
-
[1,2,...
179
-
-}
125
+
prop> \(l::[Int]) -> reverse (reverse l) == l
126
+
+++ OK, passed 100 tests.
180
127
181
-
{-
182
-
Omitting lambda abstractions in property tests:
128
+
= Haddock vs Plain Comments
183
129
184
-
prop> reverse (reverse l) == (l::[Int])
185
-
Variable not in scope: l :: [Int]
186
-
Variable not in scope: l :: [Int]
130
+
There is a conceptual difference between Haddock and plain comments.
187
131
188
-
This works:
132
+
Haddock comments constitute the external module's documentation while plain comments are internal documentation meant to explain how the code works (api vs implementation).
189
133
190
-
prop> \(l::[Int]) -> reverse (reverse l) == l
191
-
+++ OK, passed 100 tests.
192
-
-}
134
+
This conceptual difference is reflected in the way tests results are refreshed by the Eval plugin.
193
135
194
-
{-
195
-
Multiline comments like the following:
196
-
>>> :{
197
-
let
198
-
x = 1
199
-
y = 2
200
-
in x + y + multiline
201
-
:}
202
-
-}
136
+
Tests in plain comments are refreshed by overwriting the previous result.
137
+
138
+
On the contrary, when tests in Haddock comments are refreshed their current result is compared with the previous one and differences are displayed.
139
+
140
+
Say for example that we have defined a test on the `thrice` function, defined as:
203
141
204
-
{- Tip: Pretty printing.
142
+
>>> thrice = (*3)
205
143
206
-
Say that you want to pretty print a value.
144
+
If by mistake at a later time we change its definition to:
207
145
208
-
You could, for example, use the pretty-simple package:
146
+
>>> thrice = (*2)
147
+
148
+
When we refresh its test we get a warning:
149
+
150
+
>>> thrice 11
151
+
WAS 33
152
+
NOW 22
153
+
154
+
== Tip: Multiline Output
155
+
156
+
By default, the output of every expression is returned as a single line.
157
+
158
+
This is a problem if you want, for example, to pretty print a value (in this case using the <https://hackage.haskell.org/package/pretty-simple pretty-simple> package):
209
159
210
160
>>> import Text.Pretty.Simple
211
161
>>> pShowNoColor [1..3]
212
162
"[ 1\n, 2\n, 3\n]"
213
163
214
-
But what we get is just a String.
215
-
216
-
We could try to print it, but stdout is not captured:
164
+
We could try to print the pretty-print output, but stdout is not captured so we get just a ():
217
165
218
166
>>> print $ pShowNoColor [1..7]
219
167
()
220
168
221
-
To 'print' it properly, we can exploit the fact that the output of an error is displayed as a multi-line text:
169
+
To display it properly, we can exploit the fact that the output of an error is displayed as a multi-line text:
222
170
223
171
>>> import qualified Data.Text.Lazy as TL
224
172
>>> import Text.Pretty.Simple
@@ -228,4 +176,71 @@ To 'print' it properly, we can exploit the fact that the output of an error is d
228
176
, 2
229
177
, 3
230
178
]
179
+
180
+
= Differences with doctest
181
+
182
+
Though the Eval plugin functionality is quite similar to that of <https://hackage.haskell.org/package/doctest doctest>, some doctest features are not supported.
183
+
184
+
== Capturing Stdout
185
+
186
+
Only the value of the expression is spliced in, not its output:
187
+
188
+
>>> print "foo"
189
+
()
190
+
191
+
== Pattern Matching
192
+
193
+
The arbitrary content matcher __...__ is unsupported.
194
+
195
+
== Missing lambda abstractions in property tests
196
+
197
+
Variables are not automatically introduced:
198
+
199
+
prop> reverse (reverse l) == (l::[Int])
200
+
Variable not in scope: l :: [Int]
201
+
Variable not in scope: l :: [Int]
202
+
203
+
This works:
204
+
205
+
prop> \(l::[Int]) -> reverse (reverse l) == l
206
+
+++ OK, passed 100 tests.
207
+
208
+
== Multiline Expressions
209
+
210
+
@
211
+
>>> :{
212
+
let
213
+
x = 1
214
+
y = 2
215
+
in x + y + multiline
216
+
:}
217
+
@
218
+
-}
219
+
moduleIde.Plugin.Eval.Tutorial (
220
+
double,
221
+
) where
222
+
223
+
{- | Double a number
224
+
225
+
An example of a simple test suite for a function (all tests are executed together).
0 commit comments