-
Notifications
You must be signed in to change notification settings - Fork 864
Expand file tree
/
Copy pathInterpolatedStringsTests.fs
More file actions
443 lines (399 loc) · 16.2 KB
/
Copy pathInterpolatedStringsTests.fs
File metadata and controls
443 lines (399 loc) · 16.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information.
namespace Language
open System
open Xunit
open FSharp.Test.Compiler
module InterpolatedStringsTests =
[<Fact>]
let ``Regression: Empty Interpolated String properly typechecks with explicit type on binding`` () =
Fsx """ let a:byte = $"string" """
|> compile
|> shouldFail
|> withSingleDiagnostic (Error 1, Line 1, Col 15, Line 1, Col 24, "This expression was expected to have type" + Environment.NewLine + " 'byte' " + Environment.NewLine + "but here has type" + Environment.NewLine + " 'string' ")
[<Fact>]
let ``Interpolated String with hole properly typechecks with explicit type on binding`` () =
Fsx """ let a:byte = $"strin{'g'}" """
|> compile
|> shouldFail
|> withSingleDiagnostic (Error 1, Line 1, Col 15, Line 1, Col 28, "This expression was expected to have type" + Environment.NewLine + " 'byte' " + Environment.NewLine + "but here has type" + Environment.NewLine + " 'string' ")
[<Fact>]
let ``Interpolated String without holes properly typeckecks with explicit type on binding`` () =
Fsx """
let a: obj = $"string"
let b: System.IComparable = $"string"
let c: System.IFormattable = $"string"
"""
|> compile
|> shouldSucceed
[<Fact>]
let ``Interpolated string literal typed as FormattableString handles double braces correctly`` () =
Fsx """
let a = $"{{hello}} world" : System.FormattableString
printf $"{a.Format}"
"""
|> withLangVersion80
|> compileExeAndRun
|> shouldSucceed
|> withStdOutContains "{{hello}} world"
[<Fact>]
let ``Interpolated string with 2 leading dollar characters uses double braces for delimiters`` () =
// let s = $$"""{{42 + 0}} = {41 + 1}"""
// printfn "%s" s
Fsx "let s = $$\"\"\"{{42 + 0}} = {41 + 1}\"\"\"\n\
printfn \"%s\" s"
|> withLangVersion80
|> compileExeAndRun
|> shouldSucceed
|> withStdOutContains "42 = {41 + 1}"
[<Fact>]
let ``Too many consecutive opening braces in interpolated string result in an error`` () =
// $$"""{{{{42 - 0}}"""
Fsx "$$\"\"\"{{{{42 - 0}}\"\"\""
|> withLangVersion80
|> compile
|> shouldFail
|> withSingleDiagnostic (Error 1248, Line 1, Col 1, Line 1, Col 10, "The interpolated triple quoted string literal does not start with enough '$' characters to allow this many consecutive opening braces as content.")
[<Fact>]
let ``Too many consecutive closing braces in interpolated string result in an error`` () =
// $$"""{{42 - 0}}}}"""
Fsx "$$\"\"\"{{42 - 0}}}}\"\"\""
|> withLangVersion80
|> compile
|> shouldFail
|> withSingleDiagnostic (Error 1249, Line 1, Col 14, Line 1, Col 21, "The interpolated string contains unmatched closing braces.")
[<Fact>]
let ``Percent sign characters in interpolated strings`` () =
Assert.Equal("%", $"%%")
Assert.Equal("42%", $"{42}%%")
Assert.Equal("% 42", $"%%%3d{42}")
[<Fact>]
let ``Double percent sign characters in triple quote interpolated strings`` () =
Fsx "printfn \"%s\" $$$\"\"\"%%\"\"\""
|> withLangVersion80
|> compileExeAndRun
|> shouldSucceed
|> withStdOutContains "%%"
[<Fact>]
let ``Percent sign after interpolation hole in triple quote strings`` () =
Fsx "printfn \"%s\" $$\"\"\"{{42}}%\"\"\""
|> withLangVersion80
|> compileExeAndRun
|> shouldSucceed
|> withStdOutContains "42%"
[<Fact>]
let ``Percent sign before format specifier in triple quote interpolated strings`` () =
Fsx "printfn \"%s\" $$\"\"\"%%%3d{{42}}\"\"\""
|> withLangVersion80
|> compileExeAndRun
|> shouldSucceed
|> withStdOutContains "% 42"
[<Fact>]
let ``Percent signs separated by format specifier's flags`` () =
Fsx """
let s = $"...%-%...{0}"
"""
|> compile
|> shouldFail
|> withSingleDiagnostic (Warning 3376, Line 2, Col 9, Line 2, Col 24, "Bad format specifier: '%'")
[<Fact>]
let ``Interpolated expression can be offside`` () =
Fsx """
let a() =
let b() =
$"
{1}"
b()
type Foo () =
member _.Bar () =
let x =
$"
{2}"
x
"""
|> compile
|> shouldSucceed
[<Fact>]
let ``Percent signs and format specifiers with string expression`` () =
Fsx """
let x = "abc"
let s = $"%%%s{x}%%"
printfn "%s" s
"""
|> compileExeAndRun
|> shouldSucceed
|> withStdOutContains "%abc%"
[<Theory>]
// Test different number of interpolated string parts
[<InlineData("$\"\"\"abc{\"d\"}e\"\"\"")>]
[<InlineData("$\"\"\"abc{\"d\"}{\"e\"}\"\"\"")>]
[<InlineData("$\"\"\"a{\"b\"}c{\"d\"}e\"\"\"")>]
let ``Interpolated expressions are strings`` (strToPrint: string) =
Fsx $"""
let x = {strToPrint}
printfn "%%s" x
"""
|> compileExeAndRun
|> shouldSucceed
|> withStdOutContains "abcde"
let ``Multiline interpolated expression is a string`` () =
let strToPrint = String.Join(Environment.NewLine, "$\"\"\"a", "b", "c", "{\"d\"}", "e\"\"\"")
Fsx $"""
let x = {strToPrint}
printfn "%%s" x
"""
|> compileExeAndRun
|> shouldSucceed
|> withStdOutContains """a
b
c
d
e"""
[<Theory>]
[<InlineData("$\"\"\"abc{\"d\"}e\"\"\"", 1)>]
[<InlineData("$\"\"\"abc{\"d\"}{\"e\"}\"\"\"", 2)>]
[<InlineData("$\"\"\"a{\"b\"}c{\"d\"}e\"\"\"", 2)>]
let ``In FormattableString, interpolated expressions are strings`` (formattableStr: string, argCount: int) =
Fsx $"""
let x = {formattableStr} : System.FormattableString
assert(x.ArgumentCount = {argCount})
printfn "%%s" (System.Globalization.CultureInfo "en-US" |> x.ToString)
"""
|> compileExeAndRun
|> shouldSucceed
|> withStdOutContains "abcde"
[<Fact>]
let ``Warn when lambda is used as interpolated string argument`` () =
Fsx """
let f = fun x -> x + 1
let s = $"{f}"
"""
|> withLangVersionPreview
|> compile
|> shouldFail
|> withSingleDiagnostic (Warning 3884, Line 3, Col 12, Line 3, Col 13, "This expression is a function value. When used in an interpolated string it will be formatted using its 'ToString' method, which is likely not the intended behavior. Consider applying the function to its arguments.")
[<Fact>]
let ``Warn when underscore dot shorthand is used as interpolated string argument`` () =
Fsx """
type R = { Name: string }
let r = { Name = "hello" }
let s = $"{_.Name}" : string
"""
|> withLangVersionPreview
|> compile
|> shouldFail
|> withSingleDiagnostic (Warning 3884, Line 4, Col 12, Line 4, Col 18, "This expression is a function value. When used in an interpolated string it will be formatted using its 'ToString' method, which is likely not the intended behavior. Consider applying the function to its arguments.")
[<Fact>]
let ``Warn when partially applied function is used as interpolated string argument`` () =
Fsx """
let add x y = x + y
let s = $"{add 1}"
"""
|> withLangVersionPreview
|> compile
|> shouldFail
|> withSingleDiagnostic (Warning 3884, Line 3, Col 12, Line 3, Col 17, "This expression is a function value. When used in an interpolated string it will be formatted using its 'ToString' method, which is likely not the intended behavior. Consider applying the function to its arguments.")
[<Fact>]
let ``Warn when named function is used as interpolated string argument`` () =
Fsx """
let myFunc (x: int) = string x
let s = $"result: {myFunc}"
"""
|> withLangVersionPreview
|> compile
|> shouldFail
|> withSingleDiagnostic (Warning 3884, Line 3, Col 20, Line 3, Col 26, "This expression is a function value. When used in an interpolated string it will be formatted using its 'ToString' method, which is likely not the intended behavior. Consider applying the function to its arguments.")
[<Fact>]
let ``No warn when non-function value is used as interpolated string argument`` () =
Fsx """
let x = 42
let s1 = $"{x}"
let s2 = $"{System.DateTime.Now}"
"""
|> withLangVersionPreview
|> compile
|> shouldSucceed
[<Fact>]
let ``No warn when function is applied in interpolated string argument`` () =
Fsx """
let f x = x + 1
let s = $"{f 42}"
"""
|> withLangVersionPreview
|> compile
|> shouldSucceed
[<Fact>]
let ``No warn for function value in interpolated string with older language version`` () =
Fsx """
let f = fun x -> x + 1
let s = $"{f}"
"""
|> withLangVersion10
|> compile
|> shouldSucceed
[<Fact>]
let ``Warn when multiple function values are used in interpolated string`` () =
Fsx """
let f x = x + 1
let g x = x * 2
let s = $"{f} and {g}"
"""
|> withLangVersionPreview
|> compile
|> shouldFail
|> withDiagnostics [
(Warning 3884, Line 4, Col 12, Line 4, Col 13, "This expression is a function value. When used in an interpolated string it will be formatted using its 'ToString' method, which is likely not the intended behavior. Consider applying the function to its arguments.")
(Warning 3884, Line 4, Col 20, Line 4, Col 21, "This expression is a function value. When used in an interpolated string it will be formatted using its 'ToString' method, which is likely not the intended behavior. Consider applying the function to its arguments.")
]
[<Fact>]
let ``Warn for function value in FormattableString interpolated string`` () =
Fsx """
let f x = x + 1
let s : System.FormattableString = $"{f}"
"""
|> withLangVersionPreview
|> compile
|> shouldFail
|> withSingleDiagnostic (Warning 3884, Line 3, Col 39, Line 3, Col 40, "This expression is a function value. When used in an interpolated string it will be formatted using its 'ToString' method, which is likely not the intended behavior. Consider applying the function to its arguments.")
[<Fact>]
let ``Warn for function value with format specifier in interpolated string`` () =
Fsx """
let f x = x + 1
let s = $"{f:N2}"
"""
|> withLangVersionPreview
|> compile
|> shouldFail
|> withDiagnostics [
(Warning 3884, Line 3, Col 12, Line 3, Col 13, "This expression is a function value. When used in an interpolated string it will be formatted using its 'ToString' method, which is likely not the intended behavior. Consider applying the function to its arguments.")
]
[<Fact>]
let ``Warn can be suppressed with nowarn`` () =
Fsx """
#nowarn "3884"
let f x = x + 1
let s = $"{f}"
"""
|> withLangVersionPreview
|> compile
|> shouldSucceed
[<Fact>]
let ``Warn when System.Action delegate is used as interpolated string argument`` () =
Fsx """
let a = System.Action(fun () -> ())
let s = $"{a}"
"""
|> withLangVersionPreview
|> compile
|> withDiagnosticMessageMatches "This expression is a function value"
[<Fact>]
let ``Warn when System.Func delegate is used as interpolated string argument`` () =
Fsx """
let f = System.Func<int, string>(fun x -> string x)
let s = $"{f}"
"""
|> withLangVersionPreview
|> compile
|> shouldFail
|> withSingleDiagnostic (Warning 3884, Line 3, Col 12, Line 3, Col 13, "This expression is a function value. When used in an interpolated string it will be formatted using its 'ToString' method, which is likely not the intended behavior. Consider applying the function to its arguments.")
[<Fact>]
let ``No warn when delegate is invoked in interpolated string argument`` () =
Fsx """
let f = System.Func<int, string>(fun x -> string x)
let s = $"{f.Invoke(42)}"
"""
|> withLangVersionPreview
|> compile
|> shouldSucceed
// See https://github.com/dotnet/fsharp/issues/19367.
[<CulturedFact([|"th"|])>]
let ``Hole without specifier parsed correctly when culture set to Thai`` () =
Fsx
"""
let s = $"{3}"
if s <> "3" then
failwith $"Expected \"3\" but got \"%s{s}\"."
"""
|> compileExeAndRun
|> shouldSucceed
// See https://github.com/dotnet/fsharp/issues/19367.
[<CulturedFact([|"th"|])>]
let ``Explicit %P does not cause exception when culture set to Thai`` () =
Fsx
"""
let s = $"%P({3})"
"""
|> compileExeAndRun
|> shouldSucceed
// Issue 16696: '=' immediately followed (no space) by an interpolated-string opener was
// greedily lexed as the invalid operator '=$' instead of '=' + an interpolated string.
// The hole {n} proves an interpolated string (not a plain one) is what gets lexed.
[<Fact>]
let ``Issue 16696 - '=' adjacent to an interpolated string binds it`` () =
Fsx """
let n = 42
let x =$"{n}"
if x <> "42" then failwith "expected 42"
"""
|> compileExeAndRun
|> shouldSucceed
// (The triple-quote form '=$"""..."""' is covered by the SyntaxTree baseline
// SynExprInterpolatedStringAdjacentEqualsTripleQuote.fs; '=$"' is a prefix of '=$"""', so the
// same lexer rule handles it after the rewind.)
// The reported cases from the issue: named-argument and record-field initialization.
[<Fact>]
let ``Issue 16696 - '=' adjacent to an interpolated string in named-argument and record contexts`` () =
Fsx """
type C() = member val Name = "" with get, set
type R = { Name: string }
let n = 42
let c = C(Name=$"{n}")
let r = { Name=$"{n}" }
if c.Name <> "42" || r.Name <> "42" then failwith "expected 42"
"""
|> compileExeAndRun
|> shouldSucceed
// The verbatim ($@" / @$") forms, also adjacent to '='.
[<Fact>]
let ``Issue 16696 - '=' adjacent to a verbatim interpolated string binds it`` () =
Fsx """
let n = 42
let a =$@"{n}"
let b =@$"{n}"
if a <> "42" || b <> "42" then failwith "expected 42"
"""
|> compileExeAndRun
|> shouldSucceed
[<Fact>]
let ``Issue 16696 - '=' adjacent to a verbatim interpolated string in named-argument and record contexts`` () =
Fsx """
type C() = member val Name = "" with get, set
type R = { Name: string }
let n = 42
let c = C(Name=$@"{n}")
let r = { Name=@$"{n}" }
if c.Name <> "42" || r.Name <> "42" then failwith "expected 42"
"""
|> compileExeAndRun
|> shouldSucceed
// The extended multi-dollar ($$) form, also adjacent to '='. Note that a $$ string uses double
// braces for holes ({{n}}), so {n} would be literal text. Uses an escaped string literal because
// the source contains """, which cannot be embedded in an F# """...""" string.
[<Fact>]
let ``Issue 16696 - '=' adjacent to an extended multi-dollar interpolated string binds it`` () =
Fsx "let n = 42\nlet x =$$\"\"\"{{n}}\"\"\"\nif x <> \"42\" then failwith \"expected 42\""
|> compileExeAndRun
|> shouldSucceed
// Operator lexing is unchanged: a '$' anywhere in an operator is still reserved (FS0035).
// The only thing the fix changes is '=' directly before an interpolated-string opener;
// everything below still lexes as an operator exactly as before.
[<Theory>]
[<InlineData("let x =$abc")>] // '=$' not before a quote
[<InlineData("let (=$) a b = a")>] // defining (=$)
[<InlineData("let f a b = a =$ b")>] // '=$' used as infix
[<InlineData("let (<$>) f x = f x")>] // '$' inside an operator
[<InlineData("let (<=$=>) a b = a")>] // '$' in the middle of an operator
let ``Issue 16696 - operators containing '$' are still rejected (operator lexing unchanged)`` (code: string) =
Fsx code
|> compile
|> shouldFail
|> withDiagnosticMessageMatches "is not permitted as a character in operator names"